Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions mobile/src/account/plans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { earlySupporterLabel, groupFeatures, savingsPct, type Cycle, type Plan }
import { POPULAR_PLAN } from "@notestack/shared/billing";
import * as WebBrowser from "expo-web-browser";
import type { ReactNode } from "react";
import { StyleSheet, Text, View } from "react-native";
import { Platform, StyleSheet, Text, View } from "react-native";
import { Pressable } from "@/ui/pressable";

import { Card, publicText } from "@/shell/public-page";
Expand Down Expand Up @@ -77,10 +77,33 @@ export function LegalLinks() {
);
}

/**
* The subscription terms the stores ask to see wherever the plans are shown with prices (the upgrade sheet, the
* pricing page): automatic renewal, where it is charged and how to cancel, with the Terms and Privacy links.
*/
export function SubscriptionTerms() {
const ios = Platform.OS === "ios";
const open = (path: string) => void WebBrowser.openBrowserAsync(`https://notestack.ai${path}`, { controlsColor: colors.blue });
return (
<Text style={styles.terms}>
Plans renew automatically at the price shown, monthly or yearly, until you cancel. Payment is charged to your{" "}
{ios ? "Apple ID" : "Google account"} at confirmation. Cancel at least 24 hours before the period ends in your{" "}
{ios ? "App Store account settings" : "Google Play subscriptions"}.{" "}
<Text style={styles.termsLink} onPress={() => open("/terms")}>
Terms of Service
</Text>{" "}
·{" "}
<Text style={styles.termsLink} onPress={() => open("/privacy")}>
Privacy Policy
</Text>
</Text>
);
}

/**
* One plan: its name, price and what it includes. `storePrice` is the App Store / Google Play price for this cycle
* (localized, the whole period's). It is the only price the app shows for a paid plan: without it (signed out, or the
* store's plans not loaded) the card has no price, since the stores don't allow pointing at other prices. `children`
* (localized, the whole period's). It is the only price the app shows for a paid plan: without it (the store's plans
* not loaded, or a build that cannot sell plans) the card has no price, since the stores don't allow pointing at other prices. `children`
* go under the features (the upgrade sheet's buttons). `introPrice` is an early supporter discount on the first payment
* (a store offer this person can get): it takes the price's place, with the usual one struck through beside it and
* the price it renews at spelled out, as the stores require.
Expand Down Expand Up @@ -165,6 +188,8 @@ export function PlanCard({
}

const styles = StyleSheet.create({
terms: { color: colors.text3, fontSize: 12.5, lineHeight: 18 },
termsLink: { color: colors.blue },
legal: { flexDirection: "row", gap: 8, marginTop: 6, paddingTop: 16, borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: colors.lineStrong },
legalItem: { flex: 1, flexDirection: "row", alignItems: "center", justifyContent: "center", gap: 8 },
legalLink: { flexShrink: 1, color: colors.white, fontSize: 13.5, lineHeight: 20 },
Expand Down
10 changes: 10 additions & 0 deletions mobile/src/account/purchases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export async function identify(customerId: string): Promise<void> {
customer = customerId;
}

/**
* Read-only access for someone not signed in (the public Pricing page): RevenueCat set up as an anonymous user, which
* is enough to read the store's prices. Never touches an identified customer; when this person signs in, identify()
* logs the anonymous user in as their workspace.
*/
export async function ensureConfigured(): Promise<void> {
if (!API_KEY || !purchasesReady || (await Purchases.isConfigured())) return;
Purchases.configure({ apiKey: API_KEY });
}

