Blockchain API Integration for Payments

Blockchain API Integration for Payments

24 Jan 2018
Blockchain API Integration for Payments

How blockchain works

Blockchain is a distributed database, which consists of two records:

  1. Individual transactions
  2. Blocks

Individual transactions consist of “Header” and “Data” which are related to transactions. Transactions take place for a given period of time. Block is used to create an alphanumeric string called “Hash”.When the first record is created, the next block takes the previous block’s Hash for the creation of its own Hash. This is authenticated or validated. At this point of the Blockchain process, a majority of nodes in the network must agree the new block’s hash is correctly calculated and ensures that all copies of the distributed ledger share the same state. Once a block is added, cannot be changed. If we try to change or swap that block, the hashes for previous and subsequent blocks will also change. The other computers in the network will also understand that problem and they will never add a new block until the problem is resolved. When the problem is solved they will start to add the block. If a problem is found in the block it will be not accepted and the process repeats.


Integration on Blockchain API with Rails:

To integrate blockchain API to exchange bitcoin from one wallet to another wallet.



To get the API key we have to first signup on the link

then we will get the API key, for e.g. :



Create API:

First, need to create a wallet:

I have created the wallet in the method:



Description: This api is use to create blockchain wallet

# create_wallet: http://XXX.XXX.X.XX:3030/api/v2/create_wallet

# create_wallet:

Write the API method to check the wallet balance.

            wallet_address: “Address of the wallet”

            Confirmation: “Password”


Write API to send or receive bitcoin using guid.

	Guid: Uniq id of the user
	main_password: blockchain password
	second_password: blockchain second password
	to: Receiving address
	from: Sending address

make_payment_by_guid: http://localhost:3030/merchant/%{guid}/payment?password=%{main_password}&second_password=%{second_password}&to=%{address}&amount=%{amount}&from=%{from}



Description: “This API will use for getting bitcoin value as per the local currency.”

for ex., 1btc = ? (USD)


The following method is used to call the bitcoin exchange API using guid

# def self.transaction_by_guid(receiver_address, amount_in_btc, guid, main_pass, second_pass)

def self.receive_btc_by_guid(guid, sender_main_password, sender_second_password, to_wallet_address, amt_in_btc, from_wallet_address)

# convert btc to satoshi and add transaction fee

amount = (btc_to_satoshi(amt_in_btc))

# Prepare params

request_url = Bitcoinexchange::Application.blockchain[“api”][“make_payment_by_guid”] % {guid: guid || ”, main_password: sender_main_password || ”, second_password: sender_second_password || ”, address: to_wallet_address, amount: amount, from: from_wallet_address}

# If in response failure occurred using third party API or http client it generate error and return nil into the response

response =  begin

RestClient.get(request_url, {“Content-Type”: “application/x-www-form-urlencoded”})

rescue RestClient::ExceptionWithResponse => e



# If response is success(code == 200)

response_body = JSON.parse(response.body)

if response.code == 200


elsif response_body[“error”]!=””


# it gives the the converted value in BTC

# response_body = “#{response_b[“message”]}” +” to “+”#{response_b[“to”]}”+”successfully.”


# To raise the error if error occured in executing blockchain api

raise Exceptions::TransactionError::ParamsError, response.body



Leave a comment: