Skip to main content

Blockchain API Payments Integration in Ruby

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 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

Comments

Popular posts from this blog

GraphQL With Ruby

Now a day’s most of the web or mobile applications fetch data from server which is stored in a database. REST API provides an interface to stored data that require by the applications. GraphQL is a query language for REST API's not for server databases. It is database agnostic and effectively can be used in any context where an API is used. GraphQL provide platform for declarative data fetching where client need to specify what data needs from API in response. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for. GraphQL minimizes the amount of data that needs to be transferred over the network and improves applications operating under these conditions. Introduction to GraphQL API on Ruby on Rails Start with adding gem in Gemfile gem ‘graphql’ Run command bundle install Run command rails generate graphql:install Above command will add gr

Best In Place Gem In Ruby On Rails Tutorial

The best_in_place gem is the easiest solution for in place editing in Ruby on Rails. This gem provides functionality of “in place editing” in ruby on rails without writing any extra ajax code. It supports text inputs, textarea, select dropdown, checkboxes, jQuery UI Datepickers, etc. Also Displays server-side validation Installation Steps of “best_in_place” Gem : Installing best_in_place is very easy and straight-forward. Just begin including the gem in your Gemfile: gem ‘best_in_place’ After that, specify the use of the jquery and best in place javascripts in your application.js, and optionally specify jquery-ui if you want to use jQuery UI datepickers: //= require jquery //= require best_in_place //= require jquery-ui //= require best_in_place.jquery-ui Then, just add a binding to prepare all best in place fields when the document is ready: $(document).ready(function() { /* Activating Best In Place */ jQuery(".best_in_place").best_in_place(); });

Data scraping in Ruby on Rails using Nokogiri and Mechanize Gem

What is Data scraping? Website/Data  Scraping  is a technique to operating large amounts of  data  from websites whereby the  data  is extracted and displayed in own sites or it can be stored to a File/Database. Data scraping is basically used where the websites does not provides API. Some Applications do not provide API to collect records. For the same , Data Scraping technique is used. The data can be scraped using Nokogiri Gem. The steps are required: Add the gem “gem ‘nokogiri’, ‘~> 1.8’, ‘>= 1.8.1'” . Then run the bundle install Add the “require ‘nokogiri'” , “require ‘open-uri'” line where you will write the code for the scraping. The controller of the page will look like below: The view of the code of view page will look like : The result in our application will look like: Mechanize Gem in rails The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follo