function after_process() { global $insert_id, $db, $order; $info = $order->info; $name = "Order #" . $insert_id; $custom = $insert_id; $currencyCode = $info['currency']; $total = $info['total']; $callback = zen_href_link('coinbasepp_callback.php', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true, $static = true); $params = array('description' => $name, 'callback_url' => $callback . "?type=" . MODULE_PAYMENT_COINBASE_CALLBACK_SECRET, 'success_url' => $callback . "?type=success", 'cancel_url' => $callback . "?type=cancel", 'info_url' => zen_href_link('index')); require_once dirname(__FILE__) . "/coinbase/Coinbase.php"; $oauth = new Coinbase_Oauth(MODULE_PAYMENT_COINBASE_OAUTH_CLIENTID, MODULE_PAYMENT_COINBASE_OAUTH_CLIENTSECRET, null); $tokens = unserialize(MODULE_PAYMENT_COINBASE_OAUTH); $coinbase = new Coinbase($oauth, $tokens); try { $code = $coinbase->createButton($name, $total, $currencyCode, $custom, $params)->button->code; } catch (Coinbase_TokensExpiredException $e) { try { $tokens = $oauth->refreshTokens($tokens); } catch (Exception $f) { $this->tokenFail($f->getMessage()); } $coinbase = new Coinbase($oauth, $tokens); $db->Execute("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $db->prepare_input(serialize($tokens)) . "' where configuration_key = 'MODULE_PAYMENT_COINBASE_OAUTH'"); $code = $coinbase->createButton($name, $total, $currencyCode, $custom, $params)->button->code; } $_SESSION['cart']->reset(true); $_SESSION['coinbasepp_order_id'] = $insert_id; zen_redirect("https://coinbase.com/checkouts/{$code}"); return false; }
function testCreateButton() { $requestor = new MockCoinbase_Requestor(); $requestor->returns('doCurlRequest', array("statusCode" => 200, "body" => ' { "success": true, "button": { "code": "93865b9cae83706ae59220c013bc0afd", "type": "buy_now", "style": "custom_large", "text": "Pay With Bitcoin", "name": "test", "description": "Sample description", "custom": "Order123", "price": { "cents": 123, "currency_iso": "USD" } } }')); $coinbase = new Coinbase(""); $coinbase->setRequestor($requestor); $response = $coinbase->createButton("test", "1.23", "USD", "Order123", array("style" => "custom_large", "description" => "Sample description")); $this->assertEqual($response->button->code, '93865b9cae83706ae59220c013bc0afd'); $this->assertEqual($response->embedHtml, "<div class=\"coinbase-button\" data-code=\"93865b9cae83706ae59220c013bc0afd\"></div><script src=\"https://coinbase.com/assets/button.js\" type=\"text/javascript\"></script>"); }
function testCreateOrderFromButtonCode() { $requestor = new MockCoinbase_Requestor(); $requestor->returns('doCurlRequest', array("statusCode" => 200, "body" => ' { "success": true, "button": { "code": "93865b9cae83706ae59220c013bc0afd", "type": "buy_now", "style": "custom_large", "text": "Pay With Bitcoin", "name": "test", "description": "Sample description", "custom": "Order123", "price": { "cents": 123, "currency_iso": "USD" } } }')); $coinbase = new Coinbase(""); $coinbase->setRequestor($requestor); $response = $coinbase->createButton("test", "1.23", "USD", "Order123", array("style" => "custom_large", "description" => "Sample description")); $this->assertEqual($response->button->code, '93865b9cae83706ae59220c013bc0afd'); $buttonCode = $response->button->code; $requestor = new MockCoinbase_Requestor(); $requestor->returns('doCurlRequest', array("statusCode" => 200, "body" => ' { "success": true, "order": { "id": "7RTTRDVP", "created_at": "2013-11-09T22:47:10-08:00", "status": "new", "total_btc": { "cents": 100000000, "currency_iso": "BTC" }, "total_native": { "cents": 100000000, "currency_iso": "BTC" }, "custom": "Order123", "receive_address": "mgrmKftH5CeuFBU3THLWuTNKaZoCGJU5jQ", "button": { "type": "buy_now", "name": "test", "description": "Sample description", "id": "93865b9cae83706ae59220c013bc0afd" }, "transaction": null } }')); $coinbase = new Coinbase(""); $coinbase->setRequestor($requestor); $response = $coinbase->createOrderFromButtonCode($buttonCode); $this->assertEqual($response->order->button->id, $buttonCode); $this->assertEqual($response->order->status, 'new'); }
<?php require_once dirname(__FILE__) . '/../lib/Coinbase.php'; // Create an application at https://coinbase.com/oauth/applications and set these values accordingly $_CLIENT_ID = "83a481f96bf28ea4bed1ee8bdc49ba4265609efa40d40477c2a57e913c479065"; $_CLIENT_SECRET = "a8dda20b94d09e84e8fefa5e7560133d9c5af9da93ec1d3e79ad0843d2920bbb"; // Note: your redirect URL should use HTTPS. $_REDIRECT_URL = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; $coinbaseOauth = new Coinbase_OAuth($_CLIENT_ID, $_CLIENT_SECRET, $_REDIRECT_URL); if (isset($_GET['code'])) { // Request tokens $tokens = $coinbaseOauth->getTokens($_GET['code']); // The user is now authenticated! Access and refresh tokens are in $tokens // Store these tokens safely, and use them to make Coinbase API requests in the future. // For example: $coinbase = new Coinbase($coinbaseOauth, $tokens); try { echo 'Balance: ' . $coinbase->getBalance() . '<br>'; echo $coinbase->createButton("Alpaca socks", "10.00", "CAD")->embedHtml; } catch (Coinbase_TokensExpiredException $e) { $newTokens = $coinbaseOauth->refreshTokens($tokens); // Store $newTokens and retry request } } else { // Redirect to Coinbase authorization page // The provided parameters specify the access your application will have to the // user's account; for a full list, see https://coinbase.com/docs/api/overview // You can pass as many scopes as you would like echo "<a href=\"" . $coinbaseOauth->createAuthorizeUrl("balance", "buttons") . "\">Connect with Coinbase</a>"; }