# ctx.purchases

Sell things inside your app for Fave1 credits — coins, yarn, a theme, a power-up. You name the item; Fave1 holds the price, shows the confirm, takes the credits, and keeps a receipt you deliver against.

storage

## Not open to your app yet

> **Watch out:** **Fave1’s own apps sell with it today — Buddy’s yarn is the first.** For your app, every call — `items()`, `buy()`, `pending()`, `finish()` — rejects with `not-yet` (in a DEV build `items()` can instead come back empty, `pending()` answers `[]` — nothing was bought — and `finish()` rejects with `not-found`), until we open in-app purchases to developers with an item table in the console and your share of the sale. This page is the contract that day keeps, so you can plan your app around it now.

Until then, check for it and fold the shop: `items()` rejecting with `not-yet` means there’s nothing to sell yet.

## Calls

`ctx.purchases`

```ts
await ctx.purchases.items();      // → { items: PurchaseItem[], balance: number | null }
await ctx.purchases.buy(itemId);   // → PurchaseReceipt — after they press Fave1's Buy
await ctx.purchases.finish(id);    // → 'done' | 'returned' — after you delivered
await ctx.purchases.pending();     // → PurchaseReceipt[] paid, not finished
```

`PurchaseReceipt`

```ts
{
  id: '7b0e2c4a-…',   // the receipt — store it next to what you deliver
  item: 'coins-500',  // your item id
  name: '500 coins',  // Fave1's name for it
  qty: 500,           // how many to put in
  credits: 299,       // what they paid (1 credit = $0.01)
}
```

## The price is ours, not your code’s

Your code only ever says an item id. The item’s name, amount and price live on Fave1’s side, so nothing on the page — your bundle, the site, an extension — can change what someone pays. `buy()` shows a confirm line in your panel; the **Buy** button in it is Fave1’s own, in a frame your code can’t reach, with Fave1’s name and price on it. Nobody can press it for them, and it only counts a real tap that was on screen.

Every buy asks — there’s no “remember this”. The line says what it costs, how many credits they have (in app mode, or on a site your app is allowed on), and that it can’t be turned back into credits once it’s in.

> **Out of credits?** `buy()` rejects with `credits`, with `err.balance` and `err.needed`. The first time in a session we also show Get credits ourselves — after that it’s up to you to say it.

## Deliver, then finish

Credits leave the moment they press Buy. The thing they bought arrives when *you* put it in — so the receipt goes into the **same write** as the thing. That one write is what makes it safe to try again: if the receipt id is already there, it was delivered.

`src/index.ts`

```ts
type Wallet = { coins: number; receipts: string[] };

async function deliver(ctx: Ctx, r: PurchaseReceipt): Promise<boolean> {
  const w = ((await ctx.storage.get('wallet')) as Wallet | undefined) ?? { coins: 0, receipts: [] };
  if (!w.receipts.includes(r.id)) {
    w.coins += r.qty;
    w.receipts = [...w.receipts, r.id].slice(-20);
    await ctx.storage.set('wallet', w);         // coins and receipt, one write
  }
  return (await ctx.purchases.finish(r.id)) === 'done';
}

async function buyCoins(ctx: Ctx) {
  try {
    const r = await ctx.purchases.buy('coins-500');
    await deliver(ctx, r);
    ctx.ui.render([ctx.ui.text(`+${r.qty} coins`)]);
  } catch (err) {
    const e = err as Fave1Error;
    if (e.code === 'cancelled') return;                           // they said Not now
    if (e.code === 'credits') return showShort(e.needed, e.balance); // Fave1 showed Get credits the first time only
    // The answer may have been lost on the way back — the credits could be gone. Settle what's paid:
    for (const r of await ctx.purchases.pending().catch(() => [])) await deliver(ctx, r);
  }
}
```

## When something stops halfway

A tab closes between Buy and your write, the network drops the answer, the storage write fails — the receipt stays **pending** and the credits stay taken. Call `pending()` when your app opens (app mode is a good place) and run each one through the same `deliver()`: the receipt id in your storage tells you whether to add the thing or just finish.

