> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pesarc.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# SaaS billing

> Charge for plans and usage in local currency, settled in stablecoins, and pay out to your users.

Use Pesarc to bill your SaaS customers in their own currency and settle in
stablecoins — without holding funds yourself or building a payments stack.

## Charge for a plan

Create a payment when a customer subscribes or renews, and send them to the
hosted checkout.

```ts theme={null}
async function chargePlan(customer: { id: string; email: string }, planNgn: number) {
  const res = await fetch("https://pesarc.xyz/api/v1/payments", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.PESARC_SECRET_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      amount: planNgn,
      currency: "NGN",
      reference: `sub_${customer.id}`,
      merchant_name: "Your SaaS",
      description: "Pro plan — monthly",
      redirect_url: `https://app.your-saas.com/billing/return`,
      webhook_url: `https://app.your-saas.com/api/pesarc/webhook`,
      metadata: { customerId: customer.id, plan: "pro" },
    }),
  });
  const { checkout_url } = await res.json();
  return checkout_url; // redirect the customer here
}
```

Use `reference` for your own invoice/subscription id and `metadata` to carry any
context you'll want back on the webhook.

## Activate on settlement

Don't grant access on the browser redirect alone. Wait for the
[webhook](/integrations/webhooks) (or poll
[`GET /checkout/:id`](/api-reference/get-checkout)) and activate the subscription
when the payment is settled.

```ts theme={null}
// POST /api/pesarc/webhook
export async function POST(req: Request) {
  const event = await req.json();
  if (event.type === "payment.settled") {
    const { reference, metadata } = event.data;
    await activateSubscription(metadata.customerId, metadata.plan);
  }
  return new Response("ok");
}
```

<Warning>
  Always verify the webhook is really from Pesarc before acting on it — see
  [Webhooks](/integrations/webhooks) for signature verification.
</Warning>

## Usage-based billing

For metered billing, aggregate usage over the period and create one payment for
the total at the end of the cycle, passing the line-item summary in `metadata`.

## Paying your users out

To pay a user (payouts, refunds, revenue share), include a `payout_address` on
the payment, or move money with the agent — see
[Agents & AI](/integrations/agents).
