---
name: oncharge-checkout
description: Integrates OnCharge checkout into a store or web app. Covers HMAC-signed calls to the OnCharge merchant API (checkout sessions, attaching orders, refunds), hosted-redirect or embedded-iframe checkout, webhook signature verification, shipment/tracking reporting for delivery-based payouts, and the RUO compliance feed. Use when the user mentions OnCharge, oncharge.io, app.oncharge.io, X-Oncharge-Signature or OnCharge-Signature, or asks to add OnCharge payments to Next.js, React, Node, PHP, Python, WooCommerce, Magento, Wix, headless Shopify, Webflow/Framer or a custom site.
license: Proprietary
metadata:
  author: OnCharge
  version: "1.0.0"
  docs: https://oncharge.io/docs
---

# OnCharge checkout integration

OnCharge hosts the card checkout. The merchant's **server** creates a checkout session with a signed API call, the shopper pays on OnCharge's page (redirect or iframe), OnCharge sends a signed webhook, and the merchant reports tracking numbers so the order is paid out after delivery.

## Workflow

1. **Inspect the project.** Identify the framework, language, where orders and carts live, how the app is deployed, and whether a CSP is set. Check whether `@oncharge/node` or `@oncharge/js` are installed (they are not yet on npm; do not add them unless they resolve). The hosted script `https://app.oncharge.io/js/v1/oncharge.js` is available for embedding.
2. **Platform shortcuts.** WooCommerce: use the official plugin (1.4.0+), do not write a gateway. Magento 2 (`OnCharge_Payments`) and Wix (Velo payment provider): beta integrations exist; offer them first (see `references/platforms.md`).
3. **Ask only what you cannot find:** currency, whether products are shipped (physical) or not, whether the store sells research-use-only (RUO) products, the store's public domain(s), and redirect vs. embedded checkout (default: embedded on desktop sites, redirect when no page can host an iframe).
4. **Server-side client.** Implement request signing exactly as below (or use the SDK if installed). Read credentials from server env vars only. Add a unit test with the test vector.
5. **Create sessions on the server** from the merchant's own order/cart data, with `Idempotency-Key` = order or cart id and `shipping` for physical goods. Store `session_id` on the order.
6. **Checkout UI.** Redirect to `https://app.oncharge.io` + `iframe_url`, or embed it on a registered domain with `mountCheckout` (hosted script or `@oncharge/js`) or a plain iframe with `embed=1`, `parent_origin` and a fresh `embed_nonce`, accepting only messages from that iframe at the checkout origin that echo the nonce. Details: `references/checkout.md`.
7. **Webhook endpoint.** Verify `OnCharge-Signature` on the raw body, deduplicate on `OnCharge-Event-Id`, update orders, reply 2xx fast. Details and tested Node/PHP/Python code: `references/webhooks.md`.
8. **Shipments.** Report carrier + tracking number after capture (`POST /api/v1/shipments`). Details: `references/shipments.md`.
9. **Refunds/cancel** with idempotency keys if the app has an admin flow.
10. **RUO only:** serve the compliance feed (`references/compliance-feed.md`).
11. **Finish** with: env vars to set, `.env.example` entries (placeholders only), the webhook URL and domains to register in the OnCharge portal, and the go-live checklist from `references/testing.md`.

## Non-negotiable rules

- The API secret (`oc_sec_…`), webhook secret (`oc_cbsec_…`) and compliance connection code (`occs_…`) never reach the browser, client-exposed env vars (`NEXT_PUBLIC_`, `VITE_`, `REACT_APP_`, `PUBLIC_`), mobile apps, logs or git. Never call the OnCharge API from client code. Never ask the user to paste secrets into the chat.
- Amounts come from server-side data, in integer minor units. Never trust a browser-supplied amount.
- An order is paid only after a verified `payment.captured` webhook or a server-side `GET /api/v1/checkout/sessions/{id}` showing `captured`. The `return_url` visit and the iframe `complete` message are not proof.
- Do not invent endpoints, fields, events or SDK methods. Everything that exists is in `references/api.md`. If something is missing, say so.

