These days online payments are very common in web applications. There are a lot of payment gateways available in the market. Razorpay is one of them. 

In this blog, we will learn how to integrate razorpay with your Ruby on Rails application. We will use ‘razorpay’ gem Rails for using this payment gateway.

Let’s start with the setup!

Step 1: Generate API key for razorpay gem Rails integration. If you have already generated an API key, you will get it here.

Step 2: Let’s generate a new rails application for razorpay integration.

$ rails new RazorPayDemo

Step 3: Add the following gems in your gemfile:

gem 'razorpay'

Step 4: Add api key to 'credentials.yml' file. As this is a demo application we are adding credentials as shown below. If you are having different keys for different environments you can place this block as per environment.

razorpay:
  Key_id: RAZORPAY_KEY_ID
  secret_key: RAZORPAY_SECRET_KEY

Step 5: For setting razorpay keys add 'razorpay.rb' file under 'config/initializers' folder.

key_id = Rails.application.credentials.dig(:razorpay, :key_id)
secret_key = Rails.application.credentials.dig(:razorpay, :secret_key)
Razorpay.setup(key_id, secret_key)

Hurray! We are done with the set up. Let’s make payment! 

We will make a payment of 100 Rs. 

Note: Razorpay international payments gateway accepts amounts in currency subunits.

We are generating a payment of 100 Rs so we will pass 10000 as an amount.

order = Razorpay::Order.create amount: 10000, currency: 'INR', receipt: 'TEST'

For getting all orders:

orders = Razorpay::Order.all

For getting details of a particular order we will pass order_id as argument. 

order = Razorpay::Order.fetch(ORDER_ID)

Let’s fetch all the payments associated with this order.

payments = order.payments

For getting details of a particular payment and then capturing it.

payment = Razorpay::Payment.fetch("PAYMENT_ID")payment.capture!

Here it will capture the whole amount associated with this payment. If you want to capture partial amount we can do this by:

Razorpay::Payment.fetch("PAYMENT_ID").capture({amount: PARTIAL_AMOUNT})

Note that here we need to provide an amount in currency subunit.

Let’s initiate a full refund!

payment = Razorpay::Payment.fetch("PAYMENT_ID")payment.refund!

We can use ‘Razorpay::Refund’ class for generating refunds.

refund = Razorpay::Refund.create(payment_id:"PAYMENT_ID")

For generating partial refund, pass amount as argument.

Razorpay::Payment.fetch("PAYMENT_ID").refund({amount:PARTIAL_AMOUNT})

Getting all refunds associated with a payment.

payment = Razorpay::Payment.fetch("PAYMENT_ID")
payment.refunds

We can check the status of a payment by using the 'status' method.

payment = Razorpay::Payment.fetch("PAYMENT_ID")
payment.status

This was the final step for razorpay integration in Ruby on Rails. I hope you enjoyed this article. Keep following for more such articles.

Happy coding!

Read Also: How to Integrate PayUMoney in your Spree Commerce Store with Ruby on Rails