@pushparajunipay/unipay
v1.1.11
Published
A unified payment gateway SDK for JavaScript.
Downloads
27
Maintainers
Readme
@pushparajunipay/unipay
UniPay is a unified payment gateway solution that simplifies integration with multiple payment providers. This JavaScript SDK provides a seamless way to integrate various payment gateways into your JavaScript applications.
Table of Contents
Installation
To install the UniPay SDK, run the following command:
npm i @pushparajunipay/unipay
You can also use Yarn or pnpm:
yarn add @pushparajunipay/unipay
pnpm add @pushparajunipay/unipay
Supported Payment Gateways
- Stripe
- Razorpay
- Braintree
- Cashfree
- Square
- PayU
Usage
Initializing a Payment Gateway
import UniPay from '@pushparajunipay/unipay';
const unipay = new UniPay();
unipay.registerPaymentGateway('stripe', {
apiKey: 'your_stripe_api_key'
});
unipay.registerPaymentGateway('razorpay', {
apiKey: 'your_razorpay_key_id',
apiSecret: 'your_razorpay_key_secret'
});
Processing a Payment
try {
const paymentResult = await unipay.initiatePayment('stripe', {
amount: 1000,
currency: 'USD',
description: 'Test payment',
customerEmail: '[email protected]',
customerPhone: '+1234567890'
});
console.log('Payment processed:', paymentResult);
} catch (error) {
console.error('Payment processing error:', error.message);
}
Capturing a Payment
try {
const captureResult = await unipay.capturePayment('stripe', 'payment_intent_id');
console.log('Payment captured:', captureResult);
} catch (error) {
console.error('Payment capture error:', error.message);
}
Checking Payment Status
try {
const paymentStatus = await unipay.getPaymentStatus('razorpay', 'payment_id');
console.log('Payment status:', paymentStatus);
} catch (error) {
console.error('Payment status error:', error.message);
}
Handling Webhooks
app.post('/webhook/stripe', async (req, res) => {
try {
const event = await unipay.handleWebhook('stripe', {
body: req.body,
signature: req.headers['stripe-signature']
});
console.log('Webhook handled:', event);
res.sendStatus(200);
} catch (error) {
console.error('Webhook error:', error.message);
res.sendStatus(400);
}
});
Error Handling
The SDK uses a custom UniPayError
class for error handling. All errors thrown by the SDK will be instances of this class. You can catch and handle these errors as shown in the usage examples above.
try {
// SDK operation
} catch (error) {
if (error instanceof UniPayError) {
console.error('UniPay error:', error.message);
// Handle UniPay-specific error
} else {
console.error('Unexpected error:', error);
// Handle other errors
}
}
Development
To set up the project for development:
Clone the repository:
git clone https://github.com/Pushparaj13811/unipay.git
Navigate to the
js-sdk
directory:cd unipay/js-sdk
Install dependencies:
npm install
Testing
To run tests:
npm test
To run tests in watch mode:
npm run test:watch
Contributing
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a feature branch:
git checkout -b feature/AmazingFeature
- Commit your changes:
git commit -m 'Add some AmazingFeature'
- Push to your branch:
git push origin feature/AmazingFeature
- Open a Pull Request.
For more details, refer to the main README file in the root directory of the project.