@snc0x/baseupi
v1.4.0
Published
Official Node.js SDK for BaseUPI
Readme
baseupi
The official SDK for the BaseUPI payment platform. Integrate UPI payments into your SaaS frictionlessly without writing raw HTTP requests or complex cryptographic signature validation.
Installation
npm install baseupi
# or
yarn add baseupi
# or
pnpm add baseupiSetup
import BaseUPI from 'baseupi';
const baseupi = new BaseUPI('your_baseupi_api_key', {
baseURL: 'https://your-baseupi-domain.com' // Optional. Defaults to https://baseupi.app
});Creating an Order
Generate a dynamic payment URL for your customer.
const order = await baseupi.orders.create({
merchant_order_id: 'sub_xyz_123',
customer_email: '[email protected]',
redirect_url: 'https://your-saas.com/success',
line_items: [
{
name: 'Pro Subscription',
amount_paise: 99900, // ₹999.00
quantity: 1
}
],
metadata: {
userId: 'us_89234'
}
});
console.log(order.checkout_url); // Redirect user to this URLVerifying Webhooks securely (Fulfillment)
BaseUPI relies on webhooks to tell you when a user has completed payment via their UPI app. Because webhooks hit your public server, you must cryptographically verify that the request actually came from BaseUPI.
The SDK does this automatically in one line:
import express from 'express';
import BaseUPI from 'baseupi';
const app = express();
const baseupi = new BaseUPI('your_baseupi_api_key');
// Crucial: we need the raw body as a string/buffer to verify the signature!
app.post('/api/webhooks/baseupi', express.text({ type: '*/*' }), (req, res) => {
const signature = req.headers['x-baseupi-signature'];
const webhookSecret = process.env.BASEUPI_WEBHOOK_SECRET;
let event;
try {
event = baseupi.webhooks.constructEvent(req.body, signature, webhookSecret);
} catch (err) {
console.error('Webhook payload was compromised or invalid');
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle the event
if (event.event === 'payment.completed') {
const merchantOrderId = event.merchant_order_id;
console.log(`Order ${merchantOrderId} paid successfully!`);
// Fulfill the order (e.g. upgrade subscription)
}
res.json({ received: true });
});Retrieving an Order
You can manually poll an order status using its ID.
const order = await baseupi.orders.retrieve('saas_sub_987654');
// or retrieve by BaseUPI ID:
// const order = await baseupi.orders.retrieve('ord_K9EFTDQV');
if (order.status === 'COMPLETED') {
// Payment confirmed
}Types
The SDK is fully typed with TypeScript, providing rich autocomplete in VS Code for payloads and responses.
License
MIT