/** Sign out: the next person on this phone starts as nobody, not as the last workspace. */
export function forgetCustomer() {
if (!customer) return;
Expand Down
54 changes: 54 additions & 0 deletions mobile/src/account/store-prices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { Cycle } from "@notestack/shared/content/marketing";
import { useQuery } from "@tanstack/react-query";

import {
ensureConfigured,
getIntroOffers,
getPackages,
identify,
isStorePlan,
type PlanIntroOffers,
type PlanPackages,
purchasesReady,
} from "@/account/purchases";

export type StorePrices = { plans: PlanPackages; intros: PlanIntroOffers };

/**
* The plans for sale in this phone's store, with their localized prices and the early supporter offers this person
* can get. `customerId` is the signed-in workspace's RevenueCat customer (the upgrade sheet; undefined until the plan
* status has loaded); null reads them as an anonymous visitor (the public Pricing page). Off where the build cannot
* sell plans (no RevenueCat key: Expo web, a build without keys).
*/
export function useStorePrices(customerId: string | null | undefined) {
return useQuery({
queryKey: ["store", "packages", customerId ?? "anonymous"],
queryFn: async (): Promise<StorePrices> => {
if (customerId) await identify(customerId);
else await ensureConfigured();
const plans = await getPackages();
// An early supporter discount that can't be read just leaves the usual prices
const intros: PlanIntroOffers = await getIntroOffers(plans).catch(() => ({}));
return { plans, intros };
},
enabled: purchasesReady && customerId !== undefined,
staleTime: 10 * 60_000,
});
}

/** The price to show for a plan and cycle: what it renews at (an intro offer is shown separately), else the store's. */
export function priceFor(data: StorePrices | undefined, planId: string, cycle: Cycle): string | null {
if (!data || !isStorePlan(planId)) return null;
const key = `${planId}_${cycle}` as const;
return data.intros[key]?.renews ?? data.plans[key]?.product.priceString ?? null;
}

/**
* The early supporter price for a plan and cycle, when this phone's store account can get it (Google Play only lists
* offers the account is eligible for; on iOS the App Store is asked per Apple ID), else null. The card shows it in
* place of the price, with the usual price struck through and the renewal price spelled out.
*/
export function introFor(data: StorePrices | undefined, planId: string, cycle: Cycle): string | null {
if (!data || !isStorePlan(planId)) return null;
return data.intros[`${planId}_${cycle}`]?.price ?? null;
}
37 changes: 7 additions & 30 deletions mobile/src/account/upgrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ import { api, PLAN_LIMIT_EVENT, type PlanLimitDetail } from "@notestack/shared/c
import { FALLBACK_PLANS, type Cycle, type Plan } from "@notestack/shared/content/marketing";
import type { BillingStatus } from "@notestack/shared/types";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import * as WebBrowser from "expo-web-browser";
import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from "react";
import { Platform, StyleSheet, Text, View } from "react-native";

import { planQuery } from "@/account/plan";
import { track } from "@/lib/analytics";
import { on } from "@/lib/platform";
import { CycleToggle, defaultPlanTab, PlanCard, PlanTabs } from "@/account/plans";
import { buy, getIntroOffers, getPackages, identify, type PlanIntroOffers, isStorePlan, manageSubscriptions, purchasesReady, restore } from "@/account/purchases";
import { CycleToggle, defaultPlanTab, PlanCard, PlanTabs, SubscriptionTerms } from "@/account/plans";
import { buy, identify, isStorePlan, manageSubscriptions, purchasesReady, restore } from "@/account/purchases";
import { useStorePrices } from "@/account/store-prices";
import { colors } from "@/ui/theme";
import { ErrorText, Loading } from "@/ui";
import { PillButton, Sheet } from "@/ui/widgets";

const SITE = "https://notestack.ai";
const STORE = Platform.OS === "ios" ? "the App Store" : "Google Play";
/** Where this app sells and manages plans: its phone's store. */
const HERE: BillingPlace = Platform.OS === "ios" ? "app_store" : "play_store";
Expand Down Expand Up @@ -92,18 +91,9 @@ function Paywall({ opening }: { opening: Opening }) {
const status = useQuery(planQuery);
const plansQuery = useQuery({ queryKey: ["plans"], queryFn: () => api<{ billing_enabled: boolean; plans: Plan[] }>("/api/billing/plans") });
const customerId = status.data?.store_customer_id;
const packages = useQuery({
queryKey: ["store", "packages", customerId],
queryFn: async () => {
await identify(customerId!);
const plans = await getPackages();
// An early supporter discount that can't be read just leaves the usual prices
const intros: PlanIntroOffers = await getIntroOffers(plans).catch(() => ({}));
return { plans, intros };
},
enabled: purchasesReady && Boolean(customerId),
staleTime: 10 * 60_000,
});
// Signed in, prices are read as this workspace's customer, never anonymously: a purchase must land on the workspace.
// No customer id yet (or none) keeps the query off.
const packages = useStorePrices(customerId || undefined);

const plans = plansQuery.data?.plans?.length ? plansQuery.data.plans : FALLBACK_PLANS;
const shownId = tab && plans.some((p) => p.id === tab) ? tab : defaultPlanTab(plans);
Expand Down Expand Up @@ -229,18 +219,7 @@ function Paywall({ opening }: { opening: Opening }) {
) : null}

{/* The subscription terms the stores ask to see wherever the plans are shown */}
<Text style={styles.terms}>
Plans renew automatically at the price shown, monthly or yearly, until you cancel. Payment is charged to your{" "}
{Platform.OS === "ios" ? "Apple ID" : "Google account"} at confirmation. Cancel at least 24 hours before the
period ends in your {Platform.OS === "ios" ? "App Store account settings" : "Google Play subscriptions"}.{" "}
<Text style={styles.link} onPress={() => void WebBrowser.openBrowserAsync(`${SITE}/terms`, { controlsColor: colors.blue })}>
Terms of Service
</Text>{" "}
·{" "}
<Text style={styles.link} onPress={() => void WebBrowser.openBrowserAsync(`${SITE}/privacy`, { controlsColor: colors.blue })}>
Privacy Policy
</Text>
</Text>
<SubscriptionTerms />
</View>
);
}
Expand Down Expand Up @@ -272,6 +251,4 @@ const styles = StyleSheet.create({
notice: { color: colors.blue, fontSize: 15, fontWeight: "600" },
buy: { marginTop: 12, gap: 10, alignItems: "flex-start" },
row: { flexDirection: "row", flexWrap: "wrap", gap: 10 },
terms: { color: colors.text3, fontSize: 12.5, lineHeight: 18 },
link: { color: colors.blue },
});
30 changes: 24 additions & 6 deletions mobile/src/app/(public)/pricing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { router } from "expo-router";
import { useState } from "react";
import { StyleSheet, View } from "react-native";

