ROR Application Development Tips

I'm Ruby on Rails developer. I love to share my experience, technical knowledge. I work at Crypex Technologies which is developing applications in ROR for more than a decade.

Blockchain API Integration for Payments

How blockchain works

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

  1. Individual transactions
  2. Blocks

f:id:michaleleo432:20180130144507j:plain

Individual transactions consist of “Header” and “Data” which is related to transactions. Transactions take place for a given period of time. Block is used to create alphanumeric string called as “Hash”.When the first record is created, the next block takes previous block’s Hash for creation of their 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 block 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 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.

API KEY:

To get the API key we have to first signup on the link https://api.blockchain.info/customer/signup

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

api_code: XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX

Create API:

First need to create a wallet:

I have created the wallet in the method:

http://localhost:3030/api/v2/create_wallet

Description: This api is use to create blockchain wallet

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

# create_wallet: https://blockchain.info/api/v2/create_wallet

Write the API method to check the wallet balance.

            wallet_address: “Address of the wallet”

            Confirmation: “Password”

address_balance: https://blockchain.info/q/addressbalance/%{wallet_address}?confirmations=%{confirmation}

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}

current_rate: https://blockchain.info/ticker

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

e.response

end

# If response is success(code == 200)

response_body = JSON.parse(response.body)

if response.code == 200

response_body

elsif response_body[“error”]!=””

response_body[“error”]

# it gives the the converted value in BTC

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

else

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

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

end

end

Source: http://www.cryptextechnologies.com/blogs/blockchain-api-integration