mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
Show verified store trials in the native cloud purchase flow [skip ci]
This commit is contained in:
+96
-27
@@ -8,7 +8,7 @@ import logoWhite from '../public/logo-light.svg'; // #fff wordmark, for the land
|
||||
import { pageUnderStatusBar } from '../src/native/appBoot';
|
||||
import { AUTH_GATE_NAMES, canSignInInApp, type AuthGate } from '../src/native/authGate';
|
||||
import { isNewCloudPlan, opensSoon, planForUnclaimedPurchase, refundedBeforeOpening } from '../src/native/cloudOrders';
|
||||
import { fakeIapClient, getIapClient, productIdFor, type Purchase } from '../src/native/iap';
|
||||
import { fakeIapClient, getIapClient, productIdFor, type IapProduct, type Purchase } from '../src/native/iap';
|
||||
import {
|
||||
APP_STORE_SUBSCRIPTIONS_URL,
|
||||
AUTH_PROXY_DOCS_URL,
|
||||
@@ -315,7 +315,8 @@ function showWelcome(opts: { back?: boolean } = {}): void {
|
||||
const perk = (glyph: string, title: string, sub: string) =>
|
||||
el('div', {}, el('i', {}, icon(glyph, 17)), el('span', {}, el('b', {}, title), el('small', {}, sub)));
|
||||
|
||||
const discover = el('button', { type: 'button', className: 'cta' }, t('native.welcome.discover'));
|
||||
const trial = el('span', { className: 'trial-badge', hidden: true }, icon(I.spark, 12), t('native.trial.badge'));
|
||||
const discover = el('button', { type: 'button', className: 'cta discover' }, t('native.welcome.discover'), trial);
|
||||
discover.addEventListener('click', () => {
|
||||
void tap();
|
||||
showPlans();
|
||||
@@ -326,7 +327,7 @@ function showWelcome(opts: { back?: boolean } = {}): void {
|
||||
showConnect();
|
||||
});
|
||||
|
||||
show(
|
||||
const screen = show(
|
||||
'welcome',
|
||||
WELCOME_CAMERA,
|
||||
{
|
||||
@@ -351,6 +352,9 @@ function showWelcome(opts: { back?: boolean } = {}): void {
|
||||
},
|
||||
opts
|
||||
);
|
||||
void availableWelcomeTrial().then((available) => {
|
||||
if (screen.node.isConnected) trial.hidden = !available;
|
||||
});
|
||||
}
|
||||
|
||||
// ── Connect (address) ───────────────────────────────────────────────────────
|
||||
@@ -686,7 +690,7 @@ const SAMPLE_PLANS: CloudPlan[] = [
|
||||
currency: 'USD',
|
||||
aiDocuments: 0,
|
||||
aiDocumentLimit: 0,
|
||||
storageGb: 200,
|
||||
storageGb: 50,
|
||||
},
|
||||
{
|
||||
id: 'plus',
|
||||
@@ -696,9 +700,9 @@ const SAMPLE_PLANS: CloudPlan[] = [
|
||||
currency: 'USD',
|
||||
aiDocuments: 100,
|
||||
aiDocumentLimit: null,
|
||||
aiAssistant: 'planned',
|
||||
aiAssistant: 'included',
|
||||
bookingMailImport: true,
|
||||
storageGb: 200,
|
||||
storageGb: 50,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -706,6 +710,8 @@ interface CloudPlans {
|
||||
plans: CloudPlan[];
|
||||
/** Orders are closed: only a test build may buy, and the store charges it nothing. */
|
||||
closed: boolean;
|
||||
/** Only the live catalogue's explicit opening permits customer trial advertising. */
|
||||
open?: boolean;
|
||||
}
|
||||
|
||||
async function fetchPlans(): Promise<CloudPlans | null> {
|
||||
@@ -714,7 +720,8 @@ async function fetchPlans(): Promise<CloudPlans | null> {
|
||||
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
|
||||
const body = (data ?? {}) as { plans?: CloudPlan[]; ordering?: string };
|
||||
const plans = Array.isArray(body.plans) ? body.plans.filter((plan) => isNewCloudPlan(plan.id)) : [];
|
||||
if (plans.length) return { plans, closed: body.ordering === 'closed' };
|
||||
if (plans.length)
|
||||
return { plans, closed: body.ordering === 'closed', open: res.status === 200 && body.ordering === 'open' };
|
||||
} catch {
|
||||
// Fall through to the preview fallback.
|
||||
}
|
||||
@@ -730,22 +737,44 @@ async function fetchPlans(): Promise<CloudPlans | null> {
|
||||
*/
|
||||
interface Offer {
|
||||
canBuy: boolean;
|
||||
storePrices: Map<string, string> | null;
|
||||
storeProducts: Map<string, IapProduct> | null;
|
||||
canShowTrial: boolean;
|
||||
/** Orders are closed and this is no test build: the plans show, nothing is for sale yet. */
|
||||
opensSoon: boolean;
|
||||
}
|
||||
|
||||
function offerFor(offer: Offer, plan: CloudPlan, interval: 'month' | 'year'): { label: string; buyable: boolean } {
|
||||
if (!offer.canBuy) return { label: '', buyable: false };
|
||||
function offerFor(
|
||||
offer: Offer,
|
||||
plan: CloudPlan,
|
||||
interval: 'month' | 'year'
|
||||
): { label: string; buyable: boolean; freeTrial: boolean } {
|
||||
if (!offer.canBuy) return { label: '', buyable: false, freeTrial: false };
|
||||
if (iap !== fakeIapClient) {
|
||||
const label = offer.storePrices?.get(productIdFor(plan.id, interval)) ?? '';
|
||||
return { label, buyable: label !== '' };
|
||||
const product = offer.storeProducts?.get(productIdFor(plan.id, interval));
|
||||
const label = product?.displayPrice ?? '';
|
||||
return {
|
||||
label,
|
||||
buyable: label !== '',
|
||||
freeTrial: label !== '' && offer.canShowTrial && product?.freeTrial === true,
|
||||
};
|
||||
}
|
||||
const cents = interval === 'year' ? plan.yearlyCents : plan.monthlyCents;
|
||||
const currency = plan.currency ?? 'EUR';
|
||||
const label =
|
||||
typeof cents === 'number' ? new Intl.NumberFormat(locale, { style: 'currency', currency }).format(cents / 100) : '';
|
||||
return { label, buyable: label !== '' };
|
||||
return { label, buyable: label !== '', freeTrial: false };
|
||||
}
|
||||
|
||||
function storeIds(plans: CloudPlan[]): string[] {
|
||||
return plans.flatMap((p) => [productIdFor(p.id, 'month'), productIdFor(p.id, 'year')]);
|
||||
}
|
||||
|
||||
async function availableWelcomeTrial(): Promise<boolean> {
|
||||
if (iap === fakeIapClient || !(await iapReady)) return false;
|
||||
const list = await fetchPlans();
|
||||
if (!list?.open) return false;
|
||||
const products = await iap.products(storeIds(list.plans));
|
||||
return Array.from(products.values()).some((product) => product.freeTrial && product.displayPrice !== '');
|
||||
}
|
||||
|
||||
async function loadOffer(list: CloudPlans): Promise<Offer> {
|
||||
@@ -755,9 +784,13 @@ async function loadOffer(list: CloudPlans): Promise<Offer> {
|
||||
const sandbox = list.closed && !onPlay ? await iap.isSandbox().catch(() => false) : false;
|
||||
const soon = opensSoon({ available, closed: list.closed, sandbox, onPlay });
|
||||
const canBuy = available && !soon;
|
||||
if (!canBuy || iap === fakeIapClient) return { canBuy, storePrices: null, opensSoon: soon };
|
||||
const ids = list.plans.flatMap((p) => [productIdFor(p.id, 'month'), productIdFor(p.id, 'year')]);
|
||||
return { canBuy, storePrices: await iap.prices(ids), opensSoon: soon };
|
||||
if (!canBuy || iap === fakeIapClient) return { canBuy, storeProducts: null, canShowTrial: false, opensSoon: soon };
|
||||
return {
|
||||
canBuy,
|
||||
storeProducts: await iap.products(storeIds(list.plans)),
|
||||
canShowTrial: list.open === true,
|
||||
opensSoon: soon,
|
||||
};
|
||||
}
|
||||
|
||||
/** The plan the picker starts on and marks: the smallest one with AI documents, the one most trips want. */
|
||||
@@ -782,6 +815,14 @@ function per(interval: 'month' | 'year'): string {
|
||||
return interval === 'year' ? t('native.plans.perYear') : t('native.plans.perMonth');
|
||||
}
|
||||
|
||||
function trialPrice(price: string, interval: 'month' | 'year'): string {
|
||||
return t('native.trial.price', { price, period: per(interval) });
|
||||
}
|
||||
|
||||
function trialCancellation(): string {
|
||||
return t(onPlay ? 'native.trial.cancelPlay' : 'native.trial.cancelApple');
|
||||
}
|
||||
|
||||
/** The restore button of the plans on screen, which redeems a payment that cleared later. */
|
||||
let resumeRestore: (() => void) | null = null;
|
||||
let watchingTransactions = false;
|
||||
@@ -852,7 +893,7 @@ function restoreButton(plans: CloudPlan[]): HTMLButtonElement {
|
||||
function showPlans(opts: { back?: boolean } = {}): void {
|
||||
let interval: 'month' | 'year' = chosenInterval;
|
||||
let plans: CloudPlan[] = [];
|
||||
let offer: Offer = { canBuy: false, storePrices: null, opensSoon: false };
|
||||
let offer: Offer = { canBuy: false, storeProducts: null, canShowTrial: false, opensSoon: false };
|
||||
let selectedId: string | null = null;
|
||||
|
||||
const tiers = el('div', { className: 'tiers', role: 'radiogroup', hidden: true });
|
||||
@@ -876,9 +917,11 @@ function showPlans(opts: { back?: boolean } = {}): void {
|
||||
const pricePer = el('span', {});
|
||||
const price = el('div', { className: 'price' }, priceValue, pricePer);
|
||||
const equals = el('div', { className: 'equals' });
|
||||
const pricebox = el('div', { className: 'pricebox' }, cycle, price, equals);
|
||||
const trial = el('p', { className: 'trial-price', hidden: true });
|
||||
const pricebox = el('div', { className: 'pricebox' }, cycle, price, equals, trial);
|
||||
const specs = el('div', { className: 'specs' });
|
||||
const foot = el('div', { className: 'pad', hidden: true });
|
||||
const legal = el('p', { className: 'legal' });
|
||||
|
||||
const loading = el('div', { className: 'loading' }, el('span', { className: 'spin' }));
|
||||
const details = el('div', { className: 'stack', hidden: true }, trip, pricebox, specs);
|
||||
@@ -943,7 +986,7 @@ function showPlans(opts: { back?: boolean } = {}): void {
|
||||
text.textContent = tripText;
|
||||
text.hidden = !tripText;
|
||||
|
||||
const { label, buyable } = interval === 'month' ? month : year;
|
||||
const { label, buyable, freeTrial } = interval === 'month' ? month : year;
|
||||
// No price yet (orders not open): the plan shows without one rather than with an empty one.
|
||||
price.hidden = !label;
|
||||
pricePer.textContent = per(interval);
|
||||
@@ -955,13 +998,18 @@ function showPlans(opts: { back?: boolean } = {}): void {
|
||||
yearBtn.disabled = !year.label;
|
||||
const yearAmount = amountIn(year.label);
|
||||
equals.textContent =
|
||||
!label || !year.label
|
||||
!label || !year.label || freeTrial
|
||||
? ''
|
||||
: interval === 'month'
|
||||
? t('native.plans.yearlyIs', { price: year.label })
|
||||
: yearAmount
|
||||
? t('native.plans.monthlyIs', { price: yearAmount.render(yearAmount.value / 12) })
|
||||
: '';
|
||||
trial.hidden = !freeTrial;
|
||||
trial.textContent = freeTrial ? trialPrice(label, interval) : '';
|
||||
legal.textContent = freeTrial
|
||||
? trialCancellation()
|
||||
: t(onPlay ? 'native.plans.legalAndroid' : 'native.plans.legal');
|
||||
|
||||
const gb = plan.storageGb >= 1000 ? `${count(plan.storageGb / 1000)} TB` : `${count(plan.storageGb)} GB`;
|
||||
const notIncluded = t('native.plans.aiNone');
|
||||
@@ -1042,7 +1090,7 @@ function showPlans(opts: { back?: boolean } = {}): void {
|
||||
showSubscribed();
|
||||
return;
|
||||
}
|
||||
showClaim(plan, selectedInterval, price.label);
|
||||
showClaim(plan, selectedInterval, price.label, { freeTrial: price.freeTrial });
|
||||
});
|
||||
|
||||
const load = async () => {
|
||||
@@ -1083,7 +1131,7 @@ function showPlans(opts: { back?: boolean } = {}): void {
|
||||
);
|
||||
} else {
|
||||
foot.replaceChildren(
|
||||
el('p', { className: 'legal' }, t(onPlay ? 'native.plans.legalAndroid' : 'native.plans.legal')),
|
||||
legal,
|
||||
el(
|
||||
'div',
|
||||
{ className: 'legal-links' },
|
||||
@@ -1195,7 +1243,7 @@ function showClaim(
|
||||
plan: Pick<CloudPlan, 'id' | 'name'>,
|
||||
interval: 'month' | 'year',
|
||||
priceLabel: string,
|
||||
opts: { back?: boolean; bought?: Purchase } = {}
|
||||
opts: { back?: boolean; bought?: Purchase; freeTrial?: boolean } = {}
|
||||
): void {
|
||||
/** A purchase that went through but whose redeem did not: a retry redeems it again instead of buying twice. */
|
||||
let bought: Purchase | null = opts.bought ?? null;
|
||||
@@ -1300,7 +1348,11 @@ function showClaim(
|
||||
void loadConsent();
|
||||
|
||||
const subscribeLabel = () =>
|
||||
bought ? t('native.claim.completeSetup') : t('native.claim.subscribe', { price: priceLabel });
|
||||
bought
|
||||
? t('native.claim.completeSetup')
|
||||
: opts.freeTrial
|
||||
? t('native.trial.badge')
|
||||
: t('native.claim.subscribe', { price: priceLabel });
|
||||
const submit = el('button', { type: 'button', className: 'cta' }, subscribeLabel());
|
||||
// What is about to be bought, so nobody pays for a plan they did not mean.
|
||||
const change = el('button', { type: 'button' }, t('native.claim.change'));
|
||||
@@ -1315,7 +1367,13 @@ function showClaim(
|
||||
'span',
|
||||
{},
|
||||
el('b', {}, plan.name),
|
||||
...(priceLabel ? [el('small', { dir: 'ltr' }, `${priceLabel} ${per(interval)}`)] : [])
|
||||
...(priceLabel
|
||||
? [
|
||||
opts.freeTrial
|
||||
? el('small', {}, trialPrice(priceLabel, interval))
|
||||
: el('small', { dir: 'ltr' }, `${priceLabel} ${per(interval)}`),
|
||||
]
|
||||
: [])
|
||||
),
|
||||
// A purchase already made is not a choice any more.
|
||||
...(bought ? [] : [change])
|
||||
@@ -1349,7 +1407,12 @@ function showClaim(
|
||||
overlay: [card.node],
|
||||
content: [
|
||||
form,
|
||||
el('div', { className: 'dock' }, submit, el('p', { className: 'fine' }, t('native.claim.renews'))),
|
||||
el(
|
||||
'div',
|
||||
{ className: 'dock' },
|
||||
submit,
|
||||
el('p', { className: 'fine' }, opts.freeTrial ? trialCancellation() : t('native.claim.renews'))
|
||||
),
|
||||
],
|
||||
},
|
||||
opts
|
||||
@@ -1460,7 +1523,13 @@ function showClaim(
|
||||
};
|
||||
|
||||
if (!bought) {
|
||||
const outcome = await iap.purchase(productIdFor(plan.id, interval));
|
||||
const trialRenewalPrice = opts.freeTrial ? priceLabel : undefined;
|
||||
const outcome = await iap.purchase(productIdFor(plan.id, interval), trialRenewalPrice);
|
||||
if (outcome.kind === 'offer-changed') {
|
||||
restore();
|
||||
setNotice(error, t('native.trial.changed'));
|
||||
return;
|
||||
}
|
||||
if (outcome.kind === 'cancelled') {
|
||||
restore();
|
||||
setNotice(error, t('native.claim.cancelled'));
|
||||
|
||||
@@ -628,6 +628,33 @@ h1 {
|
||||
font: 700 17px var(--text-face);
|
||||
box-shadow: 0 14px 28px -14px rgba(17, 24, 39, 0.55);
|
||||
}
|
||||
.discover {
|
||||
position: relative;
|
||||
padding-inline: 16px;
|
||||
}
|
||||
.trial-badge {
|
||||
position: absolute;
|
||||
inset-block-start: -10px;
|
||||
inset-inline-end: 10px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
max-inline-size: calc(100% - 20px);
|
||||
padding: 3px 8px;
|
||||
border: 1px solid #bee3d7;
|
||||
border-radius: 999px;
|
||||
background: #e7f6ef;
|
||||
color: #195247;
|
||||
box-shadow: 0 2px 6px #11182720;
|
||||
font: 700 11px/1.4 var(--text-face);
|
||||
}
|
||||
.trial-price {
|
||||
margin: 6px 12px 0;
|
||||
color: #195247;
|
||||
text-align: center;
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
}
|
||||
.cta2 {
|
||||
min-height: 52px;
|
||||
background: #fff;
|
||||
|
||||
Reference in New Issue
Block a user