Guide

Saving cards

Save a card with a SetupIntent, a setup-mode Checkout Session or a payment with setup_future_usage, then read it back and charge it later.

Saving cards and charging them later is switched on per account. If a save is refused with 403 VAULTING_NOT_ENABLED, or a charge with 403 OFF_SESSION_NOT_ALLOWED, it is not on for your account yet. Contact support to have it enabled.

Ways to save a card

On the hosted checkout page you never build a card form: we render it, run 3DS when the issuer demands it, and tokenize. Every option below ends in the same artifacts (a payment_method and a mandate); they differ in whether you charge today and who confirms the card. A card saved with no active mandate cannot be charged off-session.

You want to…UseWhy
Save a card now, charge later (free trial, save-for-renewal)Checkout Session in setup modeVaults a card with no charge today
Save a card now from your own serverSetupIntent, then confirm itYou state the customer's consent yourself. See Setup Intents.
Charge now and save the cardCheckout Session or PaymentIntent with setup_future_usageCharge and vault in a single hosted checkout
Charge now, never saveCheckout Session, mode: "payment"The default. Don't set setup_future_usage.

Rule of thumb: collecting money today? Use setup_future_usage. Just saving a card? Use setup mode.

Two rules apply to every way of saving a card:

  • The customer is required. A SetupIntent needs a customer when you create it, and the consent is recorded against that customer. A SetupIntent always records a mandate. On a payment, send setup_future_usage: "off_session" for a card you will charge with the customer absent: a card saved with "on_session" gets no mandate and cannot be charged off-session.
  • Full billing details are needed at save. A card that will be charged off-session must be saved with a first and last name, an email, and an address with line1, city and country, plus state and postal_code for the US and Canada. Depending on how your account is set up, a save without them is refused with 422 MISSING_BILLING_DETAILS, the response names the missing fields, and nothing is saved.

Flow A — hosted setup (no charge today)

Create a Checkout Session in setup mode. It mints and owns a SetupIntent for you — there is no separate POST/v1/setup-intents call, and you do not pass a setup_intent. Redirect the customer to session.url; we render the card form, run 3DS when the issuer demands it, and tokenize.

Request
1curl -X POST https://api.cxpay.net/v1/checkout-sessions \
2 -H "X-Key-Id: ak_test_..." \
3 -H "Content-Type: application/json" \
4 -H "Idempotency-Key: $(uuidgen)" \
5 ... HMAC headers ... \
6 -d '{
7 "mode": "setup",
8 "customer": "cus_01JQX...",
9 "return_url": "https://yoursite.com/cards/saved",
10 "cancel_url": "https://yoursite.com/cards/cancel"
11 }'
TestPOST /v1/checkout-sessions

Wait for setup_intent.succeeded

JSON
1{
2 "id": "evt_01JQX...",
3 "object": "event",
4 "type": "setup_intent.succeeded",
5 "created": 1744054500,
6 "createdAt": "2026-04-07T18:35:00.000Z",
7 "apiVersion": "2026-01-01",
8 "livemode": false,
9 "merchantOrgId": "morg_01JQX...",
10 "data": {
11 "object": {
12 "setupIntentId": "seti_01JQX...",
13 "customerId": "cus_01JQX...",
14 "paymentMethodId": "pm_01JQX...",
15 "paymentMethodTypes": ["card"],
16 "usage": "off_session",
17 "status": "succeeded",
18 "description": null,
19 "metadata": {}
20 }
21 }
22}

The paymentMethodId in data.object is the id of the saved card. To read the full card details and its mandate, GET/v1/payment-methods/{paymentMethodId} — the PaymentMethod object carries both. Don't poll — verify the signature, return 200, and you're done.

Failures and retries

setup_intent.setup_failed fires when an attempt couldn't tokenize. The parent SetupIntent stays open — the customer can try again. Inspect last_setup_error.code and surface a human-readable reason.

Flow B — Checkout Session + setup_future_usage (charge + save in one)

For checkout flows where you're collecting money today AND want to save the card for next time, set setup_future_usage on the Checkout Session, or on a PaymentIntent with a customer that the customer pays on its hosted page.

1. Create the Checkout Session with setup_future_usage

