# Custom & headless

This page is the complete server-side contract for a custom or headless store (your own checkout, or a commerce backend with a separate storefront). Every platform guide is a special case of it.

## The flow

```text
 Shopper                Your storefront / server                       OnCharge
    |  checkout  ------>  1. POST /api/v1/checkout/sessions  ------->  session_id, iframe_url
    |  <------- 2. show checkout (redirect or iframe on your domain)
    |  pays in the OnCharge checkout  ---------------------------------> 
    |                    3. POST /api/v1/checkout/sessions/{id}/order  (if the order was created after the session)
    |                    4. webhook payment.captured  <----------------  signed POST to your endpoint
    |                    5. fulfil, then POST /api/v1/shipments  ----->  carrier tracking
    |                    6. webhook delivery.confirmed  <--------------  payout released 1 day later
```

## What to store

Keep these on your order record:

| Field | From |
|---|---|
| `oncharge_session_id` | `session_id` from session creation. Every later call uses it. |
| `oncharge_status` | The latest webhook `data.status`. |
| `oncharge_reference` | The `reference` you sent (your order number or cart ID). |
| Processed webhook IDs | `OnCharge-Event-Id`, to skip duplicates. |

## 1. Create the session

Server-side, from the cart or order you hold. Never accept an amount from the browser.

- `reference`: your order number, or the cart ID if the order does not exist yet.
- `Idempotency-Key`: the same order or cart ID, so double clicks and retries return the same session.
- `shipping`: the delivery address for anything you ship (needed for payout on delivery).
- `return_url`: on a registered store domain.
- If the cart changes (amount, currency or address), create a new session with a new idempotency key (for example `cart-123-v2`).

## 2. Show the checkout

Redirect to `https://app.oncharge.io` + `iframe_url`, or embed it on a page served from a registered domain. See [Checkout](https://oncharge.io/docs/checkout).

## 3. Attach the order (cart-first checkouts)

If your platform creates the order only after payment starts, attach it as soon as it exists:

```http
POST /api/v1/checkout/sessions/{session_id}/order
{ "order_id": "1001", "shipping": { … } }
```

Values are set once. Retry failures for up to 24 hours (after that, and after tracking is reported, the address can no longer be recorded).

## 4. Webhooks

Handle at least `payment.captured`, `payment.refused`, `payment.cancelled`, `refund.succeeded` and `chargeback.opened`. See [Webhooks](https://oncharge.io/docs/webhooks). Reconcile anything missed by reading [`GET /api/v1/checkout/sessions/{id}`](https://oncharge.io/docs/api#getcheckoutsession), for example for orders still pending after an hour.

| Session status | Set your order to |
|---|---|
| `created`, `pending` | Awaiting payment |
| `authorized` | Payment approved (wait for capture) |
| `captured` | Paid |
| `refused`, `cancelled`, `expired`, `error` | Payment failed / abandoned |

## 5. Shipments

Report carrier and tracking numbers with [`POST /api/v1/shipments`](https://oncharge.io/docs/shipments) when labels are created or orders are marked shipped (USPS, UPS, FedEx, DHL Express). Store the per-number results; show rejected numbers to your staff. Poll [`GET /api/v1/shipments?session_id=…`](https://oncharge.io/docs/api#getdeliverystatus) or listen for `delivery.confirmed`.

## 6. Refunds, cancellations, disputes

- **Refund** a captured payment: [`POST /api/v1/refunds`](https://oncharge.io/docs/api#createrefund) with `Idempotency-Key` = your refund ID.
- **Cancel** an authorized, uncaptured payment: [`POST /api/v1/checkout/sessions/{id}/cancel`](https://oncharge.io/docs/api#cancelcheckoutsession).
- **Disputes**: list with [`GET /api/v1/disputes`](https://oncharge.io/docs/api#listdisputes) and upload evidence before `defense_deadline`. When a dispute has `responded_by: "oncharge"`, the OnCharge team submits the response from your uploads and the defend, accept and auto-defend calls return `409 responded_by_oncharge`; otherwise defend or accept it yourself. Or use the merchant portal.

## Retries and errors

| Situation | Do this |
|---|---|
| Network error or timeout | Retry with the same `Idempotency-Key`. |
| `429 rate_limited` | Wait until `X-RateLimit-Reset`, then retry. |
| `5xx` | Retry with exponential backoff. |
| `401` | Fix signing (see [Errors](https://oncharge.io/docs/authentication#errors)); do not retry blindly. |
| `400 validation_error` | Fix the request; the message names each invalid field. |
| `409` | The action does not fit the payment's state; read the session first. |

## Platform notes

**Shopify (headless / Hydrogen).** OnCharge is not a Shopify payments app, so it cannot appear inside Shopify's own checkout. A headless storefront with its own checkout page can take payment with OnCharge and then record the order in Shopify from your server (for example as a paid draft order via the Admin API) when the `payment.captured` webhook arrives. Check that this fits your Shopify plan and terms before you build it.

**Medusa, Saleor, commercetools and similar.** Implement OnCharge as a payment provider/plugin of the platform: create the session in the provider's "initiate payment" step, attach the order on "authorize", map webhooks to the platform's payment status updates, and report tracking from the fulfilment step.

**Mobile apps.** Create the session from your backend and open the checkout URL in the system browser or an in-app browser tab, with `return_url` pointing at a page (or universal link) that returns to the app. Confirm the payment from your backend, never from the app.
