Skip to main content
This tutorial was written by Claude Code (an AI) and has not yet been reviewed. Follow along with caution. If the tutorial was helpful or a specific part was not clear/correct, please provide feedback at the bottom of the page. Thank you.
This guide covers integrating Razorpay with CoCart Preview API. Requires CoCart v4.6+ and a configured Razorpay payment gateway.

Overview

Razorpay integration with CoCart uses Razorpay Checkout for secure payment processing with automatic tokenization. This ensures PCI DSS Level 1 compliance while providing access to 100+ payment methods popular in India and Southeast Asia. Razorpay is the leading payment gateway in India, serving over 10 million businesses with support for UPI, cards, wallets, net banking, and EMI.

Prerequisites

Before implementing Razorpay checkout, ensure you have:
  1. Razorpay payment gateway configured in WooCommerce
  2. Razorpay API keys (Key ID and Key Secret)
  3. A valid cart with items added
  4. Customer billing address information
  5. Razorpay account (test mode available)

Integration Flow

1

Create Order

Generate a Razorpay order from your server
2

Load Razorpay Checkout

Initialize the Razorpay Checkout JavaScript library
3

Configure Checkout

Set up checkout options with order details
4

Open Checkout Modal

Display the Razorpay payment modal to customer
5

Handle Payment Response

Capture payment details and signature
6

Complete Checkout

Submit payment details to CoCart for verification

Step 1: Load Razorpay Checkout

Include the Razorpay Checkout script in your page:
The Razorpay Checkout library is always served over HTTPS and automatically stays up to date.

Step 2: HTML Structure

Create a checkout form for customer information:

Step 3: Create Razorpay Order

Request a Razorpay order from your server via CoCart:

Step 4: Initialize Razorpay Checkout

Set up and configure Razorpay Checkout:

Step 5: Handle Payment Success

Process successful payment response:

Step 6: Handle Payment Failure

Manage failed payment scenarios:

Complete Integration Example

Here’s a complete working implementation:

Styling

Add CSS for a polished checkout experience:

Testing

For development and testing with Razorpay:

Test Mode Credentials

Use these credentials from your Razorpay Test Mode dashboard:
  • Key ID: Starts with rzp_test_
  • Key Secret: Use for server-side operations

Test Payment Methods

Test Card Numbers:
  • Success: 4111 1111 1111 1111 (Visa)
  • Success: 5555 5555 5555 4444 (Mastercard)
  • Failure: Any card number ending in 0002
Test Card Details:
  • CVV: Any 3 digits
  • Expiry: Any future date
  • Cardholder: Any name
Test UPI IDs:
  • Success: success@razorpay
  • Failure: failure@razorpay
Test Net Banking:
  • Select any bank from test mode
  • Use credentials provided in test interface

Test Wallets

All wallet payments in test mode are automatically successful.

Supported Payment Methods

Razorpay supports 100+ payment methods:

Cards

  • Credit Cards (Visa, Mastercard, RuPay, Amex)
  • Debit Cards (All major banks)
  • International Cards

UPI

  • Google Pay
  • PhonePe
  • Paytm
  • BHIM UPI
  • Other UPI apps

Wallets

  • Paytm
  • PhonePe
  • Mobikwik
  • FreeCharge
  • Amazon Pay

Net Banking

  • All major Indian banks
  • Corporate net banking

EMI

  • Credit Card EMI
  • Debit Card EMI
  • Cardless EMI

Other Methods

  • PayLater (LazyPay, Simpl, etc.)
  • NACH/eNACH
  • Bank Transfer (NEFT/RTGS)

Troubleshooting

Common Issues

Problem: Razorpay object is undefined.Solution:
  • Check browser console for errors
  • Verify no ad blockers blocking script
  • Ensure internet connectivity
Problem: rzp.open() doesn’t show modal.Solution:
  • Disable browser popup blockers
  • Ensure HTTPS is enabled
  • Verify all required options are provided
Problem: Getting Razorpay error codes.Solution:
Common errors:
  • BAD_REQUEST_ERROR: Invalid parameters
  • GATEWAY_ERROR: Payment gateway issue
  • SERVER_ERROR: Razorpay server issue
  • PAYMENT_DECLINED: Card/bank declined
Problem: Backend doesn’t accept Razorpay.Solution:
  • Verify Razorpay gateway enabled in WooCommerce
  • Check API keys are configured
  • Ensure payment method ID is correct
Problem: Works in test but not live.Solution:
Checklist:
  • Switch to live API keys in WooCommerce
  • Verify account is activated
  • Complete KYC if required
  • Test with real card (small amount)
Problem: UPI payments failing.Solution:
  • Ensure customer has valid UPI ID
  • Verify UPI is enabled in Razorpay dashboard
  • Check UPI app is installed on device
  • Test with different UPI apps (GPay, PhonePe, Paytm)
  • Verify amount is within UPI limits

Debug Mode

Getting Help

  1. Razorpay Documentation: Razorpay Checkout Docs
  2. Razorpay Support: Contact through dashboard
  3. WooCommerce Logs: Check WooCommerce → Status → Logs

Best Practices

Security

  • Always verify payment signature on server
  • Never expose Key Secret on frontend
  • Use HTTPS for all requests
  • Implement webhook verification
  • Store payment IDs securely
  • Enable fraud detection features

User Experience

  • Pre-fill customer information
  • Show clear order summary
  • Handle modal dismissal gracefully
  • Provide payment retry options
  • Display multiple payment methods
  • Support preferred languages

Compliance

  • PCI DSS Level 1 certified
  • RBI compliant tokenization
  • Implement proper error logging
  • Follow data retention policies
  • Enable auto-refunds when needed
  • Maintain transaction records

Performance

  • Load Checkout.js from CDN
  • Cache order contexts briefly
  • Implement proper timeouts
  • Handle network failures
  • Monitor success rates
  • Set up webhook endpoints

Advanced Features

Payment Capture

By default, payments are auto-captured. For manual capture:

Recurring Payments

Set up subscriptions and recurring payments:

Offers and Discounts

Apply offers during checkout:

Webhook Handling

Razorpay sends webhooks for payment events:

Error Handling

Handle common Razorpay errors:

Troubleshooting

Common issues and solutions: Checkout modal not opening: Verify Checkout.js is loaded and Key ID is correct. Payment verification fails: Ensure signature verification logic is implemented correctly on server. Webhook not received: Check webhook URL configuration in Razorpay dashboard. Amount mismatch: Razorpay amounts are in smallest currency unit (paise for INR). Order creation fails: Verify API credentials and order amount is within limits.
Always test your Razorpay integration thoroughly using test mode before going live. Configure webhooks to handle asynchronous payment updates. Monitor your Razorpay dashboard for failed payments and disputes. Implement proper logging for debugging payment issues.

Additional Resources