Have you ever thought about selling their services in exchange for currency Bitcoin? Today, many major players in the market are doing it, and since OkCupid Khan Academy, finishing even WordPress. In addition, some countries have started thinking about Bitcoin as a currency. In this series of lessons we will learn Bitcoin API and Coinbase SDK.
Coinbase SDK
To work with Coinbase you can use special tools and SDK, which is available to absolutely everyone.
Terms of Use:
- Accepting payments using Coinbase done absolutely free of charge;
- You agree to pay a fee (1%), if you want to transfer the money to your bank account, but only if your sales exceed $ 1,000,000 (one million dollars);
Another important point: you can connect the service “Instant exchange”. This service converts the payment amount immediately in Bitcoin currency of your choice without further action.
Types of integration
Like many other online payment service, Coinbase offers two basic ways of integration. The first is faster and easier, the second is more difficult, but provides advanced features, which are more suitable for large projects.
The first option is to use a tool Coinbase, namely MerchantTools. You can use the buttons and page frames. If you are using a CMS or e-commerce management system (WordPress, WooCommerce, Magento …), you probably will find a lot of the respective plugins.
The second way is to complete the integration of service, excluding recourse to Coinbase. In fact, we will use a specific PHP SDK.
What we can do with the help of the SDK?
- sell or buy bitcoin (or perform currency exchange);
- Send bitcoin request by e-mail or bitcoin address;
- accept payments bitcoin as a merchant service;
- bitcoin stored in one or more purses;
- have access to a list of operations on bitcoin (blocks transaction, etc.);
- handle current and micro payments;
Note: Before proceeding to the next step, you need be a registered in Coinbase.
You will receive your personal Bitcoin Wallet for example:
PHP SDK
Installation
Let’s start with the installation of the library. On GitHub page of this SDK you can not find the opportunity to interact with the Composer. However, with a simple search can find the appropriate package coinbase/coinbase.
To install place the following code in the file composer.json:
{ "require": { "coinbase/coinbase": "dev-master" } }
Next, use the composer (if you have it installed) to download the library:
composer update
Authentication
Before you start writing code, let’s talk about the authentication. Developers have two options for authentication to gain access to a method of API. The first – is to use a pair of keys API and API Secret. Second, the use OAuth2.
Documentation Coinbase quite clear: if you intend to interact only with their account, make changes to it, you can use the API Key. If you want the user to use your account as part of your application, it is best to use OAuth2.
Api Key + Secret
Create an API key is very simple, if you have an account Coinbase. All you need to do is go here and click the “+ New API Key”.
If you do it the first time, you will likely need to verify your account using Authy.
Before you should see the following form:
You will need to specify the account information and a list of permission that must be assigned to a specific key. You can also select one or more IP addresses for the entry in the “white list”.
To create and activate an API key, first press “Create”, and then “Enable”.
OAuth 2.0
If you want to use OAuth 2.0, you first have to go through a simple procedure. At this time you need not create the key API, however, and OAuth 2.0 app. To do this, go to the address https://coinbase.com/oauth/applications. Next, click on the “+ Create an Application”:
Fill out the form below: select the name of your application, select the icon, as well as provide a list of URL-addresses for future redirects.
Note: Each URL must start with (https: // …). Other options will be ignored.
Click OK and you’re done! After that you should receive an email with your new ClientID and Client Secret. It’s certainly not all of the information about authentication, a more detailed description can be found on the page.
Principles of work with the SDK
Now that we have the keys and download library, we can proceed to the next steps. First of all, let’s look at the implementation of the authentication procedure.
Access via the API key and Secret
$coinbase = Coinbase::withApiKey($coinbaseAPIKey, $coinbaseAPISecret);
Insert the API key and API Secret as parameters to the method withApiKey(). Next work with the SDK, we will use the object $coinbase.
Access via OAuth
After creating the OAuth applications, we need to create an object $coinbase Oauth, specifying Client ID and Client Secret that we received earlier. As in this example:
$coinbaseOauth = new Coinbase_OAuth($_CLIENT_ID, $_CLIENT_SECRET, $_REDIRECT_URL); header("Location: " . $coinbaseOauth->createAuthorizeUrl("all"));
After successful authentication, the user is redirected to the URL, specified earlier during installation. To obtain a valid token, just need to pass a parameter code:
$tokens = $coinbaseOauth->getTokens($_GET['code']);
Next we need to create an object $coinbase, using token:
$coinbase = Coinbase::withOauth($coinbaseOauth, $tokens);
No matter which way we passed the authentication process with the object $coinbase same.
Access to data
Get your data is very simple. For this we use the object $coinbase.
To check your balance, do the following code:
echo $coinbase->getBalance() . " BTC";
Here’s how to get the user data:
$user = $coinbase->getUser() echo $user->name; echo $user->email; echo $user->time_zone; echo $user->native_currency;
Using object $coinbase, we can also get information about the seller:
$user->merchant->company_name; $user->merchant->logo;
Note: The property returns a logo URL-address of the logo.
In addition, we can get contact details by calling getContacts().
$response = $coinbase->getContacts("user"); foreach($response->contacts as $contact) { echo $contact; // '[email protected]' }
Data on currencies
The SDK also provides data on currencies:
$currencies = $coinbase->getCurrencies(); echo $currencies[0]->name;
Method getCurrencies() returns a list of all active currencies (in the format ISO), available in the Bitcoin. Here’s how you can get some information about currency exchange rates:
$rates = $coinbase->getExchangeRate(); echo $rates->btc_to_usd; // is the same as... echo $coinbase->getExchangeRate('btc', 'usd');
Depending on the parameters, which we will give to getExchangeRate(), get different results.
You may obtain information about the exchange rate using the methods getBuyPrice() and getSellPrice():
echo $coinbase->getBuyPrice('1'); // '125.31' echo $coinbase->getSellPrice('1'); // '122.41'
Note: this amount includes 1% levy and $ 0.15 Coinbase bank.
Create payment buttons
To create a button, use the method of payment createButton() with a specific set of parameters:
createButton($name, $price, $currency, $custom=null, $options=array())
Example:
$paymentButton = $coinbase->createButton( "Order #1", "19.99", "EUR", "TRACKING_CODE_1", array( "description" => "1 item at 19.99" ) );
Pretty simple, is not it?
The first parameter of $name – this is the “name” of payment, which you want to create. Next is $price, the payment amount. Then this, select the currency of payment, and $custom settings for a particular transaction code which will be sent to you after the payment procedures.
Finally, $options allows you to customize the appearance of buttons, configure the URL-address to which to send the user when successful or not successful payment. More information on this parameter can be found on the documentation page.
To display the button itself, use the following fields:
echo $response->button->code; // '93865b9cae83706ae59220c013bc0afd' echo $response->embedHtml; // '<div class="coinbase-button" data-code="93865b9cae83706ae59220c013bc0afd"></div><script data-after="https://coinbase.com/assets/button.js" type="text/javascript"></script>'