> ## 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.

# E-commerce

> Add a Pay-with-Pesarc button to your store and settle orders in stablecoins.

Add Pesarc as a checkout option in any store. The customer pays in their own
currency; you're settled in stablecoins, with an optional direct payout address.

## The flow

<Steps>
  <Step title="Create the payment">
    When the customer clicks **Pay with Pesarc**, create a payment for the cart
    total on your server and get back a `checkout_url`.
  </Step>

  <Step title="Redirect to checkout">
    Send the customer to `checkout_url`. Pesarc hosts the payment page.
  </Step>

  <Step title="Return & confirm">
    Pesarc returns the customer to your `redirect_url`. Mark the order paid only
    after the webhook (or a status poll) confirms it settled.
  </Step>
</Steps>

## Server: create the order payment

```ts theme={null}
// POST /api/checkout  — called when the customer chooses Pesarc
export async function POST(req: Request) {
  const { cart } = await req.json();
  const total = cart.items.reduce((s, i) => s + i.priceNgn * i.qty, 0);

  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: total,
      currency: "NGN",
      reference: cart.orderId,
      merchant_name: "Ada Fabrics",
      description: `${cart.items.length} item(s)`,
      redirect_url: `https://ada-fabrics.com/order/${cart.orderId}`,
      webhook_url: "https://ada-fabrics.com/api/pesarc/webhook",
      payout_address: process.env.STORE_PAYOUT_ADDRESS, // optional: settle here
      metadata: { orderId: cart.orderId },
    }),
  });

  const { checkout_url } = await res.json();
  return Response.json({ checkout_url });
}
```

## Client: the button

```tsx theme={null}
async function payWithPesarc(cart) {
  const res = await fetch("/api/checkout", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ cart }),
  });
  const { checkout_url } = await res.json();
  window.location.href = checkout_url;
}
```

## Fulfil on settlement

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

<Tip>
  Set an appropriate `ttl_minutes` on the payment (1–1440) so abandoned carts
  expire instead of lingering as open sessions.
</Tip>

<Note>
  Building on a platform like Shopify, WooCommerce or Medusa? The same two calls
  (create payment, handle webhook) map onto that platform's custom-payment or
  app-extension APIs. Point the webhook at your app's endpoint and mark the order
  paid on `payment.settled`.
</Note>
