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 send() { /* Create invoice. */ require DIR_APPLICATION . '../coinapult/coinapult.php'; $this->load->model('checkout/order'); $orderid = $this->session->data['order_id']; $order = $this->model_checkout_order->getOrder($orderid); $data = array('callback' => $this->url->link('payment/coinapult/callback'), 'transaction_amount' => $this->currency->format($order['total'], $order['currency_code'], $order['currency_value'], FALSE), 'currency_code' => $order['currency_code']); $log = new Log('coinapult.log'); $coinapult = new Coinapult($this->config->get('coinapult_api_key'), $this->config->get('coinapult_api_secret')); $response = $coinapult->receive(null, 'BTC', $data['transaction_amount'], $data['currency_code'], null, $data['callback']); $log->write('Coinapult invoice response: ' . print_r($response, TRUE)); $json = array(); $tid = $response['transaction_id']; if (is_null($tid) || $response['state'] == 'canceled') { $json['error'] = 'Failed to create the invoice'; $log->write("failed :/"); } else { /* Associate the orderid to the transaction_id received from Coinapult. */ $sql = "INSERT INTO `" . DB_PREFIX . "order_bitcoin_coinapult`\n (`order_id`, `transaction_id`) VALUES ('{$orderid}', '{$tid}');"; $this->db->query($sql); $message = "To finalize your order...\n"; $message .= "Send <b>" . $response['in']['expected'] . "</b>btc to the following Bitcoin address:\n"; $message .= "<b>" . $response['address'] . "</b>\n\n"; $message .= "Once the payment is received, your order will be complete.\n\n"; $message .= "Coinapult Order ID is: " . $tid . "\n"; $this->model_checkout_order->confirm($orderid, $this->config->get('coinapult_order_status_id_pending'), $message, true); $newurl = $this->url->link('account/order/info&order_id=' . $order['order_id']); $json['success'] = $newurl; $this->cart->clear(); } $this->response->setOutput(json_encode($json)); }