PAYMENT GATEWAY
By Himanshu Shekhar | 12 Sep 2022 | (0 Reviews)
Suggest Improvement on Payment Gateway Integration Basics β Click here
Module 00 : Payment Gateway Introduction & Foundation
This module provides a complete foundation of payment gateway systems, how online payments work, the payment ecosystem, and key terminology. Understanding payment gateways is essential for developers, security testers, and merchants to ensure secure and reliable financial transactions.
0.1 What is a Payment Gateway?
A Payment Gateway is a technology service that securely transmits payment information between a customer, the merchant's website, and the bank (or payment processor). It acts as a digital βpointβofβsaleβ terminal for online transactions, encrypting sensitive card data and ensuring secure communication.
In simple words, when a customer enters their credit card details on a shopping website, the payment gateway collects that information, sends it to the acquiring bank in a secure format, and returns a response (approved or declined) so the merchant can either confirm or cancel the order.
π Key Functions
- Encryption: Protects cardholder data using TLS/SSL and often PCIβcompliant tokenisation.
- Routing: Directs the transaction to the appropriate card network (Visa, Mastercard, etc.).
- Authorization: Requests approval from the issuing bank.
- Notification: Sends approval/decline back to the merchant website.
- Settlement: Transfers funds from the customerβs bank to the merchantβs account.
π§ Real World Examples
- Stripe, PayPal, Razorpay, Square, Authorize.Net
- Paytm Payment Gateway, CC Avenue, PayU
- Amazon Pay, Google Pay for merchants
0.2 How Online Payments Work (StepβbyβStep)
An online payment involves multiple parties and a series of secure messages. Below is the typical flow for a cardβbased transaction. Understanding each step helps identify where security breaches can happen.
- Customer initiates purchase: Adds items to cart and clicks βCheckoutβ.
- Enters payment details: Fills in card number, expiry, CVV, and billing address on the merchantβs site.
- Browser sends encrypted data: Data is encrypted using TLS and sent to the merchantβs server (or directly to the gateway via API).
- Merchant forwards to payment gateway: The merchantβs server sends the payment data to the gateway (Stripe, Razorpay, etc.).
- Gateway validates and routes: The gateway checks for fraud, tokenises the card if needed, and sends the request to the acquiring bank / processor.
- Processor contacts card network: Acquirer sends the request to the card network (Visa/Mastercard).
- Card network forwards to issuing bank: The customerβs bank receives the request, checks funds/validity, and records the transaction.
- Authorization response travels back: Approve/decline message goes back through the chain (issuer β network β acquirer β gateway β merchant).
- Merchant displays result: The website shows βPayment Successfulβ or βDeclinedβ.
- Settlement (later): Funds move from issuing bank to acquiring bank to merchant account (usually after 2β3 days).
Customer β Merchant β Gateway β Acquirer β Card Network β Issuer β (response reverse)
0.3 Payment Gateway vs Payment Processor (Key Differences)
Though often used interchangeably, a payment gateway and a payment processor serve different roles. A gateway transmits payment data; a processor settles the funds.
| Aspect | Payment Gateway | Payment Processor |
|---|---|---|
| Role-- | Authorisation & communication | Settlement & fund movement |
| Action-- | Encrypts and routes transaction | Transfers money between banks |
| When used-- | During checkout (realβtime) | After approval (batch settlement) |
| Example companies-- | Stripe, Braintree, Razorpay (they often act as both) | First Data, Worldpay, Fiserv |
Many modern providers (like Stripe, Razorpay) offer both gateway and processing services, so the distinction blurs.
0.4 Payment Ecosystem β Who is Involved?
The payment ecosystem includes several entities, each with specific responsibilities. Understanding them helps identify attack surfaces and compliance requirements.
πΉ Core Players
- Customer (Cardholder): The person buying a product/service.
- Merchant: The business selling products.
- Issuing Bank: The customerβs bank that issued the credit/debit card.
- Acquiring Bank (Acquirer): The merchantβs bank that receives the money.
- Card Networks (Schemes): Visa, Mastercard, American Express, RuPay, etc. β set rules and route transactions.
- Payment Gateway: Technology interface for transmitting data.
- Payment Processor: Entity that handles settlement and sometimes gateway services.
πΈ Additional Entities
- Payment Service Provider (PSP): Allβinβone gateway + merchant account (e.g., Stripe).
- Independent Sales Organisation (ISO): Reseller of merchant services.
- Payment Facilitator (PayFac): Subβmerchant onboarding (e.g., Square).
- ThirdβParty Security Assessors (QSA): Auditors for PCI compliance.
0.5 Merchant Accounts & Settlements
A merchant account is a special bank account that allows businesses to accept credit/debit card payments. Settlement is the process of moving funds from the customerβs bank to the merchantβs account.
π Types of Merchant Accounts
- Dedicated Merchant Account: Owned by one business β better rates, longer approval.
- Aggregated Account (PayFac): Shared account (Stripe, Square) β fast setup, higher fees.
- ThirdβParty Processor: A service that provides both gateway and merchant account.
π° Settlement Flow
- Transaction authorised β customerβs funds are reserved.
- Capture β merchant initiates capture (or autoβcapture after shipping).
- Batch settlement β at end of day, all captured transactions are sent as a batch.
- Funding β funds appear in merchant account after 1β3 business days (including fees deduction).
0.6 Payment Flow Architecture (APIβbased & Hosted)
Payment gateways offer different integration methods. The architecture affects security responsibilities and PCI DSS scope.
Customer is redirected to the gatewayβs payment page (e.g., PayPal).
- β Minimal PCI burden (merchant never touches card data).
- β Less customisation.
- β Customer leaves your site.
Gateway provides a JavaScript library that collects card data directly (Stripe Elements, Razorpay).
- β Card data never touches your server.
- β High customisation.
- β Requires handling webhooks.
Merchant collects card data and sends it to gateway via API.
- β High PCI compliance burden (SAQ D).
- β Complete control.
- β Risk of storing/transmitting card data insecurely.
Modern best practice is tokenisation / iFrame β it balances security and user experience.
0.7 Types of Online Payments (Use Cases)
Payment gateways support different transaction types depending on the business model.
| Type | Description | Example |
|---|---|---|
| Oneβtime payment-- | Single charge for a product/service. | Buying a laptop. |
| Recurring (Subscription)-- | Charged automatically at intervals. | Netflix, Spotify. |
| Authorisation & Capture-- | Authorise amount then capture later (e.g., after shipping). | Hotel booking, preβorder. |
| MOTO (Mail Order / Telephone Order)-- | Card details taken over phone/email. | Callβcentre orders. |
| Tokenised payments-- | Use a token instead of card number for repeat purchases. | Saved cards in Uber. |
| Refunds / Voids-- | Reverse a transaction (full or partial). | Customer returns item. |
0.8 UPI, Cards, Wallets & Net Banking (Popular Methods)
Modern gateways support multiple payment instruments. Each has a different security model.
| Method | How It Works | Authentication | Security Note |
|---|---|---|---|
| Credit/Debit Card | Card number, expiry, CVV, sometimes 3D Secure (OTP). | OTP or password | High fraud risk if card details leaked. |
| UPI (Unified Payments Interface) | Virtual ID (user@bank) + UPI PIN or biometric. | Mobileβbased PIN / device binding | Relatively secure, but device cloning is a risk. |
| Digital Wallets (PayTM, Google Pay, Apple Pay) | Stored payment methods, tokenised transactions. | Biometrics, PIN, device unlock | Tokenisation reduces exposure of actual card details. |
| Net Banking | Redirect to bank login page, authenticate with credentials + OTP. | Password + OTP / hardware token | Vulnerable to phishing and MITM if not properly validated. |
| Buy Now, Pay Later (BNPL) | Shortβterm credit (Klarna, Afterpay). | Soft credit check + identity verification | Risk of identity theft. |
0.9 Sandbox vs Production Environment (Testing vs Live)
Every payment gateway provides a sandbox (test) environment that mimics the live system but uses test credentials, test card numbers, and does not move real money.
- Use test API keys.
- Simulate all transaction responses (approve, decline, error).
- No real money deducted.
- Use test card numbers (e.g., 4242 4242 4242 4242 for Stripe).
- Safe for development and automated testing.
- Use live API keys (must be kept secret).
- Real money moves.
- Requires PCI DSS compliance.
- Logging must avoid PAN.
- Webhook URLs must be secure (HTTPS, signature validation).
0.10 Common Payment Terminology β Glossary
Familiarise yourself with these terms β they appear in every payment integration and security assessment.
- PAN: Primary Account Number (full card number).
- CVV/CVC: Card verification value (3 or 4 digits).
- Tokenisation: Replace PAN with a nonβsensitive token.
- 3D Secure: Extra authentication step (OTP/password) for cards.
- PCI DSS: Payment Card Industry Data Security Standard.
- Acquirer: Merchantβs bank.
- Issuer: Customerβs bank.
- Authorisation: Freezing funds (not yet transferred).
- Capture: Transferring authorised funds.
- Settlement: Final movement of funds.
- Chargeback: Customer disputes a transaction.
- Refund: Return of funds to customer (reverse transaction).
- Webhook: HTTP callback from gateway to merchant for async events.
- Recurring Billing: Scheduled automatic payments.
- MOTO: Mail Order / Telephone Order (card not present).
- ISO 8583: Standard for financial transaction messages.
π Module 00 : Payment Gateway Introduction & Foundation β Completed
You have successfully completed this module of Payment Gateway Integration Basics.
Keep building your expertise step by step β Learn Next Module β
Module 01 : Payment Gateway Core APIs
This module dives deep into the core APIs of payment gateways β REST fundamentals, authentication, webhooks, signature verification, and transaction management. Mastering these APIs is essential for developers integrating payment systems and for security testers auditing payment flows.
1.1 REST API Fundamentals
Most modern payment gateways (Stripe, Razorpay, PayPal) expose a RESTful API that allows merchants to programmatically create payments, refunds, manage customers, and handle webhooks.
πΉ Core REST Concepts
- Resources: Objects like payments, refunds, customers, subscriptions.
- HTTP Methods: GET (retrieve), POST (create), PUT/PATCH (update), DELETE.
- Statelessness: Each request contains all necessary information; server does not store client context.
- JSON: Most gateways use JSON for requests and responses.
πΈ Typical Payment API Endpoints
POST /v1/paymentsβ Create a payment.GET /v1/payments/{id}β Retrieve a payment.POST /v1/payments/{id}/refundsβ Refund a payment.GET /v1/refunds/{id}β Check refund status.POST /v1/webhooksβ Configure webhook endpoints.
1.2 API Authentication & Tokens
Payment gateways require authentication to ensure only authorised merchants can access APIs. Common methods include API keys, OAuth2, and JWT (JSON Web Tokens).
| Method | How it works | Security Level | Examples |
|---|---|---|---|
| API Key (Basic Auth)-- | Key sent as username:password in Authorization header. | Medium β risk if sent over HTTP (must use TLS). | Stripe (sk_live_...), Razorpay (rzp_live_...). |
| Bearer Token (OAuth2)-- | Short-lived token obtained via client credentials or user authorisation. | High β tokens can be scoped and rotated. | PayPal, Braintree. |
| JWT (JSON Web Tokens)-- | Signed token containing claims; verified by gateway. | High if signature algorithm is strong (HS256/RS256). | Some custom integrations. |
# Example: API Key in curl
curl -X GET https://api.paymentgateway.com/v1/payments \
-H "Authorization: Basic base64(publishable_key:secret_key)"
# Bearer token
curl -X GET https://api.paymentgateway.com/v1/payments \
-H "Authorization: Bearer sk_live_abc123"
1.3 API Keys & Secret Keys (Publishable vs Secret)
Payment gateways typically provide two types of keys: a publishable key (clientβside) and a secret key (serverβside). Mixing them up leads to security breaches.
- Used in clientβside code (web/mobile).
- Can only create tokens (PCIβsafe) β cannot charge or refund.
- Example:
pk_live_4eC39HqLyjWDarjtT(Stripe). - Safe to embed in HTML/JavaScript.
- Never exposed to the client β kept on the server.
- Can perform any operation (create payment, refund, manage webhooks).
- Example:
sk_live_4eC39HqLyjWDarjtT(Stripe). - Compromise = full account takeover.
# Correct usage (serverβside Node.js)
const stripe = require('stripe')('sk_live_...'); // secret key
# Incorrect (exposing secret key in frontend JS)
const stripe = Stripe('sk_live_...'); // DANGEROUS!
1.4 JSON Requests & Responses
Payment APIs use JSON (JavaScript Object Notation) for data exchange. Understanding the structure helps in integration and security testing.
π€ Create Payment Request (example β Razorpay style)
POST /v1/payments HTTP/1.1
Host: api.razorpay.com
Authorization: Basic base64(key:secret)
Content-Type: application/json
{
"amount": 1000, // βΉ10.00 (in paise)
"currency": "INR",
"receipt": "order_123",
"payment_capture": 1,
"notes": {
"customer_email": "user@example.com"
}
}
π₯ Success Response
{
"id": "pay_abc123",
"entity": "payment",
"amount": 1000,
"currency": "INR",
"status": "captured",
"method": "card",
"created_at": 1612345678
}
1.5 Webhooks & Event Handling
Webhooks are asynchronous HTTP callbacks that notify your server when an event occurs (e.g., payment success, refund processed). They are essential for realβtime order updates, especially when the user is not on the website.
π‘ How a Webhook Works
- Merchant registers a webhook URL (e.g.,
https://shop.com/webhook) in the gateway dashboard. - When a payment succeeds, the gateway sends a POST request to that URL with a JSON payload.
- Your server processes the payload and updates the order status.
π Security for Webhooks
- Signature verification: Gateways sign webhook payloads with a secret. Verify the signature before processing (prevents fake events).
- Idempotency: Webhooks may be retried β ensure your handler can process duplicates safely.
- Authentication: Require a custom header or use the signature for authentication.
- Logging: Log all incoming webhooks for auditing and debugging.
# Example webhook receiver (Node.js/Express)
app.post('/webhook', express.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['x-razorpay-signature'];
const webhookSecret = process.env.WEBHOOK_SECRET;
try {
const event = razorpay.validateWebhookSignature(req.body, sig, webhookSecret);
if (event.event === 'payment.captured') {
// Update order
}
res.status(200).send('OK');
} catch (err) {
res.status(400).send('Invalid signature');
}
});
1.6 Callback URLs & Redirects (Synchronous)
While webhooks are asynchronous, many gateways also support redirectβbased callbacks for hosted payment pages. After payment, the customer is redirected back to your site with payment status in the URL (GET parameters).
π Typical Redirect Flow
- Customer clicks βPayβ and is sent to gatewayβs hosted page.
- After payment, gateway redirects to
return_url(e.g.,https://shop.com/return) with parameters likepayment_id,order_id,status. - Merchant validates the data (often using a signature) and updates the order.
β οΈ Security Risks with Redirects
- Parameter tampering: A user might modify URL parameters (e.g., change status to success).
- Missing signature: Without signature validation, anyone can fake a success callback.
- Open redirect: If the merchant does not validate return_url, attackers can redirect customers to phishing sites.
# Example vulnerable redirect handling (DO NOT DO THIS)
if ($_GET['status'] == 'success') {
$order->paid = true; // Attacker can craft ?status=success
}
1.7 Signature Verification (Integrity & Authenticity)
Payment gateways sign webhook payloads and sometimes redirect parameters using a secret key. Verifying the signature proves the data was sent by the gateway and not tampered with.
π How Signature Verification Works
- The gateway generates a hash (HMAC) of the payload using a secret key (webhook secret).
- The hash is sent in a header (e.g.,
X-Razorpay-Signature). - Your server computes the same hash using the payload and its copy of the secret.
- If they match, the payload is genuine.
# Python example (Razorpay)
import razorpay
client = razorpay.Client(auth=("key", "secret"))
webhook_secret = "your_webhook_secret"
payload = request.get_data()
signature = request.headers.get('X-Razorpay-Signature')
try:
client.utility.verify_webhook_signature(payload, signature, webhook_secret)
# Payment is legitimate
except razorpay.errors.SignatureVerificationError:
# Reject request
1.8 Payment Verification Workflow (Best Practices)
A secure payment verification workflow combines synchronous callbacks, webhooks, and API fallbacks to ensure no failed payment is marked successful and no successful payment is lost.
β Recommended Workflow
- Create order β Store order details, generate unique order ID.
- Initiate payment β Call gateway API to create a payment session (or redirect to hosted page).
- After redirect (if used) β Extract payment ID from URL parameters; do NOT rely on status parameter.
- Query payment status via API β Call
GET /v1/payments/{id}to get authoritative status. - Update order β Only after API confirms
capturedorpaid. - Handle webhooks β Listen to
payment.capturedevents to cover cases where user closes the browser before redirect. - Idempotent processing β Use order ID as idempotency key to prevent double processing.
1.9 Transaction Status APIs (GET Payment)
The status API allows you to query a specific transaction by its ID. It returns the most upβtoβdate information including status, amount, method, and failure reason.
Common Status Values
| Status | Meaning |
|---|---|
createdPayment object created but not yet authorised. | |
authorized--_:Funds reserved, not captured yet. | |
captured--_:Funds transferred to merchant β success. | |
failed--_:Payment declined (insufficient funds, expired card, etc.). | |
refunded--_:Full refund processed. |
# Example GET status API (Stripe)
curl https://api.stripe.com/v1/payments/py_abc123 \
-u sk_live_...:
# Response
{
"id": "py_abc123",
"object": "payment_intent",
"amount": 2000,
"status": "succeeded"
}
1.10 Refund APIs (Processing Returns)
Refund APIs allow merchants to return funds to customers partially or fully. They are critical for customer satisfaction but also a vector for fraud if not properly secured.
π¦ Typical Refund Request
POST /v1/payments/pay_abc123/refunds
Authorization: Basic base64(key:secret)
Content-Type: application/json
{
"amount": 500, // refund amount in minor units (paise/cents)
"reason": "customer_request",
"notes": {
"order_id": "order_xyz"
}
}
π₯ Refund Response
{
"id": "ref_abc123",
"payment_id": "pay_abc123",
"amount": 500,
"status": "processed",
"created_at": 1612345678
}
π Security Considerations
- Authentication: Only authorised users (admin) should be able to initiate refunds.
- Idempotency: Use unique refund IDs or idempotency keys to prevent double refunds.
- Audit logs: Record who initiated refund and why.
- Partial refunds: Some gateways allow multiple partial refunds β track remaining amount.
- Webhooks: Listen to
refund.created/refund.updatedevents to sync status.
π Module 01 : Payment Gateway Core APIs β Completed
You have successfully completed this module of Payment Gateway Integration Basics.
Keep building your expertise step by step β Learn Next Module β
Module 02 : Payment Gateway Security & Compliance
This module covers the essential security controls and compliance requirements for payment gateway integrations. Topics include HTTPS, PCI-DSS, API key management, webhook security, fraud detection, rate limiting, and secure logging. Implementing these measures protects both merchants and customers from financial fraud and data breaches.
2.1 HTTPS & SSL Certificates (Encryption in Transit)
All communication between the merchant server, payment gateway, and customer browser must be encrypted using TLS (Transport Layer Security). HTTPS protects payment card data, API keys, and webhook payloads from eavesdropping and tampering.
π Why HTTPS is Mandatory
- Prevents manβinβtheβmiddle (MITM) attacks.
- PCI-DSS requirement (all cardholder data transmission).
- Protects API credentials in transit.
- User trust β browsers show βSecureβ lock icon.
β οΈ Common HTTPS Misconfigurations
- Mixed content (HTTP resources on HTTPS page).
- Weak cipher suites (RC4, 3DES, export ciphers).
- Expired or selfβsigned certificates.
- Missing HTTP Strict Transport Security (HSTS).
π‘οΈ Best Practices
- Use TLS 1.2 or TLS 1.3 only β disable SSLv2, SSLv3, TLS 1.0, TLS 1.1.
- Obtain certificates from trusted Certificate Authorities (CA).
- Enable HSTS (Strict-Transport-Security) to enforce HTTPS for all subdomains.
- Regularly test with SSL Labs (https://www.ssllabs.com/ssltest/).
# Example HSTS header (Apache)
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
2.2 PCI-DSS Compliance (Payment Card Industry Data Security Standard)
PCI-DSS is a set of security standards enforced by major card brands (Visa, Mastercard, etc.). Any business that stores, processes, or transmits cardholder data must comply. Nonβcompliance can result in heavy fines, increased transaction fees, and loss of ability to accept credit cards.
π Key PCI-DSS Requirements (v3.2.1)
| Requirement | Description |
|---|---|
| Build & Maintain Secure Network | Configure firewalls properly, use secure system configurations, and avoid default usernames/passwords. |
| Protect Cardholder Data | Encrypt cardholder data during transmission using TLS/HTTPS and never store sensitive authentication data such as CVV or track data. |
| Maintain Vulnerability Management | Regularly update systems, apply security patches, and use anti-malware or endpoint protection solutions. |
| Implement Strong Access Control | Restrict access to cardholder data based on business need-to-know and enforce strong authentication mechanisms. |
| Monitor & Test Networks | Enable logging, monitor network activity, use intrusion detection systems, and perform regular penetration testing. |
| Maintain Information Security Policy | Create documented security policies, conduct risk assessments, and train employees on security best practices. |
π― SAQ (SelfβAssessment Questionnaire) Types
- SAQ A: Redirect / iFrame (tokenisation) β lowest burden.
- SAQ A-EP: Partially hosted eβcommerce.
- SAQ D: Store or process card data directly β highest burden.
2.3 Secure API Key Management (Secrets Protection)
API keys (especially secret keys) are the digital keys to your payment account. A leaked secret key allows an attacker to create charges, refunds, and even modify webhook endpoints.
π Doβs and Donβts
- Store keys in environment variables, never in code.
- Use dedicated secrets manager (AWS Secrets Manager, HashiCorp Vault, Doppler).
- Rotate keys regularly (e.g., every 90 days).
- Create separate keys for development and production.
- Restrict key permissions (e.g., readβonly API keys for monitoring).
- Hardβcode keys in source code (especially public repos).
- Commit keys to GitHub, GitLab, Bitbucket.
- Send keys via email, Slack, or chat.
- Embed keys in clientβside JavaScript (publishable key is fine).
π οΈ Detection & Response
- Use git hooks (e.g., gitβsecrets) to prevent accidental commits.
- Enable key rotation API β gateways allow instant revocation.
- Monitor API activity for unexpected geographic locations or IPs.
# .env file example (never commit)
STRIPE_SECRET_KEY=sk_live_...
RAZORPAY_KEY_ID=rzp_live_...
2.4 Payment Tampering Prevention (Amount, Currency, ID Manipulation)
Attackers often try to modify payment parameters β amount, currency, merchant ID, or order reference β to pay less, receive money, or bypass checks. Preventing tampering requires serverβside validation and cryptographic signatures.
π― Common Tampering Attacks
- Amount manipulation: Change amount from $100 to $1 in request.
- Currency change: Switch from USD to INR to reduce value.
- ID substitution: Replace merchant ID to pay a different seller.
- Callback spoofing: Fake success notification.
π‘οΈ Prevention Techniques
- Serverβside amount validation: Never trust clientβside price β recalculate from order data.
- Signed payloads: Use HMAC signatures for all callbacks and webhooks.
- Idempotency keys: Prevent replay of successful requests.
- Strict input validation: Reject unexpected fields or data types.
# Example: Serverβside amount validation (Node.js)
function createPayment(orderId) {
const order = db.getOrder(orderId);
const total = order.totalPrice; // recalculated from DB, not from client
return gateway.payments.create({ amount: total, currency: 'INR' });
}
2.5 Webhook Security (Signature Verification & Idempotency)
Webhooks are critical for asynchronous payment status updates, but they are also a prime attack vector. Without proper security, attackers can send fake βpayment succeededβ events and trick your system into fulfilling orders without actual payment.
π Essential Webhook Security Controls
| Control | Why It Matters |
|---|---|
| Signature Verification | Ensures the webhook request genuinely came from the payment gateway and was not modified or sent by an attacker. |
| Idempotency Keys | Prevents duplicate transaction processing when gateways retry webhook delivery multiple times. |
| HTTPS Only | Encrypts webhook payloads during transmission. Most payment gateways require secure HTTPS endpoints. |
| IP Whitelisting (Optional) | Restricts webhook requests to trusted payment gateway IP addresses for additional security protection. |
| Authentication via Custom Header | Adds an extra security layer using secret API keys or tokens inside request headers. |
# Example: Webhook signature verification (Stripe in Node.js)
const stripe = require('stripe')('sk_...');
const endpointSecret = 'whsec_...';
app.post('/webhook', express.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle event (payment_intent.succeeded, etc.)
res.json({received: true});
});
2.6 Fraud Detection Basics (Rules & Machine Learning)
Payment fraud includes card testing, unauthorised transactions, friendly fraud (chargebacks), and account takeover. Gateways offer builtβin fraud detection, but merchants should add extra layers.
π Common Fraud Indicators
- Multiple failed payment attempts in short time (card testing).
- Orders from highβrisk countries or anonymous proxies (VPN / Tor).
- Unusually large order amounts or high velocity.
- Mismatch between billing and shipping address (for physical goods).
- Suspicious email addresses (temporary, misspelt).
π‘οΈ Fraud Prevention Techniques
- 3D Secure (3DS2): Transfers liability to issuer β reduces chargeback risk.
- Velocity checks: Limit number of attempts from same IP or card.
- AVS (Address Verification Service): Match billing address with cardholderβs bank.
- CVV check: Require CVV for cardβnotβpresent transactions.
- Machine learning models: Stripe Radar, Braintree Advanced Fraud Protection.
- Manual review queue: Flag highβrisk orders for human verification.
payment.fraud_risk β can autoβcancel or hold orders.
2.7 Rate Limiting & Abuse Prevention (Throttling)
Attackers may abuse payment APIs by submitting many requests β to test stolen cards, bruteβforce discounts, or cause denial of service. Rate limiting prevents such abuse.
π― What to Rate Limit
- Create payment endpoint: Limit by IP, session, or user account.
- Refund API: Stricter limits β typically adminβonly with low threshold.
- Webhook receiver: Not usually rateβlimited, but ensure idempotency.
- Login / OTP generation: Critical for preventing credential stuffing.
π§ Implementation Strategies
- Sliding window counters: Redis + timeβbuckets.
- Token bucket: Allows bursts then throttles.
- Perβuser/account limits: Prevent one account from flooding.
- Penalty box: Temporary blocks (e.g., 1 hour) after exceeding limits.
# Example: Expressβrateβlimit (Node.js)
const rateLimit = require('express-rate-limit');
const createPaymentLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 10, // limit each IP to 10 requests
message: 'Too many payment attempts, try later'
});
app.post('/api/payments', createPaymentLimiter, paymentHandler);
2.8 Secure Transaction Logging (Audit Trails)
Comprehensive logging is required by PCI-DSS and helps during incident investigations. However, logs must never contain sensitive cardholder data (PAN, CVV, track data).
π What to Log (and what NOT to log)
β Log these (safe)
- Payment ID, order ID, amount, currency, status.
- Timestamp, IP address (may be limited due to PII laws).
- User ID / session ID (if not personal).
- API endpoint called, response status code.
- Webhook received events (with payload but not secrets).
β Never log these
- Full PAN (card number) β hashing or truncation allowed.
- CVV / CVC β PCI forbids storage even in logs.
- Expiry date (can be kept separately with PAN hashed).
- Track data from magnetic stripe.
- API secret keys, signatures, or authentication tokens.
π Best Practices
- Use centralized logging (ELK, Splunk, Datadog) with strict access controls.
- Enable log rotation and retention policies (e.g., 1 year for PCI).
- Mask or truncate PAN β only last 4 digits can be stored.
- Integrate with SIEM for realβtime alerts.
- Regularly audit log access β who can read them.
# Example: Logging a payment event (JSON format)
{
"timestamp": "2025-01-15T10:30:00Z",
"event": "payment.created",
"payment_id": "pay_abc123",
"order_id": "order_xyz",
"amount": 1000,
"currency": "INR",
"status": "created",
"ip": "203.0.113.45",
"user_id": "user_789",
"panic": null
}
π Module 02 : Payment Gateway Security & Compliance β Completed
You have successfully completed this module of Payment Gateway Integration Basics.
Keep building your expertise step by step β Learn Next Module β
Module 03 : Stripe Payment Gateway
This module provides a comprehensive guide to integrating Stripe, one of the most popular payment gateways. You'll learn about account setup, dashboard navigation, API keys, Checkout, Payment Intents, webhooks, refunds, and subscriptions β with practical examples and security best practices.
3.1 Stripe Account Setup (Production & Test Mode)
Stripe offers both Test Mode (sandbox) and Live Mode (production). Always start with Test Mode to integrate and test without moving real money.
π StepβbyβStep Setup
- Go to https://dashboard.stripe.com/register and sign up.
- Verify your email address.
- Complete business details (country, type, product description).
- Toggle between Test Mode and Live Mode using the switcher in the dashboard.
- In Test Mode, you can use test card numbers (e.g.,
4242 4242 4242 4242) to simulate payments.
3.2 Stripe Dashboard Overview (Key Sections)
The Stripe Dashboard is your central control panel for managing payments, customers, disputes, and settings.
| Section | Purpose |
|---|---|
| Payments | View all transactions and filter payments by status such as succeeded, pending, failed, or refunded. |
| Customers | Manage customer profiles, billing details, and saved payment methods. |
| Subscriptions | Handle recurring billing, subscription plans, renewals, and automated payment cycles. |
| Disputes | Monitor chargebacks, manage disputes, and submit evidence for representment. |
| Webhooks | Configure webhook endpoints to receive real-time asynchronous payment events and notifications. |
| Developers | Access API keys, integration logs, SDK documentation, and event monitoring tools. |
π§ Key Developer Tools
- API Keys: Publishable key (pk_test_...) and secret key (sk_test_...).
- Webhooks: Add endpoints, view recent deliveries, and retry failed events.
- Logs: Detailed request/response logs for debugging.
- Events: All webhookβtriggered events listed with payloads.
3.3 Stripe API Keys (Publishable vs Secret)
Stripe provides two types of keys: publishable key (clientβside) and secret key (serverβside). Never expose the secret key in clientβside code.
- Used in frontend JavaScript (Stripe.js, Elements).
- Can create tokens and payment methods β cannot charge or refund.
- Safe to embed in HTML/JS.
- Example:
pk_test_4eC39HqLyjWDarjtT
- Used on the server to perform any API action (create payment, refund, manage subscriptions).
- Must be kept secret β never commit to version control.
- Rotate if compromised.
- Example:
sk_test_4eC39HqLyjWDarjtT
# Storing keys in .env (Node.js)
STRIPE_PUBLISHABLE_KEY=pk_test_...
STRIPE_SECRET_KEY=sk_test_...
# Using keys
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
3.4 Stripe Checkout Integration (Preβbuilt Payment Page)
Stripe Checkout is a hosted payment page that handles card collection, 3D Secure, and payment confirmation. It reduces PCI compliance burden and is the quickest way to start accepting payments.
π Integration Steps (Serverβside)
- Create a Checkout Session on your server.
- Redirect the customer to the session URL.
- Stripe handles the rest β customer pays, and you receive webhooks.
# Node.js (Express) example
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
app.post('/create-checkout-session', async (req, res) => {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [{
price_data: {
currency: 'usd',
product_data: { name: 'T-shirt' },
unit_amount: 2000, // $20.00
},
quantity: 1,
}],
mode: 'payment',
success_url: 'https://your-site.com/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url: 'https://your-site.com/cancel',
});
res.json({ id: session.id });
});
On the frontend, redirect the customer using session.id with Stripe.js:
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe('pk_test_...');
stripe.redirectToCheckout({ sessionId: '{{session.id}}' });
</script>
3.5 Stripe Payment Intents API (Manual Payment Flow)
Payment Intents API gives you full control over the payment lifecycle β from authorisation to capture. It is used for custom checkout forms, delayed capture, or when you need to handle 3D Secure yourself.
π¦ Payment Intent Steps
- Create a PaymentIntent on the server with amount and currency.
- Return the client secret to the frontend.
- Confirm the payment on the client side (Stripe.js).
- Listen to webhook events (
payment_intent.succeeded) to update order status.
# Create PaymentIntent (Node.js)
const paymentIntent = await stripe.paymentIntents.create({
amount: 2000,
currency: 'usd',
payment_method_types: ['card'],
});
// Response includes client_secret (for frontend)
{ id: 'pi_123', client_secret: 'pi_123_secret_abc' }
# Frontend confirmation (Stripe Elements)
const { error, paymentIntent } = await stripe.confirmCardPayment(clientSecret, {
payment_method: { card: cardElement }
});
π§ Advanced Options
- Capture method:
manual(authorise only, capture later). - Payment method types: Add
card,link,us_bank_account. - Statement descriptor: Custom text on customerβs bank statement.
- Metadata: Store custom data (order ID, user ID).
3.6 Stripe Webhooks (Asynchronous Event Handling)
Stripe sends webhooks for asynchronous events like payment success, refund completion, subscription renewal, and failed payments. Webhooks are critical for reliable order processing.
π‘ Common Webhook Events
| Event | When triggered |
|---|---|
payment_intent.succeeded | Payment succeeded (captured). |
payment_intent.payment_failed | Payment failed β decline, insufficient funds. |
charge.refunded | Refund processed. |
checkout.session.completed | Checkout session completed. |
invoice.payment_succeeded | Subscription invoice paid. |
π Webhook Security
- Stripe signs each webhook with a secret (whsec_...).
- Verify signature using Stripe library β reject unsigned requests.
- Use HTTPS for webhook endpoint.
- Respond with
200 OKwithin 10 seconds; otherwise Stripe retries. - Implement idempotency β process each event only once (use
event.id).
# Webhook handler (Node.js)
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const endpointSecret = 'whsec_...';
app.post('/webhook', express.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
// Update order status in your DB
break;
default:
console.log(`Unhandled event type ${event.type}`);
}
res.status(200).end();
});
3.7 Stripe Refunds & Subscriptions (Recurring Billing)
Stripe supports full and partial refunds via API or dashboard. Subscriptions allow recurring billing with flexible plans, trial periods, and automatic invoicing.
π° Processing Refunds
# Full refund of a payment intent
const refund = await stripe.refunds.create({
payment_intent: 'pi_123',
});
# Partial refund
const refund = await stripe.refunds.create({
charge: 'ch_123',
amount: 500, // refund $5.00
});
- Refunds are synchronous β they happen immediately (but bank processing may take days).
- You can refund up to the original captured amount.
- Webhook event:
charge.refunded.
π Setting Up Subscriptions
- Create a Product and Price (oneβtime, recurring).
- Create a Customer (if not exists).
- Create a Subscription with the price ID and customer ID.
- Stripe handles recurring charges, prorations, and invoice emails.
# Create a subscription (Node.js)
const subscription = await stripe.subscriptions.create({
customer: 'cus_123',
items: [{ price: 'price_1ABC123' }],
expand: ['latest_invoice.payment_intent'],
});
trial_period_days) and enable automatic tax calculation.
invoice.payment_succeeded and customer.subscription.deleted.
π Module 03 : Stripe Payment Gateway β Completed
You have successfully completed this module of Payment Gateway Integration Basics.
Keep building your expertise step by step β Learn Next Module β
Module 04 : PayPal Payment Gateway
This module provides a comprehensive guide to integrating PayPal, one of the worldβs most recognised payment platforms. You will learn about account setup, API credentials, Checkout, Instant Payment Notifications (IPN), webhooks, and subscriptions β with realβworld examples and security considerations.
4.1 PayPal Business Setup (Sandbox & Live)
To integrate PayPal, you need a Business Account. PayPal provides a Sandbox environment for testing with fake money and a Live environment for real transactions.
π StepβbyβStep Setup
- Go to https://www.paypal.com/business/signup and sign up for a Business account.
- Verify your email address and provide basic business information.
- Access the Developer Dashboard at https://developer.paypal.com.
- Create Sandbox test accounts (buyer and seller) under Sandbox β Accounts.
- Toggle between Sandbox and Live mode in your API credentials.
4.2 PayPal Developer Console β Key Sections
The PayPal Developer Dashboard is your central hub for managing apps, API credentials, webhooks, and testing tools.
| Section | Purpose |
|---|---|
| My Apps & Credentials--_:Create REST API apps, get Client ID and Secret. | |
| Sandbox Accounts--_:Create and manage test buyers/sellers with preβloaded balances. | |
| Webhooks--_:Register HTTPS endpoints for realβtime event notifications. | |
| IPN Simulator--_:Test Instant Payment Notifications manually. | |
| Transactions Search (Sandbox)--_:View test payments and refunds. |
4.3 PayPal API Credentials (Client ID & Secret)
PayPal uses OAuth2 for API authentication. You receive a Client ID (public) and a Secret (confidential). Never expose the secret on the client side.
- Used in frontend JavaScript (PayPal Buttons).
- Can be safely embedded in HTML.
- Example:
Adf3jkl...abc
- Used for serverβtoβserver API calls (e.g., capturing payment, refund).
- Never expose in clientβside code.
- Store in environment variables.
- Rotate if compromised.
# Store credentials in .env (Node.js)
PAYPAL_CLIENT_ID=Adf3jkl...
PAYPAL_SECRET=EdfG45...
# Use with PayPal SDK
const paypal = require('@paypal/checkout-server-sdk');
const environment = new paypal.core.SandboxEnvironment(process.env.PAYPAL_CLIENT_ID, process.env.PAYPAL_SECRET);
const client = new paypal.core.PayPalHttpClient(environment);
4.4 PayPal Checkout Integration (Hosted & Custom)
PayPal offers two main integration methods: the hosted smart buttons (JavaScript) and the serverβside Orders API for full control. Both support cards, PayPal balance, and local payment methods.
π Smart Buttons (Hosted β Easiest)
<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID¤cy=USD"></script>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: { value: '10.00' }
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name);
// Send the order ID to your server for verification
});
}
}).render('#paypal-button-container');
</script>
βοΈ ServerβSide Orders API (More Control)
# Node.js β Create order
const request = new paypal.orders.OrdersCreateRequest();
request.requestBody({
intent: 'CAPTURE',
purchase_units: [{ amount: { currency_code: 'USD', value: '10.00' } }]
});
const order = await client.execute(request);
// Return order.id to frontend
res.json({ id: order.result.id });
4.5 PayPal IPN & Webhooks (Asynchronous Notifications)
PayPal provides two asynchronous notification mechanisms: Instant Payment Notification (IPN) (legacy, POST formβencoded) and Webhooks (modern, JSON). Both are used to update order statuses in real time.
π‘ Webhook Events (Recommended)
| Event | When triggered | |
|---|---|---|
PAYMENT.CAPTURE.COMPLETED--_:Payment captured successfully. |
PAYMENT.CAPTURE.DENIED--_:Payment declined. |
|
PAYMENT.CAPTURE.REFUNDED--_:Refund processed. |
||
BILLING.SUBSCRIPTION.ACTIVATED--_:Subscription activated. |
π Security β Verifying IPN Messages
# Node.js IPN verification (legacy)
const ipn = require('paypal-ipn');
ipn.verify(req.body, function callback(err, msg) {
if (err) { /* invalid */ }
else { /* valid β process */ }
});
4.6 PayPal Subscriptions (Billing Plans & Agreements)
PayPal Subscriptions allow merchants to charge customers on a recurring basis (weekly, monthly, yearly) with trial periods, proration, and automatic retry of failed payments.
π Subscription Steps
- Create a Product (if not exists).
- Create a Billing Plan (duration, frequency, pricing).
- Create a Subscription (assign plan to customer).
- Redirect customer to PayPal to approve.
- Listen to webhook events (e.g.,
BILLING.SUBSCRIPTION.ACTIVATED).
# Create Billing Plan (Node.js)
const request = new paypal.billingPlans.BillingPlansCreateRequest();
request.requestBody({
name: 'Monthly Membership',
description: 'Recurring monthly subscription',
type: 'INFINITE',
payment_definitions: [{
name: 'Monthly Payment',
type: 'REGULAR',
frequency_interval: '1',
frequency: 'MONTH',
amount: { currency: 'USD', value: '9.99' },
cycles: '0' // infinite
}],
merchant_preferences: {
return_url: 'https://shop.com/success',
cancel_url: 'https://shop.com/cancel'
}
});
π Module 04 : PayPal Payment Gateway β Completed
You have successfully completed this module of Payment Gateway Integration Basics.
Keep building your expertise step by step β Learn Next Module β
Module 05 : Razorpay Payment Gateway
This module provides a comprehensive guide to integrating Razorpay, a leading Indian payment gateway. You will learn about account setup, test/live modes, Orders API, Checkout integration, webhooks, and refunds β with practical examples and security considerations.
5.1 Razorpay Account Setup (Dashboard & API Access)
To start accepting payments with Razorpay, you need to create a merchant account and obtain API keys. Razorpay offers a free test environment for integration and a production environment for live transactions.
π StepβbyβStep Setup
- Go to https://dashboard.razorpay.com/signup and sign up.
- Verify your email and provide basic business details (company type, PAN, GST optional for test).
- Once logged in, navigate to Settings β API Keys to generate your Key ID and Key Secret.
- For production, you will also need to submit KYC documents and activate your account.
5.2 Razorpay Test & Live Mode (Sandbox vs Production)
Razorpay separates test and live environments using different API keys. Switching between them is manual β you must change keys in your code.
- Use test Key ID:
rzp_test_XXXXXX - Test cards:
4111 1111 1111 1111(visa success),4242 4242 4242 4242(3D secure) - No real money moves; all transactions are simulation.
- Webhook URLs can be local (using ngrok) β HTTPS not strictly required.
- Use live Key ID:
rzp_live_XXXXXX - Real money transactions; requires compliance (PCI DSS not needed if using Checkout).
- Webhook URLs must be HTTPS and publicly accessible.
- Need KYC and business verification before activation.
5.3 Razorpay Orders API (Create & Fetch Orders)
Razorpay uses an Order object to represent a payment request. You create an order on your server, then pass the order ID to the frontend Checkout.
π¦ Create Order β API Reference
POST https://api.razorpay.com/v1/orders
Authorization: Basic base64(key_id:key_secret)
Content-Type: application/json
{
"amount": 50000, // βΉ500.00 (in paise)
"currency": "INR",
"receipt": "order_rcptid_11",
"notes": {
"order_id": "order_123",
"customer_email": "user@example.com"
}
}
π₯ Sample Response
{
"id": "order_Examp1e12345",
"entity": "order",
"amount": 50000,
"amount_paid": 0,
"status": "created",
"created_at": 1650000000
}
π§ Code Example (Node.js with Razorpay NPM)
const Razorpay = require('razorpay');
const instance = new Razorpay({
key_id: process.env.RAZORPAY_KEY_ID,
key_secret: process.env.RAZORPAY_KEY_SECRET
});
async function createOrder(amount, receipt) {
const options = {
amount: amount * 100, // convert to paise
currency: 'INR',
receipt: receipt,
notes: { version: '1.0' }
};
const order = await instance.orders.create(options);
return order;
}
5.4 Razorpay Checkout Integration (JavaScript SDK)
Razorpay Checkout is a hosted payment page that handles cards, UPI, net banking, wallets, and EMI. It is the simplest and most secure way to accept payments β card data never touches your server.
π Integration Steps
- Include the Razorpay Checkout script on your payment page.
- Create an order on your server (as shown in 5.3) and pass the order ID to the frontend.
- Initialise the Razorpay Checkout object with the order ID and user details.
- Handle success (payment capture) via a callback, then verify the payment status on your server.
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
const options = {
key: "{{RAZORPAY_KEY_ID}}", // Your test/live key ID
amount: 50000, // in paise
currency: "INR",
name: "Acme Corp",
description: "Test Transaction",
order_id: "{{order_id}}", // Order ID from server
handler: function (response) {
// Send payment_id and order_id to your server for verification
fetch('/verify-payment', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
razorpay_payment_id: response.razorpay_payment_id,
razorpay_order_id: response.razorpay_order_id,
razorpay_signature: response.razorpay_signature
})
});
},
prefill: {
name: "Customer Name",
email: "customer@example.com",
contact: "9999999999"
},
theme: { color: "#F37254" }
};
const rzp = new Razorpay(options);
document.getElementById('pay-btn').onclick = function() { rzp.open(); };
</script>
5.5 Razorpay Webhooks (Payment & Refund Events)
Razorpay sends webhooks (HTTP callbacks) for important events like payment capture, refund processed, and subscription renewal. Webhooks are the most reliable way to update order status asynchronously.
π‘ Common Webhook Events
| Event | Description |
|---|---|
payment.captured--_:Payment successfully captured. |
|
payment.failed--_:Payment failed due to insufficient funds, expired card, etc. |
|
refund.created--_:Refund initiated. |
|
refund.processed--_:Refund completed. |
π Webhook Signature Verification
Razorpay signs each webhook with a secret. You must verify the signature before processing.
// Node.js example using Razorpay utility
const crypto = require('crypto');
const webhookSecret = process.env.RAZORPAY_WEBHOOK_SECRET;
app.post('/razorpay-webhook', express.raw({type: 'application/json'}), (req, res) => {
const signature = req.headers['x-razorpay-signature'];
const body = req.body.toString('utf8');
const expectedSignature = crypto.createHmac('sha256', webhookSecret).update(body).digest('hex');
if (signature !== expectedSignature) {
return res.status(400).send('Invalid signature');
}
const event = JSON.parse(body);
switch (event.event) {
case 'payment.captured':
// Update order in database
break;
}
res.status(200).end();
});
5.6 Razorpay Refund APIs (Full & Partial)
Razorpay allows you to refund a payment fully or partially via API. Refunds can be initiated from the dashboard or programmatically. Always ensure refunds are authorised (e.g., admin action) to prevent abuse.
π° Refund Request (Full Refund)
POST https://api.razorpay.com/v1/payments/{payment_id}/refund
Authorization: Basic base64(key_id:key_secret)
Content-Type: application/json
{
"amount": 50000, // amount in paise β optional, full if omitted
"notes": {
"reason": "customer_request",
"order_id": "order_123"
}
}
π₯ Sample Response
{
"id": "ref_Examp1e123",
"entity": "refund",
"amount": 50000,
"status": "processed",
"payment_id": "pay_Examp1e456",
"created_at": 1650000000
}
π§ Code Example (Node.js)
// Refund a payment (partial or full)
async function refundPayment(paymentId, amountInPaise = null) {
const options = {};
if (amountInPaise) options.amount = amountInPaise;
const refund = await instance.payments.refund(paymentId, options);
return refund;
}
π Module 05 : Razorpay Payment Gateway β Completed
You have successfully completed this module of Payment Gateway Integration Basics.
Keep building your expertise step by step β Learn Next Module β
Module 06 : Paytm Payment Gateway
This module covers the integration of Paytm, one of Indiaβs leading payment gateways. You will learn about merchant account setup, MID & merchant keys, integration workflow, callback verification, checksumβbased βwebhooksβ, and refund/status APIs β with practical examples and security best practices.
6.1 Paytm Merchant Account Setup
To accept payments via Paytm, you need to register as a merchant on Paytmβs business portal. Paytm provides separate test (staging) and production environments.
π StepβbyβStep Setup
- Go to https://business.paytm.com and click βSign Upβ.
- Fill in business details (type, PAN, GST, bank account).
- Complete KYC process β may require document submission.
- Once approved, you will receive a Merchant ID (MID) and access to the dashboard.
- For testing, also request staging credentials from Paytm support or use the sandbox portal.
6.2 Paytm MID & Merchant Keys
Paytm uses a combination of Merchant ID (MID), Merchant Key, and Industry Type to identify and authenticate merchants. These are used to generate checksums for request and response validation.
- Unique identifier for your business.
- Typically 7βdigit numeric or alphanumeric.
- Example:
1234567
- Secret key used for checksum generation.
- Never expose in clientβside code.
- Store in environment variables.
- Retail, eβcommerce, education, travel, etc.
- Determines transaction parameters.
- Obtained from Paytm dashboard.
π§ Securing Keys
# Store in .env file (never commit)
PAYTM_MID=1234567
PAYTM_MERCHANT_KEY=your_merchant_key
PAYTM_INDUSTRY_TYPE=RETAIL
# In code (Node.js)
const paytmConfig = {
mid: process.env.PAYTM_MID,
merchantKey: process.env.PAYTM_MERCHANT_KEY,
website: 'WEBSTAGING', // or 'WEBPRO' for production
industryType: process.env.PAYTM_INDUSTRY_TYPE
};
6.3 Paytm Integration Workflow (ChecksumβBased)
Paytm uses a redirectβbased flow. The merchant generates a checksum for the transaction, redirects the customer to Paytmβs hosted page, and Paytm sends the response to a callback URL.
π Steps
- Initiate Transaction: Merchant creates an order, collects customer details.
- Generate Checksum: Using Merchant Key, generate a checksum for the transaction parameters (order ID, amount, etc.).
- Redirect to Paytm: Send the checksum and parameters to Paytmβs payment page (staging or production URL).
- Customer Pays: Customer completes payment on Paytm (cards, UPI, wallet, net banking).
- Callback Response: Paytm redirects back to your
callback_urlwith a response checksum. - Verify Checksum: Merchant reβcomputes the checksum and verifies it β only then updates order status.
- Transaction Status API (optional): For additional confirmation, call Paytmβs order status API.
6.4 Paytm Callback Verification (Checksum Validation)
Paytm sends transaction response via a GET/POST callback to your specified URL. The response contains a checksum. You must reβgenerate the checksum and compare it to verify the integrity and authenticity.
π Checksum Generation & Verification
Paytm provides libraries in multiple languages. Below is a PHP example using their library.
// PHP callback handler (using Paytm's lib)
require_once("encdec_paytm.php");
$paytmChecksum = $_POST["CHECKSUMHASH"];
$paytmParams = $_POST;
unset($paytmParams["CHECKSUMHASH"]);
$isValidChecksum = verifychecksum_e($paytmParams, PAYTM_MERCHANT_KEY, $paytmChecksum);
if ($isValidChecksum && $_POST['STATUS'] == 'TXN_SUCCESS') {
// Update order status
$orderId = $_POST['ORDERID'];
$txnAmount = $_POST['TXNAMOUNT'];
// Additional verification via TxnStatus API recommended
}
π₯ Sample Callback Response Parameters
| Parameter | Description |
|---|---|
ORDERID-- | Merchantβs order ID. |
STATUS--_:TXN_SUCCESS, TXN_FAILURE, PENDING. |
|
TXNAMOUNT--_:Transaction amount. |
|
BANKTXNID--_:Paytm transaction ID. |
|
RESPCODE--_:Response code (01 = success). |
|
CHECKSUMHASH--_:Checksum for verification. |
6.5 Paytm Webhooks (ChecksumβBased Callbacks)
Paytm does not use standard JSON webhooks like Stripe. Instead, it relies on the same redirectβbased callback
mechanism. After payment, Paytm hits your callback_url with POST parameters and a checksum.
This is the equivalent of a webhook.
π‘ Handling βWebhookβ Callbacks
- Set your
callback_urlin the Paytm dashboard (Test/Live). - This URL must be publicly accessible (HTTPS in production).
- Paytm sends both success and failure notifications to the same URL.
- Your server must respond with
200 OK(any other status may be retried, but Paytm retries are limited). - Implement idempotency using your own order ID β the same callback may be sent twice.
π Security β No IP whitelisting needed (checksum is enough)
Because all responses are signed with your Merchant Key, you donβt need to whitelist Paytm IP addresses. Checksum verification alone guarantees authenticity.
6.6 Paytm Refund & Status APIs (Transaction Management)
Paytm provides APIs to check transaction status and initiate refunds. These are serverβtoβserver REST calls using the same checksum mechanism.
π Transaction Status API
After receiving a callback, itβs recommended to doubleβcheck the status using the Order Status API.
POST https://securegw.paytm.in/merchant-status/getTxnStatus
Parameters (JSON):
{
"MID": "your_mid",
"ORDERID": "order_123",
"CHECKSUMHASH": "generated_checksum"
}
Response includes STATUS, TXNAMOUNT, BANKTXNID, etc.
π° Refund API
POST https://securegw.paytm.in/refund/apply
Parameters (JSON):
{
"MID": "your_mid",
"ORDERID": "order_123",
"TXNID": "paytm_txn_id",
"REFUNDAMOUNT": "500.00",
"CHECKSUMHASH": "generated_checksum"
}
π Module 06 : Paytm Payment Gateway β Completed
You have successfully completed this module of Payment Gateway Integration Basics.
Keep building your expertise step by step β Learn Next Module β
Module 07 : PhonePe Payment Gateway
This module covers the integration of PhonePe, a popular Indian UPIβbased payment gateway. You will learn about merchant setup, API authentication (checksum/salt), UPI integration, payment status verification, and callback/refund handling β with practical examples and security best practices.
7.1 PhonePe Merchant Setup (Test & Production)
To start accepting payments via PhonePe, you need to register as a merchant on the PhonePe business portal. PhonePe provides separate test (sandbox) and production environments.
π StepβbyβStep Setup
- Go to https://www.phonepe.com/business and click βSign Upβ.
- Fill in business details (company name, PAN, GST, bank account).
- Complete KYC verification (may take a few days).
- Once approved, login to the PhonePe Merchant Dashboard.
- Obtain your Merchant ID and Salt Key (for checksum generation).
- For testing, request sandbox credentials or use PhonePeβs test environment (test.phonepe.com).
https://test.phonepe.com with test merchant IDs.
7.2 PhonePe API Authentication (Checksum & Salt)
PhonePe uses a checksumβbased authentication mechanism similar to Paytm. Each API request
must include a X-VERIFY header containing a SHAβ256 hash generated using your Merchant ID,
the request payload, and a Salt Key.
π How Checksum Works
- Concatenate
payload+/pg/v1/pay(API endpoint) +saltKey - Compute SHAβ256 hash β append
###1(suffix for the salt version). - Example header:
X-VERIFY: sha256_hash###1
// Node.js example
const crypto = require('crypto');
const payload = JSON.stringify({ merchantId: 'MERCHANTUID', ... });
const endpoint = '/pg/v1/pay';
const saltKey = process.env.PHONEPE_SALT_KEY;
const string = payload + endpoint + saltKey;
const sha256 = crypto.createHash('sha256').update(string).digest('hex');
const xVerify = sha256 + '###1';
π Required Credentials
- Unique identifier for your business.
- Example:
MERCHANTUID
- Secret key used for checksum generation.
- Never expose in clientβside code.
- Store in environment variables.
7.3 PhonePe UPI Integration (PG Pay API)
PhonePe offers a redirectβbased UPI integration using the /pg/v1/pay endpoint.
The merchant creates a payment request, redirects the customer to PhonePeβs hosted page, and handles the callback.
π Integration Flow
- Create Payment Request: On your server, call
POST https://api.phonepe.com/apis/hermes/pg/v1/pay(sandbox: test.phonepe.com). - Get Redirect URL: PhonePe returns a
data.instrumentResponse.redirectInfo.url. - Redirect Customer: Send the customer to that URL (PhonePeβs payment page).
- Customer Pays: Customer completes payment on PhonePe (UPI, cards, wallet).
- Callback Response: PhonePe redirects to your
redirectUrl(merchant provided) with a transaction ID. - Verify Checksum & Status: Verify the callback checksum, then call status API if needed.
π¦ Sample Payment Request (Node.js)
const payload = {
merchantId: 'MERCHANTUID',
merchantTransactionId: 'TXN123',
merchantUserId: 'USER456',
amount: 10000, // βΉ100.00 in paise
redirectUrl: 'https://your-site.com/phonepe-callback',
redirectMode: 'REDIRECT',
paymentInstrument: { type: 'PAY_PAGE' }
};
const base64Payload = Buffer.from(JSON.stringify(payload)).toString('base64');
const checksum = crypto.createHash('sha256').update(base64Payload + '/pg/v1/pay' + saltKey).digest('hex') + '###1';
const response = await axios.post(`${baseUrl}/pg/v1/pay`, { request: base64Payload }, {
headers: { 'Content-Type': 'application/json', 'X-VERIFY': checksum }
});
7.4 Payment Status Verification (Transaction Status API)
After receiving a callback, it is strongly recommended to verify the transaction status using PhonePeβs status API. This provides an extra layer of security and confirmation.
π‘ Status API Endpoint
GET /pg/v1/status/{merchantId}/{merchantTransactionId}
π Request Example
const endpoint = `/pg/v1/status/${merchantId}/${merchantTransactionId}`;
const checksum = crypto.createHash('sha256').update(endpoint + saltKey).digest('hex') + '###1';
const response = await axios.get(`${baseUrl}${endpoint}`, {
headers: { 'Content-Type': 'application/json', 'X-VERIFY': checksum }
});
π₯ Sample Status Response
{
"success": true,
"code": "PAYMENT_SUCCESS",
"message": "Transaction successful",
"data": {
"merchantId": "MERCHANTUID",
"merchantTransactionId": "TXN123",
"transactionId": "PhonePeTxnId",
"amount": 10000,
"state": "COMPLETED",
"paymentInstrument": { "type": "UPI", "utr": "1234567890" }
}
}
7.5 Callback & Refund Handling
PhonePe sends payment responses to your redirectUrl via GET (or POST, depending on configuration).
You must verify the checksum and process the transaction. Refunds are initiated via API and must be verified similarly.
π₯ Callback Handling
// Node.js callback handler (GET)
app.get('/phonepe-callback', async (req, res) => {
const { merchantId, transactionId, amount, checksumHash } = req.query;
// Recompute checksum for the response
const payload = `merchantId=${merchantId}&transactionId=${transactionId}&amount=${amount}`;
const computedHash = crypto.createHash('sha256').update(payload + saltKey).digest('hex') + '###1';
if (computedHash !== checksumHash) {
return res.status(400).send('Invalid checksum');
}
// Call status API for final confirmation
const status = await getTransactionStatus(merchantId, transactionId);
if (status.data.state === 'COMPLETED') {
// Update order
}
res.send('OK');
});
π° Refund API
POST /pg/v1/refund (requires checksum similar to pay API).
// Refund request payload
{
"merchantId": "MERCHANTUID",
"merchantTransactionId": "REFUND123",
"originalTransactionId": "PhonePeTxnId",
"amount": 5000,
"callbackUrl": "https://your-site.com/refund-callback"
}
π Module 07 : PhonePe Payment Gateway β Completed
You have successfully completed this module of Payment Gateway Integration Basics.
Keep building your expertise step by step β Learn Next Module β