`src/index.ts`

```ts
async function settle(ctx: Ctx) {
  if (!ctx.can('storage')) return;
  const due = await ctx.purchases.pending().catch(() => []);
  for (const r of due) await deliver(ctx, r);
}
```

Unfinished receipts don’t wait forever. After **7 days** without `finish()`, Fave1 gives the credits back and closes the receipt — `finish()` then answers `'returned'`. If you had delivered after all, take the thing back out; they have their credits.

## What it guarantees — and what it doesn’t

Fave1 guarantees the money side: the price you set is the price paid, a buy happens only on their tap, a receipt is paid exactly once, and a receipt you never finish is refunded. What you deliver lives in *their* storage, and their storage is theirs — someone determined can change their own coin count with the browser’s developer tools, just like any app that runs in a browser.

> **Sell things that are theirs to enjoy, not proof against other people.** More coins, a cosmetic, an unlock in their own copy — perfect. A rank on a public board, a prize, anything others rely on — don’t make it purchasable, because nothing on their side can be trusted for that.

## Rules for selling

- **Say what it is and what it does before they tap — then deliver exactly that.** The item’s name is Fave1’s; the reason to want it is yours, next to the button.
- **Deliver everything they paid for.** Interrupted buys too — that’s what `pending()` is for. Don’t take a delivered item back unless `finish()` said `'returned'`.
- **No chance.** No loot boxes, no random draws — what they buy is decided before they tap.
- **No pressure.** No countdowns or scarcity that isn’t real, and nothing that stops working unless they keep paying.
- **Never aim a sale at a child.**
- **Only through ctx.purchases.** No card details, payments or Fave1 credits asked for any other way, and no offer to turn items back into money or credits.

These are in the [Developer Agreement § 8](https://www.fave1.com/developer-agreement#8-fees-and-payments). Breaking them can get credits returned to people, the item taken off sale, or the app hidden.

## Limits

| What | Limit | Past it |
| --- | --- | --- |
| `pending` | 5 unfinished receipts per person in your app | rate — finish first |
| `buys` | 10 a minute per person, one at a time | rate |
| `price` | 1 to 10,000 credits an item | — |
| `item id` | Up to 40 characters — lowercase letters and digits, words joined by single dashes | invalid |
| `refund` | Unfinished receipts after 7 days | finish() → returned |
| `pulled` | Everything bought in your app while a release that got [pulled in Live QA](https://developers.fave1.com/docs/publishing.md#live-qa) was live — delivered or not | credits back · finish() → returned |

## Errors

| Code | What happened | Try |
| --- | --- | --- |
| `not-yet` | In-app purchases aren’t open to your app yet | fold the shop |
| `cancelled` | They chose Not now (`err.declined`), or the panel closed | nothing |
| `credits` | Not enough Fave1 credits — the first time while your app is open we show Get credits; after that it’s yours to say (`err.balance` · `err.needed`) | nothing · then a line |
| `signed-out` | Nobody’s signed in to Fave1 here | fold the shop |
| `not-installed` | They haven’t added your app — a Try it or a site’s button | offer Add |
| `denied` | `storage` isn’t in your manifest, or they switched it off | fold the shop |
| `not-found` | No such item, or it’s off sale — or a receipt that isn’t this person’s | fix the id |
| `invalid` | An item id that isn’t up to 40 lowercase letters and digits joined by single dashes, or a `finish()` argument that isn’t a receipt id | fix it |
| `rate` | A buy is already open, 5 receipts are unfinished, or 10 buys this minute | finish · wait |
| `unreachable · timeout · server` | The answer didn’t come back — the buy may have gone through | pending() |

## One permission: storage

Purchases come with `storage` — you deliver into [ctx.storage](https://developers.fave1.com/docs/ctx-storage.md), so you already declare it. No new sentence for people to read: the confirm line is where they say yes, every time. On a site that hasn’t allowed your app yet, `pending()` and `finish()` ask once, like `ctx.storage` — and can reject with `cancelled`.
