unified-payment-package
v1.0.3
Published
A unified payment package supporting multiple payment gateways like Stripe, Razorpay, etc.
Downloads
12
Maintainers
Readme
Unified Payment Package
A unified and extensible payment package for Node.js that supports multiple payment gateways, including Stripe, Razorpay, and others. This package simplifies the process of creating checkout sessions and verifying payments across different payment gateways with a single, unified interface.
Table of Contents
- Features
- Installation
- Configuration
- Usage
- Supported Gateways
- Extending the Package
- API Reference
- Examples
- Error Handling
- Contributing
- License
Features
- Multiple Gateway Support: Integrates with popular payment gateways like Stripe and Razorpay.
- Unified API: Provides a single interface for managing payments across different gateways.
- Extensible: Easily add support for new payment gateways.
- Customizable: Configure checkout details and payment verification according to your needs.
Installation
Install the package via npm:
npm install unified-payment-package
Configuration
Stripe Configuration
To use Stripe as your payment gateway, configure it with your Stripe API keys:
import { UnifiedPayment } from 'unified-payment-package';
const stripeConfig = {
apiKey: 'your_stripe_api_key',
environment: 'production' // 'sandbox' for testing
};
const payment = new UnifiedPayment('stripe', stripeConfig);
Razorpay Configuration
For Razorpay, you'll need to provide both the API key and the secret key:
import { UnifiedPayment } from 'unified-payment-package';
const razorpayConfig = {
apiKey: 'your_razorpay_api_key',
secretKey: 'your_razorpay_secret_key',
environment: 'production' // 'sandbox' for testing
};
const payment = new UnifiedPayment('razorpay', razorpayConfig);
Usage
Creating a Checkout Session
Create a checkout session with the provided details:
const checkoutDetails = {
amount: 5000, // Amount in smallest currency unit (e.g., cents for USD)
currency: 'USD',
description: 'Premium Membership'
};
payment.createCheckoutSession(checkoutDetails)
.then((sessionId) => {
console.log('Checkout session created:', sessionId);
})
.catch((error) => {
console.error('Error creating checkout session:', error);
});
Verifying a Payment
Verify a payment using the session ID:
const sessionId = 'your_checkout_session_id';
payment.verifyPayment(sessionId)
.then((isVerified) => {
if (isVerified) {
console.log('Payment verified successfully!');
} else {
console.log('Payment verification failed.');
}
})
.catch((error) => {
console.error('Error verifying payment:', error);
});
Supported Gateways
- Stripe: Widely used payment gateway with global reach.
- Razorpay: Popular in India, offering a range of payment options.
Extending the Package
Adding a New Gateway
To add support for a new payment gateway, implement the PaymentGateway
interface and register it with the GatewayFactory
.
Step 1: Implement the New Gateway
Create a new class that implements the PaymentGateway
interface:
import { PaymentGateway, CheckoutDetails } from 'unified-payment-package';
class NewGateway implements PaymentGateway {
constructor(private config: any) {}
async createCheckoutSession(details: CheckoutDetails): Promise<string> {
// Implement the logic to create a checkout session
}
async verifyPayment(sessionId: string): Promise<boolean> {
// Implement the logic to verify the payment
}
}
Step 2: Register the New Gateway
Update the GatewayFactory
to recognize the new gateway:
import { PaymentGateway } from 'unified-payment-package';
import { NewGateway } from './gateways/NewGateway';
export class GatewayFactory {
static createGateway(gatewayType: string, config: any): PaymentGateway {
switch (gatewayType.toLowerCase()) {
case 'stripe':
return new StripeGateway(config);
case 'razorpay':
return new RazorpayGateway(config);
case 'newgateway':
return new NewGateway(config);
default:
throw new Error('Unsupported payment gateway');
}
}
}
Customizing Checkout Details
Customize the checkout details based on the requirements of the gateway:
const checkoutDetails = {
amount: 10000,
currency: 'INR',
description: 'Purchase of Product XYZ',
// Additional fields specific to the gateway can be added here
};
API Reference
UnifiedPayment Class
constructor(gatewayType: string, config: any)
- Creates an instance of the UnifiedPayment class.
- Parameters:
gatewayType
: The type of payment gateway (e.g., 'stripe', 'razorpay').config
: Configuration options for the selected gateway.
createCheckoutSession(details: CheckoutDetails): Promise<string>
- Creates a new checkout session.
- Parameters:
details
: Object containing checkout details such as amount, currency, and description.
- Returns: A promise that resolves with the session ID.
verifyPayment(sessionId: string): Promise<boolean>
- Verifies the payment using the session ID.
- Parameters:
sessionId
: The ID of the checkout session.
- Returns: A promise that resolves with a boolean indicating whether the payment is verified.
PaymentGateway Interface
createCheckoutSession(details: CheckoutDetails): Promise<string>
- Abstract method to be implemented by each gateway for creating a checkout session.
verifyPayment(sessionId: string): Promise<boolean>
- Abstract method to be implemented by each gateway for verifying a payment.
Examples
Using Multiple Gateways
Dynamically switch between different gateways based on your business logic:
const gatewayType = determineGateway(); // Custom logic to choose gateway
const payment = new UnifiedPayment(gatewayType, config[gatewayType]);
payment.createCheckoutSession(details)
.then((sessionId) => {
// Handle success
})
.catch((error) => {
// Handle error
});
Error Handling
The package provides consistent error handling across different gateways. All methods return promises that either resolve successfully or reject with an error message.
payment.createCheckoutSession(details)
.catch((error) => {
console.error('An error occurred:', error.message);
});
Contributing
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch with a descriptive name.
- Make your changes and commit them with clear and concise messages.
- Submit a pull request to the main repository.
Please ensure your code adheres to the project's coding standards and includes appropriate tests.
License
This project is licensed under the ISC License - see the LICENSE file for details.