Adding a gateway
The artefacts every new gateway needs, and the rule that keeps the abstraction honest.
Adding a gateway is a registry entry plus a plugin file. Nothing in the service or controller layer changes.
Never modify the service architecture to support one gateway
If a gateway seems to require a service change, the abstraction is being modelled wrong — fix the plugin, not the service. Gateway-specific behaviour stays inside the plugin, always.
The artefacts
Types
src/gateways/<gateway>/<gateway>.types.ts — the gateway's own request and response shapes.
These are internal. They must never appear in a signature outside this folder.
Mapper
src/gateways/<gateway>/<gateway>.mapper.ts — implements GatewayMapper. Its job is
toUnifiedStatus(): every native status string the gateway can emit maps to a
PaymentStatus.
An unrecognized status must raise GatewayMappingError. Do not add a default case that
coerces to FAILED — that mis-reports money movement the first time the gateway ships a new
state.
Plugin
src/gateways/<gateway>/<gateway>.plugin.ts — implements GatewayPlugin and exports a
create<Gateway>Plugin(config) factory.
Keep a thin local HTTP client, as the Razorpay plugin does. Do not take the vendor's SDK as a
dependency unless you have verified it works under the CommonJS output tsc emits.
Webhook verification
Inside the plugin. Each gateway canonicalizes differently — Razorpay signs the raw body, Paytm uses a checksum. Write a dedicated function per gateway and never share one.
If the gateway needs a non-JSON body parser, mount it in src/app.ts before the webhook
router and before the global express.json().
Registration
src/gateways/gateway.registry.ts, in three places:
// 1. the supported-name list
export const supportedGateways = ['razorpay', 'paytm', 'payu', 'cashfree'] as const;
// 2. required env keys and base URLs
export const gatewayEnvDefinitions = {
mygateway: {
requiredEnvKeys: ['MYGATEWAY_KEY', 'MYGATEWAY_SECRET'] as const,
defaultBaseUrl: 'https://api.mygateway.com',
baseUrlEnvKey: 'MYGATEWAY_BASE_URL',
},
};
// 3. credential field → env key mapping
export const gatewayCredentialEnvKeys = {
mygateway: { key: 'MYGATEWAY_KEY', secret: 'MYGATEWAY_SECRET' },
};Then add the case in registerGateways() that wraps your factory in withTelemetry(...).
Telemetry
Add the gateway name to METRIC_GATEWAYS and its webhook event names to WEBHOOK_EVENTS in
src/telemetry/attributes.ts. If the plugin throws a signature-failure code other than
GATEWAY_INVALID_SIGNATURE, add it to SIGNATURE_FAILURE_CODES. The attributes test fails
until these sets match.
Tests
tests/unit/gateways/<gateway>/ for the plugin and mapper. The mapper test should cover every
native status the gateway documents, plus an unknown one asserting GatewayMappingError.
Add mock handlers at mocks/gateways/<gateway>/handlers.ts and spread them into
mocks/server.ts so integration tests have something to call.
Then update the docs
A gateway that works but is undocumented is a gateway the next contributor will re-derive from source.
Checklist
- Types, mapper, plugin, webhook verification, registration, tests
-
toUnifiedStatus()raisesGatewayMappingErroron anything unlisted -
withTelemetry(...)applied at the registry assignment - Name added to
METRIC_GATEWAYS, events toWEBHOOK_EVENTS - No gateway-specific type crosses the folder boundary
- No service or controller file touched
- Body parser mounted in the right order, if a non-JSON webhook
- Mock handlers added
-
npm run typecheckclean under strict mode - Docs updated
Engineering rules that trip people up
| Rule | Why it bites |
|---|---|
noUncheckedIndexedAccess | Indexed access returns T | undefined, so creds['key'] needs a fallback |
exactOptionalPropertyTypes | You cannot assign undefined to an optional prop — omit the key with a conditional spread |
noImplicitReturns | Every code path must return, including the default of a switch |
no-explicit-any | An error, not a warning. There is no escape hatch. |
explicit-function-return-type | An error with allowExpressions: false — annotate every function, including arrows |
import/no-default-export | An error in src/. src/app.ts is the only production file permitted a default export. |