// Kit IAP (v1) - Google Play one-off purchases via cordova-plugin-purchase (CdvPurchase v13). // From _Tools\Library\mobile\kit (XP-Battleship/src/iap.ts). Native only; on web every call // resolves to "not bought". // // configureIap({ nonConsumable: ["premium_ship_1", "remove_ads"] }); // const owned = await restorePurchases(); // every app start // const r = await purchase("remove_ads"); // "owned" | "failed" | "pending" // productPrice("remove_ads") ?? "2,99 €" // Play's localized price, or your fallback // // SKUs must exist as ACTIVE in-app products in the Play Console. Purchases only work in builds // installed from Play (internal test track and up) - sideloaded debug builds get an empty store. import { Capacitor } from "@capacitor/core"; /* eslint-disable @typescript-eslint/no-explicit-any */ type Cdv = any; // runtime global from the plugin let skus: string[] = []; export function configureIap(c: { nonConsumable: string[] }): void { skus = c.nonConsumable; } function cdv(): Cdv | null { return (window as unknown as Record).CdvPurchase ?? null; } let initPromise: Promise | null = null; function ensureStore(): Promise { if (!Capacitor.isNativePlatform() || !cdv() || !skus.length) return Promise.resolve(false); if (!initPromise) { initPromise = (async (): Promise => { const { store, ProductType, Platform } = cdv(); store.register(skus.map((id) => ({ id, type: ProductType.NON_CONSUMABLE, platform: Platform.GOOGLE_PLAY }))); store.when().approved((tx: Cdv) => tx.finish()); // Play already guarantees payment store.error(() => { initPromise = null; }); // billing disconnect: re-register next call await store.initialize([Platform.GOOGLE_PLAY]); return true; })().catch(() => { // never cache a failure: one offline launch must not disable buying for the session initPromise = null; return false; }); } return initPromise; } /** `pending` = Play accepted the order but ownership has not arrived yet. Do NOT report it as * a failure - that is how paying customers get told "not completed" and ask for refunds. */ export type PurchaseResult = "owned" | "failed" | "pending"; export async function purchase(sku: string): Promise { if (!(await ensureStore())) return "failed"; const { store, Platform } = cdv(); try { const offer = store.get(sku, Platform.GOOGLE_PLAY)?.getOffer(); if (!offer) return "failed"; const err = await offer.order(); if (err) return "failed"; // cancelled or declined const t0 = Date.now(); while (Date.now() - t0 < 60_000) { if (store.get(sku, Platform.GOOGLE_PLAY)?.owned) return "owned"; await new Promise((r) => setTimeout(r, 400)); } try { await store.restorePurchases(); if (store.get(sku, Platform.GOOGLE_PLAY)?.owned) return "owned"; } catch { // offline } return "pending"; } catch { return "failed"; } } /** Play's localized price ("2,99 €", "$3.49"), or null while unknown. */ export function productPrice(sku: string): string | null { if (!Capacitor.isNativePlatform() || !cdv()) return null; try { const { store, Platform } = cdv(); const product = store.get(sku, Platform.GOOGLE_PLAY); return product?.getOffer()?.pricingPhases?.[0]?.price ?? product?.pricing?.price ?? null; } catch { return null; } } /** SKUs the player owns (restore flow - run on every app start). */ export async function restorePurchases(): Promise { if (!(await ensureStore())) return []; const { store, Platform } = cdv(); try { await store.restorePurchases(); } catch { // offline - Play's local cache still answers below } return skus.filter((sku) => store.get(sku, Platform.GOOGLE_PLAY)?.owned); }