Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Tools/Pricing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Google Play, from one copy of the tools. A game keeps its settings in a
| `ppp.py <price>` | 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 <body.json>` takes a whole product body instead, and `--get <productId>` prints a product as Play holds it. |

`--help` on each lists its flags. Every flag overrides its setting.

Expand Down
38 changes: 34 additions & 4 deletions Tools/Pricing/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Play dir>/product.js --file <body.json> [--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 <Play dir>/product.js --get <productId>
// 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
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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 ?? [] : [];
}
Comment on lines +168 to +171

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crashes on a --file body that has no listings key, when the product is new. The !exists branch adds the problem but leaves product.listings undefined. The next line, product.listings.length (L172), then throws Cannot read properties of undefined (reading 'length'). So the run exits before it prints the problems list, and "a new product needs listings" never shows. I reproduced this with the stubbed Play from product.test.js: the output is Product … missing, would be created and then that TypeError. Nothing gets sent, but the refusal the PR describes never appears.

if (!(product.listings ?? []).length) {
if (!exists) problems.push('a new product needs listings');
else product.listings = 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`);

Suggested change
if (!(product.listings ?? []).length) {
if (!exists) problems.push('a new product needs listings');
else product.listings = existing.body.listings ?? [];
}
if (!(product.listings ?? []).length) {
if (!exists) problems.push('a new product needs listings');
product.listings = exists ? existing.body.listings ?? [] : [];
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5e4f733, with your suggestion. A new test sends a body with no listings: the run exits with 'a new product needs listings' and 'nothing was sent', and makes no PATCH.

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`);
Expand Down Expand Up @@ -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();
Expand Down
37 changes: 35 additions & 2 deletions Tools/Pricing/product.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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);
Expand Down
Loading