Razorpay Setup & Integration Guide
The razorpay provider integrates Razorpay Payment Links, Subscriptions, e-Mandates (UPI, Cards, NACH), and Dispute Management into the Kirak payments runtime.
1. Prerequisites & Installation
Section titled “1. Prerequisites & Installation”Razorpay integration requires the official razorpay Python SDK. Install the Kirak extra:
pip install "kirak[payments-razorpay]"2. Credentials & Configuration
Section titled “2. Credentials & Configuration”Step 1: Obtain API Keys
Section titled “Step 1: Obtain API Keys”In the Razorpay Dashboard:
- Navigate to Account & Settings $\rightarrow$ API Keys.
- Generate a Key ID (
rzp_test_...orrzp_live_...) and Key Secret.
Step 2: Configure kirak.json
Section titled “Step 2: Configure kirak.json”Declare the Razorpay instance:
{ "payments": { "default_provider": "razorpay", "providers": { "razorpay": { "type": "razorpay", "success_url": "https://your-domain.com/payment/success", "cancel_url": "https://your-domain.com/payment/cancel", "timeout": 15 } } }}Step 3: Set Environment Variables
Section titled “Step 3: Set Environment Variables”KIRAK_PAYMENT_RAZORPAY_KEY_ID=rzp_test_...KIRAK_PAYMENT_RAZORPAY_KEY_SECRET=...KIRAK_PAYMENT_RAZORPAY_WEBHOOK_SECRET=your_chosen_secret # Entered in Step 43. Configuring Webhooks
Section titled “3. Configuring Webhooks”In the Razorpay Dashboard, navigate to Settings $\rightarrow$ Webhooks $\rightarrow$ Add New Webhook:
- Webhook URL:
https://your-domain.com/payments/webhook/razorpay - Secret: Choose a strong secret string and set it as
KIRAK_PAYMENT_RAZORPAY_WEBHOOK_SECRET. - Select at minimum the following events:
- Payments:
payment.captured,payment.failed,payment.authorized - Subscriptions:
subscription.activated,subscription.charged,subscription.cancelled,subscription.paused,subscription.resumed - Refunds:
refund.created - e-Mandates / Tokens:
token.confirmed,token.rejected,token.cancelled
- Payments:
- Active Events: Save the webhook.
[!NOTE] Signature & Deduplication Detail: Razorpay does not sign the
x-razorpay-event-idheader. To prevent replay attacks and duplicate processing, Kirak hashes the signed raw webhook body for deduplication inpayments_webhook_events.
4. Usage Examples
Section titled “4. Usage Examples”4.1 One-Time Payment Link
Section titled “4.1 One-Time Payment Link”Amounts are passed in paise (1 INR = 100 paise):
result = await kirak.payments.initiate_payment({ "provider": "razorpay", "user_id": "usr_200", "amount": 299900, # ₹2,999.00 in paise "currency": "INR", "name": "Standard Course", "type": "one_time", "payment_email": "customer@example.in", "customer_phone": "+919876543210", # Enables SMS delivery})
payment_link_url = result["data"]["url"]4.2 Subscriptions
Section titled “4.2 Subscriptions”Create recurring plans beforehand in the Razorpay Dashboard or via API. Kirak links subscriptions via the Plan ID:
result = await kirak.payments.initiate_payment({ "provider": "razorpay", "user_id": "usr_200", "amount": 0, # Set by the Razorpay Plan "currency": "INR", "name": "Pro Monthly Plan", "type": "subscription", "subscription_plan_id": "plan_ABC123XYZ", "payment_email": "customer@example.in",})
subscription_url = result["data"]["url"][!TIP] Hosted Subscription Flow: Kirak creates the subscription without attaching a pre-existing customer ID so Razorpay’s hosted checkout page can collect customer and payment details cleanly. Razorpay’s
subscription.chargedevents are automatically processed as renewal records.
4.3 Saved Payment Methods (e-Mandates)
Section titled “4.3 Saved Payment Methods (e-Mandates)”Requires Recurring Payments enabled on your Razorpay account:
setup_payment_methodcreates a registration link.- Accepts
method(card,upi,emandate, ornach),max_amount(paise ceiling),customer_name, andcustomer_phone. - Cards and UPI charge an authorization token payment (100 paise) to register the mandate.
- e-Mandates/NACH become active only when the customer’s bank returns
token.confirmed.
5. Webhook Event Mapping
Section titled “5. Webhook Event Mapping”Normalized event |
Gateway Events | Description |
|---|---|---|
payment_completed |
payment.captured |
Payment captured and settled |
payment_failed |
payment.failed |
Payment declined or timed out |
subscription_renewed |
subscription.charged |
Recurring cycle billed |
subscription_updated |
subscription.paused, subscription.resumed |
Subscription state changed |
subscription_cancelled |
subscription.cancelled |
Subscription cancelled |
refund_completed |
refund.created |
Refund processed |
payment_method_saved |
token.confirmed |
Mandate approved by bank |
payment_method_removed |
token.rejected, token.cancelled |
Mandate rejected or revoked |