Accept payments with PayTabs in DaloyJS
PayTabs is a MENA-region payment gateway with coverage of Mada, KNET, BenefitPay, STC Pay, OmanNet, cards, and Apple Pay. This guide uses the paytabs_pt2 npm package (2.0.x, published by PayTabs) from a DaloyJS server, wrapped to feel like a normal async API.
What you should know up front
- The package is callback-based with positional array arguments. We wrap it once in Promise-returning functions with named arguments, so route handlers stay clean.
- Regions are not interchangeable. The package (2.0.12) maps these region codes to PayTabs hosts:
ARE,SAU,OMN,JOR,EGY,KWT,MAR,IRQ,QAT, andGLOBAL. Use the region your profile lives in; the wrong one fails authentication even when the key is right. - It's a redirect flow.
createPaymentPagereturns aredirect_url, the customer pays there and comes back to your return URL. The callback URL receives the server-side IPN, and that (plus a query) is the only signal you mark an order paid on. - The amount comes from your order. The browser sends an order id and billing details, never a price. See never trust client amounts.
- IPN signature is HMAC-SHA256. PayTabs signs the entire raw callback body with your profile Server Key and sends the hex digest in the
Signatureheader. - Use
tran_type: "sale"for direct capture,"auth"for an authorisation you capture later, andtran_class: "ecom"for normal online checkouts.
1. Provision
- Sign in to the PayTabs merchant dashboard.
- Developers -> Profile to grab your Profile ID and Server Key. Note your Region.
- Enable the payment methods you need (Mada, KNET, BenefitPay, and STC Pay need explicit activation, some need extra paperwork).
2. Install
3. Environment variables
4. Plugin
In the package, the misleadingly named queryTransaction(transaction[], cart[], cb) posts a follow-up transaction (refund, capture, void) to /payment/request, while validatePayment(tranRef, cb) is the actual status query. The wrapper gives them honest names.
5. Create a payment page
- 01requestCustomerDaloyJS routePOST /checkout/paytabs{ orderId, billing } only
- 02requestDaloyJS routePayTabscreatePaymentPagecart amount from the order, callback + return URLs
- 03responseDaloyJS routeCustomerReturn redirect_url201 { tranRef, redirectUrl }
- 04asyncCustomerPayTabsPays on the hosted page, returns via the return URLreturn URL is a UX hint, not proof
The route relies on the shared app wiring, including behindProxy, and the overview's orders and money modules. The client IP comes from resolveClientIp(), which only trusts X-Forwarded-For hops your behindProxy setting declares; reading the header directly lets any caller choose their own IP.
6. IPN webhook
- 01requestPayTabsDaloyJS routePOST /webhooks/paytabsSignature header (HMAC-SHA256 hex) over raw body
- 02noteDaloyJS routeDaloyJS routeverifyWebhookSignature with the Server Keyconstant-time compare
- 03responseDaloyJS routePayTabs401 when the signature does not match{ error: 'bad signature' }
- 04asyncDaloyJS routeJob workerenqueue keyed by tran_ref + status, then 200worker re-queries and compares with the order
7. Refunds and capture
Expose refunds only through a protected refund route. The plugin call:
Capturing an authorisation (tran_type: "auth") is the same follow-up call with "capture" instead of "refund". PayTabs' docs do not describe an idempotency key for follow-up transactions, so serialise refunds per order in your database before calling.
Runtimes
paytabs_pt2 is a CommonJS package built on axios. It runs on Node hosts (long-lived Node, Vercel Node.js functions, AWS Lambda, Azure Functions); other runtimes are not verified. For Cloudflare Workers or Vercel Edge, call the PT2 REST endpoints directly with fetch, as noted above. See the runtime table.
Errors
On failure, payment_result.response_code and response_message describe the gateway outcome. Log them and surface declines through problem+json with your own message; do not pass PayTabs' text to end users, because declines often include scheme-specific text the customer cannot act on.
Modernisation notes
- Wrap the package in one plugin. The positional-array signatures are easy to typo and harder to grep for than named-object arguments.
- Verify the IPN signature, always. Don't fall back to "the IP is from PayTabs"; HMAC over the raw body is the verification PayTabs documents.
- Fulfil on IPN plus a query, not on the return URL. The customer can close the tab mid-3DS.
- Consider the REST API directly if you need edge runtimes or multiple profiles; a short
resilientFetchwrapper gives you the same shape.
See also the payments overview, Tap Payments guide, Adyen guide, and problem+json errors.