import { isNativeAndroid, isNativeIos } from './platform'; import { reportNativeFailure } from './reportFailure'; import { TrekShell, type StoreRestoreResult } from './trekShell'; /** * In-app purchase, from the app's side. A seam, like {@link TrekShell}: the * screens drive a plan to a receipt through this, and never touch StoreKit * directly. Two implementations stand behind it: a fake one for development and * preview, so the whole subscription flow can be walked without a store, and the * native one that runs StoreKit 2 on iOS (feature storeKit) and Google Play * Billing on Android (feature playBilling) through TrekShell. Where neither can * take a payment (a binary from before them, the web) the client reports itself * unavailable and the shell offers no purchase at all: the store rules leave no * room for sending buyers to the web. */ type IapPlatform = 'apple' | 'google'; /** The receipt a completed purchase yields, in the shape `/api/iap/redeem` takes. */ export interface Purchase { platform: IapPlatform; productId: string; /** Apple: the signed transaction (JWS); Google: the purchase token. The control plane verifies either with its store. */ receipt: string; /** The store's id for this transaction, handed back to {@link IapClient.finish}. */ transactionId: string; } /** * What a purchase attempt came to. `cancelled` is the customer backing out, not * an error; `pending` is a purchase waiting on someone else (Ask to Buy, a bank), * which a restore picks up once it goes through. */ type PurchaseOutcome = { kind: 'bought'; purchase: Purchase } | { kind: 'cancelled' } | { kind: 'pending' } | { kind: 'failed' }; export interface IapClient { /** Whether this build can take a payment. */ isAvailable(): Promise; /** * The store's own price strings by product id, in the buyer's currency. A * product missing from the map cannot be bought here. Empty for the fake, whose * prices come from the plan list. */ prices(productIds: string[]): Promise>; /** A test build (TestFlight, App Review), whose purchases cost nothing and may go ahead before the opening. */ isSandbox(): Promise; /** Runs the platform's purchase sheet for a product and resolves with what became of it. */ purchase(productId: string): Promise; /** Tells the store a purchase was delivered, once its instance exists. */ finish(purchase: Purchase): Promise; /** The subscription this store account already holds, for a reinstall or a new device. */ restore(): Promise; /** * A paid subscription that was never delivered: a payment that was pending and * cleared later (Google Play refunds it unless it is delivered within three * days). The shell redeems it at its start, the way a restore does. */ unfinished(): Promise; /** Calls `listener` when such a payment clears while the app runs. */ onTransaction(listener: () => void): void; } /** Every store product of the cloud starts with this. */ const PRODUCT_PREFIX = 'cloud.'; /** * The store product id for a plan and interval. Must match the identifiers created * in App Store Connect and the Play Console, and the control plane's catalog * (`cloud..`); kept here as the one place the app names them. */ export function productIdFor(planId: string, interval: 'month' | 'year'): string { return `${PRODUCT_PREFIX}${planId}.${interval === 'year' ? 'yearly' : 'monthly'}`; } /** * The fake used in development and preview: it waits a beat, as a real sheet * would, then returns a receipt the control plane's fake verifier accepts. Never * reached in a store build. */ export const fakeIapClient: IapClient = { async isAvailable() { return true; }, async prices() { return new Map(); }, async isSandbox() { return true; }, async purchase(productId) { await new Promise((resolve) => setTimeout(resolve, 900)); const nonce = String(Date.now()); return { kind: 'bought', purchase: { platform: 'apple', productId, receipt: `fake-receipt:${productId}:${nonce}`, transactionId: nonce }, }; }, async finish() {}, async restore() { return null; }, async unfinished() { return null; }, onTransaction() {}, }; /** The store this app buys through: the App Store on iOS, Google Play on Android. */ function nativeStore(): { platform: IapPlatform; feature: string } | null { if (isNativeIos()) return { platform: 'apple', feature: 'storeKit' }; if (isNativeAndroid()) return { platform: 'google', feature: 'playBilling' }; return null; } /** * StoreKit 2 (iOS) or Google Play Billing (Android) through TrekShell, the same * methods on both. Available when the binary has the platform's feature; * everywhere else (the web, an older binary) it reports itself unavailable and * every call comes to nothing. */ export const nativeIapClient: IapClient = { async isAvailable() { const store = nativeStore(); if (!store) return false; try { const caps = await TrekShell.getCapabilities(); return caps.features.includes(store.feature); } catch { return false; } }, async prices(productIds) { try { const { products } = await TrekShell.storeProducts({ ids: productIds }); return new Map(products.map((p) => [p.id, p.displayPrice])); } catch { return new Map(); } }, async isSandbox() { try { return (await TrekShell.storeProducts({ ids: [] })).sandbox === true; } catch { return false; } }, async purchase(productId) { try { const result = await TrekShell.storePurchase({ productId }); const receipt = result.jws ?? result.purchaseToken; if (result.kind === 'bought' && receipt && result.transactionId) { return { kind: 'bought', purchase: { platform: nativeStore()?.platform ?? 'apple', productId: result.productId ?? productId, receipt, transactionId: result.transactionId, }, }; } if (result.kind === 'cancelled' || result.kind === 'pending') return { kind: result.kind }; return { kind: 'failed' }; } catch { return { kind: 'failed' }; } }, async finish(purchase) { try { await TrekShell.storeFinish({ transactionId: purchase.transactionId }); } catch { // Left unfinished, the store hands the transaction over again; a restore redeems it idempotently. } }, async unfinished() { // StoreKit hands an interrupted first purchase over through a restore on its own; only Play needs this. if (nativeStore()?.platform !== 'google') return null; try { const { purchaseToken, productId, transactionId } = await TrekShell.storeUnfinished({ prefix: PRODUCT_PREFIX }); if (!purchaseToken || !transactionId) return null; return { platform: 'google', productId: productId ?? '', receipt: purchaseToken, transactionId }; } catch { return null; } }, onTransaction(listener) { if (nativeStore()?.platform !== 'google') return; void TrekShell.addListener('storeTransaction', () => listener()).catch((err: unknown) => { // Without the listener a renewal that clears later is still picked up by the next restore; nothing to undo. reportNativeFailure(err); }); }, async restore() { try { // Google Play answers with a purchase token where the App Store answers with a signed transaction. const { jws, purchaseToken, productId, transactionId }: StoreRestoreResult = await TrekShell.storeRestore({ prefix: PRODUCT_PREFIX, }); if (purchaseToken && transactionId) { return { platform: 'google', productId: productId ?? '', receipt: purchaseToken, transactionId }; } if (!jws || !transactionId) return null; return { platform: 'apple', productId: productIdOf(jws), receipt: jws, transactionId }; } catch { return null; } }, }; /** * The product id inside a signed transaction, read for display and routing only: * the control plane verifies the signature and takes the product from there. */ function productIdOf(jws: string): string { try { const part = jws.split('.')[1] ?? ''; const json = atob(part.replace(/-/g, '+').replace(/_/g, '/')); const id = (JSON.parse(json) as { productId?: unknown }).productId; return typeof id === 'string' ? id : ''; } catch { return ''; } } /** * Whether to use the fake. On in a development build, and switchable on in a * preview build with `localStorage['trek-iap-fake'] = '1'`, so the flow can be * shown without a store. Off in a store build, where the native client decides. */ function fakeEnabled(): boolean { try { if (import.meta.env?.DEV) return true; return typeof localStorage !== 'undefined' && localStorage.getItem('trek-iap-fake') === '1'; } catch { return false; } } export function getIapClient(): IapClient { return fakeEnabled() ? fakeIapClient : nativeIapClient; }