A Node.js library for integrating with Thawani Payment Gateway.
- Full TypeScript support
- Promise-based API
- Comprehensive error handling
- Webhook processing
- Complete API coverage
- Built-in TypeScript types
- Security best practices
- Detailed logging
npm install thawani-nodejs
# or
yarn add thawani-nodejsimport { ThawaniClient } from "thawani-nodejs";
// Initialize the client
const thawani = new ThawaniClient({
secretKey: "your_secret_key",
publishableKey: "your_publishable_key",
sandbox: true, // Set to false for production
});
// Create a checkout session
async function createPayment() {
try {
const session = await thawani.checkout.create({
client_reference_id: "order_123",
mode: "payment",
products: [
{
name: "Product Name",
unit_amount: 1000, // 10 OMR in baisa
quantity: 1,
},
],
success_url: "https://your-website.com/success",
cancel_url: "https://your-website.com/cancel",
metadata: {
order_id: "123",
},
});
// Get checkout URL
const checkoutUrl = thawani.getPublicCheckoutUrl(session.session_id);
return checkoutUrl;
} catch (error) {
console.error("Error:", error);
throw error;
}
}// Create a customer
const customer = await thawani.customers.create({
client_customer_id: "[email protected]",
});
// List customers
const customers = await thawani.customers.list();// List payments
const payments = await thawani.payments.list({
limit: 10,
skip: 0,
});
// Retrieve payment
const payment = await thawani.payments.retrieve("payment_id");// Create refund
const refund = await thawani.refunds.create({
payment_id: "payment_id",
reason: "Customer request",
metadata: {
order_id: "123",
},
});app.post("/webhook", (req, res) => {
try {
const event = thawani.webhooks.constructEvent(
req.body,
req.headers["thawani-signature"],
req.headers["thawani-timestamp"]
);
// Handle the event
switch (event.type) {
case "payment.succeeded":
handlePaymentSuccess(event.data);
break;
}
res.json({ received: true });
} catch (error) {
res.status(400).send(`Webhook Error: ${error.message}`);
}
});try {
await thawani.checkout.create({...});
} catch (error) {
if (error instanceof ThawaniError) {
console.error('Thawani API Error:', error.message);
console.error('Error Code:', error.code);
}
}For complete documentation, visit https://thawani-nodejs.vercel.app/
- All API requests are made over HTTPS
- Webhook signatures are verified
- No sensitive data is logged
- Input validation on all methods
MIT License - see LICENSE for details.