Integrating cryptocurrency, crypto payments and commercetools

Nicholas Speeter
commercetools tech
Published in
5 min readJan 14, 2022

--

Bitcoin and other cryptocurrencies continue to gain mainstream traction as a digital currency. This post outlines three methods of integrating cryptocurrency with your commercetools project.

In the past 3 months (Q4 2021), one Bitcoin was valued at an average of 47,548.40 US dollars. These digital coins have value in many online marketplaces. Popular websites and mobile applications such as Overstock.com, NewEgg, Twitch, Twitter, and Wikipedia.com currently accept Bitcoin. The technical aspects of implementing bitcoin into your very own online store and commercetools project are outlined below.

A Bitcoin is stored as a file in a digital wallet and transactions are recorded in the blockchain. This blockchain ledger records all Bitcoin transactions in a distributed computing fashion. Bitcoin transactions from wallet to wallet can occur in a variety of ways. You’ll need a payment provider or your own manual implementation to accept Bitcoin in your webstore.

The steps below explain how to use the Coinbase Commerce API provider integration. The Coinbase Commerce API provides an easy to use and powerful REST API to integrate Bitcoin, Bitcoin Cash, DAI, Ethereum, Litecoin, Dogecoin, and USDC payments into your business applications. These steps for integrating the Coinbase API and commercetools backend allow for your webstore, mobile application, etc. to accept charges with cryptocurrency and track the status on a commercetools payment.

  1. Sign up and Setup your store’s Coinbase Commerce using the link below and get your API_KEY.

2. Checkout Coinbase Commerce

Available on npm:

npm install coinbase-commerce-node

Coinbase Commerce API: https://commerce.coinbase.com/docs/api/

3. Setup your backend AWS lambda (or similar cloud function service) behind an API Gateway for your backend Coinbase Commerce code. Kubernetes could be utilized for your backend as well (AWS EKS, Google GKE etc.) You will need to setup services to push Bitcoin wallet payments to the merchant account. A charge representing the payment is created when a customer requests to pay with Bitcoin. When a customer makes a cryptocurrency payment, they generate a transaction that is then broadcast to the cryptocurrency network for validation/confirmation.

Use the Coinbase Commerce API to dynamically create charges. The API returns the charge object with payment addresses for each currency as well as a URL to a page where the customer can complete payment. After a charge is created using the Commerce API, Coinbase starts monitoring these unique addresses on the respective networks to detect any inbound payments. Each charge has an associated payment status.

Payment status changes to Pending when the customer’s transaction is detected on the blockchain. Pending transactions have not yet been validated by the blockchain network. Once the transaction is validated by the blockchain the payment status moves to Completed.

Please note the unresolved payment states as well in the flow chart on https://commerce.coinbase.com/docs/. Unresolved payment status can be manually updated through the API to indicate a resolution.

Cryptocurrencies rely on the customer sending the payment to the merchant. Coinbase sets an expiration time for a charge at 1 hour after the creation date. If the customer does not make a payment within that timeframe, the charge expires. The Coinbase Dashboard is updated with payment information. To get information about a payment, you can fetch the current charge information by supplying the unique charge code that was returned when the charge was created. Example:

curl https://api.commerce.coinbase.com/charges/4E8UCQWO \
-H "X-CC-Version: 2019-02-22"

In commercetools you can keep track of the payment charge state using payment fields:

https://docs.commercetools.com/api/projects/payments

Your custom backend service must check for the Coinbase resolved payment state before resolving the commercetools payment object status. After payment resolution, the commercetools cart can then be converted to an order. Note that the order can be created with the Pending payment state and updated via webhook. Webhooks can be used to subscribe to charge events: https://commerce.coinbase.com/docs/api/#webhooks.

commercetools carts can reference a payment with Money "currencyCode" : "USD" and other currency codes compliant to ISO 4217. The cryptocoin value can be converted by the backend to the required common currency value and stored as `centAmount` with the `currencyCode` for the transaction. `BTC` is the unofficial ISO-like code for Bitcoin. commercetools payments, transactions, and carts can be extended to use a custom field for the Bitcoin amount and `BTC` currencyCode value.

Method 2: BitPay Payment Provider

Checkout the BitPay API Node.js library.

Step 1. Initialize your client

Step 2. Create an invoice

Step 3. Fetch an invoice status

Step 4. Receive invoice status updates and update your commercetools payment object accordingly to complete the cart checkout.

BitPay documentation.

Method 3: Manual Implementation

A third method is to develop your own custom manual implementations. One can utilize existing bitcoin backend libraries to power your crypto payments.

1. bitcoin-receive-payments: Allow your service to receive/accept bitcoin payments directly from the bitcoin P2P network.

https://www.npmjs.com/package/bitcoin-receive-payments

2. Examples for building Bitcoin-based applications: https://btcinformation.org/en/developer-examples

3. Build Your Own Bitcoin API using Node.js and Bitcoin Core: https://medium.com/@peterjd42/build-your-own-bitcoin-api-using-node-js-and-bitcoin-core-251e613623db

4. bitcore-lib Javascript Bitcoin library

Cryptocurrency in Your Virtual Store

Bitcoin may be the cryptocurrency you want to utilize as a form of payment for your digital store due to its status as the most popular cryptocurrency and its established payment integration ecosystem. Integrating Bitcoin payments into your commercetools project can be accomplished with a third party payment API or through developing your own manual flow. Give cryptocurrency a try on your webstore and compare sales to using traditional payment flows. Utilizing the latest forms of payment now could make your commerce stand out.

--

--