public function assignData($data) { $info = $this->getInfoInstance(); if (is_array($data)) { $info->addData($data); } elseif ($data instanceof Varien_Object) { $info->addData($data->getData()); } $total = $info->getQuote()->getBaseGrandTotal(); $curcode = $info->getQuote()->getBaseCurrencyCode(); $key = Mage::getStoreConfig('payment/bitcoin/coinapult_key'); $secret = Mage::getStoreConfig('payment/bitcoin/coinapult_secret'); $coinapult = new Coinapult($key, $secret); /* Create an invoice. */ $callurl = Mage::getUrl('bitcoin/callback', array('_type' => 'direct_link')); $response = $coinapult->receive(null, 'BTC', $total, 'USD', null, $callurl); Mage::log('Invoice response: ' . print_r($response, TRUE), null, 'coinapult.log'); $tid = $response['transaction_id']; if (is_null($tid)) { Mage::throwException("Couldn't set order details. Coinapult error and/or system misconfigured."); } $info->setBitcoinTotal($response['in']['expected']); $info->setBitcoinAddress($response['address']); $info->setCoinapultTID($tid); $adata = array(); $adata["BitcoinTotal"] = $response['in']['expected']; $adata["BitcoinAddress"] = $response['address']; $adata["CoinapultTID"] = $tid; $info->setAdditionalData(json_encode($adata)); $info->save(); Mage::log('Info saved (' . $tid . '): ' . print_r($adata, TRUE), null, 'coinapult.log'); return $this; }
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']); } }
<?php require_once __DIR__ . '/../../coinapult.php'; /* Replace by your key pair. */ $API = array('key' => '20a79976c8c1de9111073d40c6a429', 'secret' => '1965f49326270e3201848860060fd9e714724a60778a5d1ff7197be8429c'); $client = new Coinapult($API['key'], $API['secret']); $result = $client->account_info(); print_r($result);