<?php /** * Copyright: dtbaker 2012 * Licence: Please check CodeCanyon.net for licence details. * More licence clarification available here: http://codecanyon.net/wiki/support/legal-terms/licensing-terms/ * Deploy: 9809 f200f46c2a19bb98d112f2d32a8de0c4 * Envato: 4ffca17e-861e-4921-86c3-8931978c40ca * Package Date: 2015-11-25 02:55:20 * IP Address: 67.79.165.254 */ require_once 'includes/plugin_paymethod_coinbase/coinbase-php/lib/coinbase.php'; $coinbase = array("secret_key" => module_config::c('payment_method_coinbase_api_key'), "publishable_key" => module_config::c('payment_method_coinbase_secret_key')); coinbase::setApiKey($coinbase['secret_key']);
public function external_hook($hook) { switch ($hook) { case 'event_ipn': $body = @file_get_contents('php://input'); $event_json = json_decode($body); ob_start(); echo "UCM coinbase DEBUG:<br><br>JSON: <br>\n"; print_r($event_json); echo "<br><br>\n"; $success = false; $bits = explode(':', isset($event_json->order->custom) ? $event_json->order->custom : ''); if (count($bits) == 4) { // we have our custom bits, invoice_id, invoice_payment_id and hash // check they are right $invoice_id = (int) $bits[0]; $invoice_payment_id = (int) $bits[1]; $invoice_payment_subscription_id = (int) $bits[2]; $hash = $bits[3]; $correct_hash = self::get_payment_key($invoice_id, $invoice_payment_id, $invoice_payment_subscription_id, true); if ($invoice_id && $invoice_payment_id && $hash == $correct_hash) { // This will send receipts on succesful invoices // todo - coinbase doesnt sent this callback correctly just yet if ($event_json && isset($event_json->recurring_payment) && $invoice_payment_subscription_id) { // status changes on a recurring payment. $invoice_payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $invoice_payment_subscription_id); if (!$invoice_payment_subscription['date_start'] || $invoice_payment_subscription['date_start'] == '0000-00-00') { // no start date yet, set the start date now. if ($event_json->recurring_payment->status == 'active') { update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d'))); } } if ($event_json->recurring_payment->status == 'paused' || $event_json->recurring_payment->status == 'canceled') { update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_FAILED)); } } if ($event_json && isset($event_json->order->status) && $event_json->order->status == 'completed' && isset($event_json->order->total_native) && isset($event_json->order->custom)) { // crab out the custom bits so we know what to deal with. $invoice_payment_data = module_invoice::get_invoice_payment($invoice_payment_id); $currency = module_config::get_currency($invoice_payment_data['currency_id']); if ($invoice_payment_subscription_id) { // this API result is for a subscription payment. $invoice_payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $invoice_payment_subscription_id); if ($invoice_payment_subscription && $invoice_payment_subscription['invoice_payment_subscription_id'] == $invoice_payment_subscription_id && $currency['code'] == $event_json->order->total_native->currency_iso) { if (!$invoice_payment_subscription['date_start'] || $invoice_payment_subscription['date_start'] == '0000-00-00') { // no start date yet, set the start date now (this should really happen in the above callback, but coinbase isn't working right now) update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d'))); } // we have a subscription payment. woo! // this gets a bit tricky, we have to work out if the invoice has been generated for this subscription yet. // if this invoice hasn't been generated yet then we have to generate it. // pass this back to the invoice class so we can reuse this feature in the future. $data = module_invoice::create_new_invoice_for_subscription_payment($invoice_id, $invoice_payment_id, $invoice_payment_subscription_id); if ($data && $data['invoice_id'] && $data['invoice_payment_id']) { $next_time = time(); $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['days']) . ' days', $next_time); $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['months']) . ' months', $next_time); $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['years']) . ' years', $next_time); update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('date_last_pay' => date('Y-m-d'), 'date_next' => date('Y-m-d', $next_time))); update_insert("invoice_payment_id", $data['invoice_payment_id'], "invoice_payment", array('date_paid' => date('Y-m-d'), 'amount' => $event_json->order->total_native->cents / 100, 'method' => self::get_payment_method_name() . ' (Subscription)', 'invoice_payment_subscription_id' => $invoice_payment_subscription_id)); self::add_payment_data($data['invoice_payment_id'], 'log', "Invoice Payment Subscription Received!"); self::add_payment_data($data['invoice_payment_id'], 'log', "API IP is " . $_SERVER['REMOTE_ADDR']); self::add_payment_data($data['invoice_payment_id'], 'log', "Received BTC: " . $event_json->order->total_btc->cents / 10000000); self::add_payment_data($data['invoice_payment_id'], 'log', "Received " . $event_json->order->total_native->currency_iso . ': ' . $event_json->order->total_native->cents / 100); self::add_payment_data($data['invoice_payment_id'], 'log', "Destination Address: " . $event_json->order->receive_address); self::add_payment_data($data['invoice_payment_id'], 'log', "Currency code matches, marking invoice as paid."); self::add_payment_data($data['invoice_payment_id'], 'log', "Raw Event Data: \n" . json_encode($event_json)); module_invoice::save_invoice($data['invoice_id'], array()); echo "Successful Subscription Payment!"; } else { send_error("Coinbase Subscription Error (failed to generate new invoice!) " . var_export($data, true)); } } else { send_error('Currency code missmatch on coinbase subscription payment'); } } else { // this is a normal once off payment. self::add_payment_data($invoice_payment_id, 'log', "API IP is " . $_SERVER['REMOTE_ADDR']); self::add_payment_data($invoice_payment_id, 'log', "Received BTC: " . $event_json->order->total_btc->cents / 10000000); self::add_payment_data($invoice_payment_id, 'log', "Received " . $event_json->order->total_native->currency_iso . ': ' . $event_json->order->total_native->cents / 100); self::add_payment_data($invoice_payment_id, 'log', "Destination Address: " . $event_json->order->receive_address); if ($currency['code'] == $event_json->order->total_native->currency_iso) { self::add_payment_data($invoice_payment_id, 'log', "Currency code matches, marking invoice as paid."); update_insert("invoice_payment_id", $invoice_payment_id, "invoice_payment", array('date_paid' => date('Y-m-d'), 'amount' => $event_json->order->total_native->cents / 100)); module_invoice::save_invoice($invoice_id, array()); echo "Successful Payment!"; $success = true; } else { self::add_payment_data($invoice_payment_id, 'log', "Currency code missmatch, please check settings!"); } self::add_payment_data($invoice_payment_id, 'log', "Raw Event Data: \n" . json_encode($event_json)); } } } } $debug = ob_get_clean(); if (module_config::c('coinbase_payment_debug', 0)) { send_error("Coinbase Debug: {$debug}"); } exit; break; case 'pay_subscription': $invoice_id = isset($_REQUEST['invoice_id']) ? $_REQUEST['invoice_id'] : false; $invoice_payment_id = isset($_REQUEST['invoice_payment_id']) ? $_REQUEST['invoice_payment_id'] : false; $invoice_payment_subscription_id = isset($_REQUEST['invoice_payment_subscription_id']) ? $_REQUEST['invoice_payment_subscription_id'] : false; $coinbase_plan_id = isset($_REQUEST['coinbase_plan_id']) ? $_REQUEST['coinbase_plan_id'] : false; $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : false; if ($invoice_id && $invoice_payment_id && $coinbase_plan_id && $invoice_payment_subscription_id && $user_id && isset($_POST['coinbaseToken'])) { $user_data = module_user::get_user($user_id); $email = isset($_REQUEST['coinbaseEmail']) && strlen($_REQUEST['coinbaseEmail']) ? $_REQUEST['coinbaseEmail'] : $user_data['email']; if (!$email || !strpos($email, '@')) { die('Please ensure your user account has a valid email address before paying with coinbase'); } $invoice_payment = get_single('invoice_payment', 'invoice_payment_id', $invoice_payment_id); $invoice_payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $invoice_payment_subscription_id); if (!$invoice_payment || !$invoice_payment_subscription || $invoice_payment['invoice_id'] != $invoice_id || $invoice_payment['invoice_payment_subscription_id'] != $invoice_payment_subscription_id) { die('Invalid invoice payment subscription id'); } $invoice_payment_data = module_invoice::get_invoice_payment($invoice_payment_id); $invoice_data = module_invoice::get_invoice($invoice_id); if ($invoice_payment_data && $invoice_data && $invoice_id == $invoice_data['invoice_id'] && $invoice_payment_data['invoice_id'] == $invoice_data['invoice_id']) { $currency = module_config::get_currency($invoice_payment_data['currency_id']); $currency_code = $currency['code']; $description = isset($_REQUEST['description']) ? $_REQUEST['description'] : 'N/A'; $template = new module_template(); ob_start(); require_once 'includes/plugin_paymethod_coinbase/coinbase-php/lib/coinbase.php'; $coinbase = array("secret_key" => module_config::c('payment_method_coinbase_api_key'), "publishable_key" => module_config::c('payment_method_coinbase_secret_key')); coinbase::setApiKey($coinbase['secret_key']); try { // todo- search for existing customer based on email address??? // todo: check if adding new plan to existing customer work?? $coinbase_customer = coinbase_Customer::create(array("card" => $_POST['coinbaseToken'], "email" => $email, 'metadata' => array('user_id' => $user_id))); if ($coinbase_customer && $coinbase_customer->id) { //} && $coinbase_customer->subscriptions){ $coinbase_subscription = $coinbase_customer->subscriptions->create(array('plan' => $coinbase_plan_id)); if ($coinbase_subscription && $coinbase_subscription->id) { update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d'), 'coinbase_customer' => $coinbase_customer->id, 'coinbase_subscription' => $coinbase_subscription->id)); module_paymethod_coinbase::add_payment_data($invoice_payment_id, 'log', "Started coinbase Subscription: " . var_export(array('customer.id' => $coinbase_customer->id, 'plan.id' => $coinbase_plan_id, 'subscription.id' => $coinbase_subscription->id), true)); // success! // redirect to receipt page. redirect_browser(module_invoice::link_public_payment_complete($invoice_id)); } else { echo 'Failed to create subscription with coinbase'; } } $error = "Something went wrong during coinbase payment. Please confirm invoice payment went through: " . htmlspecialchars($description); send_error($error); echo $error; } catch (coinbase_CardError $e) { // The card has been declined $body = $e->getJsonBody(); $err = $body['error']; $error = "Sorry: Payment failed. <br><br>\n\n" . htmlspecialchars($description) . ". <br><br>\n\n"; $error .= $err['message']; echo $error; $error .= "\n\n\n" . var_export($err, true); send_error($error); } catch (Exception $e) { $body = $e->getJsonBody(); $err = $body['error']; $error = "Sorry: Payment failed. <br><br>\n\n" . htmlspecialchars($description) . ". <br><br>\n\n"; $error .= $err['message']; echo $error; $error .= "\n\n\n" . var_export($err, true); send_error($error); } $template->content = ob_get_clean(); echo $template->render('pretty_html'); exit; } } echo 'Error paying via coinbase'; exit; } }
<?php session_start(); include 'classes/coinbase.php'; $func = $_POST['func']; $rtn = array(); switch ($func) { case 'start': $_SESSION['tic'] = 0; $_SESSION['messages'] = array(); break; case 'book': $cb = new coinbase(); $rtn['book'] = $cb->test(); break; case 'run': $tic = ++$_SESSION['tic']; $rtn['tic'] = $tic; $rtn['stop'] = 0; $rtn['book'] = 0; $cReceived = 0; $cOpen = 0; $cDone = 0; $cMatch = 0; $cError = 0; $cSeq = 0; if (isset($_POST['payload'])) { $payload = $_POST['payload']; foreach ($payload as $p) { $o = json_decode($p, true); $s = $o['sequence'];