Skip to Content
Test

Payments

Fake Razorpay, Stripe and PayU servers behind the real signet-payments adapters.

Usage

import { createPayments, memoryStore } from "@dapplelight/signet-payments"
import { fakeGateway } from "@dapplelight/signet-test/payments"

const gw = fakeGateway("razorpay")
const payments = createPayments({ gateways: [gw], db: memoryStore() })

const c = await payments.checkout({ amount: 49900, currency: "INR", idempotencyKey: "k1", userId: "u1" })
await payments.handleWebhook("razorpay", await gw.complete(c.gatewayId)) // or { status: "failed" }

console.log(gw.calls.map((x) => x.method)) // ["createCheckout", "verifyWebhook"]
console.log(gw.state.payments.size) // 1, in Razorpay's payment format

fakeGateway is the real adapter pointed at an in-memory server that speaks that gateway's HTTP API: id formats, bodies, checkout handoff, idempotency keys and signed webhooks. Nothing reaches the network.

API

ExportWhat it does
fakeGateway(shape, options?)A FakeGateway for "razorpay", "stripe" or "payu".
FakeGatewayType: a PaymentGateway plus the helpers below.
FakeGatewayStateType: maps of payments, orders, refunds, customers, methods, subscriptions, payouts.
GatewayShapeType: "razorpay" | "stripe" | "payu".
GatewayCallType: { method, args }.
FaultType: "decline" | "unavailable" | "timeout".

fakeGateway(shape, options?)

OptionTypeDefault
webhookSecretstring"whsec_signet_test_fake_secret"Secret used to sign webhooks.
now() => numberDate.nowEpoch ms for every timestamp and Stripe's signature check. Pass clock.now.

FakeGateway helpers

MemberWhat it does
callsInterface calls, in order.
requestsHTTP requests the adapter made to the fake server.
stateThe server's maps.
complete(checkoutId, { status? })The buyer finishes checkout: "captured" (default), "authorized" or "failed". Returns the signed webhook Request.
subscribe({ plan, amount?, currency?, customer?, periodDays?, notes? })Start an active subscription; returns { id, webhooks }. Not on PayU.
renew(id, { fail? })Charge the next period. A failed charge halts on Razorpay and goes past_due on Stripe. Returns webhooks.
settlePayout(id, outcome)"processed", "failed" or "reversed". Returns the webhook. Not on PayU.
takeWebhooks()Webhooks queued by API changes (cancelSubscription, refunds), oldest first.
fail(fault, { on?, times? })Make the next matching server requests fail.
reset()Empty every map, queued webhook and fault. Ids keep counting.

PayU has no subscription or payout API, so those reject UNSUPPORTED, as the real adapter does.

Faults

on matches "METHOD /path" (PayU: the command name). times defaults to 1.

FaultEffect
declineThe gateway refuses.
unavailableThe server is down.
timeoutThe server does the work, then the response is lost. A keyed retry must get the stored answer, not repeat it.

Webhook Requests are single-read: .clone() one to deliver it twice.

Examples

Failed renewal

import { fakeGateway } from "@dapplelight/signet-test/payments"

const gw = fakeGateway("stripe")
const { id } = await gw.subscribe({ plan: "pro_monthly", amount: 49900, currency: "INR" })
const hooks = await gw.renew(id, { fail: true }) // subscription goes past_due

Retry after a timeout

import { fakeGateway } from "@dapplelight/signet-test/payments"

const gw = fakeGateway("razorpay")
gw.fail("timeout", { on: "POST /v1/orders" })
// the first checkout attempt times out after the server created the order;
// retrying with the same idempotencyKey must return the same order