Loading…
A small app that takes payments with sub3
Use it as a customer would, and see the code behind each step.
Everything at once.
Sign in with a wallet
Your user sees
Signed in as
Your code does
The user signs a message with their wallet. sub3 returns a JWT that your server verifies: no passwords.
Opens
GET /login?merchant=…
Show the code
public/app.js
async function signIn() {
const handle = config.product.split('/')[0];
await receive(await openSub3(`/login?merchant=${handle}`, options()));
}
sub3.mjs: the token's subject is the user's address
const { payload } = await jwtVerify(token, keys.get(config.api), {
algorithms: ['EdDSA'],
issuer: config.site,
audience: config.origin
});
if (!/^0x[0-9a-fA-F]{40}$/.test(payload.sub ?? '')) throw problem(401, 'Invalid session');
return payload.sub.toLowerCase();
See what the app knows about you
Your user sees
Your account
Your code does
One question per user. Allowance and credits are this app's own numbers, not sub3's.
Your server asks
GET /v1/access?product=…&user=…
sub3 answers
Show the code
sub3.mjs: the one read, cached for 30 seconds
const access = await sub3(config, '/v1/access', { product: config.product, user });
config.mjs: this app's rules turn the answer into what the user may
do
return {
paid: Boolean(member) || ownsLifetime, // the whole-site paywall: any active tier or any lifetime item
tier: member?.tier ?? null,
level: member?.level ?? 0,
end: member?.end ?? null,
cycle: cycle?.id ?? null, // allowance is granted once per cycle id; an upgrade starts a new one
allowance: cycle ? allowanceFor(member.level) : 0n,
premium: (member?.level ?? 0) >= premiumLevel || ownsLifetime,
lifetime: access.lifetime
};
Buy something on sub3's checkout
Your user sees
What you can buy
An upgrade starts now at full price; the rest of the old plan is lost.
Your code does
Items and prices come from sub3. Each button is a link to its checkout, and the money goes straight to the merchant.
Your server asks
GET /v1/products/…
The first button opens
Show the code
public/app.js
// Buy from a click. The checkout page chooses purchase, renewal or upgrade from the chain.
async function buy(item, units) {
const path = checkoutPath(config.product, item.key, { units, user: account.user });
await receive(await openSub3(path, options()));
}
public/sub3.js, the helper to copy into your frontend
export function checkoutPath(product, item, { units, user } = {}) {
const params = new URLSearchParams({ product, item });
if (units) params.set('units', String(units));
if (user) params.set('user', user); // who receives it; default is the paying wallet
return `/checkout?${params}`;
}
Come back and see what changed
Your user sees
Your code does
The result in the URL is only a hint. Your server looks the order up and reads access again: no webhooks.
Your server asks
GET /v1/orders/{payer}/{orderId}
GET /v1/access?product=…&user=…
Show the code
sub3.mjs: pending until the payment is confirmed
const order = await sub3(config, `/v1/orders/${payer}/${orderId}`).catch((error) => {
if (error.status === 404) return null; // not visible yet: retry, never ask the user to pay again
throw error;
});
if (order && (order.user.toLowerCase() !== user || order.product !== config.product))
throw problem(403, 'This order is not for this product and account');
if (order?.confirmed) await creditPack(config, db, user, order);
await getAccess(config, user, { fresh: true });
return { pending: !order?.confirmed };
A result that never arrives is found later: syncPacks lists the user's
orders (GET /v1/orders) and credits each receipt once.
Use what you bought
Your user sees
Uppercase a text
Your code does
Charged in this app's own ledger, in one transaction: allowance first, then credits. sub3 is not called.
Your page sends
POST /api/run
Show the code
app.mjs
const op = await charge(config, db, user, activeItems, () =>
reserve(db, user, id, input, actionCost, can.cycle, can.allowance)
);
ledger.mjs, inside reserve: one database transaction
const have = balances(db, user, cycle, allowance);
const fromAllowance = cost < BigInt(have.allowance) ? cost : BigInt(have.allowance);
const fromCredits = cost - fromAllowance;
if (fromCredits > BigInt(have.credits))
throw problem(402, 'Not enough credits: buy a pack to continue');
A retry with the same Idempotency-Key is not charged again, and a
failed run gives back what it took.
What just happened
Add sub3 to your own app
Two hosted pages and one API question. No SDK, no API key.
-
Create your product
-
Give your coding agent one sentence
Add sub3 to this app for my product <handle/key>, following https://sub3.dev/llms.txt. -
Or write it yourself