diff --git a/Tools/Pricing/README.md b/Tools/Pricing/README.md index b185ee6..82a11f0 100644 --- a/Tools/Pricing/README.md +++ b/Tools/Pricing/README.md @@ -9,7 +9,7 @@ Google Play, from one copy of the tools. A game keeps its settings in a | `ppp.py ` | Prints the purchasing-power price for every storefront: World Bank price levels (and, with `--income-weight`, incomes) and live exchange rates, rounded as prices. Takes only flags. | | `appstore.py` | Plans the App Store price schedule from those levels, at each territory's nearest price point (.x9 / 9 endings first), for the app itself or for tiers of in-app purchases. `--send` replaces the schedules. | | `play.py` | Prices every Google Play region as the App Store prices it (snapshots of both), and writes gplay's one-time product file or, for a paid app, the table Play Console takes by hand, with `prices.md` comparing the two stores. Talks to neither store, but `--refresh` re-reads an app's App Store prices first (read only). | -| `product.js` | Creates or updates the one-time product on Play from the product file, through the Publisher API (`gplay iap apply` can't create a first product). The plan reads Play only; `--send` writes. `--reward` does the same for a pre-registration reward. | +| `product.js` | Creates or updates the one-time product on Play from the product file, through the Publisher API (`gplay iap apply` can't create a first product). The plan reads Play only; `--send` writes. `--reward` does the same for a pre-registration reward. For a game with more products, `--file ` takes a whole product body instead, and `--get ` prints a product as Play holds it. | `--help` on each lists its flags. Every flag overrides its setting. diff --git a/Tools/Pricing/product.js b/Tools/Pricing/product.js index 80ac8fd..686b763 100644 --- a/Tools/Pricing/product.js +++ b/Tools/Pricing/product.js @@ -17,6 +17,15 @@ // With --reward, the same for a pre-registration reward (listing.json // `reward`: `productId`, `purchaseOptionId`, `listings`). // +// For a game with more than listing.json's one product: +// +// node /product.js --file [--send|--verify] +// the same for a whole oneTimeProduct body (its productId, listings and +// purchaseOptions) in place of the product's file; a product that +// exists keeps its listings when the body has none. +// node /product.js --get +// prints the product as Play holds it (JSON), or {"missing": true}. +// // The plan reads Play only: the product as it stands (if any), and Play's own // conversion of the file's US price for new regions (pricing:convertRegionPrices, // which computes and changes nothing), whose answer names the current regions @@ -50,9 +59,18 @@ const PACKAGE = SOURCE.package; // is built from the product's file (the same regions at the same prices, // so it is no cheaper way in) with its own id, purchase option and copy. const REWARD = process.argv.includes('--reward'); -const PRODUCT_FILE = path.join(HERE, 'Monetization', 'iap', `${SOURCE.product.productId}.json`); -const PRODUCT_ID = REWARD ? SOURCE.reward.productId : SOURCE.product.productId; -const FILE = REWARD ? PRODUCT_FILE : path.join(HERE, 'Monetization', 'iap', `${PRODUCT_ID}.json`); +const argument = (flag) => { + const at = process.argv.indexOf(flag); + return at >= 0 ? process.argv[at + 1] : undefined; +}; +// --file: a whole product body of the game's own; --get: read one product. +const BODY_FILE = argument('--file'); +const GET = argument('--get'); +// Those two need only listing.json's package, not its one product. +const productFile = () => path.join(HERE, 'Monetization', 'iap', `${SOURCE.product.productId}.json`); +const PRODUCT_ID = GET ?? (BODY_FILE ? JSON.parse(fs.readFileSync(BODY_FILE, 'utf8')).productId + : REWARD ? SOURCE.reward.productId : SOURCE.product.productId); +const FILE = GET ? undefined : BODY_FILE ?? productFile(); const API = 'https://androidpublisher.googleapis.com/androidpublisher/v3'; const send = process.argv.includes('--send'); @@ -147,6 +165,10 @@ async function main() { const exists = existing.status === 200; if (!exists && existing.status !== 404) throw new Error(`reading the product: ${existing.status} ${JSON.stringify(existing.body).slice(0, 300)}`); + if (!(product.listings ?? []).length) { + if (!exists) problems.push('a new product needs listings'); + product.listings = exists ? existing.body.listings ?? [] : []; + } console.log(`Product ${PRODUCT_ID} on ${PACKAGE}: ${exists ? 'exists, would be updated' : 'missing, would be created'}`); console.log(` listings: ${product.listings.length} languages (${product.listings.map((l) => l.languageCode).join(', ')})`); console.log(` purchase option "${option.purchaseOptionId}": buy, legacy-compatible, one at a time, then activated`); @@ -184,7 +206,15 @@ async function main() { console.log(`Purchase option "${option.purchaseOptionId}" is active.`); } -if (process.argv.includes('--verify')) { +if (GET) { + // Read-only: one product as Play holds it. + (async () => { + const live = await call(await token(), 'GET', `applications/${PACKAGE}/oneTimeProducts/${GET}`); + if (live.status === 404) return console.log(JSON.stringify({ missing: true })); + if (live.status !== 200) throw new Error(`reading ${GET}: ${live.status} ${JSON.stringify(live.body).slice(0, 300)}`); + console.log(JSON.stringify(live.body)); + })().catch((error) => { console.error(error.message); process.exit(1); }); +} else if (process.argv.includes('--verify')) { // Read-only: the product as Play holds it, against the file. (async () => { const bearer = await token(); diff --git a/Tools/Pricing/product.test.js b/Tools/Pricing/product.test.js index 9b38d13..a51f167 100644 --- a/Tools/Pricing/product.test.js +++ b/Tools/Pricing/product.test.js @@ -31,12 +31,12 @@ globalThis.fetch = async (url, init = {}) => { }; `; -function game({ currency = 'EUR' } = {}) { +function game({ currency = 'EUR', product = true } = {}) { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'product-')); const money = (units, nanos, code) => ({ currencyCode: code, units: String(units), nanos }); fs.writeFileSync(path.join(dir, 'listing.json'), JSON.stringify({ package: 'com.example.game', - product: { productId: 'com.example.full', listings: {} }, + ...(product ? { product: { productId: 'com.example.full', listings: {} } } : {}), reward: { productId: 'com.example.reward', purchaseOptionId: 'reward', listings: { 'en-US': { title: 'Gift', description: 'Free' } } }, })); fs.mkdirSync(path.join(dir, 'Monetization', 'iap'), { recursive: true }); @@ -104,6 +104,39 @@ test('--reward plans the reward from the product\'s file', () => { assert.match(stdout, /listings: 1 languages \(en-US\)/); }); +// A body of the game's own for --file: the product's file under another id. +function body(dir, { listings = true } = {}) { + const product = JSON.parse(fs.readFileSync(path.join(dir, 'Monetization', 'iap', 'com.example.full.json'), 'utf8')); + product.productId = 'com.example.world'; + if (!listings) delete product.listings; + const file = path.join(dir, 'world.json'); + fs.writeFileSync(file, JSON.stringify(product)); + return file; +} + +test('--file plans a body of the game\'s own, with listing.json holding only the package', () => { + const dir = game({ product: false }); + const { status, stdout } = run(dir, '--file', body(dir)); + assert.strictEqual(status, 0); + assert.match(stdout, /Product com\.example\.world on com\.example\.game: missing, would be created/); + assert.match(stdout, /Plan only\./); +}); + +test('--file refuses a new product without listings, sending nothing', () => { + const dir = game({ product: false }); + const { status, stdout, stderr, calls } = run(dir, '--file', body(dir, { listings: false }), '--send'); + assert.strictEqual(status, 1); + assert.match(stdout, /a new product needs listings/); + assert.match(stderr, /nothing was sent/); + assert.ok(!calls.some((c) => c.method === 'PATCH')); +}); + +test('--get prints a product Play doesn\'t have as missing, with listing.json holding only the package', () => { + const { status, stdout } = run(game({ product: false }), '--get', 'com.example.world'); + assert.strictEqual(status, 0); + assert.deepStrictEqual(JSON.parse(stdout), { missing: true }); +}); + test('it needs PLAY_DIR', () => { const result = spawnSync(process.execPath, [TOOL], { encoding: 'utf8', env: { ...process.env, PLAY_DIR: '' } }); assert.strictEqual(result.status, 2);