// In-app product prices for many products and regions from one CSV (both stores). // storebound prices export [--out store/prices.csv] [--store apple|play] [--only ids] [--with-automatic] [--dry] // storebound prices import [--csv store/prices.csv] [--store apple|play] [--only ids] [--reconvert] [--all] [--dry] [--yes] // CSV columns: product, store (apple | play | both), region (ISO alpha-2), currency, price, start_date (App Store // only, a future YYYY-MM-DD = scheduled change), kind (manual | auto; auto rows are Apple's equalized prices, exported // for reference and ignored on import). // import: reads the current prices, prints the diff table, and (not --dry) asks before writing. Apple: one new price // schedule per changed product (base territory + every manual price; territories without one are equalized by Apple; // an increase cannot be undone once it starts). Play: the purchase option's regional prices are replaced: CSV regions // on top of the current ones; regions without a price get convertRegionPrices from the USD base (CSV US row, else the // config's usd); --reconvert resets every region the CSV does not list to Play's converted price. Pure parts: lib/prices.ts. import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { ascClient, type Asc, type J } from './apple/asc'; import type { Ctx } from './lib/ctx'; import { confirm, need } from './lib/ctx'; import { done, log, step, warn } from './lib/log'; import { toMoney } from './lib/money'; import { appleSchedule, appleScheduleBody, applePriceRows, diffPrices, diffTable, parsePriceCsv, type PlayProduct, playPatchBody, playPriceRows, playRegions, pointKey, type PriceRow, pricesCsv, sameSchedule, } from './lib/prices'; import { playClient, type PlayClient } from './play/client'; const TAG = 'prices'; async function iapId(c: Asc, appId: string, productId: string): Promise { return (await c.asc('GET', `/v1/apps/${appId}/inAppPurchasesV2?filter[productId]=${encodeURIComponent(productId)}`)).data?.[0]?.id ?? null; } async function appleCurrent(c: Asc, id: string, product: string, withAuto: boolean): Promise<{ rows: PriceRow[]; base: string | null }> { const q = 'include=inAppPurchasePricePoint,territory&limit=200'; const rows = applePriceRows(product, await pages(c, `/v1/inAppPurchasePriceSchedules/${id}/manualPrices?${q}`)); if (withAuto) rows.push(...applePriceRows(product, await pages(c, `/v1/inAppPurchasePriceSchedules/${id}/automaticPrices?${q}`), 'auto')); const base = (await c.maybe(`/v1/inAppPurchasePriceSchedules/${id}/baseTerritory`))?.data?.id ?? null; return { rows, base }; } /** data + included of every page of a paged GET (c.all keeps only data). */ async function pages(c: Asc, path: string): Promise<{ data: J[]; included: J[] }> { const out = { data: [] as J[], included: [] as J[] }; let url: string | null = path; while (url) { const page: J = await c.asc('GET', url); out.data.push(...(page.data ?? [])); out.included.push(...(page.included ?? [])); url = page.links?.next ?? null; } return out; } async function playProduct(play: PlayClient, id: string): Promise { try { return await play.call('GET', `${play.API}/oneTimeProducts/${encodeURIComponent(id)}`); } catch (e) { if (/-> 404/.test(String(e))) return null; throw e; } } export async function run(ctx: Ctx): Promise { const { config, root, resolve } = ctx.loaded; const a = ctx.args; const sub = a._[0]; if (sub !== 'export' && sub !== 'import') throw new Error('usage: storebound prices export | import [--csv store/prices.csv] [--dry]'); const store = a.get('store'); if (store && store !== 'apple' && store !== 'play') throw new Error('--store apple | play'); const useApple = (!store || store === 'apple') && !!config.apple; const usePlay = (!store || store === 'play') && !!config.play; const only = a.list('only'); const products = need(config.products, 'products').filter(p => !only.length || only.includes(p.id)); const csvPath = resolve(a.get(sub === 'export' ? 'out' : 'csv') ?? config.prices?.csv ?? 'store/prices.csv'); const dry = a.has('dry'); const c = useApple ? await ascClient(root, config.apple!.bundleId, config.apple?.appId) : null; const appId = c ? await c.app() : ''; const play = usePlay ? await playClient(root, config.play!.packageName) : null; // current state of every product on the chosen stores const current: PriceRow[] = []; const iaps = new Map(); const playProducts = new Map(); for (const [i, p] of products.entries()) { log(TAG, `${step(i + 1, products.length, p.id)}: reading current prices`); if (c) { const id = await iapId(c, appId, p.id); if (!id) warn(TAG, `${p.id}: not on App Store Connect (storebound apple iap creates it)`); else { const cur = await appleCurrent(c, id, p.id, sub === 'export' && a.has('with-automatic')); current.push(...cur.rows); iaps.set(p.id, { id, base: cur.base }); } } if (play) { const pp = await playProduct(play, p.id); if (!pp) warn(TAG, `${p.id}: not on Google Play (storebound play products creates it)`); else { current.push(...playPriceRows(pp)); playProducts.set(p.id, pp); } } } if (sub === 'export') { const csv = pricesCsv(current); if (dry) { console.log(csv); done(TAG, `dry run: would write ${current.length} rows to ${csvPath}`); return; } writeFileSync(csvPath, csv); done(TAG, `${current.length} rows -> ${csvPath} (edit, then storebound prices import --dry)`); return; } if (!existsSync(csvPath)) throw new Error(`no prices CSV at ${csvPath} (storebound prices export writes one)`); const wantedAll = parsePriceCsv(readFileSync(csvPath, 'utf8'), { products: config.products!.map(p => p.id) }) .filter(r => products.some(p => p.id === r.product) && ((r.store === 'apple' && useApple) || (r.store === 'play' && usePlay))); const diff = diffPrices(current, wantedAll); console.log(diffTable(diff, { all: a.has('all') })); const changed = new Set(diff.filter(d => d.change !== 'same').map(d => `${d.product}|${d.store}`)); const reconvert = a.has('reconvert'); if (reconvert && usePlay) for (const p of products) if (playProducts.has(p.id)) changed.add(`${p.id}|play`); if (!changed.size) { done(TAG, 'prices up to date'); return; } log(TAG, `${changed.size} product / store pair(s) to update`); if (dry) { done(TAG, 'dry run: nothing written'); return; } if (!(await confirm(ctx, `Write the new prices for ${changed.size} product / store pair(s)? Players see them as soon as the stores apply them; an App Store price increase cannot be undone once it starts.`))) return; for (const p of products) { const wanted = wantedAll.filter(r => r.product === p.id); if (c && changed.has(`${p.id}|apple`)) { const iap = iaps.get(p.id); if (!iap) { warn(TAG, `${p.id}: no App Store product, skipped`); continue; } const cur = current.filter(r => r.product === p.id && r.store === 'apple'); const base = config.prices?.baseTerritory ?? iap.base ?? 'USA'; const entries = appleSchedule(cur, wanted.filter(r => r.store === 'apple'), base); if (sameSchedule(entries, cur.filter(r => r.kind !== 'auto').map(r => ({ territory: r.territory!, price: r.price, startDate: r.startDate ?? null })))) { log(TAG, `${p.id} apple: schedule unchanged`); } else { const points = new Map(cur.filter(r => r.pointId).map(r => [pointKey(r.territory!, r.price), r.pointId!] as const)); for (const t of new Set(entries.filter(e => !points.has(pointKey(e.territory, e.price))).map(e => e.territory))) { for (const pp of await c.all(`/v2/inAppPurchases/${iap.id}/pricePoints?filter[territory]=${t}&limit=8000`)) points.set(pointKey(t, pp.attributes.customerPrice), pp.id); } const missing = entries.filter(e => !points.has(pointKey(e.territory, e.price))); if (missing.length) throw new Error(`${p.id}: no App Store price point for ${missing.map(e => `${e.territory} ${e.price}`).join(', ')} (Apple only takes its own price points; storebound prices export --with-automatic shows the equalized ones)`); await c.asc('POST', '/v1/inAppPurchasePriceSchedules', appleScheduleBody(iap.id, base, entries, points)); log(TAG, `${p.id} apple: schedule set (${entries.length} manual price(s), base ${base})`); } } if (play && changed.has(`${p.id}|play`)) { const pp = playProducts.get(p.id); if (!pp) { warn(TAG, `${p.id}: no Play product, skipped`); continue; } const usd = wanted.find(r => r.store === 'play' && r.region === 'US')?.price ?? p.usd; const conv = await play.call<{ convertedRegionPrices?: Record; regionVersion?: { version: string } }>( 'POST', `${play.API}/pricing:convertRegionPrices`, { price: toMoney('USD', usd) }); const opt = pp.purchaseOptions?.find(o => o.purchaseOptionId === 'buy') ?? pp.purchaseOptions?.[0]; if (!opt) { warn(TAG, `${p.id}: Play product has no purchase option, skipped`); continue; } const regions = playRegions(opt.regionalPricingAndAvailabilityConfigs ?? [], wanted.filter(r => r.store === 'play'), conv.convertedRegionPrices ?? null, { reconvert }); const v = encodeURIComponent(conv.regionVersion?.version ?? '2022/02'); await play.call('PATCH', `${play.API}/onetimeproducts/${encodeURIComponent(p.id)}?regionsVersion.version=${v}&updateMask=purchaseOptions`, playPatchBody(pp, regions, opt.purchaseOptionId)); log(TAG, `${p.id} play: ${regions.length} regions set`); } } done(TAG, 'prices imported (storebound prices export shows the stores\' view)'); }