cURL
1curl -X POST https://api.cxpay.net/v1/checkout-sessions \
2 ... HMAC headers + Idempotency-Key ... \
3 -d '{
4 "mode": "payment",
5 "amount": 4900,
6 "currency": "USD",
7 "customer": "cus_01JQX...",
8 "setup_future_usage": "off_session",
9 "return_url": "https://yoursite.com/success",
10 "cancel_url": "https://yoursite.com/cancel"
11 }'

The customer sees the same hosted page, with a consent disclosure: "By saving this card, you authorize {your business} to charge it for future purchases." When they hit pay, we charge AND vault.

setup_future_usage: "off_session" saves the card with a mandate, so you can charge it later with the customer absent. "on_session" saves the card with no mandate: nothing can charge it off-session.

In test mode, do not use an amount of exactly 45.00 for a payment that saves its card. The test card network answers that amount in a way that leaves the payment in processing. Any other amount behaves normally.

2. Wait for payment_intent.succeeded

JSON
1{
2 "id": "evt_01JQX...",
3 "object": "event",
4 "type": "payment_intent.succeeded",
5 "created": 1744054500,
6 "createdAt": "2026-04-07T18:35:00.000Z",
7 "apiVersion": "2026-01-01",
8 "livemode": false,
9 "merchantOrgId": "morg_01JQX...",
10 "data": {
11 "object": {
12 "paymentIntentId": "pi_01JQX...",
13 "amount": 4900,
14 "amountReceived": 4900,
15 "currency": "USD",
16 "status": "succeeded",
17 "chargeId": "ch_01JQX...",
18 "clientReferenceCode": null,
19 "customerId": "cus_01JQX..."
20 }
21 }
22}

The paymentIntentId in data.object confirms which PI succeeded. The webhook payload doesn't include the saved card, so read it from the payment_method.attached event that fires alongside, or list the customer's payment methods (GET/v1/payment-methods?customer=..., newest first).

Reading back the saved PaymentMethod

Three ways:

  1. Webhook + GET — setup_intent.succeeded (or the payment_method.attached event) gives you data.object.paymentMethodId. Use it to GET/v1/payment-methods/{id} — the PaymentMethod carries the card details and its mandate. This is the recommended path.
  2. GET the PaymentMethod directly — useful when a webhook is delayed.
  3. List by customer — GET/v1/payment-methods?customer=cus_01JQX... returns every saved card on a customer, newest first.
JavaScript
1// Re-read after the webhook
2const headers = signRequest({ method: "GET", path: `/v1/payment-methods?customer=${cusId}`, body: null, ... });
3const res = await fetch(`https://api.cxpay.net/v1/payment-methods?customer=${cusId}`, { headers });
4const { data: pms } = await res.json();
5const newest = pms[0];
6console.log(newest.card.brand, newest.card.last4, newest.status);

Setting the customer default

After vaulting, persist the PM as the customer's default for invoices and recurring billing:

cURL
1curl -X PATCH https://api.cxpay.net/v1/customers/cus_01JQX... \
2 ... HMAC headers ... \
3 -d '{ "default_payment_method": "pm_01JQX..." }'

Set the customer's default card by writing the top-level default_payment_method field. A PaymentIntent never uses it: every off-session charge names its payment_method.

What you've collected

After either flow:

  • A PaymentMethod (pm_...) — the saved card. Carries brand, last4, expiry, funding type, status, and latest_network_transaction_id.
  • A Mandate (mdt_...): the cardholder's authorization to charge the card. Records customer_acceptance: when the customer agreed and whether it was online (their IP address and user agent) or offline.
  • An attached Customer (cus_...) — owns the PM and Mandate.

Treat mandate.created as "chargeable", not payment_method.attached: the card can be charged off-session once its mandate exists. See Charging saved cards for the MIT flow.

  • POST/v1/payment-methods/{id}/detach is permanent. It moves the card to detached, revokes every active mandate on it and emits payment_method.detached and mandate.revoked. A detached card cannot be attached or charged again: save the card again. DELETE/v1/payment-methods/{id} purges the card, also permanently. Both calls need an Idempotency-Key header.
  • POST/v1/mandates/{id}/revoke ends the consent and leaves the card attached. It needs an Idempotency-Key header too.
  • mandate.revoked, payment_method.detached, payment_method.expired and payment_method.compromised all mean: stop charging this card.

Powered by CX Pay