Sample Code

Learn about the Ruby bindings to interact with Razorpay APIs.


Notes:

  • Amount must always be in currency subunits.
  • Time represented as Unix timestamp, which can be obtained using the Utils.ToUnixTimestamp method.

Initialization🔗

Set up your [KEY_ID] and [KEY_SECRET] using the following command:

Copyrequire "razorpay" Razorpay.setup('key_id', 'key_secret')

Generate API Key🔗

Follow these steps to generate API keys:

  1. Log into your Dashboard with appropriate credentials.
  2. Select the mode (Test or Live) for which you want to generate the API key.
    • Test Mode: The test mode is a simulation mode that you can use to test your integration flow. Your customers will not be able to make payments in this mode.
    • Live Mode: When your integration is complete, switch to live mode and generate live mode API keys. Replace test mode keys with live mode keys in the integration to accept payments from customers.
  3. Navigate to Settings → API Keys → Generate Key to generate key for the selected mode.

The Key Id and Key Secret appear on a pop-up page.

Test Mode API Keys🔗

Watch this video to see how to generate API keys in the test mode.


Live Mode API Keys🔗

Watch this video to see how to generate API keys in the live mode.

Watch Out!

  • After generating the keys from the Dashboard, download and save them securely. If you do not remember your API Keys, you need to re-generate them from the Dashboard and replace it wherever required.
  • Do not share your API Key secret with anyone or on any public platforms. This can pose security threats for your Razorpay account.
  • Once you generate the API Keys, only the Key Id is visible on the Dashboard and not the Key secret as it can pose security threats for your Razorpay account.

Add Custom Headers🔗

You can set custom headers for your requests using the following command:

CopyRazorpay.headers = {"CUSTOM_APP_HEADER" => "CUSTOM_VALUE"}

Note:
If you are using Rails, the right place to do this might be config/initializers/razorpay.rb.

Payments🔗

Note:
Click here for request parameters and an example request and response.

Capture a Payment🔗

CopyRazorpay::Payment.fetch("payment_id").capture({amount:5000, currency:'INR'})

Notes:

  • The amount must come from your session. This is required to verify that the purchase was correctly made without any tampering.

  • If a payment is captured or refunded via capture!, then the calling object is also updated:

    Copypayment = Razorpay::Payment.fetch('payment_id') payment.status # 'authorized' payment.capture! payment.status # 'captured'

Refunds🔗

Note:
Click here for request parameters and an example request and response.

Refund a Payment🔗

CopyRazorpay::Payment.fetch("payment_id").refund({amount:500}) refunds = Razorpay::Payment.fetch("payment_id").refunds

Refund a Payment without Fetching it🔗

Copyrefund = Razorpay::Refund.create(payment_id:"payment_id") Razorpay::Refund.fetch(refund.id)

Note:
If a payment is captured or refunded via refund!, then the calling object is also updated:

Copypayment = Razorpay::Payment.fetch('payment_id') payment.status # 'captured' payment.refund! payment.status # 'refunded'

Subscriptions🔗

Note:
Click here for request parameters and an example request and response.

Create a Plan🔗

Copyplan = Razorpay::Plan.create interval: 1, period: 'monthly', item: { name: 'Test Plan', description: 'Description for Test Plan', currency: 'INR', amount: 500 }, notes: { identifier: 'Test Plan' }

Create a Subscription🔗

Copysubscription = Razorpay::Subscription.create plan_id: plan.id, customer_id: customer.id, start_at: ( Time.now + (60 * 60 * 24)).to_i, total_count: 3

Note:
The above request creates a subscription that starts after 24 hours, (Time.now + (60 * 60 * 24)).

Create an Add-on🔗

Copysubscription_addon = Razorpay::Addon.create subscription.id, item: { name: 'test_plan', description: 'test_plan_desc', currency: 'INR', amount: 500 }, quantity: 1

Fetch an Add-on🔗

Copyaddon = Razorpay::Addon.fetch subscription_addon.id

Invoices🔗

Note:
Click here for request parameters and an example request and response.

Create an Invoice🔗

Copyinvoice = Razorpay::Invoice.create customer_id: customer.id, amount: 100, currency: 'INR', description: 'Test Invoice', type: 'link'

Webhooks🔗

Note:
Click here for detailed webhooks documentation.

Validate Webhook Signature🔗

You can verify the signature received in a webhook using the following:

CopyRazorpay::Utility.verify_webhook_signature(webhook_body, webhook_signature, webhook_secret)

If the signature is invalid, this throws a SecurityError exception.

The parameters are:

webhook_body
The complete webhook body. You can use:
- request.raw_post in Rails
- req.body.read in Rack
- request.body.read in Sinatra
Please consult your framework's documentation for the equivalent.
webhook_signature
We send the webhook signature in a request header with the key X-Razorpay-Signature. You can read the header using:
- request.headers["X-Razorpay-Signature"] in Rails
- env["HTTP_X_RAZORPAY_SIGNATURE"] in Rack
- request.env["HTTP_X-Razorpay-Signature"] in Sinatra
webhook_secret
The secret you set at the time of setting up the webhook.

You can parse the webhook after it has been validated by using JSON.parse webhook_body.

Utility🔗

Validate Payment Signature🔗

You can use the utility class to verify the signature received in response to a payment made using Orders API.

Copyputs payment_response # { # :razorpay_order_id => "test_order_id", # :razorpay_payment_id => "test_payment_id", # :razorpay_signature => "signature" # } Razorpay::Utility.verify_payment_signature(payment_response)

×