Files
TREK/client/src/native/cloudOrders.ts
T

66 lines
2.6 KiB
TypeScript

/**
* What the app's plan screens do while TREK Cloud's orders are closed, apart
* from the screens themselves so it can be tested.
*
* On iOS only a test build (TestFlight, App Review) may buy then: the App Store
* charges it nothing and the cloud serves it. On Android the button shows in
* every build, so the licence testers of the internal test can buy from a
* release build; their purchases are Play test purchases, which the cloud
* serves. A real purchase made before the opening is refused at the cloud with
* `ordering-closed` and never acknowledged, so Google Play refunds it after
* three days, and the app says so instead of showing a general error.
*/
/** Whether the plans show without a way to buy: orders are closed and this build may not buy before the opening. */
export function opensSoon(opts: { available: boolean; closed: boolean; sandbox: boolean; onPlay: boolean }): boolean {
return opts.available && opts.closed && !opts.sandbox && !opts.onPlay;
}
/**
* Whether the cloud refused a purchase because it came before the opening, on
* Android, where Google Play then refunds it: the app says so rather than
* showing a general error. On iOS no real purchase gets this far.
*/
export function refundedBeforeOpening(code: string | undefined, onPlay: boolean): boolean {
return onPlay && code === 'ordering-closed';
}
export function isNewCloudPlan(id: string): boolean {
return id === 'start' || id === 'plus';
}
export interface CloudPurchaseAgreement {
version: string;
text: string;
}
/** Recheck the agreement before opening the store sheet. No agreement means no new purchase. */
export function matchesPurchaseAgreement(
accepted: CloudPurchaseAgreement | null,
current: CloudPurchaseAgreement | null
): boolean {
return (
accepted !== null && current !== null && accepted.version === current.version && accepted.text === current.text
);
}
const PLAN_NAMES: Record<string, string> = { start: 'Start', basis: 'Basis', plus: 'Plus', pro: 'Pro' };
/** A verified purchase can predate the current offer and still need its instance set up. */
export function planForUnclaimedPurchase(productId: string, code: string | undefined) {
if (
code !== 'email-invalid' &&
code !== 'consent-deviations' &&
code !== 'consent-outdated' &&
!code?.startsWith('slug-')
) {
return null;
}
const match = /^cloud\.(start|basis|plus|pro)\.(monthly|yearly)$/.exec(productId);
const id = match?.[1];
if (!id) return null;
const name = PLAN_NAMES[id] ?? id;
const interval = match?.[2] === 'yearly' ? 'year' : 'month';
return { plan: { id, name }, interval } as const;
}