PlandalfDocsSign inStart free trial

Developers

Gates

A gate puts a feature in your app behind a checkout. When someone uses the feature, your access check runs. If it passes, the feature runs. If not, Plandalf opens the checkout, and after a purchase your check runs again with fresh data.

Plandalf doesn't know what your users have paid for: your app does. Gates run in the browser and call your own check and sync functions.

Register a gate

plandalf.addGate(name, checkout, check) names the gate, the checkout slug to open, and a function that returns true when the user already has access. The check can be async.

plandalf.addGate('export', 'upgrade-to-pro', () => user.plan !== 'free');
plandalf.addGate('seats', 'add-seats', () => team.seats < team.limit);
plandalf.addGate('course', 'buy-course'); // no check: always opens the checkout

Access means the check returned exactly true. Any other value, including a truthy one like 1 or a string, counts as no access.

Use it

plandalf.gate(name) runs the gate and resolves with the outcome.

document.querySelector('#export').addEventListener('click', async () => {
  const { access } = await plandalf.gate('export');
  if (access) exportReport();
});

Refresh access after a purchase

After a purchase, a gate with a check runs that check again. Unless you refresh your data, the check still sees the old plan and access stays false. Pass a sync function as the second argument: it gets the purchase and returns fresh data, which the SDK hands to your check.

plandalf.addGate('seats', 'add-seats', (fresh = team) => fresh.seats < fresh.limit);

const result = await plandalf.gate('seats', async (transaction) => {
  return await fetch('/api/team').then((r) => r.json()); // your server's current view
});

if (result.access) addMember();
else if (result.status === 'purchased') showPending(); // paid, but your server hasn't caught up yet

Whatever the sync function returns is also in result.entitlements. A gate with no check grants access as soon as the purchase completes.

The third argument takes the same options as present(), such as { frame: 'slideout' }.

Gate results

statusaccessWhat happened
existingtrueThe check passed; no checkout opened.
purchasedThe check's new answerThe buyer completed the checkout. Without a check, access is true.
dismissedfalseThe buyer closed the checkout.
errorfalseThe gate isn't registered, or the checkout, check or sync function failed. result.error has the cause.

After a purchase, result.transaction holds what the checkout returned, including the checkout slug and the customer.

Check again on your server

A gate decides what your page shows. It isn't proof of payment. Whatever the feature does on your server, check the user's access there too, from your own records or Plandalf's webhooks.

In React

<Gate> shows its children when the user has access and a fallback when they don't. Inside the fallback, useGateTrigger() opens the checkout.

import { Gate, useGateTrigger } from '@plandalf/react';

function UpgradePrompt() {
  const { trigger, checking } = useGateTrigger();
  return <button disabled={checking} onClick={() => trigger()}>Upgrade to export</button>;
}

export function ExportPanel({ user }) {
  return (
    <Gate name="export" flow="upgrade-to-pro" check={() => user.plan !== 'free'} fallback={<UpgradePrompt />}>
      <ExportButton />
    </Gate>
  );
}

useGate(name, checkout, check) gives you the same pieces as values: access (null while the check runs), checking, result and trigger(sync, options). See React.