Show verified store trials in the native cloud purchase flow [skip ci]

This commit is contained in:
Maurice
2026-12-02 10:20:26 +01:00
parent dbcdecb6a7
commit 00fcb69423
36 changed files with 440 additions and 61 deletions
+23 -7
View File
@@ -84,15 +84,22 @@ describe('native client', () => {
expect(await fakeIapClient.isSandbox()).toBe(true);
});
it('maps the store price strings by product', async () => {
shell.storeProducts.mockResolvedValue({ products: [{ id: 'cloud.plus.monthly', displayPrice: '9,99 €' }] });
const prices = await nativeIapClient.prices(['cloud.plus.monthly', 'cloud.pro.monthly']);
expect(shell.storeProducts).toHaveBeenCalledWith({ ids: ['cloud.plus.monthly', 'cloud.pro.monthly'] });
expect(prices.get('cloud.plus.monthly')).toBe('9,99 €');
expect(prices.has('cloud.pro.monthly')).toBe(false);
it('maps the regular price and explicitly confirmed trial by product', async () => {
shell.storeProducts.mockResolvedValue({
products: [
{ id: 'cloud.plus.monthly', displayPrice: '9,99 €', freeTrial: true },
{ id: 'cloud.start.monthly', displayPrice: '4,99 €' },
],
});
const ids = ['cloud.plus.monthly', 'cloud.start.monthly', 'cloud.plus.yearly'];
const products = await nativeIapClient.products(ids);
expect(shell.storeProducts).toHaveBeenCalledWith({ ids });
expect(products.get('cloud.plus.monthly')).toEqual({ displayPrice: '9,99 €', freeTrial: true });
expect(products.get('cloud.start.monthly')).toEqual({ displayPrice: '4,99 €', freeTrial: false });
expect(products.has('cloud.plus.yearly')).toBe(false);
shell.storeProducts.mockRejectedValue(new Error('down'));
expect((await nativeIapClient.prices(['cloud.plus.monthly'])).size).toBe(0);
expect((await nativeIapClient.products(['cloud.plus.monthly'])).size).toBe(0);
});
it('turns a bought transaction into a receipt', async () => {
@@ -124,6 +131,15 @@ describe('native client', () => {
expect(await nativeIapClient.held()).toBe(false);
});
it('asks the store to preserve an advertised trial and reports a changed offer without retrying as paid', async () => {
shell.storePurchase.mockResolvedValue({ kind: 'offer-changed' });
expect(await nativeIapClient.purchase('cloud.plus.monthly', '9,99 €')).toEqual({ kind: 'offer-changed' });
expect(shell.storePurchase).toHaveBeenCalledExactlyOnceWith({
productId: 'cloud.plus.monthly',
trialRenewalPrice: '9,99 €',
});
});
it('a build without storeHeld counts as holding nothing; the purchase still guards', async () => {
shell.storeHeld.mockRejectedValue(new Error('not implemented'));
expect(await nativeIapClient.held()).toBe(false);
+22 -11
View File
@@ -38,17 +38,23 @@ type PurchaseOutcome =
| { kind: 'cancelled' }
| { kind: 'pending' }
| { kind: 'failed' }
| { kind: 'offer-changed' }
| { kind: 'subscribed' };
export interface IapProduct {
displayPrice: string;
/** The store confirms an eligible first week at no charge. */
freeTrial: boolean;
}
export interface IapClient {
/** Whether this build can take a payment. */
isAvailable(): Promise<boolean>;
/**
* The store's own price strings by product id, in the buyer's currency. A
* product missing from the map cannot be bought here. Empty for the fake, whose
* prices come from the plan list.
* Store pricing and offer eligibility by product id. A missing product cannot
* be bought. Empty for the fake, whose prices come from the plan list.
*/
prices(productIds: string[]): Promise<Map<string, string>>;
products(productIds: string[]): Promise<Map<string, IapProduct>>;
/** A test build (TestFlight, App Review), whose purchases cost nothing and may go ahead before the opening. */
isSandbox(): Promise<boolean>;
/**
@@ -58,7 +64,7 @@ export interface IapClient {
*/
held(): Promise<boolean>;
/** Runs the platform's purchase sheet for a product and resolves with what became of it. */
purchase(productId: string): Promise<PurchaseOutcome>;
purchase(productId: string, trialRenewalPrice?: string): Promise<PurchaseOutcome>;
/** Tells the store a purchase was delivered, once its instance exists. */
finish(purchase: Purchase): Promise<void>;
/** The subscription this store account already holds, for a reinstall or a new device. */
@@ -94,7 +100,7 @@ export const fakeIapClient: IapClient = {
async isAvailable() {
return true;
},
async prices() {
async products() {
return new Map();
},
async isSandbox() {
@@ -151,10 +157,10 @@ export const nativeIapClient: IapClient = {
return false;
}
},
async prices(productIds) {
async products(productIds) {
try {
const { products } = await TrekShell.storeProducts({ ids: productIds });
return new Map(products.map((p) => [p.id, p.displayPrice]));
return new Map(products.map((p) => [p.id, { displayPrice: p.displayPrice, freeTrial: p.freeTrial === true }]));
} catch {
return new Map();
}
@@ -166,9 +172,9 @@ export const nativeIapClient: IapClient = {
return false;
}
},
async purchase(productId) {
async purchase(productId, trialRenewalPrice) {
try {
const result = await TrekShell.storePurchase({ productId });
const result = await TrekShell.storePurchase({ productId, ...(trialRenewalPrice ? { trialRenewalPrice } : {}) });
const receipt = result.jws ?? result.purchaseToken;
if (result.kind === 'bought' && receipt && result.transactionId) {
return {
@@ -181,7 +187,12 @@ export const nativeIapClient: IapClient = {
},
};
}
if (result.kind === 'cancelled' || result.kind === 'pending' || result.kind === 'subscribed') {
if (
result.kind === 'cancelled' ||
result.kind === 'pending' ||
result.kind === 'subscribed' ||
result.kind === 'offer-changed'
) {
return { kind: result.kind };
}
return { kind: 'failed' };
+3 -3
View File
@@ -128,14 +128,14 @@ export interface TrekShellPlugin {
/** Feature storeKit (iOS) or playBilling (Android): the store's price strings for the products it knows. */
storeProducts(options: {
ids: string[];
}): Promise<{ products: { id: string; displayPrice: string }[]; sandbox?: boolean }>;
}): Promise<{ products: { id: string; displayPrice: string; freeTrial?: boolean }[]; sandbox?: boolean }>;
/**
* The purchase sheet. Set when bought: on iOS `jws`, the signed transaction; on
* Android `purchaseToken`, which the control plane verifies with Google Play.
*/
storePurchase(options: { productId: string }): Promise<{
storePurchase(options: { productId: string; trialRenewalPrice?: string }): Promise<{
/** `subscribed`: this store account already holds a delivered subscription, so nothing was bought. */
kind: 'bought' | 'cancelled' | 'pending' | 'failed' | 'subscribed';
kind: 'bought' | 'cancelled' | 'pending' | 'failed' | 'subscribed' | 'offer-changed';
jws?: string;
purchaseToken?: string;
productId?: string;