{"object":"doc","slug":"test-billing-with-test-clocks","title":"Test your billing with test clocks and failure injection","summary":"Run renewals, declines, retries, reminders and period ends in seconds. A test clock moves a customer's subscriptions through time with the real billing code; fail_next_charges makes a test card's next charges decline or go unknown. Test mode only.","tags":["guide","billing","subscriptions","sandbox","testing"],"body":"# Test your billing with test clocks and failure injection\n\nA test clock is a frozen time that a customer, and every subscription of that customer, lives on. Advancing it walks every renewal, retry, reminder and period end in between with the same billing code that will run on real time: real sandbox charges on the saved test card, real events and real webhooks. Failure injection makes a test card's next billing charges end without reaching the processor, so you can rehearse what your app does on a decline or an unknown outcome.\n\nBoth are test mode only. A live key is refused, a clock can never hold a live merchant's customer, and an injected outcome never touches a live card. In this release the scheduler that renews on real time is not switched on, so a test clock is also the only way to see a renewal at all.\n\n## 1. Create a clock and a customer on it\n\n```\nPOST /v1/test_helpers/test_clocks\n{\"frozen_time\": 1798761600, \"name\": \"renewal test\"}\n```\n\nMCP: `create_test_clock`. Answer: `{id: clock_..., object: test_helpers.test_clock, status: ready, frozen_time}`. Start at the current time or close to it.\n\n```\nPOST /v1/customers\nIdempotency-Key: clock-customer-1\n{\"merchant_id\": \"mch_123\", \"test_clock\": \"clock_123\"}\n```\n\nMCP: `create_customer` with `test_clock`. The customer's `test_clock` names the clock. Save a test card for that customer with no browser (`POST /v1/payment_methods` or `save_payment_method` with `\"nonce\": \"tok_visa\"`; or test card `4761739001010010` typed in a browser on `sandbox_form_url`), then subscribe it: `POST /v1/subscriptions` (MCP `create_subscription`) or a subscription-mode checkout session for that customer, paid with `POST /v1/test_helpers/checkout/sessions/{id}/confirm` and `{\"card\": \"tok_visa\", \"email\": \"buyer@example.com\"}` (MCP `confirm_checkout_session`). The customer above has no email, so the confirm needs `email` (without it: `400 parameter_missing`, `param: email`); it is ignored when the session's customer has an email or the session has `customer_email`. The subscription's `test_clock` names the clock, and its first period starts at the clock's `frozen_time`.\n\nThe create, with the saved card's id (`default_payment_method` is required; without it the answer is `400 payment_method_required`):\n\n```\nPOST /v1/subscriptions\nIdempotency-Key: clock-subscription-1\n{\"merchant_id\": \"mch_123\", \"customer\": \"cus_123\", \"items\": [{\"price\": \"price_123\"}],\n \"default_payment_method\": \"pm_123\"}\n```\n\n## 2. Advance past the renewal\n\n```\nPOST /v1/test_helpers/test_clocks/{test_clock_id}/advance      (clock_123)\n{\"frozen_time\": 1801440000}\n```\n\nMCP: `advance_test_clock`. Choose a time just past `current_period_end`. The answer comes at once with `status: advancing`; a worker then processes the clock. Poll `GET /v1/test_helpers/test_clocks/{test_clock_id}` (MCP `get_test_clock`) every second or two until `status` is `ready`. A clock whose `status` is `internal_failure` can never advance again (`409 test_clock_internal_failure`): stop polling, delete it and create a new one.\n\nAn advance can move at most two intervals of the shortest subscription on the clock, or 13 months if that is more (two years with none); further is `400 parameter_invalid` on `frozen_time`. At most 3 clocks of one platform advance at once; a fourth answers `409 test_clock_advance_limit` with `Retry-After`. Advance again for more. A `ready` clock whose `frozen_time` is short of your target says why in `status_details.last_advance.stopped`:\n\n| `stopped` | Meaning | Do |\n|---|---|---|\n| `attempt_in_flight` | A charge's answer is still coming | A real charge: advance again in a few seconds. After an injected `processor_unavailable`: resolve the invoice's hold now (step 4), then advance again |\n| `invoice_held` | A charge's outcome is unknown, or a duplicate screen is held | Resolve the hold (step 4), then advance again |\n| `billing_paused` | Billing is paused for the deployment | Try later |\n| `error` | The advance failed; `detail` says where | Advance again; report it if it repeats |\n\nThen read what happened: `get_subscription` shows the new period, `list_invoices` a paid `subscription_cycle` invoice, and `list_events` (or your webhook receiver) `invoice.upcoming` (3 days before the renewal by default), `customer.subscription.updated` (`latest_invoice` names the new renewal invoice), `invoice.paid`, `invoice.payment_succeeded` and `customer.subscription.updated` (the new period). `integration_checklist` marks `renew_on_test_clock` done.\n\n`ready` means the billing work is done and its events are written, not that your webhook receiver has them: deliveries go out a few seconds later (usually under 10 seconds; longer while your receiver is failing and being retried). So after `ready`, wait for the events at your receiver, or read them at once with `list_events` (`GET /v1/events`) and `since`, before you assert on your app's state. `GET /v1/webhooks/{webhook_id}/deliveries` shows each delivery's `status` and `next_attempt`.\n\nTimes on a clock: an invoice's `created`, its periods and the subscription's dates are clock time. The event envelope's own `created`, and payments, are real time.\n\n## 3. Rehearse a failed renewal\n\n```\nPOST /v1/test_helpers/payment_methods/{payment_method_id}/fail_next_charges      (pm_123)\n{\"outcome\": \"card_declined\", \"count\": 4}\n```\n\nMCP: `fail_next_charges`. The next `count` billing charges of that card (a first payment, a renewal, a retry; 1 to 10) end with `outcome` and never reach the processor. A second call replaces the first. Then advance past the renewal and through the retries (+1, +3, +7 days by default):\n\n| `outcome` | What you see |\n|---|---|\n| `card_declined` | A decline: `invoice.payment_failed`, `past_due`, retries on schedule, and the final action at the last declined retry. See [Failed renewals and retries](failed-renewals-and-retries.md) |\n| `duplicate_transaction` | The processor's duplicate screen: never counted as a decline, never retried automatically. The invoice is held (`collection_status: reconciling`), the subscription gets `payment_hold`, and the clock stops with `invoice_held` until you resolve the hold (step 4) |\n| `processor_unavailable` | Unknown outcome: the payment stays `pending`, the invoice is `open` with `collection_status: in_progress` and the clock stops with `attempt_in_flight`. No failure event is sent. Resolve the hold (step 4) to continue; you do not wait for the 15 minutes a real lost answer takes |\n| `lost_card`, `stolen_card` | A hard decline that blocks this card at the merchant: `past_due`, `last_payment_error.decline_code` names it, `requires_new_payment_method: true`, and later retries of this card are skipped |\n| `revocation_of_authorization`, `stop_payment_order` | A hard decline that blocks this card for this subscription |\n| `transaction_not_allowed` | A hard decline that blocks this card for this invoice only |\n\nThere is no success outcome: a success comes from the card itself (a `tok_visa` card approves every charge, a typed `4761739001010010` is approved by the sandbox processor). A hard-decline outcome carries the same processor code the real decline carries, so the block, `requires_new_payment_method` and the events are the real ones; only the processor call is skipped.\n\nThe sandbox test cards cannot rehearse a hard decline: `4761739001010010` approves, and `4111111111111111` and `4242424242424242` are declined by the sandbox (reason RISK DECLINED), which billing treats as an ordinary retried decline. No sandbox card is known to answer lost, stolen or revoked. Use the outcomes above for hard declines, or save `tok_chargeDeclinedLostCard` or `tok_chargeDeclinedStolenCard`, whose every charge is that hard decline (`sandbox`, \"Test card tokens\").\n\nA hard decline leaves the card blocked, and in the sandbox that is usually the one approving card you saved. A block is matched by the card (brand, last four, expiry), and every `tok_visa` card is Visa 0010 12/2034, so a `lost_card` or `stolen_card` rehearsal blocks `tok_visa` for every customer of that test merchant, a newly saved one too, until you lift it. The checkout confirm helper's `402 payment_method_blocked` says so and names `lift_card_block`. After the rehearsal, lift the block:\n\n```\nPOST /v1/test_helpers/payment_methods/{payment_method_id}/lift_card_block      (pm_123)\n```\n\nMCP: `lift_card_block`. It lifts every block of that test card at its merchant and returns what it lifted; it charges nothing. Test mode only.\n\n## 4. End an injected hold\n\n```\nPOST /v1/test_helpers/invoices/{invoice_id}/resolve_hold      (in_123)\n```\n\nMCP: `resolve_invoice_hold`. There is no outcome to choose; the attempt always ends as not collected: nothing counted as a decline, never a sale, and a renewal is retried on schedule. Then advance again.\n\n- After `processor_unavailable` call it right away. A real lost answer stays `pending` for 15 minutes of real time before the sweeper ends it; an injected one sent nothing, so the call ends it at once the same way: `payment.failed` (`processor_unverified`), a `customer.subscription.updated` that sets `payment_hold`, then one that clears it.\n- After `duplicate_transaction` the call reads the processor back by the charge's reference. No sale (always the case for an injected one): the hold ends with one `customer.subscription.updated` that clears `payment_hold` (the hold was set when the charge was screened, so no event sets it now). A sale behind a duplicate screen is for ZeroPoint to rule on (`409 invoice_hold_has_sale`): delete the clock and start a new rehearsal.\n- A hold on an invoice that is not on a test clock answers `400 invoice_hold_not_injected`.\n\n### Paying by hand on a clock\n\nA clock's customer lives in the clock's time everywhere billing decides something, the manual pay cap included. The 60 second gap between two payments of one invoice (the portal's \"Wait a minute, then try the payment again.\", the API's `invoice_pay_limit` with a short `Retry-After`) is measured on the clock, so while the clock stands still that gap does not pass. Advance the clock by a minute or more, then pay again. The same holds for the cap's other counts (3 sends per invoice in 24 hours, 10 per card in 30 days).\n\n## 5. Clean up\n\n`DELETE /v1/test_helpers/test_clocks/{test_clock_id}` (MCP `delete_test_clock`) cancels every subscription on the clock, deletes its customers where it can, and deletes the clock. History is kept. A platform keeps at most 100 clocks, and a clock is deleted 30 days after it was created.\n\n## A full rehearsal in one pass\n\n1. `create_test_clock` at now.\n2. `create_customer` with `test_clock`, save a test card, `create_subscription` (status `active`).\n3. `advance_test_clock` past `current_period_end`; `get_test_clock` until `ready`. Renewal paid.\n4. `fail_next_charges` `card_declined` with `count: 4`.\n5. Advance past the next renewal: `past_due`. Events: `invoice.upcoming` (if the advance passes 3 days before the renewal), `customer.subscription.updated` (`latest_invoice`), `payment.processing`, `payment.failed`, `invoice.payment_failed` (`attempt_count` 1), `customer.subscription.updated` (`past_due`).\n6. Advance to the first decline plus 1 day, then plus 3 days: each time `payment.processing`, `payment.failed`, `invoice.payment_failed`; still `past_due`.\n7. Advance to the first decline plus 7 days: the last retry is declined and the final action runs in the same change: `payment.processing`, `payment.failed`, `invoice.payment_failed` (`next_payment_attempt: null`, `attempt_count` 4: one per slot, plus one for each declined manual payment if you made any), `invoice.marked_uncollectible`, `customer.subscription.deleted`. Advancing further does nothing more.\n\nEach advance that passes 3 days before a renewal sends `invoice.upcoming` for it (steps 3 and 5). Check that your app sent each email (the last-chance one on the +3 day failure) and revoked access at the end. That is the behaviour your users will see once billing is live.","updated":1795275945}