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
+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' };