play products: price, region and activation changes of existing products ask first (diff, terminal confirmation or --yes; refused without a terminal and under serve unless a desktop owner action confirmed); new products and listing text unchanged; store-iap-setup skill and MCP description; tests
This commit is contained in:
parent
3256beb59f
commit
506cceb566
5 changed files with 139 additions and 33 deletions
|
|
@ -110,7 +110,7 @@ export const TOOLS: ToolDef[] = [
|
|||
description: 'Replace icon, feature graphic and screenshots per Play language from the renders folder (or an A/B variant folder). Slow (minutes): prefer the terminal with a log for many languages.',
|
||||
props: { only, variant }, args: i => [...opt('only', i.only), ...opt('variant', i.variant)] },
|
||||
{ name: 'play_products', title: 'Sync Play one-time products', command: ['play', 'products'], write: true, network: true, idempotent: true,
|
||||
description: 'Create / update one-time products with regional prices and activate their purchase option.',
|
||||
description: 'Create one-time products with regional prices and activate them; update listing text of existing ones. dry_run shows per product what would change. Price, region or activation changes of existing products are the owner\'s (terminal confirmation): here they are skipped with a warning.',
|
||||
props: { only }, args: i => opt('only', i.only) },
|
||||
{ name: 'play_subscriptions', title: 'Sync Play subscriptions', command: ['play', 'subscriptions'], write: true, network: true, idempotent: true,
|
||||
description: 'Auto-renewable subscriptions on Google Play from the config: listings per language (title, description, benefits), one auto-renewing base plan with regional prices (convertRegionPrices + overrides + optional prices CSV), activation, and the introductory offer for new subscribers. dry_run prints the plan. Without a terminal it creates only what is new; price changes of existing base plans are an owner terminal run.',
|
||||
|
|
|
|||
|
|
@ -4,11 +4,15 @@
|
|||
// Per product: convertRegionPrices (USD base -> every region at local price points, plus regionsVersion), your
|
||||
// explicit overrides on top, PATCH ...?allowMissing=true (create or update), then activate the purchase option
|
||||
// (new options start as DRAFT) with purchaseOptions:batchUpdateStates. Idempotent.
|
||||
// New products and listing text changes are written directly. Changes to what an EXISTING product costs or where it
|
||||
// is sold (regional prices, the price for other regions, dropped regions, activating its purchase option) are shown
|
||||
// as a diff first and need a confirmation: a terminal question (or --yes), or under the desktop app an owner action
|
||||
// confirm token. Without one (MCP, CI, serve) those changes are skipped with a warning; listing changes still go.
|
||||
// Play has no non-renewing type: a `non_renewing` product is a plain one-time product here (your app consumes it
|
||||
// when the access ends). Products with `platforms` that leave out android are skipped.
|
||||
import { playCodes, sellsOn } from '../lib/config';
|
||||
import type { Ctx } from '../lib/ctx';
|
||||
import { need } from '../lib/ctx';
|
||||
import { confirm, need } from '../lib/ctx';
|
||||
import { done, log, step, warn } from '../lib/log';
|
||||
import { fromMoney, type Money, playRegionConfigs, type RegionConfig, toMoney } from '../lib/money';
|
||||
import { playClient } from './client';
|
||||
|
|
@ -30,21 +34,25 @@ export interface Existing {
|
|||
|
||||
const money = (m?: PlayMoney): string => (m ? `${m.currencyCode} ${fromMoney(m).toFixed(2)}` : '-');
|
||||
|
||||
export interface ProductChange { text: string; /** price / region / activation change of an existing product: asks first */ owner: boolean }
|
||||
|
||||
/**
|
||||
* What a run would change on one product, from the product Play has now (oneTimeProducts list) and the body the run
|
||||
* would PATCH. The PATCH replaces listings and purchase options, so a language on Play that is not in the config is
|
||||
* removed. Prices are compared per region (currency and amount). Empty: nothing would change. Pure.
|
||||
*/
|
||||
export function productChanges(cur: Existing | undefined, want: { listings: { languageCode: string; title: string; description: string }[]; regions: RegionConfig[]; newRegions: { usdPrice: Money; eurPrice: Money } }): string[] {
|
||||
if (!cur) return ['create'];
|
||||
const out: string[] = [];
|
||||
export function productChanges(cur: Existing | undefined, want: { listings: { languageCode: string; title: string; description: string }[]; regions: RegionConfig[]; newRegions: { usdPrice: Money; eurPrice: Money } }): ProductChange[] {
|
||||
if (!cur) return [{ text: 'create', owner: false }];
|
||||
const out: ProductChange[] = [];
|
||||
const text = (t: string) => out.push({ text: t, owner: false });
|
||||
const owner = (t: string) => out.push({ text: t, owner: true });
|
||||
const have = new Map((cur.listings ?? []).map(l => [l.languageCode, l]));
|
||||
const added = want.listings.filter(l => !have.has(l.languageCode)).map(l => l.languageCode);
|
||||
const changed = want.listings.filter(l => { const h = have.get(l.languageCode); return h && ((h.title ?? '') !== l.title || (h.description ?? '') !== l.description); }).map(l => l.languageCode);
|
||||
const removed = [...have.keys()].filter(c => !want.listings.some(l => l.languageCode === c));
|
||||
if (added.length) out.push(`listings added: ${added.join(', ')}`);
|
||||
if (changed.length) out.push(`listings changed: ${changed.join(', ')}`);
|
||||
if (removed.length) out.push(`listings removed: ${removed.join(', ')}`);
|
||||
if (added.length) text(`listings added: ${added.join(', ')}`);
|
||||
if (changed.length) text(`listings changed: ${changed.join(', ')}`);
|
||||
if (removed.length) text(`listings removed: ${removed.join(', ')}`);
|
||||
const opt = cur.purchaseOptions?.find(o => o.purchaseOptionId === OPTION);
|
||||
const now = new Map((opt?.regionalPricingAndAvailabilityConfigs ?? []).map(r => [r.regionCode, r]));
|
||||
const prices = want.regions.flatMap(r => {
|
||||
|
|
@ -52,12 +60,12 @@ export function productChanges(cur: Existing | undefined, want: { listings: { la
|
|||
return h && h.currencyCode === r.price.currencyCode && Math.round(fromMoney(h) * 100) === Math.round(fromMoney(r.price) * 100) ? [] : [`${r.regionCode} ${money(h)} -> ${money(r.price)}`];
|
||||
});
|
||||
const gone = [...now.keys()].filter(c => !want.regions.some(r => r.regionCode === c));
|
||||
if (prices.length) out.push(`prices in ${prices.length} region(s): ${prices.slice(0, 6).join(', ')}${prices.length > 6 ? ', ...' : ''}`);
|
||||
if (gone.length) out.push(`regions dropped: ${gone.join(', ')}`);
|
||||
if (prices.length) owner(`prices in ${prices.length} region(s): ${prices.slice(0, 6).join(', ')}${prices.length > 6 ? ', ...' : ''}`);
|
||||
if (gone.length) owner(`regions dropped: ${gone.join(', ')}`);
|
||||
const nr = opt?.newRegionsConfig;
|
||||
if (money(nr?.usdPrice) !== money(want.newRegions.usdPrice) || money(nr?.eurPrice) !== money(want.newRegions.eurPrice))
|
||||
out.push(`other regions ${money(nr?.usdPrice)} / ${money(nr?.eurPrice)} -> ${money(want.newRegions.usdPrice)} / ${money(want.newRegions.eurPrice)}`);
|
||||
if (opt?.state !== 'ACTIVE') out.push('purchase option activated');
|
||||
owner(`other regions ${money(nr?.usdPrice)} / ${money(nr?.eurPrice)} -> ${money(want.newRegions.usdPrice)} / ${money(want.newRegions.eurPrice)}`);
|
||||
if (opt?.state !== 'ACTIVE') owner(`purchase option ${opt?.state ?? 'missing'} -> ACTIVE`);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
|
@ -83,6 +91,7 @@ export async function run(ctx: Ctx): Promise<void> {
|
|||
const existing = new Map((await listOneTimeProducts(play)).map(p => [p.productId, p]));
|
||||
const langs = new Map(config.languages.map(l => [l.id, l]));
|
||||
|
||||
const plan: { p: (typeof products)[number]; tag: string; body: Record<string, unknown>; version: string; cur?: Existing; changes: ProductChange[]; regions: number; listings: number }[] = [];
|
||||
for (const [i, p] of products.entries()) {
|
||||
const tag = step(i + 1, products.length, p.id);
|
||||
const conv = await play.call<Converted>('POST', `${play.API}/pricing:convertRegionPrices`, { price: toMoney('USD', p.usd) });
|
||||
|
|
@ -101,22 +110,41 @@ export async function run(ctx: Ctx): Promise<void> {
|
|||
}],
|
||||
};
|
||||
const cur = existing.get(p.id);
|
||||
if (dry) {
|
||||
const ch = productChanges(cur, { listings, regions, newRegions: conv.convertedOtherRegionsPrice });
|
||||
log('play', `${tag}: ${!ch.length ? 'unchanged' : ch[0] === 'create' ? `would create · ${regions.length} regions · ${listings.length} listings` : 'would update ' + ch.join('; ')} (dry)`);
|
||||
const changes = productChanges(cur, { listings, regions, newRegions: conv.convertedOtherRegionsPrice });
|
||||
const what = !changes.length ? 'unchanged' : !cur ? `${dry ? 'would create' : 'create'} · ${regions.length} regions · ${listings.length} listings`
|
||||
: `${dry ? 'would update' : 'update'} ${changes.map(c => c.text).join('; ')}`;
|
||||
log('play', `${tag}: ${what}${dry ? ' (dry)' : ''}`);
|
||||
plan.push({ p, tag, body, version: conv.regionVersion?.version ?? '2022/02', cur, changes, regions: regions.length, listings: listings.length });
|
||||
}
|
||||
if (dry) { done('play', 'products checked (dry run, nothing written)'); return; }
|
||||
|
||||
// prices, regions and activation of existing products: the owner confirms the diff above first
|
||||
const gated = plan.filter(x => x.cur && x.changes.some(c => c.owner));
|
||||
const ok = !gated.length || await confirm(ctx, `Change prices / regions / activation of ${gated.length} existing product(s) on Google Play (${gated.map(x => x.p.id).join(', ')})? Buyers see new prices as soon as Play applies them.`);
|
||||
if (!ok) warn('play', `price / region / activation changes of ${gated.length} existing product(s) skipped: confirm in a terminal (or the desktop owner action)`);
|
||||
|
||||
for (const x of plan) {
|
||||
if (!x.changes.length) continue;
|
||||
const id = encodeURIComponent(x.p.id);
|
||||
const v = encodeURIComponent(x.version);
|
||||
const ownerChange = !!x.cur && x.changes.some(c => c.owner);
|
||||
const listingChange = x.changes.some(c => !c.owner);
|
||||
if (x.cur && ownerChange && !ok) {
|
||||
if (!listingChange) continue;
|
||||
await play.call('PATCH', `${play.API}/onetimeproducts/${id}?regionsVersion.version=${v}&updateMask=listings`, x.body);
|
||||
log('play', `${x.tag}: listings updated (prices unchanged)`);
|
||||
continue;
|
||||
}
|
||||
const v = encodeURIComponent(conv.regionVersion?.version ?? '2022/02');
|
||||
await play.call('PATCH', `${play.API}/onetimeproducts/${encodeURIComponent(p.id)}?allowMissing=true®ionsVersion.version=${v}&updateMask=listings,purchaseOptions`, body);
|
||||
log('play', `${tag}: ${cur ? 'updated' : 'created'} · ${regions.length} regions · ${listings.length} listings`);
|
||||
const active = cur?.purchaseOptions?.find(o => o.purchaseOptionId === OPTION)?.state === 'ACTIVE';
|
||||
if (active) continue;
|
||||
const mask = x.cur && !ownerChange ? 'listings' : 'listings,purchaseOptions';
|
||||
await play.call('PATCH', `${play.API}/onetimeproducts/${id}?allowMissing=true®ionsVersion.version=${v}&updateMask=${mask}`, x.body);
|
||||
log('play', `${x.tag}: ${x.cur ? 'updated' : 'created'} · ${x.regions} regions · ${x.listings} listings`);
|
||||
if (x.cur?.purchaseOptions?.find(o => o.purchaseOptionId === OPTION)?.state === 'ACTIVE') continue;
|
||||
try {
|
||||
await play.call('POST', `${play.API}/oneTimeProducts/${encodeURIComponent(p.id)}/purchaseOptions:batchUpdateStates`, {
|
||||
requests: [{ activatePurchaseOptionRequest: { packageName: pkg, productId: p.id, purchaseOptionId: OPTION } }],
|
||||
await play.call('POST', `${play.API}/oneTimeProducts/${id}/purchaseOptions:batchUpdateStates`, {
|
||||
requests: [{ activatePurchaseOptionRequest: { packageName: pkg, productId: x.p.id, purchaseOptionId: OPTION } }],
|
||||
});
|
||||
log('play', `${tag}: purchase option activated`);
|
||||
} catch (e) { warn('play', `${tag}: activate failed: ${(e as Error).message}`); }
|
||||
log('play', `${x.tag}: purchase option activated`);
|
||||
} catch (e) { warn('play', `${x.tag}: activate failed: ${(e as Error).message}`); }
|
||||
}
|
||||
done('play', dry ? 'products checked (dry run, nothing written)' : 'products done');
|
||||
done('play', 'products done');
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue