Skip to Content
ePay.lkDocs
Payments

Hosted checkout

Redirect payers to beta.pay.epay.lk, handle their return, and confirm the result on your server.

The hosted checkout at https://beta.pay.epay.lk (sandbox: https://beta.sandbox-pay.epay.lk) is a mobile-first payment page branded with your business name, logo and colour (set them in Settings → Branding). It shows the amount, description and order reference, lets the payer choose a payment method and handles everything up to the result.

1. Redirect the payer

After creating a payment, send the browser to checkout_url with a 303 See Other (or a plain link/button). Don't load it in an iframe — the checkout refuses to be framed (frame-ancestors 'none').

https://beta.pay.epay.lk/c/cs_Q2h4bW9uZXlfaXNfbm90X3JlYWw

The cs_… token in the URL is the only credential for that checkout. Don't log it, and don't share it with anyone but the payer.

2. The payer pays

MethodWhat happens
Card (Visa, Mastercard)The payer is redirected to the acquiring bank's page to enter card details and complete 3-D Secure, then returned to the checkout.
LankaQRThe checkout shows a QR code to scan with any LankaQR banking app and waits for confirmation. Available in the sandbox; enabled in production per account.

If an attempt is declined, the checkout shows the reason and the payer can try again with another card — the payment stays requires_payment until it succeeds, is canceled, or expires (expires_in_minutes, default 60). Each declined attempt sends a payment.failed webhook with failure.code and failure.message.

3. The payer returns

When the payment reaches a final state, the checkout's Return to merchant button sends the payer to your return_url with three query parameters appended. After a successful payment the checkout also redirects automatically after 8 seconds (the payer can choose to stay).

https://shop.example.lk/checkout/return?payment_id=pay_8Hq2LxV0cT4mZr7N&status=succeeded&order_id=1042
ParameterValue
payment_idThe payment's id
statusThe payment status at the time of the redirect: succeeded, failed, expired, canceled
order_idYour order_id, if you sent one

If the payer clicks Cancel on the checkout, the payment is canceled and they're sent to cancel_url with status=canceled (or to return_url if you didn't set a cancel_url). Expired checkouts also go back to cancel_url when it's set.

Don't trust the redirect

Anyone can type ?status=succeeded into their address bar. The return URL tells you which payment to look at, never whether it was paid. Before fulfilling, either:

  • check the payment server-side: GET /v1/payments/{payment_id} and confirm status is succeeded and amount, currency and order_id match your order; or
  • rely on the signed payment.succeeded webhook, which may arrive before or after the browser.

A robust return handler

return-handler.ts
export async function handleReturn(req: Request) {
  const paymentId = new URL(req.url).searchParams.get("payment_id");
  if (!paymentId?.startsWith("pay_")) return showOrderPage(null);

  const res = await fetch(`${process.env.EPAY_API_URL}/v1/payments/${paymentId}`, {
    headers: { Authorization: `Bearer ${process.env.EPAY_SECRET_KEY}` },
  });
  if (!res.ok) return showOrderPage(null);
  const payment = await res.json();

  const order = await db.orders.findByPaymentId(payment.id);
  if (!order || payment.amount !== order.totalCents || payment.currency !== "LKR") {
    return showOrderPage(null); // never fulfil a mismatched payment
  }
  if (payment.status === "succeeded") await fulfilOnce(order, payment); // idempotent
  return showOrderPage(order, payment.status);
}

Store the payment id against your order when you create it, so both this handler and your webhook handler can find the order.

4. Handle "processing"

Card payments usually complete within seconds, but the acquirer's confirmation can lag behind the browser. If your handler sees processing, show "We're confirming your payment" and let the webhook finish the job — don't mark the order as failed. In the sandbox, test card 4000 0000 0000 7726 reproduces a ~10 s delay.

Expiry and cancellation

  • A checkout expires expires_in_minutes after creation (5–1440, default 60). Unpaid payments become expired and a payment.expired webhook is sent.
  • You can cancel an unpaid payment yourself with POST /v1/payments/:id/cancel — for example when the cart changes. Create a new payment for the new amount.

Branding and payer experience

  • Business name, logo, brand colour and support email come from your dashboard settings.
  • The checkout is responsive, supports light and dark mode, and is keyboard and screen-reader accessible.
  • In the sandbox it always shows a Sandbox checkout badge and the test card list.

On this page