onepay-sdk
Version:
A simple payment gateway for Node.js
215 lines (162 loc) โข 5.06 kB
Markdown
OnePay-SDK is a lightweight and developer-friendly TypeScript package that simplifies integration with multiple payment gateways such as **Stripe** and **Razorpay** in a single unified interface.
> ๐ Use OnePay-SDK to easily validate and manage API keys for multiple gateways in one project.
---
- โ
Multi-gateway support (Stripe & Razorpay)
- ๐ ๏ธ Validate API keys with easy-to-use methods
- ๐ณ Create, verify, and process payments through a consistent API
- ๐ Process refunds with a unified interface
- ๐ Retrieve payment details across gateways
- ๐ Plug-and-play class-based API
- ๐ก Written in TypeScript (with full types support)
- ๐ง Easily extendable for more gateways in future
---
## ๐ฆ Installation
```bash
npm install onepay-sdk
# or
yarn add onepay-sdk
```
---
## ๐ ๏ธ Usage
### Initialize OnePay-SDK
To start using OnePay-SDK, initialize it with the API keys for the supported gateways:
```typescript
import { OnePay } from "onepay-sdk";
const onePay = new OnePay([
{ gateway: "stripe", apiKey: "your-stripe-api-key" },
{ gateway: "razorpay", apiKey: "your-razorpay-key-id", apiSecret: "your-razorpay-key-secret" },
]);
```
You can validate the configured gateways to ensure the API keys are correct:
```typescript
const stripeValidation = await onePay.validateGateway("stripe");
console.log(stripeValidation);
const razorpayValidation = await onePay.validateGateway("razorpay");
console.log(razorpayValidation);
```
OnePay allows you to create payments for the supported gateways:
```typescript
const razorpayPayment = await onePay.createPayment({
gateway: "razorpay",
amount: 50, // Amount in regular currency (will be converted to paise internally)
currency: "INR",
description: "Premium Subscription",
metadata: { customer_id: "cust_123", product_id: "prod_456" }
});
console.log(razorpayPayment);
// Returns Razorpay order details including orderId
```
```typescript
const stripePayment = await onePay.createPayment({
gateway: "stripe",
amount: 50, // Amount in regular currency (will be converted to cents internally)
currency: "usd",
description: "Test Payment",
customerEmail: "customer@example.com",
paymentMethodTypes: ["card"],
customerId: "cust_123456", // Optional Stripe customer ID
metadata: { order_id: "6735" }
});
console.log(stripePayment);
// Returns a client secret that can be used with Stripe.js
```
After payment completion on the client side, you can verify payments:
```typescript
const verifyRazorpay = await onePay.verifyPayment({
gateway: "razorpay",
paymentId: "pay_123456789",
orderId: "order_123456789",
signature: "signature_from_razorpay_callback"
});
console.log(verifyRazorpay);
```
```typescript
const verifyStripe = await onePay.verifyPayment({
gateway: "stripe",
paymentId: "pi_123456789" // Payment Intent ID
});
console.log(verifyStripe);
```
You can process refunds for completed payments:
```typescript
// Full refund
const fullRefund = await onePay.refundPayment(
"stripe",
"pi_123456789"
);
// Partial refund
const partialRefund = await onePay.refundPayment(
"razorpay",
"pay_123456789",
25.00 // Refund amount in regular currency
);
```
Get payment information at any time:
```typescript
const paymentDetails = await onePay.getPaymentDetails(
"stripe",
"pi_123456789"
);
console.log(paymentDetails);
```
---
OnePay-SDK exports TypeScript interfaces to help with development:
```typescript
// Initialize payment gateways
type KeyProps = {
gateway: 'stripe' | 'razorpay';
apiKey: string;
apiSecret?: string;
}
// Create payment payload
type PaymentPayload = {
gateway: 'stripe' | 'razorpay';
amount: number;
currency: string;
description: string;
customerEmail?: string;
metadata?: Record<string, any>;
paymentMethodTypes?: string[];
customerId?: string;
receiptId?: string;
}
// Verify payment payload
type PaymentVerificationPayload = {
gateway: 'stripe' | 'razorpay';
paymentId: string;
orderId?: string; // For Razorpay
signature?: string; // For Razorpay
}
```
All methods return a consistent response format:
```typescript
{
success: boolean;
gateway: 'stripe' | 'razorpay';
message?: string; // Error message if success is false
// For Stripe
clientSecret?: string;
paymentIntentId?: string;
// For Razorpay
order?: any;
orderId?: string;
}
```
---
Contributions are welcome! Feel free to open issues or submit pull requests.
[](LICENSE)