Quickstart
This walks through one complete payment: create a checkout session on your server, show the checkout, confirm the payment with a webhook, and report the shipment so the order is paid out. It uses Node.js; every step links to the equivalent in other languages.
Before you start#
You need:
- An approved OnCharge merchant account and an active store in the merchant portal. A newly approved store is
pendinguntil OnCharge has opened its processing account; its API keys are refused (403 store_inactive) until then. - The store's API key and API secret, generated on the store in the portal's Stores page (the secret is shown once).
- Your store's domain registered under Security → Domains (for example
shop.example.com). - A webhook endpoint URL registered for the store, and its webhook signing secret.
Put the secrets in your server environment, never in browser code:
ONCHARGE_API_KEY=oc_key_...
ONCHARGE_API_SECRET=oc_sec_...
ONCHARGE_WEBHOOK_SECRET=oc_cbsec_...1. Install the SDK#
npm install @oncharge/nodeimport { OnCharge } from '@oncharge/node';
export const oncharge = new OnCharge({
apiKey: process.env.ONCHARGE_API_KEY!,
apiSecret: process.env.ONCHARGE_API_SECRET!,
});2. Create a checkout session#
Create the session on your server when the shopper clicks Pay. Use your order ID as the idempotency key so a retry never creates a second session.
const session = await oncharge.checkout.sessions.create(
{
amount: 4900, // $49.00, in cents
currency: 'USD',
reference: 'order-1001',
order_id: '1001',
return_url: 'https://shop.example.com/checkout/complete?order=1001',
customer_email: 'shopper@example.com',
shipping: {
name: 'Alex Rivera',
line1: '500 Market St',
city: 'San Francisco',
state: 'CA',
postal_code: '94105',
country: 'US',
},
},
{ idempotencyKey: 'order-1001' },
);
// session.checkout_url is the full URL of the hosted checkout page.Without the SDK: onchargeRequest('POST', '/api/v1/checkout/sessions', body, { idempotencyKey }) returns iframe_url, a path; the checkout URL is https://app.oncharge.io + iframe_url.
Send shipping for anything you ship: OnCharge releases the payout once the carrier confirms delivery to this address, and it cannot be changed later.
3. Show the checkout#
Either redirect the shopper:
return Response.redirect(session.checkout_url, 303);or embed it on your checkout page with the script tag:
<div id="oncharge-checkout"></div>
<script src="https://app.oncharge.io/js/v1/oncharge.js"></script>
<script>
OnCharge.mountCheckout('#oncharge-checkout', {
url: CHECKOUT_URL, // session.checkout_url, passed from your server
onComplete: ({ sessionId, status }) => {
window.location.href = '/checkout/complete?order=1001';
},
onError: ({ message }) => console.warn(message),
});
</script>See Checkout for React, Next.js and plain-iframe options.
4. Confirm the payment with a webhook#
import { createWebhookHandler } from '@oncharge/node/next';
export const { POST } = createWebhookHandler({
secret: process.env.ONCHARGE_WEBHOOK_SECRET!,
async onEvent(event) {
switch (event.type) {
case 'payment.captured':
await markOrderPaid(event.data.order_id, event.data.session_id);
break;
case 'payment.refused':
case 'payment.cancelled':
await markOrderFailed(event.data.order_id);
break;
}
},
});Other frameworks and languages: Webhooks.
The handler verifies OnCharge-Signature, answers 400 to bad signatures, skips repeated event IDs, and answers 500 if onEvent throws so OnCharge retries later.
On your return_url page, show "thank you" but treat the order as paid only after the webhook arrives, or check the session yourself (GET /api/v1/checkout/sessions/{id}):
const current = await oncharge.checkout.sessions.retrieve(sessionId);
if (current.status === 'captured') {
// paid
}5. Report the shipment#
When the order ships, send the carrier and tracking number:
await oncharge.shipments.report({
session_id: session.session_id,
shipments: [{ carrier: 'UPS', tracking_number: '1Z999AA10123456784' }],
});OnCharge follows the package with the carrier. One day after it is confirmed delivered to the order's shipping address, the payout for the order is released. Check progress any time with oncharge.shipments.get(sessionId). See Shipments & payouts.
6. Refund (when needed)#
await oncharge.refunds.create({ session_id: session.session_id, amount: 1500, currency: 'USD', reference: 'refund-1001-1' });Next steps#
- Testing & going live: the launch checklist.
- API reference: every endpoint and error code.
- Build with AI: have Claude, ChatGPT or Cursor write the integration for your stack.