@gatepaybd/core
v0.0.1
Published
Official JavaScript/TypeScript SDK for GatePay payment gateway supporting bKash, Nagad, Rocket and other Bangladesh payment methods
Downloads
24
Maintainers
Readme
GatePay SDK for JavaScript/TypeScript
Official JavaScript/TypeScript SDK for the GatePay payment gateway. Supports Node.js, React, Vue, Angular, and all modern JavaScript environments.
Installation
npm install gatepay
# or
yarn add gatepayQuick Start
import { PayflowSDK, PaymentMethod } from 'payflow-sdk';
// Initialize the SDK
const payflow = new PayflowSDK({
apiKey: 'your-api-key',
environment: 'sandbox' // or 'production'
});
// Create a payment
const payment = await payflow.payments.create({
amount: 1000, // Amount in smallest currency unit (e.g., paisa for BDT)
currency: 'BDT',
paymentMethod: PaymentMethod.BKASH,
customerInfo: {
name: 'John Doe',
email: '[email protected]',
phone: '+8801234567890'
},
returnUrl: 'https://yoursite.com/success',
cancelUrl: 'https://yoursite.com/cancel'
});
console.log('Payment created:', payment);API Reference
Configuration
interface PayflowConfig {
apiKey: string;
environment?: 'sandbox' | 'production';
baseUrl?: string; // Custom API base URL
timeout?: number; // Request timeout in milliseconds
}Payment Methods
Create a payment:
const payment = await payflow.payments.create({
amount: 1000,
currency: 'BDT',
paymentMethod: PaymentMethod.BKASH,
customerInfo: {
name: 'Customer Name',
email: '[email protected]',
phone: '+8801234567890'
}
});Get payment status:
const payment = await payflow.payments.get('payment-id');List transactions:
const transactions = await payflow.payments.list({
page: 1,
limit: 20,
status: PaymentStatus.COMPLETED,
startDate: new Date('2024-01-01'),
endDate: new Date('2024-12-31')
});Process/Execute payment:
const result = await payflow.payments.execute('payment-id');Refund payment:
const refund = await payflow.payments.refund({
transactionId: 'payment-id',
amount: 500, // Partial refund (optional)
reason: 'Customer request'
});Supported Payment Methods
enum PaymentMethod {
BKASH = 'BKASH',
NAGAD = 'NAGAD',
ROCKET = 'ROCKET',
UPAY = 'UPAY',
MCASH = 'MCASH',
VISA = 'VISA',
MASTERCARD = 'MASTERCARD',
AMEX = 'AMEX',
INTERNET_BANKING = 'INTERNET_BANKING',
}Error Handling
import { PayflowSDKError, PayflowValidationError, PayflowNetworkError } from 'payflow-sdk';
try {
const payment = await payflow.payments.create(paymentData);
} catch (error) {
if (error instanceof PayflowSDKError) {
console.error('API Error:', error.code, error.message);
} else if (error instanceof PayflowValidationError) {
console.error('Validation Error:', error.message);
} else if (error instanceof PayflowNetworkError) {
console.error('Network Error:', error.message);
}
}Framework Examples
React
import React, { useState } from 'react';
import { PayflowSDK, PaymentMethod } from 'payflow-sdk';
const payflow = new PayflowSDK({
apiKey: process.env.REACT_APP_PAYFLOW_API_KEY!,
environment: 'sandbox'
});
function PaymentButton() {
const [loading, setLoading] = useState(false);
const handlePayment = async () => {
setLoading(true);
try {
const payment = await payflow.payments.create({
amount: 1000,
currency: 'BDT',
paymentMethod: PaymentMethod.BKASH,
});
// Redirect to payment URL
window.location.href = payment.paymentUrl!;
} catch (error) {
console.error('Payment failed:', error);
} finally {
setLoading(false);
}
};
return (
<button onClick={handlePayment} disabled={loading}>
{loading ? 'Processing...' : 'Pay with bKash'}
</button>
);
}Node.js
const { PayflowSDK, PaymentMethod } = require('payflow-sdk');
const payflow = new PayflowSDK({
apiKey: process.env.PAYFLOW_API_KEY,
environment: 'production'
});
// Express.js route example
app.post('/create-payment', async (req, res) => {
try {
const payment = await payflow.payments.create({
amount: req.body.amount,
currency: 'BDT',
paymentMethod: req.body.paymentMethod,
customerInfo: req.body.customerInfo
});
res.json({ success: true, payment });
} catch (error) {
res.status(400).json({ success: false, error: error.message });
}
});License
MIT
