BillStack sandbox
Test your billing integration end to end without payment provider keys - a simulated hosted checkout with test cards, a headless simulate API for agents and CI, and test clocks that move time.
The BillStack sandbox is a simulated payment provider that BillStack runs for you, in test mode only. A team whose test environment runs on the BillStack sandbox needs no Stripe or Razorpay keys: checkout, subscriptions, renewals, failed payments, refunds and entitlements all work, and nothing ever touches a payment provider or moves money.
Use it when you want fast, repeatable tests. To check how your real provider behaves, keep the provider sandbox and your provider's own test mode.
What you need:
- a team whose test environment runs on the BillStack sandbox (choose it under Integrations > Sandbox, see Switching sandbox);
- a
bs_test_API key. Abs_live_key is refused with403 SANDBOX_TEST_ONLY, and a team on the provider sandbox answers409 NOT_NATIVE_SANDBOX; - a project with a catalog price. Pull your live catalog into the sandbox or create products and prices as usual and publish them to test (
catalog.publishPriceVariant); see Multi-provider v2.
How a test runs
You integrate exactly as for any provider: create a v2 checkout with the usual endpoint and redirect the customer to the url it returns. In the sandbox that URL is a BillStack page, and the payment is decided by a test card.
import { BillStackV2Client } from "@billstack/sdk";
const billing = new BillStackV2Client({
baseUrl: process.env.BILLING_PORTAL_URL!,
apiKey: process.env.BILLING_PORTAL_API_KEY!, // bs_test_...
teamId: process.env.BILLING_PORTAL_TEAM_ID!,
projectId: process.env.BILLING_PORTAL_PROJECT_ID!,
});
// 1. Create the checkout like in production.
const checkout = await billing.checkout.create({
externalId: "user_1",
customerExternalId: "user_1",
priceVariantId,
country: "US", // an unsigned country is allowed with test keys
idempotencyKey: "test-run-1",
successUrl: "https://your-app.example/billing?checkout=success",
cancelUrl: "https://your-app.example/pricing",
});
// 2. Pay it without a browser.
const paid = await billing.sandbox.simulateCheckout(checkout.data.sessionId, {
outcome: "pay",
});
// 3. Entitlements follow, like after a real payment.
const entitlement = await billing.entitlements.get("user_1");A person can open checkout.data.url instead of step 2. The result is the same. The sandbox does not route by country, region or rule: a team on the BillStack sandbox always checks out through its sandbox, so the country is only recorded.
Hosted checkout
The checkout page at /sandbox/checkout/{token} shows the product, the price and the customer, a card field that accepts the test cards, and quick buttons: Pay, Decline, Insufficient funds, Require authentication and Cancel. After a payment or a cancel it sends the customer to your successUrl or cancelUrl.
- The token in the URL is the only thing the page trusts. A session expires after 24 hours (the page says so), and an unknown token is a 404.
- A card that fails leaves the session open and creates nothing, as Stripe Checkout does: the customer can try another card.
- A card that needs authentication creates the payment and parks the session in
requires_actionuntil the simulated 3D Secure step is completed (the session completes) or failed (the session is canceled). - Success URLs are not proof of payment, as in production. Entitlements, webhooks and invoices follow from the events the sandbox emits.
Test cards
The numbers are Stripe's, so integrations reuse the muscle memory. The checkout page asks for the card number only (no expiry or CVC), and it lists these cards for you to pick. A number that is not in the table is declined with incorrect_number.
| Card | Behaviour |
|---|---|
4242 4242 4242 4242 | Succeeds |
4000 0000 0000 0002 | Declined (card_declined, generic_decline) |
4000 0000 0000 9995 | Declined for insufficient funds |
4000 0000 0000 0341 | Attaches to a customer, then every later charge is declined. Use it with setPaymentMethod (or on a sample customer) to test a failing renewal. As the card of a checkout it is declined at once with card_error, because the checkout charges it immediately |
4000 0025 0000 3155 | Needs authentication when it is first used, later payments (renewals) succeed |
4000 0027 6000 3184 | Needs authentication every time. A renewal has nobody to authenticate, so it fails with authentication_required |
4000 0000 0000 0069 | Expired card (expired_card) |
4000 0000 0000 0119 | Processing error (processing_error) |
What the sandbox simulates
- One-time payments and subscriptions. Prices recur daily, weekly, monthly or yearly, every
intervalCountintervals. A monthly subscription started on the 31st renews on the last day of shorter months. - Trials. A price with trial days starts the subscription in
trialingand charges when the trial ends. - Renewals. When a period ends the sandbox creates the invoice, finalizes it and attempts the payment with the customer's default card.
- Failed payments. A failed renewal moves the subscription to
past_dueand retries the payment 1, 3, 5 and 7 days after the first failure (five attempts in all). If every retry fails the subscription is canceled. Paying with a new card in between (seesetPaymentMethodbelow) makes the next retry succeed. - Cancel. Cancel at period end and cancel now.
- Refunds. Full or partial, until the payment is used up.
- Lifetime deals. A lifetime deal built on a Catalog price launches into the sandbox, sells its current step in the hosted checkout, counts each simulated payment and moves to the next step at its boundary, exactly as in live.
- Events. Every transition emits Stripe-shaped events (ids like
cus_sbx_...,sub_sbx_...,in_sbx_...,pi_sbx_...,livemode: false) into the same pipelines real provider events use. Dashboard pages, entitlements, outbound webhooks and BillingQL therefore behave as they do with a provider.
What it does not simulate yet (proration, coupons, tax, metered billing, disputes, quantities above one, a customer portal) is listed under limitations.
Simulate API
The simulate API finishes a hosted checkout, changes a customer's card and refunds a payment without a browser. It is for agents and CI. Every route needs a bs_test_ key; the complete reference is Sandbox API.
POST /api/billstack/v2/teams/{teamId}/sandbox/checkout-sessions/{sessionId}/simulate
POST /api/billstack/v2/teams/{teamId}/sandbox/customers/{customerId}/payment-method
POST /api/billstack/v2/teams/{teamId}/sandbox/payments/{paymentId}/refundsessionId is the sessionId of the v2 checkout result (cs_sbx_...). The simulation outcome is one of:
| Outcome | Effect |
|---|---|
pay | Pay with card (default 4242 4242 4242 4242) |
decline | A generic decline; the session stays open |
insufficient_funds | An insufficient funds decline; the session stays open |
require_authentication | The payment needs 3D Secure and waits for the next two outcomes |
complete_authentication | Complete the pending authentication; the session completes |
fail_authentication | Fail the pending authentication; the session is canceled |
cancel | The customer cancels; the session is canceled |
With the SDK:
const result = await billing.sandbox.simulateCheckout(sessionId, {
outcome: "pay",
card: "4242 4242 4242 4242", // optional, this is the default
});
// result.data: { status: "complete", subscriptionId, paymentId, redirectUrl }
// Make later renewals fail: swap in a card that attaches, then declines every charge.
await billing.sandbox.setPaymentMethod(customerId, { card: "4000 0000 0000 0341" });
// Make the next renewal succeed again.
await billing.sandbox.setPaymentMethod(customerId, { card: "4242 4242 4242 4242" });
// Give part of a payment back.
await billing.sandbox.refund(paymentId, { amount: 500 });4000 0000 0000 0341 cannot be the card of a checkout simulation: the checkout charges the card at once, so the call returns card_error and creates no subscription. Pay with a good card first, then switch the customer to 0341 with setPaymentMethod.
Checkout outcomes are returned as status: complete, canceled, requires_action or card_error (a declined card, with code, declineCode and message). Replaying an outcome on a finished session is a 409 (SESSION_ALREADY_COMPLETE or SESSION_CLOSED).
Test clocks
A test clock freezes simulated time for the customers attached to it. You advance the clock, and everything that falls due in between happens, in time order: trial ends, renewal invoices, payment attempts, retries, cancellations at period end. A month of billing takes one call.
const clock = await billing.sandbox.clocks.create({
name: "renewal test",
frozenTime: "2026-01-01T00:00:00Z", // optional, defaults to now
});
// ... customers created by the dashboard's sample data are attached to this clock ...
const advanced = await billing.sandbox.clocks.advanceAndWait(clock.data.id, {
to: "2026-02-01T00:00:00Z",
});
// advanced.data.status === "ready", advanced.data.frozenTime === "2026-02-01T00:00:00.000Z"- Attachment. A customer belongs to a clock from the moment it is created and never moves. A clock holds up to 10 customers. Customers created by your own checkouts run on real time and cannot be attached to a clock through the API (see below); the dashboard creates clock customers for you with sample data and the Attach to a new test clock option, and the Test clocks page of the dashboard lists each clock with its customers and lets you advance it.
- Advance.
advanceanswers202with the clock inadvancingand runs the work in the background. Pollgetuntilstatusisreadyagain, or useadvanceAndWait. The target must be later than the clock's frozen time, and only one advance runs at a time (409 CONFLICTotherwise). A clock that ends ininternal_failurecan be deleted and recreated. - Reads follow the clock. For a customer on a clock, entitlement reads judge access at the clock's time: advance a clock past the end of a period without a successful renewal and the customer loses access. Customers without a clock, and everything in live mode, always use real time.
- Delete. Deleting a clock deletes its customers and subscriptions (each emits
customer.deleted). It is refused while the clock is advancing. - Real time. Subscriptions that are not on a clock run on real time: BillStack checks for due work every minute, so a one-day trial ends a day later. Customers created through the API (your own checkouts) have no way to join a clock yet, so they renew on real time only; to move time for a customer, use sample data created with Attach to a new test clock.
Quick recipes
A successful first payment and an entitlement. Create a checkout, simulateCheckout with pay, read the entitlement.
A failed payment. simulateCheckout with decline or the card 4000 0000 0000 9995 and check that no entitlement is granted and the session is still open.
A failing renewal. In the dashboard, generate sample data with Attach to a new test clock (sample customers already have a subscription and a card, so there is no first checkout to pay). Take a sample customer's id from the dashboard, switch its card to 4000 0000 0000 0341 with setPaymentMethod, then advance the clock past the end of the period. The subscription becomes past_due; advance a week more and it is canceled. In between, setPaymentMethod with a good card makes the next retry succeed.
3D Secure. simulateCheckout with require_authentication, then complete_authentication or fail_authentication.
A refund. Take the paymentId from the simulation result and refund it in full or in part.