Back

Step by Step Guide to Building a Razorpay API Integration in Ruby

Aug 16, 20245 minute read

Hey there, fellow Ruby developer! Ready to add some payment magic to your app? Let's dive into integrating Razorpay's API using Ruby. It's easier than you might think, and I'll walk you through it step by step.

Introduction

Razorpay is a powerhouse when it comes to payment gateways in India. Their API is robust, well-documented, and a joy to work with. By the end of this guide, you'll be processing payments like a pro!

Prerequisites

Before we jump in, make sure you've got:

  • Ruby 2.5 or higher
  • A Razorpay account with API keys handy
  • The razorpay gem (we'll install it in a sec)

Installation

First things first, let's add the Razorpay gem to your project. Pop this into your Gemfile:

gem 'razorpay'

Then run:

bundle install

Easy peasy!

Configuration

Now, let's set up those Razorpay credentials. Create an initializer file (config/initializers/razorpay.rb) and add:

Razorpay.setup('YOUR_KEY_ID', 'YOUR_KEY_SECRET')

Pro tip: Use environment variables for those keys in production!

Creating an Order

Time to create your first order. Here's how:

order = Razorpay::Order.create(amount: 50000, currency: 'INR', receipt: 'ORDER_ID')

This creates an order for ₹500 (amount is in paise). You'll get back an order object with all the details you need.

Implementing Payment Capture

After a successful payment, you'll want to capture it:

payment = Razorpay::Payment.fetch(payment_id) payment.capture({ amount: payment.amount })

Remember to wrap this in a begin-rescue block to handle any hiccups!

Verifying Payment Signature

Security first! Always verify the payment signature:

Razorpay::Utility.verify_payment_signature({ 'razorpay_order_id' => order_id, 'razorpay_payment_id' => payment_id, 'razorpay_signature' => signature })

If it's invalid, it'll raise a Razorpay::Error.

Handling Webhooks

Webhooks are your friend for real-time updates. Set up an endpoint in your app:

post '/razorpay_webhook' do # Verify the webhook signature webhook_signature = request.env['HTTP_X_RAZORPAY_SIGNATURE'] webhook_body = request.body.read begin Razorpay::Utility.verify_webhook_signature(webhook_body, webhook_signature, webhook_secret) # Process the webhook event rescue Razorpay::Error => e # Handle invalid signature end end

Refunds

Oops, need to refund? No sweat:

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

Testing

Razorpay provides a test mode - use it! And don't forget to write tests:

RSpec.describe RazorpayService do it 'creates an order' do # Your test here end end

Best Practices

  • Always handle errors gracefully
  • Log important events (but never log sensitive data!)
  • Keep your API keys secret and use environment variables

Troubleshooting

Running into issues? Check these common culprits:

  • Incorrect API keys
  • Mismatched currency or amount format
  • Network connectivity problems

Conclusion

And there you have it! You're now equipped to handle payments like a boss. Remember, practice makes perfect, so don't be afraid to experiment in test mode.

Happy coding, and may your transactions always be successful! 🚀💰