Skip to content
OnCharge.io
View as Markdown
Developer docs

Customers & saved cards

OnCharge keeps your store's customers: who paid, what they spent, their refunds and disputes, and the cards they chose to save with you. A customer belongs to one store. The same shopper at two of your stores is two customers, and a card saved for one store can never be charged by another.

Customers are added three ways:

  • By paying. A paid checkout with an email makes the shopper a customer of the store.
  • Through the API. POST /api/v1/customers with an email, a name or your own reference.
  • By saving a card. At checkout or on a save-card page (below).

See them in the portal under Payments > Customers: lifetime value, payments, refunds, disputes and saved cards, with search and a CSV export.

Add a customer#

Shell
POST /api/v1/customers
{ "email": "ann@example.com", "name": "Ann Smith", "reference": "crm-1042", "metadata": { "tier": "gold" } }

One email, and one reference, is one customer of a store. A second is refused with 409 customer_exists and the existing customer's ID in customer_id. GET /api/v1/customers?q=ann searches by an OnCharge customer ID, or part of an email, name, phone or your reference; GET /api/v1/customers/{customer_id} returns one customer with their totals and saved cards.

Save a card#

A card is saved only when the shopper ticks a box that says what they agree to: that your store keeps the card and may charge it later, including when they are not at the checkout, for purchases and payments they agree to with you. The words and their version are recorded with each consent. Use a saved card only for what the shopper agreed to.

At checkout. Create the session with the customer and save_card: true:

JSON
{ "amount": 2900, "currency": "USD", "reference": "order-1042", "return_url": "https://shop.example.com/thanks",
  "customer_id": "0b6e4a52-6c3f-4b7e-9d1a-2f3e4a5b6c7d", "save_card": true }

The checkout shows the box under the card form. It is never ticked for the shopper; when they leave it unticked, nothing is saved. (Instead of customer_id you can send customer_email: the shopper becomes your customer with that email.)

Without a payment. Create a save-card page and send the customer to its url:

Shell
POST /api/v1/customers/{customer_id}/payment-methods
{ "return_url": "https://shop.example.com/account" }

The page is hosted by OnCharge. It shows the consent first and the card form only after the customer agrees, then sends them to return_url. It is open for 24 hours (expires_in_hours, up to 72).

Saved cards show only the brand, the last four digits and the expiry. When a card network renews or replaces a saved card, OnCharge updates its expiry and last four digits for you.

Charge a saved card#

Shell
POST /api/v1/customers/{customer_id}/charges
Idempotency-Key: subscription-2026-12-ann
{ "amount": 2900, "currency": "USD", "reference": "subscription-2026-12" }

Always send an Idempotency-Key: a retry returns the same charge and never charges twice. Without payment_method_id the customer's default card is used.

A charge is a payment like any checkout payment: the same fee, the same delivery-gated payout (an order that ships by carrier is paid out after delivery, so send shipping with order_id), the same payment.* webhooks, and it appears under Payments. Its session_id works with refunds and shipments.

statusWhat happened
succeededPaid (201).
processingThe bank has not answered yet. A payment webhook follows (201).
requires_authenticationThe customer's bank wants them to confirm the payment. OnCharge emailed them a link to an OnCharge page where they confirm it (authentication.emailed). The link goes to the customer only, so the cardholder confirms it, not whoever holds your API key (201).
declineddecline.reason and decline.message say why in plain words, for example insufficient_funds or expired_card (402).

If the answer is 502 try_again, the outcome is not known yet: retry with the same Idempotency-Key within 20 hours. A retry always charges the card the charge was started with. After 20 hours the charge answers 409 charge_not_completed; check Payments, then start a new charge with a new key.

POST /api/v1/recurring/charge keeps working: on a store with its own card account it charges the saved card of your customer with that shopper_email. New integrations should use the customers API.

Remove a saved card#

Shell
DELETE /api/v1/customers/{customer_id}/payment-methods/{payment_method_id}

The card is removed for good and can never be charged again. Owners and admins can also remove a card from the customer's page in the portal.

SDK#

TypeScript
const customer = await oncharge.customers.create({ email: 'ann@example.com' });
const page = await oncharge.customers.paymentMethods.createSetupSession(customer.id, { return_url: 'https://shop.example.com/account' });
const charge = await oncharge.customers.charges.create(customer.id, { amount: 2900, currency: 'USD' }, { idempotencyKey: 'subscription-2026-12-ann' });