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

# Embed the Checkout on Your Site

> Show a checkout link inside your own page, inline or in a modal, with one script tag.

A checkout link can open inside your own page instead of sending the buyer away. Add one script
and point it at the link's URL: the checkout loads in a frame, the buyer pays without leaving your
site, and your page is told when the payment finishes.

## Happy path

1. Your server creates the link with `POST /v2/checkout-links` (see [Create Checkout Links](https://developer.pagou.ai/payments/checkout-links/create.md)),
   with the cart total and your order id in `reference`.
2. Your page receives the returned `data.url` and renders one of the two snippets below.
3. The buyer pays inside your page.
4. Your server confirms the order on the `transaction.paid` webhook, where `data.checkout_link.reference`
   is your order id.

> [!WARNING]
>
> Treat the browser events as a hint for the interface. The order is paid when your server receives
> `transaction.paid`.

## Inline

The checkout takes the place of a `div` in your page and grows with its content.

```html theme={null}
<div id="pagou-checkout" data-checkout="https://checkout.pagou.ai/checkout/chk_..."></div>
<script src="https://checkout.pagou.ai/embed.js" async></script>
```

* `data-checkout` takes the link's `url` (or just its slug).
* The script follows the page: a `div` rendered later, as in React or Next, mounts when it appears,
  and changing its `data-checkout` loads the new link.
* Optional: `data-locale="en"` sets the checkout language.

To mount into an element of your own instead of `#pagou-checkout`, give it the same attributes and
call `PagouCheckout.mount(element)`:

```js theme={null}
PagouCheckout.mount(document.querySelector("#my-checkout"));
```

## Modal

The checkout opens over your page when the buyer clicks a button.

```html theme={null}
<button type="button" id="buy">Buy</button>
<script src="https://checkout.pagou.ai/embed.js" async></script>
<script>
  document.getElementById("buy").addEventListener("click", () => {
    PagouCheckout.open({
      checkout: "https://checkout.pagou.ai/checkout/chk_...",
      onComplete: ({ transaction_id, status }) => {
        // show a "thank you" state; confirm the order on your server
      },
      onClose: () => {
        // the buyer closed the modal; the checkout session stays open
      },
    });
  });
</script>
```

`PagouCheckout.close()` closes it from your code. Opening a second modal closes the first one.

## Knowing the payment finished

| Mode | How your page is told |
| - | - |
| Modal | `onComplete({ transaction_id, status })` |
| Inline | the `pagou:checkout:completed` DOM event on the `div` (it bubbles), with `detail = { transaction_id, status }` |

```js theme={null}
document
  .getElementById("pagou-checkout")
  .addEventListener("pagou:checkout:completed", (event) => {
    const { transaction_id, status } = event.detail;
  });
```

It fires once per transaction, when the transaction reaches a final status (paid or refused), and
`status` is the transaction status in lowercase. On a subscription sale it fires when the
subscription starts (`active`, `trialing` or `cancel_scheduled`), and `transaction_id` is the
subscription id. No buyer data comes with it.

## After the payment

When the link has a `success_url`:

* **Same domain as your page:** your page navigates to it.
* **Another domain:** the checkout shows a **Continue** button, and the buyer clicks it to go on.

## Requirements and limits

* **Your page must be served over HTTPS.** The checkout refuses to load inside an `http://` page.
* **Payment methods:** Pix and cards (with 3-D Secure) work. Apple Pay is not offered inside an
  embedded checkout.
* **Custom domain:** when the link URL carries your custom checkout domain, `data-checkout` and
  `checkout` accept it as is.
* **Custom CSS:** the checkout's [custom CSS](https://developer.pagou.ai/frontend/custom-css.md) applies here too. Inline, avoid
  viewport heights such as `100vh`, because the frame grows with its content.

## Where the checkout was embedded

Every checkout session records the origin of the page that embedded it (for example,
`https://shop.com`), and the sale carries it in `attribution.embed_origin` on the transaction and
its webhooks. It is `unknown` when the browser hides the embedding page, and `null` when the
checkout was opened on its own page. It is a record, not an access control: any HTTPS site can embed
a checkout link.

## Sandbox

Use `https://checkout.sandbox.pagou.ai/embed.js` with links created on the sandbox API.

## Next steps

* [Create Checkout Links](https://developer.pagou.ai/payments/checkout-links/create.md)
* [Payment events](https://developer.pagou.ai/webhooks/payment-events.md)
* [Checkout Links reference](https://developer.pagou.ai/api-reference/checkout-links/create.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.