#native_company# #native_desc#
#native_cta#

Integrate Bitcoin Payments with PHP

By Voja Janjic
on October 27, 2016

As the usage of Bitcoin as a payment method is growing, many companies have started accepting payments in this virtual currency. More than 100,000 companies are currently accepting Bitcoin (some of them are large and well-known, such as Microsoft and Dell), making it the most popular virtual currency today. Let’s see how it works and how to accept payments in Bitcoins in your Web application.

How Does Bitcoin Work?

Bitcoin is a digital currency and payment system that enables peer-to-peer transactions between users. That means that there is no intermediary in the transactions. However, all transactions are verified and recorded in a public distributed ledger, which is called the blockchain.

Since Bitcoin is a decentralized system, the computer resources from many people are used for payment processing. These people are called miners and are rewarded with Bitcoins for their service. Miners can also prioritize transactions, which means that they can charge transaction fees and offer faster processing of transactions to the users who have paid the higher fee.

Information necessary to make transactions in Bitcoins is stored in a wallet. A wallet is a software application that holds information about the user’s account, as well as credentials that prove ownership.

PHP Integration

The easiest Bitcoin integration solution is offered by Bitcoin payment processors. In return for a small transaction fee, these Web sites provide you with a wallet, a REST API, prioritize your transactions with miners and send payment information to your Web site. Many of them also offer libraries that can be used to easily integrate with their APIs. In this example, we will use one from Coinbase. It can be installed through Composer:

require coinbase/coinbase

Instantiate the class by entering the application key and secret:

<?php

require 'vendor/autoload.php';

use CoinbaseWalletClient;
use CoinbaseWalletConfiguration;
use CoinbaseWalletValueMoney;
use CoinbaseWalletResourceCheckout;

$configuration = Configuration::apiKey('app_key', 'app_secret');
$client = Client::create($configuration); 

Now, we can create a checkout — an API resource that is used when integrating Bitcoin as a payment option on your Web site.

$params = array(
	'name'               => 'My cool product',
	'description'		 => 'Describe my product here',
	'amount'             => new Money('15.99', 'USD'),
	'metadata'           => array(
								'email' => '[email protected]',
							),
	'style'				=> 'buy_now_small'
);

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

The Checkout class is able to generate an iframe or button code that will be displayed on your Web site and that the users will use to make the transaction:

<iframe id="coinbase_inline_iframe_<?php echo $checkout->getEmbedCode(); ?>"
        src="https://www.coinbase.com/checkouts/<?php echo $checkout->getEmbedCode(); ?>/inline"
        style="width:460px;height:370px; border: none; box-shadow: 0 1px 3px rgba(0,0,0,0.25)"
        allowtransparency="true" frameborder="0">
</iframe>

The iframe will show the details about your product, such as the name, description and price. It will also show the address that can be used to make the payment. The user will then make the payment with his or her Bitcoin wallet. After that, Coinbase will process the transaction and send a callback to your server. The callback notifies your application that the transaction was successful and is sent to the URL that you have specified in your Coinbase account.

<?php

require 'vendor/autoload.php';

use CoinbaseWalletClient;
use CoinbaseWalletConfiguration;
use CoinbaseWalletValueMoney;
use CoinbaseWalletResourceCheckout;

$configuration = Configuration::apiKey('app_key', 'app_secret');
$client = Client::create($configuration);

$raw_body = file_get_contents('php://input');
$signature = @$_SERVER['HTTP_CB_SIGNATURE'];

// Check if the callback is valid
$authenticity = $client->verifyCallback($raw_body, $signature); // boolean

if(!$authenticity) {
	die();
}	

// Fetch data sent by Coinbase
$obj = json_decode($raw_body);
$data = ($obj->data->metadata);

// e.g. we have sent "email" as metadata - access that value with $data->email

// Payment successful - do something here

Conclusion

This tutorial has shown how to allow users to pay for your product or service in Bitcoins. Many other options are also available in the API, such as buying/selling Bitcoins, or showing the current exchange rates. For more information, check the documentation.