# Checkout blocks

Source: https://snizu.dev/docs/apps/checkout-blocks

## Declare it [#declare-it]

A block is a page of your app that merchants place in the checkout, from the places you allow. Declare it in your manifest:

```json title="snizu.app.json"
{
  "manifest": {
    "checkout_blocks": [
      {
        "handle": "gift-wrap-note",
        "name": "Gift wrap note",
        "url": "https://gift-wrap.example.com/checkout-block",
        "targets": ["checkout.delivery", "thank_you"],
        "height": 120
      }
    ]
  }
}
```

<Fields>
<Field name="handle" type="string" required>
Up to 40 lower-case letters, digits and dashes, unique in the version.
</Field>

<Field name="name" type="string" required>
What merchants see when placing it, 1 to 60 characters.
</Field>

<Field name="url" type="url" required>
The page the frame opens: HTTPS, or `http://localhost` while developing.
</Field>

<Field name="targets" type="string[]" required>
Where merchants may place it: `checkout.contact`, `checkout.delivery`, `checkout.payment`, `checkout.summary`, `thank_you` or `order_status`.
</Field>

<Field name="height" type="number">
The frame's height until the block resizes it, 40 to 600 pixels. 120 by default.
</Field>
</Fields>

An app version declares up to 10 blocks.

## Talk to the checkout [#talk-to-the-checkout]

```ts title="block.ts"
import { createCheckoutBlock } from "@snizu/app-bridge/checkout";

const block = createCheckoutBlock();
block.autoResize();

const { target, locale, direction, cart, order } = await block.init;
render(cart ?? order);
// Again whenever the cart changes.
block.onUpdate((next) => render(next.cart ?? next.order));

// Kept on the cart as app.<your app>.gift_wrap,
// and on the order like a checkout field.
await block.setAttribute("gift_wrap", "yes");

const token = await block.getSessionToken();
await fetch("/api/checkout-block", {
  headers: { authorization: `Bearer ${token}` },
});
```

<Fields>
<Field name="init" type="Promise">
The first message: `target`, `locale`, `direction`, `store` (its name), and `cart` while paying or `order` after.
</Field>

<Field name="onUpdate(listener)">
Called with every later `init`, when the cart changes. Returns a function that stops it.
</Field>

<Field name="resize(height?)">
Sets the frame's height, 0 to 1,200 pixels; the page's own height by default.
</Field>

<Field name="autoResize()">
Keeps the frame as tall as the page. Returns a function that stops it.
</Field>

<Field name="setAttribute(key, value)">
Keeps a value on the cart: up to 10 keys of 500 characters; `null` clears one. Only while paying.
</Field>

<Field name="getSessionToken()">
A token for your backend about this cart or order and place.
</Field>

<Field name="destroy()">
Stops listening to the checkout.
</Field>
</Fields>

The cart and order come without personal data: their lines, totals, currency and the country they go to, and an order's name. No name, email, phone or street.

A block that says nothing within ten seconds is put away, so a slow app never stops a shopper paying.

## Verify its token [#verify-its-token]

A shopper holds the block's token, so it's never signed with your client secret itself. Its key is made from it:

<Flow steps={[{"value":"client_secret","label":"your app's"},{"join":"→","value":"HMAC-SHA256","label":"of \"snizu checkout block token v1\"","mark":true},{"join":"→","value":"hex","label":"the HS256 key"}]} />

<Fields>
<Field name="typ" type="checkout_block">
A checkout block's token. An admin session token never passes as one, and the other way round.
</Field>

<Field name="dest" type="store_…">
The store.
</Field>

<Field name="sub" type="cart_… | ord_…">
The cart or order it was shown for.
</Field>

<Field name="target" type="string">
Where it was shown.
</Field>

<Field name="aud" type="string">
Your app's client ID.
</Field>

<Field name="nbf, exp, iat" type="seconds">
Valid for five minutes.
</Field>

<Field name="jti" type="string">
Unique to each token, so you can refuse a replay.
</Field>
</Fields>

```js title="lib/verify.js"
export function verifyCheckoutToken(token, secret, clientId, now = Date.now() / 1000) {
  const key = createHmac("sha256", secret)
    .update("snizu checkout block token v1")
    .digest("hex");
  return verifyToken(token, key, clientId, "checkout_block", now);
}
```

`verifyToken` is the same as for [embedded pages](/docs/apps/embedded-pages#verify-the-session-token). The app [`snizu app create`](/docs/cli/app/create) writes has both, with tests, and a `GET /api/checkout-block` route that uses this one.

<Callout tone="warning" title="Trust the token, not the page">
Anyone can open your block's URL with made-up messages. Act on what your backend verified: the store, the cart and the place in the token.
</Callout>