public function indexAction() { $key = Mage::getStoreConfig('payment/bitcoin/coinapult_key'); $secret = Mage::getStoreConfig('payment/bitcoin/coinapult_secret'); $coinapult = new Coinapult($key, $secret); $auth = $coinapult->authenticate_callback($_SERVER['HTTP_CPT_KEY'], $_SERVER['HTTP_CPT_HMAC'], $_POST); if (!$auth['auth']) { Mage::log('Callback: failed to authenticate! ' . print_r($_SERVER, TRUE), null, 'coinapult.log'); exit; } /* Lookup transaction. */ $response = $coinapult->search(array("transaction_id" => $_POST['transaction_id'])); Mage::log('Search result: ' . print_r($response, TRUE), null, 'coinapult.log'); if (get_class($response) == "payError") { exit; } elseif ($response['state'] == 'complete') { $write = Mage::getSingleton('core/resource')->getConnection('core_write'); $query = sprintf("SELECT entity_id FROM sales_flat_order_payment WHERE additional_data LIKE '%%\"CoinapultTID\":\"%s\"%%'", $response['transaction_id']); $readresult = $write->query($query); $row = $readresult->fetch(); if ($row) { $oid = $row['entity_id']; } if (!empty($oid)) { $order = Mage::getModel('sales/order')->load($oid); $comment = "Coinapult callback. Received: " . $response['in']['amount'] . "btc"; $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, $comment, true); $order->sendOrderUpdateEmail(true, $comment); $order->save(); } } elseif ($response['state'] == 'canceled') { $write = Mage::getSingleton('core/resource')->getConnection('core_write'); $query = sprintf("SELECT entity_id FROM sales_flat_order_payment WHERE additional_data LIKE '%%\"CoinapultTID\":\"%s\"%%'", $response['transaction_id']); $readresult = $write->query($query); $row = $readresult->fetch(); if ($row) { $oid = $row['entity_id']; } if (!empty($oid)) { $order = Mage::getModel('sales/order')->load($oid); $payment = $order->getPayment(); $adata = json_decode($payment->getAdditionalData()); } $short = $response['in']['expected'] - $response['in']['amount']; $comment = "Coinapult callback. Insufficient payment. Received: " . $response['in']['amount'] . "btc. Expected: " . $response['in']['expected'] . "btc. Please send an additional " . $short . "btc to Bitcoin address: " . $response['address'] . "."; $order->setState(Mage_Sales_Model_Order::STATE_NEW, true, $comment, true); $order->sendOrderUpdateEmail(true, $comment); $order->save(); } else { exit; } }
public function callback() { /* Validate the received callback to confirm (or not) the payment. */ require DIR_APPLICATION . '../coinapult/coinapult.php'; $log = new Log('coinapult.log'); $log->write('callback!'); $log->write(print_r($_POST, TRUE)); $coinapult = new Coinapult($this->config->get('coinapult_api_key'), $this->config->get('coinapult_api_secret')); if (!(isset($_SERVER['HTTP_CPT_KEY']) && isset($_SERVER['HTTP_CPT_HMAC']))) { /* Invalid callback. */ $log->write('Callback: basic headers missing.'); return; } $auth = $coinapult->authenticate_callback($_SERVER['HTTP_CPT_KEY'], $_SERVER['HTTP_CPT_HMAC'], $_POST); if (!$auth['auth']) { $log->write('Callback: failed to authenticate! ' . print_r($_SERVER, TRUE)); $log->write('Auth result: ' . print_r($auth)); return; } if (!isset($_POST['transaction_id'])) { $log->write('Callback missing transaction id, ignored.'); return; } $this->load->model('checkout/order'); $tid = $_POST['transaction_id']; $sql = "SELECT `order_id` FROM `" . DB_PREFIX . "order_bitcoin_coinapult`\n WHERE `transaction_id` = '{$tid}';"; $result = $this->db->query($sql); if (!$result->num_rows) { $log->write("No order found for tid = {$tid}"); return; } $orderid = $result->row['order_id']; $log->write("SQL result: " . print_r($result, TRUE)); $transaction = $coinapult->search(array("transaction_id" => $tid)); if ($transaction['transaction_id'] != $tid) { $log->write('Transaction ID does not match, how did that happen?'); return; } $this->language->load('payment/coinapult'); if ($transaction['state'] == 'complete') { /* Invoice got paid. */ $message = "Received " . $transaction['in']['amount'] . "btc\n"; $log->write("Order {$orderid}: " . $message); $this->model_checkout_order->update($orderid, $this->config->get('coinapult_order_status_id_received'), $message, true); } elseif ($transaction['state'] == 'canceled') { $message = "Insufficient payment. Received " . $transaction['in']['amount'] . "btc. Expected " . $transaction['in']['expected'] . "btc."; $log->write("Order {$orderid}: " . $message); $this->model_checkout_order->update($orderid, $this->config->get('coinapult_order_status_id_pending'), $message, true); } else { $log->write('Unexpected transaction status: ' . $transaction['state']); } }