Back

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

Aug 16, 20245 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of online payments? Let's talk Razorpay. It's a robust payment gateway that's taking the Indian fintech scene by storm. In this guide, we'll walk through integrating Razorpay's API into your Python project. We'll be using the razorpay package, so buckle up and let's get coding!

Prerequisites

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

  • A Python environment set up (I'm assuming you're good to go here)
  • A Razorpay account with API keys handy

If you're missing either of these, take a quick detour and get them sorted. Don't worry, we'll wait!

Installation

First things first, let's get the razorpay package installed. It's as simple as:

pip install razorpay

Easy peasy, right?

Initializing the Razorpay Client

Now that we've got our tools, let's set up shop:

import razorpay client = razorpay.Client(auth=("YOUR_KEY_ID", "YOUR_KEY_SECRET"))

Replace those placeholders with your actual API keys, and you're ready to roll!

Creating an Order

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

order_amount = 50000 # Amount in paise (100 paise = ₹1) order_currency = "INR" order_receipt = "order_rcptid_11" order = client.order.create({ 'amount': order_amount, 'currency': order_currency, 'receipt': order_receipt })

Boom! You've just created an order. The order variable now contains all the details you need.

Handling Payment Verification

Once the payment's done, you'll want to verify it:

params_dict = { 'razorpay_order_id': 'order_ID_from_frontend', 'razorpay_payment_id': 'payment_ID_from_frontend', 'razorpay_signature': 'signature_from_frontend' } client.utility.verify_payment_signature(params_dict)

If this doesn't raise an exception, you're golden!

Fetching Payment Information

Want to know more about a payment? Here's how:

payment = client.payment.fetch('payment_ID_here')

Now you can check payment['status'] to see if it's a success or not.

Refunds

Sometimes, you gotta give the money back. No worries, it's straightforward:

refund = client.payment.refund('payment_ID_here', {'amount': 'amount_to_refund'})

To check on a refund:

refund_status = client.refund.fetch('refund_ID_here')

Webhooks

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

@app.route('/razorpay-webhook', methods=['POST']) def razorpay_webhook(): webhook_secret = 'your_webhook_secret' webhook_signature = request.headers.get('X-Razorpay-Signature') client.utility.verify_webhook_signature(request.data, webhook_signature, webhook_secret) # Process the webhook event # ... return '', 200

Error Handling and Best Practices

Always wrap your Razorpay calls in try-except blocks. The package throws specific exceptions that you can catch and handle gracefully.

And remember, keep those API keys secret! Never expose them in client-side code.

Testing

Razorpay provides a test mode. Use it liberally before going live. Here's a quick test case:

def test_order_creation(): order = client.order.create({'amount': 100, 'currency': 'INR', 'receipt': 'test_receipt'}) assert order['status'] == 'created'

Conclusion

And there you have it! You're now equipped to handle payments like a pro. Remember, this is just scratching the surface. Razorpay's API can do a lot more, so don't be afraid to explore.

Keep coding, keep learning, and may your transactions always be successful!