fix(native): a store account with a running TREK Cloud subscription buys no second one

One subscription per Apple ID or Google account. Choosing another plan in the app changed the running subscription on the App Store, and the redeem then failed with 'We could not set up your TREK'. The app now asks the store first and shows a screen of its own: the account already has TREK Cloud, a new one can be taken out once it has ended, with Open my TREK and the store's page to manage it. The native purchase refuses as well; a paid one that was never set up is still redeemed. On the address screens the link 'What is a TREK server?' is gone, and the small print under a purchase is set in the system text face at regular weight.
This commit is contained in:
Maurice
2026-11-12 09:28:56 +01:00
parent 5395d53332
commit 3c0c3b3f6e
37 changed files with 429 additions and 64 deletions
+27
View File
@@ -8,6 +8,7 @@ const shell = vi.hoisted(() => ({
storeFinish: vi.fn(),
storeRestore: vi.fn(),
storeUnfinished: vi.fn(),
storeHeld: vi.fn(),
addListener: vi.fn(),
}));
@@ -34,6 +35,17 @@ describe('in-app purchase client', () => {
expect(productIdFor('pro', 'year')).toBe('cloud.pro.yearly');
});
it('the fake client can play a store account that already holds a subscription', async () => {
localStorage.setItem('trek-iap-fake-held', '1');
try {
expect(await fakeIapClient.held()).toBe(true);
expect(await fakeIapClient.purchase('cloud.plus.monthly')).toEqual({ kind: 'subscribed' });
} finally {
localStorage.removeItem('trek-iap-fake-held');
}
expect(await fakeIapClient.held()).toBe(false);
});
it('the fake client yields a receipt for the product asked for', async () => {
expect(await fakeIapClient.isAvailable()).toBe(true);
const outcome = await fakeIapClient.purchase('cloud.plus.monthly');
@@ -102,6 +114,21 @@ describe('native client', () => {
expect(await nativeIapClient.purchase('cloud.plus.monthly')).toEqual({ kind: 'failed' });
});
it('says when the store account already holds a subscription, and buys nothing then', async () => {
shell.storePurchase.mockResolvedValue({ kind: 'subscribed' });
expect(await nativeIapClient.purchase('cloud.plus.monthly')).toEqual({ kind: 'subscribed' });
shell.storeHeld.mockResolvedValue({ held: true });
expect(await nativeIapClient.held()).toBe(true);
expect(shell.storeHeld).toHaveBeenCalledWith({ prefix: 'cloud.' });
shell.storeHeld.mockResolvedValue({ held: false });
expect(await nativeIapClient.held()).toBe(false);
});
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);
});
it('finishes by transaction id and swallows a failure', async () => {
const purchase = { platform: 'apple' as const, productId: 'cloud.plus.monthly', receipt: 'x', transactionId: '7' };
shell.storeFinish.mockResolvedValue(undefined);
+34 -3
View File
@@ -29,10 +29,16 @@ export interface Purchase {
/**
* What a purchase attempt came to. `cancelled` is the customer backing out, not
* an error; `pending` is a purchase waiting on someone else (Ask to Buy, a bank),
* which a restore picks up once it goes through.
* which a restore picks up once it goes through. `subscribed` means the store
* account already holds a subscription: one per Apple ID or Google account, and
* a new one only once it has ended, so nothing was bought.
*/
type PurchaseOutcome =
{ kind: 'bought'; purchase: Purchase } | { kind: 'cancelled' } | { kind: 'pending' } | { kind: 'failed' };
| { kind: 'bought'; purchase: Purchase }
| { kind: 'cancelled' }
| { kind: 'pending' }
| { kind: 'failed' }
| { kind: 'subscribed' };
export interface IapClient {
/** Whether this build can take a payment. */
@@ -45,6 +51,12 @@ export interface IapClient {
prices(productIds: string[]): Promise<Map<string, string>>;
/** A test build (TestFlight, App Review), whose purchases cost nothing and may go ahead before the opening. */
isSandbox(): Promise<boolean>;
/**
* Whether the store account already holds a delivered subscription. While it
* does, nothing can be bought: on the App Store another plan would change that
* subscription, on Google Play it would run beside it.
*/
held(): Promise<boolean>;
/** Runs the platform's purchase sheet for a product and resolves with what became of it. */
purchase(productId: string): Promise<PurchaseOutcome>;
/** Tells the store a purchase was delivered, once its instance exists. */
@@ -88,7 +100,16 @@ export const fakeIapClient: IapClient = {
async isSandbox() {
return true;
},
async held() {
// `localStorage['trek-iap-fake-held'] = '1'` shows what a store account with a running subscription sees.
try {
return localStorage.getItem('trek-iap-fake-held') === '1';
} catch {
return false;
}
},
async purchase(productId) {
if (await this.held()) return { kind: 'subscribed' };
await new Promise((resolve) => setTimeout(resolve, 900));
const nonce = String(Date.now());
return {
@@ -160,12 +181,22 @@ export const nativeIapClient: IapClient = {
},
};
}
if (result.kind === 'cancelled' || result.kind === 'pending') return { kind: result.kind };
if (result.kind === 'cancelled' || result.kind === 'pending' || result.kind === 'subscribed') {
return { kind: result.kind };
}
return { kind: 'failed' };
} catch {
return { kind: 'failed' };
}
},
async held() {
try {
return (await TrekShell.storeHeld({ prefix: PRODUCT_PREFIX })).held === true;
} catch {
// A build without storeHeld: the purchase itself still refuses a second subscription.
return false;
}
},
async finish(purchase) {
try {
await TrekShell.storeFinish({ transactionId: purchase.transactionId });
+3 -3
View File
@@ -6,9 +6,6 @@
/** The privacy policy the store listings name too. */
export const PRIVACY_URL = 'https://liketrek.com/privacy?app=1';
/** What a TREK server is and how to run one, for someone who has none yet. */
export const SERVER_DOCS_URL = 'https://github.com/liketrek/TREK#readme';
/** Running the app with a sign-in proxy (Cloudflare Access, Pangolin and the like) in front of TREK. */
export const AUTH_PROXY_DOCS_URL = 'https://github.com/liketrek/TREK/wiki/Native-Apps#behind-a-sign-in-proxy';
@@ -27,5 +24,8 @@ export const CLOUD_URL = 'https://liketrek.com';
* store rules ask for it next to the purchase, together with the privacy policy.
*/
export const STORE_TERMS_URL = 'https://www.apple.com/legal/internet-services/itunes/dev/stdeula/';
/** Where the App Store and Google Play let someone manage or end a subscription. */
export const APP_STORE_SUBSCRIPTIONS_URL = 'https://apps.apple.com/account/subscriptions';
export const PLAY_SUBSCRIPTIONS_URL = 'https://play.google.com/store/account/subscriptions?package=com.liketrek.trek';
/** Our terms for TREK Cloud (§ 5a covers a purchase in the app), as the app opens them: without the site's sales header. */
export const CLOUD_TERMS_URL = 'https://liketrek.com/terms?app=1';
+7 -1
View File
@@ -112,7 +112,8 @@ export interface TrekShellPlugin {
* Android `purchaseToken`, which the control plane verifies with Google Play.
*/
storePurchase(options: { productId: string }): Promise<{
kind: 'bought' | 'cancelled' | 'pending' | 'failed';
/** `subscribed`: this store account already holds a delivered subscription, so nothing was bought. */
kind: 'bought' | 'cancelled' | 'pending' | 'failed' | 'subscribed';
jws?: string;
purchaseToken?: string;
productId?: string;
@@ -134,6 +135,11 @@ export interface TrekShellPlugin {
}): Promise<{ result: 'shown' | 'unsupported' }>;
/** iOS, feature storeKit: the newest active subscription under `prefix` on this Apple ID, if any. */
storeRestore(options: { prefix: string }): Promise<{ jws?: string; transactionId?: string }>;
/**
* Whether this Apple ID or Google account holds a delivered subscription under
* `prefix`, read without asking for a password. Not in builds before 5.1.4.
*/
storeHeld(options: { prefix: string }): Promise<{ held: boolean }>;
/** Feature watch: what the Apple Watch app shows (native/watch.ts), as JSON, sent on through WatchConnectivity. */
setWatchSnapshot(options: { json: string }): Promise<void>;
/** Feature watch: the ticks the watch sent since the last call, oldest first (parsed by parseWatchActions). */