Tuesday 20 June 2017

Bitcoin Coinbase API for PHP - Example

Bitcoin Coinbase API for PHP - Example

Bitcoin is an innovative payment network and a new kind of money. Bitcoin is the world's most widely used alternative currency with a total market cap of approximately $10 billion. The bitcoin network is made up of thousands of computers run by individuals all over the world.
Here i am going to present a small example to send money with Coinbase API. You can create a new payment button with Coinbase api. Let see step by step how it will work.

Step 1: Create a payment.php file


<?phprequire __DIR__ . '/vendor/autoload.php';
use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Value\Money;
use Coinbase\Wallet\Resource\Checkout;
use Coinbase\Wallet\Resource\Order;
    
$apiKey = 'API KEY';
$apiSecret = 'API SECRET';

$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);

$amount = 0.1;
$orderId = "YOUR ORDER ID";

$params = array(
    'name'          => 'Site order ID: '.$orderId,
    'amount'        => new Money($amount, 'USD'),
    'metadata'      => array('order_id' => $orderId),
    'auto_redirect' => true
);

$checkout = new Checkout($params);
$client->createCheckout($checkout);
$code = $checkout->getEmbedCode();

$redirect_url = "https://www.coinbase.com/checkouts/$code";
?>

Step 2: Create a notification.php file


<?php require __DIR__ . '/vendor/autoload.php';
use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Value\Money;
use Coinbase\Wallet\Resource\Checkout;
use Coinbase\Wallet\Resource\Order;

$apiKey = 'API KEY';
$apiSecret = 'API SECRET';
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);

$raw_body = file_get_contents('php://input');
$signature = $_SERVER['HTTP_CB_SIGNATURE'];
$authenticity = $client->verifyCallback($raw_body, $signature); // boolean
if($authenticity){
 $order = json_decode($raw_body, true);
    mail("YOUR EMAIL ID","Coinbase Patment Notifications",print_r($order, true));
}
?>

Step 3: Create a success.php file


<?php
$order = $_GET['order'];
var_dump($order);
?>

Note: You need to set these all files url in your coinbase merchant account

0 comments: