How to integrate Paytm in Ruby On Rails

Total Blog Views: 97

Blog Status: publish

Created By: swazahmad Created at: 08-13-2021

Tags: ruby on rails payment gateway integration UPI integration paytm inetgration ruby on rails ror paytm india integrate

Paytm Is the India's Top Payment Gateway And Ecommerce Platform

Today We will learn

How to integrate Paytm in Ruby On Rails

To Configure Paytm In Ruby On Rails Application We will require Below Keys from Paytm

 

  • Merchant ID
  • Merchant KEY
  • wallet GUID

You Can Get Above Keys By Clicking on the paytm API console

Configure the keys in paytm_env_file.rb

 

WALLET_MID: "<MERCHANT_ID>"
WALLET_KEY: "<MERCHANT_KEY>"
WALLET_GUID: "<WALLET_GUID>"

 

Then We need to add a Service For Paytm which Will accept 2 params

  • Mobile Number(On Which Account Amount will be transfered)
  • Amount (Total Amount to pay)

We Will create a base service.

app/services/paytm/base_service.rb

module Paytm
  class BaseService
    private

    def paytm_response
      uri = URI.parse(paytm_url)
      req = paytm_request(uri)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      res = http.request(req)
      JSON.parse(res.body)
    end

    def paytm_request(uri)
      headers = Hash.new
      headers['Content-Type'] = 'application/json'
      headers['x-mid'] = ENV['WALLET_MID']
      headers['x-checksum'] = paytm_checksum
      req = Net::HTTP::Post.new(uri.request_uri, headers)
      req.body = paytm_params
      return req
    end

    def paytm_checksum
      ::PaytmChecksum.new.generateSignature(paytm_params, ENV['WALLET_KEY'])
    end

    def paytm_base_url
      paytm_env = Rails.env.development? ? 'staging-dashboard' : 'dashboard'
      "https://#{paytm_env}.paytm.com/bpay/api/v1/disburse/order/"
    end
  end
end
#File name Will be base_service.rb

 

Add Paytm wallet transfer service which is derived from base_service

 

 

app/services/paytm/paytm_wallet_transfer.rb

module Paytm
  class PaytmWalletTransfer < BaseService
    attr_reader :phone_number, :amount, :order_id
    
    def initialize(user_id: , amount: )
      @phone_number = phone_number
      @amount = amount.to_s
      @order_id = SecureRandom.hex
    end
    
    def transfer
      response_body = HashWithIndifferentAccess.new(paytm_response)
    end
    
    private
    
    def paytm_params
      '{"orderId":"' + order_id.to_s + '","beneficiaryPhoneNo":"' + phone_number.to_s + '","amount":"' + amount.to_s + '","subwalletGuid":"' + ENV['WALLET_GUID'] + '"}'
    end
    
    def paytm_url
      paytm_base_url + 'wallet/gratification'
    end
  end
end

 

For example, If we need to transfer Money from Our wallet to a customer We Can Just call like below

PaytmWalletTransfer.new(phone_number: '9890135096', amount: 100).transfer

Above will generate a secure random Hex and Request to transfer Money Of a provided amount to Given Mobile Number

Response sample Like Below:

 

{
  "status": "ACCEPTED",
  "statusCode": "DE_002",
  "statusMessage": "Request accepted"
}

Response to this above call having status as ACCEPTED or SUCCESS means your request is accepted or done successfully.

If it returns FAILURE, then there must be some issue with the request so please check the status message.

 

To know about paytm wallet transfer API, please visit  

To know about paytm check order status API, please visit below link: 

If You do have Some Question and Suggestion Please add-In Comments


swazahmad

Swaz is Practice Lead of ROR Developer. he's a Blogger, Mentor, and Rubyst. He has expertise in Javascript also . he's highly skilled in designing, developing and testing web applications. He enjoys being challenged and interesting with projects that need him to



Comments



  • swazahmad | almost 4 years ago
    Awesome Information Thanks For Sharing
Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads