DEV Community

Wilson Shrestha
Wilson Shrestha

Posted on Originally published at wilson.com.np

Adding eSewa and Khalti Payments to Your Website: Merchant Setup, API Flow, and the Verification Step You Must Not Skip

If your Nepali website still asks customers to pay and then send a screenshot, you are losing orders every night, every weekend, and every time a customer gets bored waiting for a reply. Adding eSewa and Khalti fixes that: the customer pays inside the wallet, your server checks the payment, and the order confirms itself.

Both wallets use the same redirect-and-verify flow, but they start the payment differently, and that difference shapes your code.

The five-step flow both wallets share

  1. Your site creates an order with a unique ID.
  2. You send the customer to the wallet — a signed form for eSewa, an initiate API call for Khalti.
  3. The customer pays with login, MPIN or OTP on the wallet's page.
  4. The wallet redirects the customer back to your success or return URL.
  5. Your server verifies the payment with the wallet before marking the order paid.

Step 5 is the one developers skip, and it is the one that stops fraud. A redirect is just a URL in the customer's browser — anyone can type your success URL with made-up values. Khalti states plainly that the callback is not final proof of payment. Deliver only when the wallet's own status check says so: COMPLETE for eSewa, Completed for Khalti.

eSewa ePay v2: a signed form POST

With eSewa, the browser POSTs an HTML form to https://epay.esewa.com.np/api/epay/main/v2/form (sandbox: https://rc-epay.esewa.com.np/api/epay/main/v2/form). The form carries the amount broken into parts — amount, tax_amount, product_service_charge, product_delivery_charge — plus total_amount, a unique transaction_uuid, your product_code, and success/failure URLs.

The security lives in the signature: an HMAC-SHA256 of total_amount, transaction_uuid and product_code, Base64-encoded, built with your secret key on the server:

$message = "total_amount={$total},transaction_uuid={$uuid},product_code={$code}";
$signature = base64_encode(hash_hmac('sha256', $message, $secret, true));
Enter fullscreen mode Exit fullscreen mode

Join the fields in exactly the order listed in signed_field_names, with no spaces. The most common first-attempt failure is an invalid signature caused by field order, a stray space, or signing 110 while posting 110.00.

After payment, eSewa returns the customer to your success URL with a Base64-encoded JSON data parameter. Decode it, re-check its signature, then — this is the important part — call the transaction status API with your own order's amount and UUID and only trust COMPLETE.

Khalti KPG-2: two JSON calls

Khalti needs no SDK and no signature. Your server POSTs JSON to the initiate endpoint with your secret key in an Authorization: Key <secret_key> header:

const r = await fetch('https://dev.khalti.com/api/v2/epayment/initiate/', {
  method: 'POST',
  headers: {
    'Authorization': 'Key ' + process.env.KHALTI_SECRET_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    return_url: 'https://yoursite.com/khalti/return',
    website_url: 'https://yoursite.com',
    amount: 110 * 100,
    purchase_order_id: 'ORD-1042',
    purchase_order_name: 'Order 1042'
  })
});
const { pidx, payment_url } = await r.json();
Enter fullscreen mode Exit fullscreen mode

Save the pidx against your order and redirect the customer to payment_url. Amounts are in paisa — Rs. 1 is 100 paisa, and the minimum is 1000 paisa (Rs. 10). Sending rupees, a decimal, or less than Rs. 10 is the classic Khalti validation error. When the customer returns, treat the query parameters as a hint only and confirm with the lookup API (POST .../epayment/lookup/ with the pidx). Deliver only if the status is Completed and the amount matches your order in paisa.

Before you write code: the paperwork

Code is the easy part; most delays come from paperwork. Both wallets need a registered business before they release live keys or let you withdraw money — business registration, PAN/VAT and tax clearance are the core documents. Since 2026 there is also an e-commerce listing rule: businesses selling online should register their platform with the Department of Commerce, Supply and Consumer Protection under the E-Commerce Act 2081 and Directive 2082, and use gateways approved by Nepal Rastra Bank. Start the merchant application the same week you start building the checkout — approval can take days or weeks.

You can build and test everything without a business, using the public sandboxes. eSewa's sandbox uses product code EPAYTEST with test IDs 9806800001–9806800005 (password Nepal@123, MPIN 1122, OTP 123456). Khalti's test keys come from test-admin.khalti.com, with test IDs 9800000000–9800000005 (MPIN 1111, OTP 987654). Sandbox testing costs nothing on either wallet.

The no-code route

On WooCommerce, a plugin does all of this for you. Nepal Payments for WooCommerce (free) adds eSewa ePay v2, Khalti KPG-2, connectIPS and NIC Asia cards in one plugin, verifies payments server-side, and holds orders whose amount does not match. There is also a single-gateway eSewa plugin on WordPress.org. You still need your own merchant accounts, and your store currency must be NPR. Check the plugin's last update date, read its support threads, and run a full test order — including a cancelled one — before going live.

Go-live checklist

  • Merchant KYC approved on both wallets; live merchant code and secret keys received.
  • Site listed on the e-commerce portal; refund and contact policy pages published.
  • Test orders: a successful payment marks the order paid, a cancelled payment marks it failed, an edited success URL is rejected, and refreshing the success page does not deliver twice (make the handler idempotent).
  • Swap sandbox URLs and test keys for production, then make one small real payment and refund it, and confirm the money shows in the merchant dashboard.
  • Keep secret keys in environment variables on the server — never in JavaScript, never in Git. Use HTTPS on every success, failure and return URL, and log every request and response so you can settle disputes later.

Fees are worth asking about early: neither wallet publishes a standard merchant commission rate — it is set in your merchant agreement by category and volume — and Khalti's terms mention a one-time API fee plus an annual maintenance charge from the second year. As a rough guide, one Kathmandu agency quotes about Rs. 15,000 to 35,000 per gateway to add eSewa or Khalti to an existing WordPress site.

The full guide, with the complete merchant document tables, the PHP and Node.js code, the sandbox credentials and the error-by-error fixes, is on wilson.com.np: eSewa and Khalti Payment Integration for Your Website (2026)


Top comments (1)

Collapse
 
david_williambalbino_7ec profile image
David William Balbino •

the step 5 part is so true, we see the same thing with Pix in Brazil, the return URL is just a hint, never the proof. one thing i'd add: the customer sometimes pays and closes the tab before the redirect, so step 4 never happens and the order stays pending forever. imo is better to have a small job that re-checks pending orders against the status API every few minutes. also make the "mark as paid" idempotent (unique constraint on transaction_uuid or a status check inside a transaction), pq the redirect and the reconcile job can hit at the same time and you dont wanna deliver twice lol