> Short examples for checkouts and deadline timers: send buyers on after paying, prefill fields, track sales, and build your own tiers and redirects.

Source: https://plandalf.com/docs/developers/recipes

Developers

# SDK recipes

Each recipe below is a few lines you can copy. They run once the SDK has loaded, so put them in a click handler or inside `plandalf('ready', async () => { … })`. See [Call the SDK before it loads](https://plandalf.com/docs/developers/sdk#the-command-queue).

## Checkouts

### Send buyers to your thank-you page

The handle from `present()` resolves when the checkout closes, whether the buyer paid or not.

```js
const result = await plandalf.present('YOUR-CHECKOUT');
if (result.status === 'complete' && !result.test) location.href = '/thanks';
```

### Prefill the buyer you already know

`properties` fills checkout fields by their field ID. `metadata` goes to the Stripe customer, and the buyer never sees it.

```js
plandalf.present('YOUR-CHECKOUT', {
  properties: { email: currentUser.email, first_name: currentUser.firstName },
  metadata: { crm_id: currentUser.crmId },
});
```

### Count each sale in your analytics

A `purchase` event arrives for every payment, upsells included. `invoice_total` is in cents.

```js
const flow = plandalf.present('YOUR-CHECKOUT');
for await (const ev of flow.events) {
  if (ev.type === 'purchase' && !ev.test) {
    gtag('event', 'purchase', { value: ev.invoice_total / 100, currency: ev.currency });
  }
}
```

### Ask before they close it

`confirmClose` runs when the buyer tries to close the checkout. Return `false` to keep it open.

```js
plandalf.present('YOUR-CHECKOUT', {
  confirmClose: async () => {
    const stay = await myModal.ask('Leave now and the 20% discount goes too. Stay?');
    return !stay;
  },
});
```

## Deadline timers

### Close the checkout when the deadline passes

Open the deadline timer's handle beside the checkout and close the checkout when it sends `expired`.

```js
const promo = plandalf('promo', 'YOUR-PROMO', { redirect: false });
const flow = plandalf.present('YOUR-CHECKOUT', { promo: 'YOUR-PROMO' });

for await (const ev of promo.events) {
  if (ev.type === 'expired') {
    flow.close();
    showSoldOutMessage();
  }
}
```

You don't need code for a tier change while the checkout is open: with `promo` set, the SDK switches the checkout to the new tier's price.

### Send each tier to your own page

`redirect: false` stops the SDK from moving the visitor to the tier's Destination URL, so your code decides. `from` and `to` are the tier labels.

```js
const promo = plandalf('promo', 'YOUR-PROMO', { redirect: false });

for await (const ev of promo.events) {
  if (ev.type === 'tier-change' && ev.to === 'Last chance') location.href = '/offer/last-chance';
  if (ev.type === 'expired') location.href = '/offer/closed';
}
```

### Show the current price on your button

```js
const promo = plandalf('promo', 'YOUR-PROMO', { redirect: false });
const state = await promo;
buyButton.textContent = 'Buy for ' + (state?.activeTier?.price ?? '');

for await (const ev of promo.events) {
  if (ev.type === 'tier-change') buyButton.textContent = 'Buy for ' + (ev.tier?.price ?? '');
}
```

## Free countdown timers

### Your own tiers on a free timer

A free timer has one deadline. To run tiers yourself, move it on with `update()` when it runs out, and open a different checkout for each tier.

```js
const tiers = [
  { label: 'Early bird ends in', until: '2026-12-10T00:00:00Z', checkout: 'course-early-bird' },
  { label: 'Launch price ends in', until: '2026-12-17T00:00:00Z', checkout: 'course-launch' },
];
let tier = 0;
const timer = plandalf.countdown('#timer', { until: tiers[0].until, style: { label: tiers[0].label } });
buyButton.onclick = () => plandalf.present(tiers[tier].checkout);

for await (const ev of timer.events) {
  if (ev.type === 'expired' && tiers[tier + 1]) {
    tier += 1;
    timer.update({ until: tiers[tier].until, style: { label: tiers[tier].label } });
  }
}
```

### Take the deadline from your backend

Pass just the element to get the handle of a timer the HTML snippet made, then change it.

```js
const timer = plandalf.countdown('#timer');
const { endsAt } = await fetch('/api/offer').then((r) => r.json());
timer.update({ until: endsAt });
```

### Turn off the buy button at zero

Both kinds of timer send a bubbling `plandalf:expired` event on their element, so this works with the HTML snippet alone.

```js
document.querySelector('#timer').addEventListener('plandalf:expired', () => {
  buyButton.disabled = true;
});
```

More detail: [Open a checkout](https://plandalf.com/docs/developers/open-a-checkout), [Events](https://plandalf.com/docs/developers/events), [Deadline timers from code](https://plandalf.com/docs/developers/promos-from-code) and the [free countdown timer](https://plandalf.com/docs/promos/free-countdown-sdk).

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
