naafipay
v1.0.1
Published
Official NaafiPay SDK — accept one-time and subscription payments in Bangladesh (bKash, Nagad, Upay, cards) with one install.
Maintainers
Readme
NaafiPay
Official NaafiPay SDK for Node.js, Bun, Deno, and edge runtimes. Accept one-time payments and recurring subscriptions in Bangladesh (bKash, Nagad, Upay, cards) with a single install.
Install
npm install naafipay
# or
bun add naafipay
# or
pnpm add naafipay
# or
yarn add naafipayRequires Node 18+ (uses global fetch).
Quick start
import { NaafiPay } from "naafipay";
const pay = new NaafiPay(process.env.NAAFIPAY_API_KEY!);Get your API key from Dashboard → API Keys at https://naafipay.com.
One-time payment link
const link = await pay.payments.createLink({
name: "Order #1234",
amount: 1500,
currency: "BDT",
success_url: "https://shop.example.com/thanks",
cancel_url: "https://shop.example.com/cart",
});
// Redirect the customer
res.redirect(link.checkout_url);Subscription (SaaS / recurring digital products)
const sub = await pay.subscriptions.checkout({
plan_id: "plan_pro_monthly",
customer_email: "[email protected]",
customer_name: "Tasin Fahim",
success_url: "https://app.example.com/welcome",
cancel_url: "https://app.example.com/pricing",
});
res.redirect(sub.checkout_url!);Create plans in Dashboard → Subscriptions → Plans (amount, currency, interval, optional trial). NaafiPay handles recurring charges + dunning automatically.
Retrieve & cancel
await pay.payments.retrieve("pay_01HZY...");
await pay.subscriptions.retrieve("sub_01HZY...");
await pay.subscriptions.cancel("sub_01HZY...");
await pay.payments.refund("pay_01HZY...", 500); // partial refundWebhooks
import { NaafiPay } from "naafipay";
app.post("/webhooks/naafipay", express.raw({ type: "application/json" }), (req, res) => {
const sig = req.header("x-naafipay-signature") ?? "";
const ok = NaafiPay.verifyWebhook(req.body.toString("utf8"), sig, process.env.NAAFIPAY_WEBHOOK_SECRET!);
if (!ok) return res.status(401).end();
const event = JSON.parse(req.body.toString("utf8"));
switch (event.type) {
case "payment.completed": /* fulfill order */ break;
case "subscription.renewed": /* extend access */ break;
case "subscription.cancelled": /* revoke access */ break;
}
res.json({ received: true });
});Framework examples
Next.js (App Router)
// app/api/checkout/route.ts
import { NaafiPay } from "naafipay";
export async function POST() {
const pay = new NaafiPay(process.env.NAAFIPAY_API_KEY!);
const link = await pay.payments.createLink({ name: "Pro plan", amount: 500 });
return Response.json({ url: link.checkout_url });
}Bun
import { NaafiPay } from "naafipay";
const pay = new NaafiPay(Bun.env.NAAFIPAY_API_KEY!);Cloudflare Workers / edge
Works out of the box — uses global fetch, no Node-only modules at runtime (webhook helper uses node:crypto which is available on Workers with nodejs_compat).
API
| Method | Description |
|---|---|
| payments.create(input) | Create a payment record |
| payments.createLink(input) | Create a hosted checkout link |
| payments.retrieve(id) | Fetch a payment by id or reference |
| payments.list({ limit, status }) | List payments |
| payments.refund(id, amount?) | Refund (full or partial) |
| subscriptions.checkout(input) | Start a subscription checkout |
| subscriptions.retrieve(id) | Fetch a subscription |
| subscriptions.cancel(id) | Cancel a subscription |
| NaafiPay.verifyWebhook(body, sig, secret) | Verify webhook signature |
TypeScript
Fully typed. All inputs and responses ship with .d.ts.
License
MIT © NaafiPay
