NexGenStudioDev/FastKit

Create Pluggable Multi-Gateway Payment Module (Stripe, Razorpay + More) 💳

Aberta

#9 aberto em 27 de jun. de 2025

 (0 comentário) (0 reação) (0 responsável)TypeScript (0 fork)auto 404
TypeScriptbackenddocumentationenhancementgood first issuenpm

Métricas do repositório

Stars
 (1 estrela)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

🧩 Description

Build a modular, multi-gateway, and plug-and-play payment module that supports popular gateways like Razorpay and Stripe, while allowing developers to easily add new providers (like PayPal, Cashfree, etc.) in the future.

This feature should be:

  • Provider-agnostic from the top-level controller/servi

  • Developer-configurable (choose provider via config)

  • Secure (signature verification, safe token handling)

  • Reusable across apps (SaaS, ecommerce, donation, subscriptions)

🧱 Why This Is Important

  • ✅ Real-world apps need robust payment handling

  • 💼 Companies use different gateways based on country or pricing

  • 🛠 Avoids duplicating logic across Stripe, Razorpay, etc.

  • 🧩 Developers can choose & plug only what they need

  • 🚀 Makes monetization & checkout easier and faster to ship

  • 🟢 Difficulty Level: Intermediate to Advanced

Needs experience with:

  • Payment flows (Checkout Sessions, Payment Intents)

  • Signature verification

  • SDKs (Stripe, Razorpay)

  • Middleware and secure architecture

✅ Tasks

1. 📁 Proposed Folder Structure


src/
└── features/
    └── Payment/
        └── v1/
            ├── Payment.controller.ts         # Handles routes
            ├── Payment.service.ts            # Gateway-agnostic manager
            ├── Payment.constant.ts           # Errors, messages
            ├── Payment.validators.ts         # Joi/Zod for inputs
            ├── Payment.model.ts              # Optional: track transactions
            ├── Payment.middleware.ts         # Signature verification, etc.
            ├── Payment.config.ts             # Environment config
            ├── Payment.demo.ts               # Sample usage
            ├── README.md
            ├── stripe/
            │   ├── Stripe.service.ts         # Stripe logic
            │   ├── Stripe.config.ts
            ├── razorpay/
            │   ├── Razorpay.service.ts       # Razorpay logic
            │   ├── Razorpay.config.ts
            └── gateway/
                ├── types.ts                  # Gateway interface
                └── index.ts                  # Gateway factory

2. 🧩 Core Payment Gateway Interface (gateway/types.ts)

  • Define a common structure all providers must follow:
export interface IPaymentGateway {
  createPayment(data: PaymentData): Promise<PaymentResponse>;
  verifyPayment(payload: any): Promise<VerificationResult>;
}

All providers must implement this.

  1. 🧠 Gateway Factory (gateway/index.ts)

Return the right gateway instance based on config:

import StripeGateway from '../stripe/Stripe.service';
import RazorpayGateway from '../razorpay/Razorpay.service';

export const getPaymentGateway = (provider: string) => {
  switch (provider) {
    case 'stripe':
      return new StripeGateway();
    case 'razorpay':
      return new RazorpayGateway();
    default:
      throw new Error('Unsupported payment provider');
  }
};

4. 🎮 Controller (Payment.controller.ts)

  • startPayment(req, res)

  • verifyPayment(req, res)

  • [ ] Internally uses:

const gateway = getPaymentGateway(process.env.PAYMENT_PROVIDER); 

5. ⚙️ Service (Payment.service.ts)

  • Acts as a façade layer:

  • Handles shared logic like logging

  • Calls the correct gateway

  • Returns a consistent response format


{
  success: true,
  redirectUrl: 'https://checkout.stripe.com/...',
  transactionId: 'txn_123456'
}

6. 🛡️ Middleware (Payment.middleware.ts)

  • Optional: verifySignature() for Razorpay

  • Optional: verifyWebhookToken() for Stripe

  • Reusable in webhook routes

7. ❗ Constants (Payment.constant.ts)

export const PAYMENT_ERRORS = {
  PROVIDER_NOT_FOUND: 'Payment provider not configured.',
  INVALID_SIGNATURE: 'Signature mismatch.',
  FAILED: 'Transaction failed. Try again later.'
};

8. 📘 README.md

Include:

  • How to select provider (via .env)

  • How to use PaymentController

  • How to verify Razorpay or Stripe responses

  • How to add your own gateway in gateway/

9. 🧪 Demo File (Payment.demo.ts)

  • Start Stripe & Razorpay sessions

  • Sample payloads and mock webhooks

🔌 Supported Providers (Default)

  • ✅ Stripe

  • SDK-based checkout

  • Easy test cards

  • ✅ Razorpay

  • India-friendly

  • Signature-based verification

🧩 Easily extendable to:

  • PayPal

  • Cashfree

  • PhonePe

  • Flutterwave

🎯 Example Route Usage

import { verifyToken } from '../auth/Auth.middleware';
import { PaymentController } from '../features/Payment/v1/Payment.controller';

const paymentController = new PaymentController();

router.post('/pay', verifyToken, paymentController.startPayment);
router.post('/pay/verify', verifyToken, paymentController.verifyPayment);

✨ Expected Outcome

[x] One clean API regardless of the payment provider

[x] Pluggable gateway interface

[x] Signature-safe verification for Razorpay

[x] Factory to choose gateway dynamically

[x] Easy future expansion to other providers

[x] Stripe & Razorpay ready out of the box

🙋🏻‍♂️ Looking For

Contributions to:

  • Add PayPal, Cashfree, or UPI

  • Improve webhook security

  • Add metadata support (e.g., productId, userId)

  • Enhance transaction model (status, failureReason)

  • Write tests for multiple flows (refund, failure, retry)

  • Would you like the code scaffolded now or the next issue (like Blog, Upload, or Order) created in this same format?

Guia do colaborador