Exemplo n.º 1
0
 function after_process()
 {
     require_once 'coinapult/coinapult.php';
     global $insert_id, $order, $db, $messageStack;
     $this->_log("after_process");
     $coinapult = new CoinapultClient(MODULE_PAYMENT_COINAPULT_API_KEY, MODULE_PAYMENT_COINAPULT_API_SECRET);
     $response = $coinapult->receive(null, 'BTC', $order->info['total'], $order->info['currency'], null, zen_href_link('bitcoin_coinapult_callback.php', '', 'NONSSL', true, true, true));
     $this->_log("response from coinapult: " . print_r($response, true));
     if (!isset($response['transaction_id']) || is_null($response['transaction_id']) || isset($response['error'])) {
         /* Invoice not created. */
         $this->_log("failed");
         $messageStack->add_session('checkout_payment', MODULE_PAYMENT_COINAPULT_TEXT_ERROR_INVOICEFAIL, 'error');
         zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'NONSSL', true, false));
     } else {
         $this->_log("transaction started at coinapult");
         /* Set status for the new order. */
         $db->Execute("UPDATE " . TABLE_ORDERS . " SET orders_status = " . (int) MODULE_PAYMENT_COINAPULT_ORDERNEW_ID . " WHERE orders_id = " . (int) $insert_id);
         /* Store $tid. */
         $tid = $response['transaction_id'];
         $db->Execute("INSERT INTO " . TABLE_COINAPULT_LINK . " (`order_id`,\n        `transaction_id`) VALUES ('{$insert_id}', '{$tid}')");
         /* Include payment data. */
         $btc_address = $response['address'];
         $in_expected = $response['in']['expected'];
         $replacements = array('%btc_address%' => $btc_address, '%in_expected%' => $in_expected, '%tid%' => $tid);
         $_SESSION['payment_method_messages'] = str_replace(array_keys($replacements), $replacements, MODULE_PAYMENT_COINAPULT_TEXT_CHECKOUT_SUCCESS_HTML);
         $comment = str_replace(array_keys($replacements), $replacements, MODULE_PAYMENT_COINAPULT_TEXT_CHECKOUT_SUCCESS);
         $sql = "INSERT INTO " . TABLE_ORDERS_STATUS_HISTORY . " (comments, orders_id,\n        orders_status_id, customer_notified, date_added) VALUES (:orderComments,\n        :orderID, :orderStatus, 0, now())";
         $sql = $db->bindVars($sql, ':orderComments', $comment, 'string');
         $sql = $db->bindVars($sql, ':orderID', $insert_id, 'integer');
         $sql = $db->bindVars($sql, ':orderStatus', $order->info['order_status'], 'integer');
         $db->Execute($sql);
         $_SESSION['cart']->reset(true);
         $this->_log("redirecting..");
         zen_redirect("https://coinapult.com/invoice/" . $tid);
     }
     return false;
 }
require 'includes/application_top.php';
function _log($msg)
{
    global $logDir;
    $file = $logDir . '/bitcoin_coinapult.log';
    $fp = @fopen($file, 'a');
    @fwrite($fp, $msg . "\n");
    @fclose($fp);
}
$logDir = defined('DIR_FS_LOGS') ? DIR_FS_LOGS : DIR_FS_SQL_CACHE;
if (!defined('TABLE_COINAPULT_LINK')) {
    /* Associate orders with tid from coinapult so we can search for that
     * in callbacks. */
    define('TABLE_COINAPULT_LINK', DB_PREFIX . 'coinapult_link');
}
$coinapult = new CoinapultClient(MODULE_PAYMENT_COINAPULT_API_KEY, MODULE_PAYMENT_COINAPULT_API_SECRET);
if (!(isset($_SERVER['HTTP_CPT_KEY']) && isset($_SERVER['HTTP_CPT_HMAC']))) {
    _log('received invalid callback');
    die;
}
/* Valid callback so far. */
$auth = $coinapult->authenticate_callback($_SERVER['HTTP_CPT_KEY'], $_SERVER['HTTP_CPT_HMAC'], $_POST);
if (!($auth['auth'] && isset($_POST['transaction_id']))) {
    _log('failed to authenticate the callback: ' . print_r($auth, true));
    die;
}
$tid = $_POST['transaction_id'];
$result = $db->Execute("SELECT `order_id` FROM " . TABLE_COINAPULT_LINK . "\n  WHERE `transaction_id` = '{$tid}'");
if ($result->RecordCount() == 1) {
    $orderid = $result->fields['order_id'];
    _log('found order ' . $orderid . ' for tid ' . $tid);