If you are building a checkout in JavaScript and searching for bKash payment gateway Node.js integration, you are in good company. Node.js and Express power a large share of new Bangladeshi e-commerce sites, booking platforms and SaaS backends. This guide explains how a bKash payment flow works on a Node.js server, the security checks you should never skip, and how to add Nagad, Rocket, Upay, Cellfin and internet banking through one integration with KarentPay instead of maintaining separate code for every wallet.
How a bKash Payment Gateway Works in Node.js
Every hosted wallet checkout, whether you connect to bKash directly or through an aggregator, follows the same basic pattern. Your Node.js server talks to the gateway; the customer's browser only sees a redirect and a return page.
- Authenticate your server. Your backend obtains an access token (bKash calls this the grant-token step) using credentials stored in environment variables, never in front-end code.
- Create the payment. The server sends the order amount, currency (BDT), your own invoice/order ID and a callback URL. The gateway returns a payment ID and a checkout URL.
- Redirect the customer. The shopper is sent to the hosted page, enters their wallet number, OTP and PIN, and confirms.
- Handle the callback. The customer returns to your callback route with a status and payment ID.
- Execute and verify on the server. Your backend executes or queries the payment and checks the amount, status and order ID before marking the order paid.
The key lesson: the callback query string is not proof of payment. Only a server-to-server verification is.
Setting Up Your Node.js Project
A typical stack looks like this:
- Express (or Fastify/NestJS) for routes
- dotenv for API keys and secrets
- The built-in
fetch(Node 18+) or axios for HTTPS calls - A database table for orders with columns like
order_id,amount,status,gateway_payment_idandpaid_at
Keep separate sandbox and live credentials, and load them by environment so a test key can never reach production.
Example route structure
Below is a simplified outline of three routes. Exact endpoint paths and field names differ by provider; always follow the official documentation of the gateway you use (for KarentPay, see karentpay.com/documentations).
POST /checkout– create an order in your database with statuspendingand an amount calculated on the server, call the gateway's create-payment endpoint with the amount, your order ID and your callback URL, save the returned payment ID against the order, and send the checkout URL back to the front end.GET /payment/callback– read the payment ID from the query string, call the gateway's verify or query endpoint from the server, and mark the order paid only if the status is successful, the amount matches your database and the order is stillpending. Then redirect to a success or failure page.POST /payment/webhook(optional) – if the gateway sends server notifications, process them with the same verification and idempotency rules as the callback.
Wrap gateway calls in a small service module (for example services/payment.js) using async/await and try/catch, so your routes stay clean and you can swap sandbox and live settings in one place.
Security Checklist for bKash Integration in Node.js
Most payment bugs in Node.js projects come from trusting the browser. Use this checklist before going live:
- Secrets in environment variables only – never commit API keys to Git or ship them in React/Vue bundles.
- Verify every payment server-side – compare amount, currency and order ID with your database.
- Make fulfilment idempotent – a callback or webhook may arrive twice; update the order only when it is still
pending. - Handle timeouts gracefully – if the customer closes the tab, run a scheduled status query for pending orders instead of assuming failure.
- Use HTTPS everywhere – SSL on your callback and webhook URLs is essential.
- Log, but mask – store gateway responses for reconciliation, but never log full wallet numbers, OTPs or PINs.
- Validate webhook origin – if the gateway signs webhook requests, check the signature before processing.
One Integration for bKash, Nagad and More
Integrating bKash alone solves only part of the problem. Bangladeshi customers also pay with Nagad, Rocket, Upay, Cellfin and internet banking, and writing a separate module for each wallet multiplies token logic, callback handling and reconciliation work.
KarentPay is a Bangladeshi payment gateway that brings these options together:
- bKash, Nagad, Rocket, Upay, Cellfin, internet banking and multiple banks through one integration
- A developer API with documentation at karentpay.com/documentations, which fits naturally into Express or NestJS services
- Auto and manual transfer options, so you can choose how payments are confirmed
- SSL-secured payment processing
- A WooCommerce plugin (search "KarentPay" on WordPress.org) if part of your business also runs on WordPress
Your Node.js code then needs just one create-payment call and one verification routine, and the customer picks their preferred wallet on the payment page.
Testing Before You Go Live
- Run the full flow in sandbox: success, cancel and failure.
- Simulate a duplicate callback and confirm the order is fulfilled only once.
- Try a tampered amount in the callback URL and confirm verification rejects it.
- Check that refunds or failed payments leave the order in the right state.
- Switch to live keys and make one small real transaction before announcing the launch.
Frequently Asked Questions
Is there an official bKash payment gateway package for Node.js?
Several community packages exist on npm, but quality varies. Many teams prefer calling the documented REST API directly with fetch or axios so they fully control token handling and verification.
How do I implement bKash payment gateway in a MERN stack app?
Keep all gateway calls in the Express/Node layer. The React front end only requests a checkout URL from your API and redirects the user; the backend creates and verifies the payment and updates MongoDB.
Can I accept bKash and Nagad with one API in Node.js?
Yes. With an aggregator such as KarentPay, one API integration covers bKash, Nagad, Rocket, Upay, Cellfin and internet banking, so you do not maintain separate code for each wallet.
Why does my payment show success but the order stays unpaid?
Usually the callback route failed, timed out or rejected the verification. Log the verification response and add a scheduled job that re-checks pending orders.
Do I need a merchant account to integrate?
Yes, live payments require an approved merchant account with completed KYC. You can usually build and test against a sandbox first.
Start Accepting Payments in Your Node.js App
Whether you are launching a MERN store, a booking engine or a subscription service, a clean server-side flow is the foundation of reliable payments. Explore the API and create your merchant account at karentpay.com to accept bKash, Nagad, Rocket, Upay, Cellfin and bank payments through one secure integration.