import { CycleToggle, defaultPlanTab, LegalLinks, PlanCard, PlanTabs } from "@/account/plans";
import { CycleToggle, defaultPlanTab, LegalLinks, PlanCard, PlanTabs, SubscriptionTerms } from "@/account/plans";
import { introFor, priceFor, useStorePrices } from "@/account/store-prices";
import { Eyebrow, PageTitle, PublicPage } from "@/shell/public-page";
import { Button } from "@/ui";

/**
* The plans, as on the landing page's pricing section, for people not signed in: what each plan includes, then Start
* free. No prices here: the app shows only the store's own prices, and those load in the upgrade sheet once signed in
* (src/account/upgrade.tsx), where plans are bought with an in-app purchase.
* The plans, as on the landing page's pricing section, for people not signed in: what each plan includes, its price,
* then Start free. The prices are the store's own (App Store / Google Play, localized), read through RevenueCat as an
* anonymous visitor; the app shows no other prices. An early supporter offer this phone's store account can get is
* shown the same way as in the upgrade sheet (offer price, usual price struck through, then the renewal price). Plans are bought in the upgrade sheet once signed in
* (src/account/upgrade.tsx). Until the store answers, or where it can't (no RevenueCat key), the cards show no price.
*/
export default function Pricing() {
const [cycle, setCycle] = useState<Cycle>("annual");
Expand All @@ -24,6 +27,7 @@ export default function Pricing() {
});
const plans = data?.plans?.length ? data.plans : FALLBACK_PLANS;
const shownId = tab && plans.some((p) => p.id === tab) ? tab : defaultPlanTab(plans);
const prices = useStorePrices(null);

return (
<PublicPage>
Expand All @@ -36,18 +40,32 @@ export default function Pricing() {
{plans
.filter((plan) => plan.id === shownId)
.map((plan) => (
<PlanCard key={plan.id} plan={plan} cycle={cycle} />
<PlanCard
key={plan.id}
plan={plan}
cycle={cycle}
storePrice={priceFor(prices.data, plan.id, cycle)}
introPrice={introFor(prices.data, plan.id, cycle)}
/>
))}

<View style={styles.cta}>
<Button title="Start free" onPress={() => router.navigate("/sign-up")} />
</View>

<LegalLinks />
{/* Prices are shown: so are the subscription terms the stores ask for beside them */}
{prices.data ? (
<View style={styles.terms}>
<SubscriptionTerms />
</View>
) : (
<LegalLinks />
)}
</PublicPage>
);
}

const styles = StyleSheet.create({
cta: { marginTop: 16 },
terms: { marginTop: 16 },
});
Loading