Skip to content
All articles
Web Development7 min read

Payment gateway integration in Nepal: eSewa, Khalti and more

Payment gateway integration in Nepal, step by step: merchant account, sandbox testing, server-side verification, pending payments, refunds and reconciliation.

EK

EK Ra Sunya team

EK Ra Sunya Inc.

Payment gateway integration in Nepal follows the same order whichever wallet or gateway you pick. Get the merchant account moving first, build and test against the gateway's sandbox, and treat the customer's return to your site as a hint, never as proof. Your server confirms every payment with the gateway's own verification API before anything is delivered. Then plan for the payments that end up pending, cancelled or refunded, because those are where the support calls come from.

The code for a happy-path payment is the small part. The work is in everything around it.

What do you need before a developer writes any code?

A merchant account with each gateway you want to offer. eSewa, Khalti and Fonepay each have their own onboarding, their own documents to submit and their own credentials. Developers can build against a sandbox while onboarding runs, but you can't go live until the production keys arrive, so start the application early and treat it as a launch dependency.

Settle a few business questions at the same time, because they change the build:

  • Which gateways, and why. Each one adds testing, support and reconciliation work. Pick the ones your customers actually use.
  • What is being paid for. A one-off order, a booking with a deposit, a subscription, a top-up. Each has a different flow when something fails.
  • Who handles refunds, and through which screen.
  • Who reconciles, and how often.

Write these into the requirements before asking for a quote. A quote that says "eSewa and Khalti integration" and nothing else hides most of the work (we explain why in what to prepare before asking for a quote).

One thing this post doesn't cover: the shop counter QR code a customer scans in person. That's a different product. Here we mean payments taken inside your own website or app.

How does sandbox testing work for eSewa and Khalti?

Both gateways publish a test environment, and you should finish your integration there before touching production keys.

eSewa's ePay documentation gives a test merchant code, a test secret key and test eSewa IDs, and lists separate test and production URLs. Every payment request carries an HMAC SHA256 signature built from the total amount, a transaction ID and the product code, in that order. The same page publishes a set of test logins so you can complete payments end to end without real money.

Khalti's ePayment docs separate the two environments by base URL and by admin panel. You sign up as a sandbox merchant at test-admin.khalti.com and take your secret key from there, and production keys come from admin.khalti.com. Khalti also states amounts in paisa, so a rupee figure sent as-is is off by a factor of a hundred. That's a bug worth a test case of its own.

Test more than the success path. In the sandbox, try a cancelled payment, a closed browser tab halfway through, a double click on the pay button and a payment retried with the same order. Write each one into your user acceptance testing checklist so it gets checked again before every release.

Why must every payment be verified on the server?

Because the redirect back to your site can be faked, replayed or simply lost. A customer's browser returning to your "success" URL tells you the customer reached that page. It doesn't tell you the money moved.

Both gateways say this in their own documentation. eSewa sends the result back Base64-encoded with a signature, and tells merchants to verify that signature by generating it the same way they generated the request's. eSewa's FAQ goes further: if the amount eSewa confirms doesn't match the amount your application requested, treat it as a possible fraudulent transaction and stop delivery. Khalti sends the customer back to your return_url with a status in the query string, and its documentation says plainly to use the lookup API for the final validation.

In practice the flow looks like this:

  1. Your server creates the order and records the amount before redirecting the customer. Khalti's initiate call carries your secret key in its header, so it belongs on your server, never in the browser.
  2. The customer pays on the gateway's page and returns to your site.
  3. Your server calls the gateway's status or lookup API with the transaction ID.
  4. Your server compares the confirmed amount and status with the order it saved in step 1.
  5. Only then does the order become paid, and only then does anything ship, unlock or get booked.

Secret keys live on the server. If a key ever appears in a mobile app build or in JavaScript sent to the browser, assume it has leaked and rotate it.

What happens when a payment is pending, cancelled or lost?

This is the part most first integrations skip, and the gateways' own status lists show why it matters.

SituationeSewa statusKhalti statusWhat your system should do
Paid and confirmedCOMPLETECompletedMark paid, deliver
Started, not finishedPENDINGInitiated or PendingHold the order, check again later
Unclear stateAMBIGUOUSPendingHold, don't deliver, contact the gateway
Customer cancelled or session expiredNOT_FOUND or CANCELEDUser canceled or ExpiredRelease the order, let the customer retry
Money returnedFULL_REFUND or PARTIAL_REFUNDRefunded or Partially RefundedDon't deliver, or adjust the order

eSewa's documentation says that if no response arrives within five minutes, the status check API can be used to confirm the payment. Khalti's lookup table says a Pending transaction should be held, with no service given, and the Khalti team contacted.

So build a background job that re-checks unresolved orders, and a screen where your staff can see every order stuck between "started" and "paid". Without it, a customer who paid while their connection dropped gets nothing, and your team finds out from an angry phone call.

How should refunds and reconciliation work?

Refunds touch money and customer trust at once, so give them a clear owner. Decide who can approve one and record why it was given. Make the order's status follow the gateway's refund status, so a refunded order can't be fulfilled by mistake later. Check with each gateway, during onboarding, how refunds are requested on your account, because that decides whether your software triggers them or your staff do it from the merchant panel.

Reconciliation is the daily or weekly check that your records and the gateway's agree. Pull the transaction list from the merchant panel and match each line to an order by its transaction ID. Look at three lists: payments with no matching order, paid orders with no matching payment, and orders whose amounts differ. Each one is a bug, a fraud attempt or a manual fix someone forgot to record. Store the gateway's transaction reference on every order from day one. Adding it later means matching by amount and time, which is slow and wrong often enough to hurt.

Accounting and tax treatment of these payments is a question for your accountant, not your developer.

Does a mobile app need anything different?

The same rules apply: server-side initiation, server-side verification, the same pending and refund handling. eSewa's FAQ notes it offers mobile SDKs for Android and iOS. The store rules add one more question. Google Play's billing rules are different for digital goods and for physical goods and services, which we covered in publishing an Android app on Google Play from Nepal. Check which side of that line your product falls on before you design checkout.

Frequently asked questions

Can I integrate eSewa or Khalti without a registered business?

Each gateway sets its own onboarding requirements and they can change, so ask the gateway directly. Either way, you can build and test against the sandbox while the merchant application is in progress.

Is it enough to check the status in the return URL?

No. The return URL tells you where the customer went. Confirm every payment with the gateway's status or lookup API from your server, and compare the amount with your saved order, before you deliver anything.

How many gateways should a Nepali website offer?

As many as your customers use, and no more. Each extra gateway is another sandbox, another set of keys, another status list to map and another statement to reconcile.

What about Fonepay?

The same principles apply. Ask Fonepay for its current integration document and test credentials when you apply as a merchant, and build to that document rather than to sample code found online.

Getting payments right on your site or app

If you're adding online payments to a website or app, our web development team can review your flow against the checks above. Tell us what you sell and how customers pay.

Have a project in mind?

Let us help you build something scalable, fast, and built to last.

Start a conversation