## Signing (every API request)

```text
X-Oncharge-Key:       <API key>
X-Oncharge-Timestamp: <unix seconds>
X-Oncharge-Signature: hex(hmac_sha256(key = hex(sha256(api_secret)), msg = "{ts}.{METHOD}.{path}.{hex(sha256(raw_body))}"))
```

- `key` is the 64-char lower-case hex digest of the secret used as an ASCII string (not decoded).
- `path` excludes the query string (`/api/v1/shipments`, not `/api/v1/shipments?session_id=…`).
- Empty/GET bodies hash the empty string. Hash exactly the bytes sent.
- Tolerance 300 s. Base URL `https://app.oncharge.io`.
- Test vector: secret `oc_sec_example_secret_do_not_use`, ts `1767225600`, `POST /api/v1/checkout/sessions`, body `{"amount":4900,"currency":"USD","reference":"order-1001","return_url":"https://shop.example.com/thanks"}` → `7b0e1f9370475ae2a926804c0c9927c5f391c8a3c2e09a8071d98891aa01fdc7`.

Tested implementations in curl, Node, PHP and Python: `references/authentication.md`.

## Endpoints at a glance

| Call | Purpose |
|---|---|
| `POST /api/v1/checkout/sessions` | Create a session: `amount`, `currency`, `reference`, `return_url` (registered domain), optional `order_id`, `customer_email`, `customer_phone`, `shipping`, `line_items`, `metadata`. Returns `session_id`, `iframe_url` (path), `pay_token`, `expires_at` (30 min). |
| `GET /api/v1/checkout/sessions/{id}` | Status: `created`, `pending`, `authorized`, `captured`, `refused`, `cancelled`, `error`, `expired`. |
| `POST /api/v1/checkout/sessions/{id}/order` | Attach `order_id` and `shipping` once (cart-first checkouts). |
| `POST /api/v1/checkout/sessions/{id}/cancel` | Cancel an authorized, uncaptured payment. |
| `POST /api/v1/shipments` | Report `[{carrier?, tracking_number}]` by `session_id` or `order_id` (USPS, UPS, FedEx, DHL Express). |
| `GET /api/v1/shipments?session_id=` | Delivery state and per-package results. |
| `POST /api/v1/refunds` | Refund a captured payment (`Idempotency-Key`). |

Webhook types: `payment.pending`, `payment.authorized`, `payment.captured`, `payment.capture_failed`, `payment.refused`, `payment.cancelled`, `payment.expired`, `refund.succeeded`, `refund.failed`, `chargeback.opened`, `chargeback.won`, `chargeback.lost`, `delivery.confirmed`. Fulfil only on `payment.captured`; acknowledge types you do not handle.

Payouts: 1 day after carrier-confirmed delivery to the checkout address; 7 days after payment for orders with nothing to ship.

## Reference files

Load only what the task needs:

- `references/authentication.md`: signing, tested examples, idempotency, domains, errors.
- `references/checkout.md`: session fields, redirect, iframe + postMessage protocol, SDK components, CSP.
- `references/webhooks.md`: payload, events, verification code (Node, PHP, Python), retries.
- `references/shipments.md`: shipping address, tracking reporting, delivery states, payouts.
- `references/compliance-feed.md`: RUO compliance feed protocol, reference implementation, test vectors.
- `references/platforms.md`: WooCommerce, Magento, Wix, Next.js, React, HTML and headless guides.
- `references/sdks.md`: `@oncharge/node`, `@oncharge/node/next`, `@oncharge/js` interfaces (npm packages not yet published).
- `references/api.md`: every endpoint, field and error code.
- `references/testing.md`: test vectors, webhook fixtures, go-live checklist.

Online: https://oncharge.io/llms-full.txt, https://oncharge.io/openapi.json.
