rapid_pay
v1.0.0
Published
A highly secure, extremely fast, and easy-to-implement npm package for integrating with various payment gateways, featuring advanced and latest functionalities.
Downloads
2
Readme
RapidPay - Universal Payment Gateway NPM Package
RapidPay is a highly secure, extremely fast, and easy-to-implement npm package for integrating with various payment gateways. It provides a unified API for processing payments, handling refunds, checking transaction statuses, and offers advanced features like fraud detection, tokenization, and recurring payments.
Features
- Universal Gateway Support: Designed to integrate with multiple payment gateways through a unified interface.
- Easy to Implement: Simple API for quick integration into your applications.
- Highly Secure: Includes utilities for encryption, hashing, and tokenization to protect sensitive data.
- Extremely Fast: Optimized for performance with built-in measurement tools.
- Advanced Features:
- Fraud Detection: Basic fraud scoring and rule-based detection.
- Tokenization: Securely tokenize payment information.
- Recurring Payments: Support for creating and managing subscriptions.
- Extensible Architecture: Easily add new payment gateways by extending the
PaymentGatewayabstract class.
Installation
To install RapidPay, use npm:
npm install rapid_payUsage
Initializing RapidPay
First, require the RapidPay class and initialize it:
const RapidPay = require('rapid_pay');
const rapidPay = new RapidPay();Adding a Payment Gateway
RapidPay is designed to work with different payment gateways. You need to add each gateway you plan to use. Here's an example with the included StripeGateway:
const StripeGateway = require('./gateways/StripeGateway'); // Adjust path if not in your project
rapidPay.addGateway('stripe', StripeGateway, {
apiKey: process.env.STRIPE_SECRET_KEY,
// other Stripe specific configurations
});
// You can add other gateways similarly
// const PayPalGateway = require('./gateways/PayPalGateway');
// rapidPay.addGateway('paypal', PayPalGateway, { /* PayPal config */ });Processing a Payment
To process a payment, specify the amount, currency, payment method (which corresponds to the gateway name you added), and any gateway-specific options:
async function makePayment() {
try {
const result = await rapidPay.processPayment(
1000, // amount in cents (e.g., $10.00)
'USD',
'stripe', // Use the gateway name you registered
{ card: 'tok_visa' } // Stripe-specific token or card details
);
console.log('Payment Result:', result);
if (result.success) {
console.log(`Payment successful! Transaction ID: ${result.transactionId}`);
} else {
console.error('Payment failed.', result);
}
} catch (error) {
console.error('Error processing payment:', error.message);
}
}
makePayment();Refunding a Payment
async function refundExistingPayment(transactionId) {
try {
const result = await rapidPay.refundPayment(
transactionId,
500, // amount to refund in cents
'USD'
);
console.log('Refund Result:', result);
if (result.success) {
console.log(`Refund successful! Refund ID: ${result.refundId}`);
} else {
console.error('Refund failed.', result);
}
} catch (error) {
console.error('Error refunding payment:', error.message);
}
}
// Call with an actual transaction ID
// refundExistingPayment('stripe_txn_1234567890');Getting Transaction Status
async function getPaymentStatus(transactionId) {
try {
const result = await rapidPay.getTransactionStatus(transactionId);
console.log('Transaction Status:', result);
} catch (error) {
console.error('Error getting transaction status:', error.message);
}
}
// Call with an actual transaction ID
// getPaymentStatus('stripe_txn_1234567890');Advanced Features
RapidPay provides methods for advanced functionalities. If a specific gateway doesn't support these features, RapidPay will attempt to use its general-purpose utilities.
Fraud Detection
async function checkFraud(transactionData) {
try {
const result = await rapidPay.detectFraud('stripe', { // Specify gateway or use null for general
amount: 15000,
country: 'US',
cardAttempts: 1,
transactionId: 'txn_abc',
});
console.log('Fraud Detection Result:', result);
} catch (error) {
console.error('Error during fraud detection:', error.message);
}
}
// checkFraud();Tokenization
async function tokenizeCard(cardDetails) {
try {
const result = await rapidPay.tokenizePaymentInfo('stripe', {
cardNumber: '4111222233334444',
expiry: '12/25',
cvv: '123',
});
console.log('Tokenization Result:', result);
} catch (error) {
console.error('Error during tokenization:', error.message);
}
}
// tokenizeCard();Recurring Payments (Subscriptions)
async function createSubscription() {
try {
const customer = { id: 'cust_001', name: 'Jane Doe', email: '[email protected]' };
const plan = { id: 'plan_premium', name: 'Premium Plan', amount: 9900, interval: 'month' };
const startDate = new Date().toISOString();
const result = await rapidPay.createSubscription('stripe', customer, plan, startDate);
console.log('Subscription Result:', result);
} catch (error) {
console.error('Error creating subscription:', error.message);
}
}
// createSubscription();Extending RapidPay (Adding New Gateways)
To add support for a new payment gateway, you need to create a new class that extends the PaymentGateway abstract class located in gateways/PaymentGateway.js.
Your new gateway class must implement the following methods:
processPayment(amount, currency, paymentMethod, options)refundPayment(transactionId, amount, currency, options)getTransactionStatus(transactionId)
Optionally, you can override these methods to provide gateway-specific implementations for advanced features:
setupWebhook(eventType, callbackUrl, secret)detectFraud(transactionData)tokenizePaymentInfo(cardDetails)createSubscription(customer, plan, startDate)
Example Structure for a New Gateway (e.g., PayPalGateway.js):
const PaymentGateway = require('./PaymentGateway');
class PayPalGateway extends PaymentGateway {
constructor(config) {
super(config);
// Initialize PayPal SDK or API client with config
console.log('PayPalGateway initialized with config:', config);
}
async processPayment(amount, currency, paymentMethod, options) {
console.log(`PayPal processing payment: ${amount} ${currency} via ${paymentMethod}`);
// Implement actual PayPal payment processing logic
return { success: true, transactionId: `paypal_txn_${Date.now()}` };
}
async refundPayment(transactionId, amount, currency, options) {
console.log(`PayPal refunding payment: ${amount} ${currency} for ${transactionId}`);
// Implement actual PayPal refund logic
return { success: true, refundId: `paypal_refund_${Date.now()}` };
}
async getTransactionStatus(transactionId) {
console.log(`PayPal getting status for transaction: ${transactionId}`);
// Implement actual PayPal transaction status retrieval logic
return { status: 'completed', transactionId: transactionId };
}
// Override optional methods if PayPal has specific implementations
// async detectFraud(transactionData) { /* ... */ }
// async tokenizePaymentInfo(cardDetails) { /* ... */ }
// async createSubscription(customer, plan, startDate) { /* ... */ }
}
module.exports = PayPalGateway;Running Tests
To run the unit and integration tests, use the following command:
npm testThis will execute tests located in the test/ directory.
Security Considerations
- API Keys and Secrets: Never hardcode API keys or sensitive credentials directly in your code. Use environment variables or a secure configuration management system.
- PCI DSS Compliance: RapidPay provides utilities for tokenization, but achieving full PCI DSS compliance requires careful consideration of your entire payment processing flow, including how you handle, store, and transmit cardholder data. Always refer to your chosen payment gateway's documentation for PCI compliance best practices.
- Webhooks: Ensure your webhook endpoints are secured and verify the authenticity of incoming webhook events using signatures provided by the payment gateways.
License
This project is licensed under the ISC License.
