Platform
Gateway Routing
Route payments across your connected payment gateways with priority-ordered rules and a fallback gateway.
Overview
PaymentHooks sits between your application and the payment gateways you have connected. Instead of hard-coding a single gateway, you define routing rules that select a gateway connection for each payment based on its amount, currency, country, payment method, card brand, customer, or metadata.
Rule-based
Match on amount, currency, country, method, card brand, metadata
Failover
Up to two more gateways on a soft decline; never on a hard decline
Health-aware
Gateways that keep failing are skipped until they recover
Routing rules
Each rule has one or more conditions of the form { field, operator, value }. Supported fields:
| Field | Example condition | Description |
|---|---|---|
| amount | { "field": "amount", "operator": "gt", "value": 100000 } | Rule DSL field, not a stored column: the amount being charged, in the smallest unit of the charge currency (the currency condition below), compared numerically. It is not USD cents, so pair an amount threshold with a currency condition (100000 is INR 1,000.00 but USD 1,000.00) |
| currency | { "field": "currency", "operator": "eq", "value": "INR" } | ISO currency code the payment is charged in, i.e. what becomes transactionLocalCurrency (uppercase) |
| country | { "field": "country", "operator": "in", "value": "IN,LK,NP" } | Country code supplied with the payment context |
| payment_method | { "field": "payment_method", "operator": "eq", "value": "upi" } | Payment method, e.g. card or upi |
| card_brand | { "field": "card_brand", "operator": "in", "value": "visa,mastercard" } | Card brand detected from the card number at checkout, lowercase: visa, mastercard, amex or discover (empty string when unknown or not a card payment) |
| customer_id | { "field": "customer_id", "operator": "eq", "value": "ph_cus_8f14e45fceea167a5a36dedd4bea2543" } | Customer ID |
| merchant_id | { "field": "merchant_id", "operator": "eq", "value": "ph_mrc_c9f0f895fb98ab9159f51fd0297e236d" } | Your merchant ID |
| metadata.<key> | { "field": "metadata.tier", "operator": "eq", "value": "enterprise" } | A key in the payment's metadata (empty string when absent) |
Any other field name evaluates as an empty string.
| Operator | Behaviour |
|---|---|
| eq | Equal, compared as strings. Case-sensitive. |
| neq | Not equal, compared as strings. Case-sensitive. |
| gt / gte | Greater than / greater than or equal, compared as numbers. |
| lt / lte | Less than / less than or equal, compared as numbers. |
| contains | Substring match, case-insensitive. |
| in | Value is a comma-separated list (e.g. "US,CA,GB"); matches if the field equals any entry. Case-sensitive. |
How rules are evaluated
- A connection is usable only when it is enabled, active, allows the payment currency, and its mode matches your account's mode: a test-mode account is routed only to sandbox connections and a live account only to live connections. A connection's
currencyRestrictionsis an allow-list of currencies; an empty list allows every currency. - If the payment explicitly requests a gateway connection (for example, the customer picked one at checkout) and that connection belongs to your account and is usable, it is used and rules are skipped. Otherwise the payment is routed normally.
- Otherwise enabled rules are evaluated in ascending
priorityorder. WithconditionLogic: "AND"every condition must match; with"OR"any one is enough. - The first matching rule that can take the payment wins. A
route_to_gatewayrule uses itsconnectionId; apercentage_splitorlowest_costrule picks one of itstargets(see Strategies below), considering only targets that are usable and whose country and payment method limits allow the payment. If none is available, itsfallbackConnectionIdis used instead when usable; otherwise evaluation continues with the next rule. - If no rule matches, PaymentHooks picks from your usable connections whose country restrictions do not list the payment country and whose enabled payment methods (if any) include the payment method, and uses the one with the lowest connection
prioritynumber (set the order on the Gateways page). To send a share of traffic to another gateway, add a rule for it. - Connections whose gateway is currently failing are skipped (see Health below). If every candidate is failing, the payment is still attempted on the first one rather than refused.
Strategies
route_to_gateway: every matching payment goes toconnectionId.percentage_split: 2-10targets, each with a whole-numberpercentage; the percentages must add up to 100. The pick is deterministic: the same customer, amount, currency and country always reach the same connection. When a target cannot take a payment, the others share its traffic in proportion to their percentages.lowest_cost: 2-10targets(no percentage). Each payment goes to the target with the lowest fee for its amount: the connection's own fee when set; otherwise the effective rate the gateway actually charged you on your captured payments over the last 90 days (once it has reported a fee on at least 5 of them); otherwise the gateway's standard fee. Gateways that report their fee on the charge (Stripe, Razorpay for INR payments, Airwallex) build up that history; a gateway that does not report its fee has its payments' fee recorded as unknown, never as zero.
A connection may appear once per rule, and the fallback must not also be a target. Every connection must be one of yours and gatewayId must be its gateway. When the rule's conditions fix the currency (currency eq or currency in), every connection must be able to charge it, or the rule is refused with 422 TARGET_CURRENCY_UNSUPPORTED.
{
"type": "percentage_split",
"targets": [
{ "gatewayId": "gw_stripe", "connectionId": "ph_gwc_98f13708210194c475687be6106a3b84", "percentage": 70 },
{ "gatewayId": "gw_paypal", "connectionId": "ph_gwc_3c59dc048e8850243be8079a5c74d079", "percentage": 30 }
],
"fallbackGatewayId": null,
"fallbackConnectionId": null
}Connection limits and disconnect
Each connection has its own limits, set on its page under Gateways or with PATCH /api/v1/gateway-marketplace/connections/:id: currencyRestrictions (currencies it charges, a subset of the gateway's), countryRestrictions (ISO 3166-1 payment countries it is never used for; empty means every country) and enabledPaymentMethods(methods it is used for, a subset of the gateway's; empty means all). Rules, strategies and the default order all honour them.
GET /api/v1/gateway-marketplace/connections/:id/disconnect-impact returns what disconnecting would affect (pendingPayments, activeMandates, subscriptions, routingRules). POST /api/v1/gateway-marketplace/:gatewayId/disconnect removes the connection and its credentials, records gateway.disconnected in the audit log and sends the gateway.disconnected webhook. Pending payments are not confirmed and saved payment authorisations cannot be charged until the gateway is connected again; rules skip it and use their fallback.
Failover and gateway health
When a charge definitively fails, PaymentHooks can retry it on the next gateway, up to two more gateways per payment (three gateway calls at most). The order is: the matched rule's other candidates (its fallbackConnectionId, or for percentage_split / lowest_cost its remaining targets, then its fallback), followed by your other usable connections in priority order. With no rule matched, the order is your connections by the default strategy. Each retry goes to a different gateway than every earlier attempt.
- Retried: a soft decline (a generic decline, the issuer or bank temporarily unavailable) and a gateway that did not take the request (unreachable host, credentials refused).
- Never retried: a hard decline (lost or stolen card, suspected fraud, do not honour, pick up card, invalid card number or account, expired card, restricted card), insufficient funds, failed 3D Secure or OTP authentication, and a payment the customer cancelled. Another gateway would get the same answer.
- Never retried, to rule out a double charge: a charge still in progress (pending, redirect, 3D Secure), a timeout or gateway server error whose outcome is unknown, and any failure that is not recognised.
- A payment sent to a specific gateway (
gatewayIdonPOST /payments, or the gateway the customer picked at checkout) is never retried elsewhere.
The Payment records the gateway that actually charged it (gatewayId); refunds, disputes and analytics use that gateway. The routing reason on the payment's timeline lists every gateway tried and why the next one was or was not tried.
Health.Every charge outcome and a health check of each connection every 15 minutes feed a per-connection circuit shared by all PaymentHooks servers. Only gateway faults count (timeouts, server errors, unreachable gateway, refused credentials); card declines do not. Five faults within two minutes open the circuit: the connection is skipped for 60 seconds, then takes traffic on probation, where one more fault re-opens it and three healthy answers close it. If every candidate's circuit is open, the payment is attempted on the first candidate rather than refused. See a connection's circuit and its last 100 health checks with GET /api/v1/gateway-marketplace/:connectionId/health, or run a check now with POST on the same path.
Configuration
Routing rules can be managed in the merchant dashboard under Settings → Routing Rules, or through the API:
curl -X POST https://paymenthooks.com/api/v1/routing-rules \
-H "Authorization: Bearer phk_live_..." \
-H "Content-Type: application/json" \
-d '{
"name": "Route INR to Razorpay",
"priority": 1,
"conditions": [{ "field": "currency", "operator": "eq", "value": "INR" }],
"action": {
"type": "route_to_gateway",
"gatewayId": "gw_razorpay",
"connectionId": "ph_gwc_98f13708210194c475687be6106a3b84",
"fallbackGatewayId": "gw_cashfree",
"fallbackConnectionId": "ph_gwc_3c59dc048e8850243be8079a5c74d079"
}
}'| Field | Type | Description |
|---|---|---|
name | string | Rule name, 1-255 characters. |
conditions | array | At least one { field, operator, value } object. value is a string or number. |
action | object | One of: { type: "route_to_gateway", gatewayId, connectionId, fallbackGatewayId?, fallbackConnectionId? }; { type: "percentage_split", targets: [{ gatewayId, connectionId, percentage }], fallback... } (2-10 targets, whole percentages summing to 100); { type: "lowest_cost", targets: [{ gatewayId, connectionId }], fallback... } (2-10 targets). connectionId is one of your gateway connections (GET /v1/gateway-marketplace/connections); gatewayId is its catalog gateway (e.g. gw_razorpay) and must match it. A connection may appear once and the fallback must not be a target. A connection that is not yours or does not exist returns 422 CONNECTION_NOT_FOUND; a gatewayId that does not match returns 422 GATEWAY_MISMATCH; a connection that cannot charge the currency fixed by the rule's currency condition returns 422 TARGET_CURRENCY_UNSUPPORTED (error.fields names the field, e.g. action.targets.1.connectionId). |
conditionLogic | stringDefault: | AND (all conditions must match) or OR (any condition). |
priority | integerDefault: | Non-negative. Lower numbers are evaluated first. |
isEnabled | booleanDefault: | Disabled rules are skipped. |
description | string | Free text, max 1000 characters. |
Invalid bodies return 400 VALIDATION_ERROR with per-field messages in error.fields. Rule IDs use the ph_rul_ prefix; gateway connection IDs use ph_gwc_.
Endpoints
Rules that do not exist or belong to another account return 404 NOT_FOUND on update and delete. There is no endpoint that retrieves a single rule; use the list endpoint. Each rule carries stats.matchCount and stats.lastMatchedAt, which are server-managed and cannot be updated.
| Field | Type | Description |
|---|---|---|
name | string | Rule name, 1-255 characters. |
description | string | Free text, max 1000 characters. |
isEnabled | boolean | Enable or disable the rule. |
priority | integer | Non-negative. Lower numbers are evaluated first. |
conditions | array | Replaces all conditions. At least one { field, operator, value } object. |
conditionLogic | string | AND or OR. |
action | object | Replaces the whole action; send the complete object (type, gatewayId, connectionId, optional fallbacks). |
/api/v1/routing-rulesList your routing rules, ordered by priority (not paginated){
"data": [
{
"id": "ph_rul_1f0e3dad99908345f7439f8ffabdffc4",
"merchantId": "ph_mrc_c9f0f895fb98ab9159f51fd0297e236d",
"name": "Route INR to Razorpay",
"description": "India traffic",
"isEnabled": true,
"priority": 1,
"conditions": [{ "field": "currency", "operator": "eq", "value": "INR" }],
"conditionLogic": "AND",
"action": {
"type": "route_to_gateway",
"gatewayId": "gw_razorpay",
"connectionId": "ph_gwc_98f13708210194c475687be6106a3b84",
"fallbackGatewayId": "gw_cashfree",
"fallbackConnectionId": "ph_gwc_3c59dc048e8850243be8079a5c74d079"
},
"stats": { "matchCount": 0, "lastMatchedAt": null },
"createdAt": "2026-05-16T10:30:00.000Z",
"updatedAt": "2026-05-16T10:30:00.000Z"
}
],
"error": null
}/api/v1/routing-rulesCreate a routing rule (201){
"name": "Route INR to Razorpay",
"description": "India traffic",
"priority": 1,
"conditions": [{ "field": "currency", "operator": "eq", "value": "INR" }],
"action": {
"type": "route_to_gateway",
"gatewayId": "gw_razorpay",
"connectionId": "ph_gwc_98f13708210194c475687be6106a3b84",
"fallbackGatewayId": "gw_cashfree",
"fallbackConnectionId": "ph_gwc_3c59dc048e8850243be8079a5c74d079"
}
}{
"data": {
"id": "ph_rul_1f0e3dad99908345f7439f8ffabdffc4",
"merchantId": "ph_mrc_c9f0f895fb98ab9159f51fd0297e236d",
"name": "Route INR to Razorpay",
"description": "India traffic",
"isEnabled": true,
"priority": 1,
"conditions": [{ "field": "currency", "operator": "eq", "value": "INR" }],
"conditionLogic": "AND",
"action": {
"type": "route_to_gateway",
"gatewayId": "gw_razorpay",
"connectionId": "ph_gwc_98f13708210194c475687be6106a3b84",
"fallbackGatewayId": "gw_cashfree",
"fallbackConnectionId": "ph_gwc_3c59dc048e8850243be8079a5c74d079"
},
"stats": { "matchCount": 0, "lastMatchedAt": null },
"createdAt": "2026-05-16T10:30:00.000Z",
"updatedAt": "2026-05-16T10:30:00.000Z"
},
"error": null
}/api/v1/routing-rules/:ruleIdUpdate a rule: send at least one updatable field (e.g. isEnabled or priority). Unknown or server-managed fields, or an empty body, return 400 VALIDATION_ERROR{
"isEnabled": false
}{
"data": {
"id": "ph_rul_1f0e3dad99908345f7439f8ffabdffc4",
"merchantId": "ph_mrc_c9f0f895fb98ab9159f51fd0297e236d",
"name": "Route INR to Razorpay",
"description": "India traffic",
"isEnabled": false,
"priority": 1,
"conditions": [{ "field": "currency", "operator": "eq", "value": "INR" }],
"conditionLogic": "AND",
"action": {
"type": "route_to_gateway",
"gatewayId": "gw_razorpay",
"connectionId": "ph_gwc_98f13708210194c475687be6106a3b84",
"fallbackGatewayId": "gw_cashfree",
"fallbackConnectionId": "ph_gwc_3c59dc048e8850243be8079a5c74d079"
},
"stats": { "matchCount": 0, "lastMatchedAt": null },
"createdAt": "2026-05-16T10:30:00.000Z",
"updatedAt": "2026-05-16T10:30:00.000Z"
},
"error": null
}/api/v1/routing-rules/:ruleIdDelete a rule (404 NOT_FOUND if it does not exist or belongs to another account){
"data": { "success": true },
"error": null
}/api/v1/routing/simulateDry-run the routing decision for a hypothetical payment. Read-only: no payment, gateway call or webhook is created (the request appears in Developers > API Logs like any call), so it is safe to call from tests. Uses your account's current platform mode. A body that is not a JSON object returns 400 VALIDATION_ERROR{
"transactionLocalCurrencyAmount": 250000,
"transactionLocalCurrency": "INR",
"country": "IN",
"paymentMethod": "card"
}{
"data": {
"matched": true,
"reason": "Rule \"Route INR to Razorpay\" matched (priority 1)",
"rule": { "id": "ph_rul_1f0e3dad99908345f7439f8ffabdffc4", "name": "Route INR to Razorpay", "priority": 1 },
"gateway": { "id": "ph_gwc_98f13708210194c475687be6106a3b84", "displayName": "Razorpay", "mode": "live" },
"fallbackGateway": { "id": "ph_gwc_3c59dc048e8850243be8079a5c74d079", "displayName": "Cashfree", "mode": "live" }
},
"error": null
}Simulate routing
POST /api/v1/routing/simulate runs the same decision logic as a live payment without charging anyone, and returns the matched rule (if any), the selected connection, its fallback, and a human-readable reason. It applies your account's current mode, so a test-mode account only ever sees sandbox connections. Gateway credentials are never included.
| Field | Type | Description |
|---|---|---|
transactionLocalCurrencyAmount | integer | Positive amount in the smallest unit of transactionLocalCurrency (250000 = ₹2,500.00), as matched by amount conditions. |
transactionLocalCurrency | string | 3-letter ISO 4217 charge currency, matched by currency conditions (uppercased before evaluation). |
country | string | Country code (uppercased before evaluation). |
paymentMethod | string | Payment method, e.g. card. |
customerId | string | Customer ID used for customer_id conditions. |
cardBrand | string | Card brand used for card_brand conditions, e.g. visa (lowercased before evaluation). |