Setting up a payment gateway for your Indian website is crucial for online sales. Beyond just accepting payments, you need to ensure security, handle errors gracefully, and manage your finances accurately. This checklist focuses on key technical aspects often overlooked.

Key takeaways

  • Securely verify incoming payment notifications using webhooks and signature checks.
  • Implement robust logic for processing refunds through the gateway API.
  • Establish a reconciliation process to match gateway settlements with your internal records.
  • Understand different payment statuses and handle them appropriately in your application.

Understanding Webhooks

Webhooks are automated messages sent by the payment gateway to your server when an event occurs, like a successful payment, a failed transaction, or a refund. They are essential for real-time updates without constant polling.

How Webhooks Work

When a payment status changes, the gateway sends an HTTP POST request containing the transaction details to a specific URL you've configured in their dashboard. Your server receives this data and processes it.

Setting up a Webhook Endpoint

You need a publicly accessible URL on your server that can receive these POST requests. For development, tools like ngrok can expose your local server to the internet. In production, ensure your server is stable and can handle these incoming requests reliably.

# Example of a simple webhook handler in Python/Flask
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/webhook/payment', methods=['POST'])
def payment_webhook():
    payload = request.json
    # Process the payload here
    print(f"Received webhook: {payload}")
    return jsonify({'status': 'success'}), 200

if __name__ == '__main__':
    app.run(port=5000)

Signature Verification: The Security Layer

Webhooks are a security risk if not verified. Anyone could send fake requests to your endpoint. Payment gateways provide a secret key and a signing mechanism to ensure the webhook request genuinely came from them.

The Process

The gateway typically signs the payload using a secret key known only to you and the gateway. This signature is sent as a header in the webhook request. Your server must then:

  1. Receive the webhook payload and signature.
  2. Re-generate the signature on your server using the same payload and your secret key.
  3. Compare the generated signature with the signature received from the gateway.
  4. If they match, the request is legitimate. Process it. If not, reject it with an error code (e.g., 400 Bad Request).

Most gateways provide libraries or clear instructions on how to perform this verification. For example, Razorpay's documentation details how to verify the signature using their SDKs.

From our projects: Always store your gateway secret keys securely, preferably in environment variables, and never hardcode them in your application's source code. This is crucial for maintaining security.

Handling Payment Statuses

Transactions can have various statuses: success, failed, pending, refunded, etc. Your application must correctly interpret and act upon these statuses.

Common Statuses and Actions

  • Success: Grant access to the service, confirm the order, update inventory.
  • Failed: Inform the user, provide an option to retry, log the error.
  • Pending: This often applies to bank transfers or other asynchronous methods. Update the user and wait for a success/failure webhook.
  • Refunded: Update the order status, notify the user if applicable.

It's a common mistake to rely solely on the immediate response from the payment API call. Always use the webhook notification as the source of truth for the final transaction status.

Refunds Management

Allowing customers to request refunds is a standard business practice. Integrating refund functionality requires interacting with the payment gateway's API.

Initiating a Refund

You will typically need the transaction ID (often called `payment_id` or similar) and the amount to be refunded. Some gateways allow partial refunds.

# Conceptual example of initiating a refund via an API call
import requests

GATEWAY_API_URL = "https://api.example-gateway.com/v1/refund"
API_KEY = "YOUR_SECRET_API_KEY"

def process_refund(payment_id, amount):
    headers = {
        "Authorization": f"Basic {API_KEY}",
        "Content-Type": "application/json"
    }
    payload = {
        "payment_id": payment_id,
        "amount": amount
    }
    response = requests.post(GATEWAY_API_URL, headers=headers, json=payload)
    return response.json()

# Example usage:
# refund_details = process_refund("pay_abc123xyz", 50000) # Amount in paise

Refund Statuses

Refunds themselves can take time to process and may have statuses like `pending`, `processed`, or `failed`. Monitor these statuses via webhooks or by querying the gateway's API.

Reconciliation: Matching Your Books

Reconciliation is the process of matching the payments that have settled in your bank account against the transactions recorded in your website's database. This is vital for financial accuracy.

The Need for Reconciliation

Payment gateways typically provide settlement reports or statements. These reports list all transactions that have been successfully processed and transferred to your account, minus any fees. You need to compare this with your internal records to ensure:

  • All successful transactions are accounted for.
  • No fraudulent transactions have slipped through.
  • Fees charged by the gateway are correct.
  • Your accounting books are balanced.

Steps for Reconciliation

  1. Obtain Settlement Reports: Download daily or monthly settlement reports from your payment gateway's dashboard.
  2. Extract Internal Data: Export a list of all transactions recorded on your website within the same period.
  3. Match Transactions: Use unique identifiers like transaction IDs or order IDs to match records between the settlement report and your internal data.
  4. Identify Discrepancies: Note any transactions present in one list but not the other, or transactions with differing amounts.
  5. Investigate and Resolve: Follow up on discrepancies. This might involve contacting the gateway support, checking for failed refunds, or identifying potential fraud.

Automating this process is highly recommended for businesses with a large volume of transactions. This often involves building custom scripts or using accounting software that can integrate with gateway reports. Our team at Infrared Techno has extensive experience in API Development & Integration, including building custom reconciliation tools.

Choosing the Right Gateway

While this guide focuses on technical integration aspects common to most gateways, consider factors like transaction fees, supported payment methods (UPI, credit/debit cards, net banking, wallets), ease of integration, and customer support when selecting a provider like Razorpay or others for your Indian website.