# 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](https://app.oncharge.io). A newly approved store is `pending` until 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:

```bash title=".env"
ONCHARGE_API_KEY=oc_key_...
ONCHARGE_API_SECRET=oc_sec_...
ONCHARGE_WEBHOOK_SECRET=oc_cbsec_...
```

## 1. Install the SDK

```bash
npm install @oncharge/node
```

> **Coming soon.** `@oncharge/node` is not yet published to npm. Until it is, copy the `onchargeRequest()` helper from [Authentication & signing](https://oncharge.io/docs/authentication#nodejs) (about 30 lines, no dependencies); the request bodies below are the same.

```ts title="lib/oncharge.ts"
import { 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.

```ts title="server: create the 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:

```ts title="server: redirect"
return Response.redirect(session.checkout_url, 303);
```

or embed it on your checkout page with the script tag:

```html title="checkout page"
<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](https://oncharge.io/docs/checkout) for React, Next.js and plain-iframe options.

## 4. Confirm the payment with a webhook

```ts title="app/api/webhooks/oncharge/route.ts (Next.js)"
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](https://oncharge.io/docs/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}`](https://oncharge.io/docs/api#getcheckoutsession)):

```ts
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:

```ts
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](https://oncharge.io/docs/shipments).

## 6. Refund (when needed)

```ts
await oncharge.refunds.create({ session_id: session.session_id, amount: 1500, currency: 'USD', reference: 'refund-1001-1' });
```

## Next steps

- [Testing & going live](https://oncharge.io/docs/testing): the launch checklist.
- [API reference](https://oncharge.io/docs/api): every endpoint and error code.
- [Build with AI](https://oncharge.io/docs/ai): have Claude, ChatGPT or Cursor write the integration for your stack.
