Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: fix plan changing if subscription is trialing

Abdullah Atta827930e28f11
diff --git a/apps/web/src/dialogs/buy-dialog/change-plan-dialog.tsx b/apps/web/src/dialogs/buy-dialog/change-plan-dialog.tsx
index ce3c7d62b..e603a1836 100644
--- a/apps/web/src/dialogs/buy-dialog/change-plan-dialog.tsx
+++ b/apps/web/src/dialogs/buy-dialog/change-plan-dialog.tsx
@@ -24,7 +24,7 @@ import { getAllPlans, PERIOD_METADATA, PLAN_METADATA } from "./plans";
import { SelectComponent } from "../settings";
import { useStore as useUserStore } from "../../stores/user-store";
import { db } from "../../common/db";
-import { useState } from "react";
+import { useCallback, useEffect, useState } from "react";
import dayjs from "dayjs";
import { showToast } from "../../utils/toast";
import { usePromise } from "@notesnook/common";
@@ -34,6 +34,47 @@ import { getCurrencySymbol } from "../../common/currencies";
export type ChangePlanDialogProps = {
onClose: () => void;
+ selectedPlan?: string;
+};
+
+type ChangePreviewResponse = {
+ update_summary?: {
+ result: {
+ action: "charge";
+ };
+ charge: {
+ amount: string;
+ };
+ credit: { amount: string };
+ };
+ immediate_transaction?: { details: Details };
+ recurring_transaction_details: Details;
+ next_transaction: {
+ billing_period: BillingPeriod;
+ details: Details;
+ };
+ currency_code: string;
+ billing_cycle: {
+ interval: "month" | "year";
+ };
+};
+type BillingPeriod = {
+ ends_at: string;
+ starts_at: string;
+};
+type Totals = {
+ total: number;
+ balance: number;
+};
+type Details = {
+ line_items: LineItem[];
+ totals: Totals;
+};
+type LineItem = {
+ proration: Proration;
+};
+type Proration = {
+ billing_period: BillingPeriod;
};
export const ChangePlanDialog = DialogManager.register(
@@ -44,8 +85,127 @@ export const ChangePlanDialog = DialogManager.register(
const [changeSummary, setChangeSummary] = useState<
{ title: string; amount: string }[]
>([]);
- const [selectedPlan, setSelectedPlan] = useState<string>();
+ const [selectedPlan, setSelectedPlan] = useState(props.selectedPlan);
const [loading, setLoading] = useState(false);
+ const [error, setError] = useState<Error>();
+
+ const changePlan = useCallback(async (id: string) => {
+ setLoading(true);
+ setChangeSummary([]);
+ try {
+ const response = (await db.subscriptions.preview(
+ id
+ )) as ChangePreviewResponse | null;
+ if (!response) return;
+
+ const {
+ update_summary,
+ immediate_transaction,
+ recurring_transaction_details,
+ next_transaction,
+ currency_code: currencyCode,
+ billing_cycle
+ } = response;
+ const proration = immediate_transaction?.details.line_items.find(
+ (l) => !!l.proration
+ )?.proration;
+ const recurringBalance = recurring_transaction_details.totals.balance;
+ const recurringTotal = recurring_transaction_details.totals.total;
+
+ const planPrice = update_summary
+ ? parseInt(update_summary?.charge.amount || "0")
+ : recurringTotal;
+
+ const balance =
+ recurringTotal -
+ recurringBalance +
+ parseInt(update_summary?.credit.amount || "0") * -1;
+ const charge = planPrice - balance;
+
+ const summary: { title: string; amount: string }[] = [
+ {
+ title: "Plan price",
+ amount: formatPrice(planPrice, currencyCode)
+ },
+ {
+ title: "Balance from current subscription",
+ amount: formatPrice(balance, currencyCode)
+ }
+ ];
+ if (!update_summary) {
+ const chargeStartDate = dayjs(
+ next_transaction.billing_period.starts_at
+ );
+ const amount = next_transaction.details.totals.total;
+ summary.push({
+ title: `Charged ${chargeStartDate.format("YYYY-MM-DD")}`,
+ amount: formatPrice(amount, currencyCode)
+ });
+ } else if (update_summary.result.action === "charge") {
+ summary.push(
+ {
+ title: "Charged today",
+ amount: formatPrice(charge, currencyCode)
+ },
+ {
+ title: `Charged ${dayjs(
+ next_transaction.billing_period.starts_at
+ ).format("YYYY-MM-DD")}`,
+ amount: formatPrice(recurringTotal, currencyCode)
+ }
+ );
+ } else {
+ const nextCharge = recurringTotal - charge;
+ summary.push({
+ title: "Charged today",
+ amount: formatPrice(0, currencyCode)
+ });
+ if (nextCharge > 0 && proration) {
+ summary.push({
+ title: `Charged ${dayjs(proration.billing_period.ends_at).format(
+ "YYYY-MM-DD"
+ )}`,
+ amount: formatPrice(nextCharge, currencyCode)
+ });
+ summary.push({
+ title: `Charged ${dayjs(proration.billing_period.ends_at)
+ .add(1, billing_cycle.interval)
+ .format("YYYY-MM-DD")}`,
+ amount: formatPrice(planPrice, currencyCode)
+ });
+ } else {
+ const freeIntervals = Math.floor(charge / planPrice);
+ const amount = planPrice - (charge - planPrice * freeIntervals);
+ const chargeStartDate = dayjs(
+ next_transaction.billing_period.starts_at
+ ).add(freeIntervals, billing_cycle.interval);
+ summary.push(
+ {
+ title: `Charged ${chargeStartDate.format("YYYY-MM-DD")}`,
+ amount: formatPrice(amount, currencyCode)
+ },
+ {
+ title: `Charged ${chargeStartDate
+ .add(1, billing_cycle.interval)
+ .format("YYYY-MM-DD")}`,
+ amount: formatPrice(planPrice, currencyCode)
+ }
+ );
+ }
+ }
+ setChangeSummary(summary);
+ } catch (e) {
+ console.error(e);
+ setError(e as Error);
+ } finally {
+ setLoading(false);
+ }
+ }, []);
+
+ useEffect(() => {
+ if (!props.selectedPlan) return;
+ changePlan(props.selectedPlan);
+ }, [props.selectedPlan, changePlan]);
return (
<BaseDialog
@@ -61,6 +221,10 @@ export const ChangePlanDialog = DialogManager.register(
setLoading(true);
try {
await db.subscriptions.change(selectedPlan);
+ showToast(
+ "success",
+ "Subscription changed successfully. It might take a couple of minutes for the changes to reflect in the app."
+ );
onClose();
} catch (e) {
showToast("error", (e as Error).message);
@@ -74,7 +238,9 @@ export const ChangePlanDialog = DialogManager.register(
onClick: onClose
}}
>
- {plans.status === "rejected" ? (
+ {error ? (
+ <ErrorText error={error} />
+ ) : plans.status === "rejected" ? (
<ErrorText error={plans.reason} />
) : (
<Flex sx={{ flexDirection: "column" }}>
@@ -97,103 +263,7 @@ export const ChangePlanDialog = DialogManager.register(
const plan = plans.value?.find((p) => p.id === id);
if (!plan) return;
setSelectedPlan(id);
- setLoading(true);
- setChangeSummary([]);
- try {
- const {
- data: {
- update_summary,
- immediate_transaction,
- recurring_transaction_details,
- next_transaction,
- currency_code: currencyCode,
- billing_cycle
- }
- } = await db.subscriptions.preview(id);
- const proration =
- immediate_transaction.details.line_items.find(
- (l: any) => !!l.proration
- ).proration;
- const planPrice = parseInt(update_summary.charge.amount);
- const balance =
- recurring_transaction_details.totals.total -
- recurring_transaction_details.totals.balance +
- update_summary.credit.amount * -1;
- const charge = planPrice - balance;
- const recurringTotal = parseInt(
- recurring_transaction_details.totals.total
- );
- const summary: { title: string; amount: string }[] = [
- {
- title: "Plan price",
- amount: formatPrice(planPrice, currencyCode)
- },
- {
- title: "Balance from current subscription",
- amount: formatPrice(balance, currencyCode)
- }
- ];
- if (update_summary.result.action === "charge") {
- summary.push(
- {
- title: "Charged today",
- amount: formatPrice(charge, currencyCode)
- },
- {
- title: `Charged ${dayjs(
- next_transaction.billing_period.starts_at
- ).format("YYYY-MM-DD")}`,
- amount: formatPrice(recurringTotal, currencyCode)
- }
- );
- } else {
- const nextCharge = recurringTotal - charge;
- summary.push({
- title: "Charged today",
- amount: formatPrice(0, currencyCode)
- });
- if (nextCharge > 0 && proration) {
- summary.push({
- title: `Charged ${dayjs(
- proration.billing_period.ends_at
- ).format("YYYY-MM-DD")}`,
- amount: formatPrice(nextCharge, currencyCode)
- });
- summary.push({
- title: `Charged ${dayjs(
- proration.billing_period.ends_at
- )
- .add(1, billing_cycle.interval)
- .format("YYYY-MM-DD")}`,
- amount: formatPrice(planPrice, currencyCode)
- });
- } else {
- const freeIntervals = Math.floor(charge / planPrice);
- const amount =
- planPrice - (charge - planPrice * freeIntervals);
- const chargeStartDate = dayjs(
- next_transaction.billing_period.starts_at
- ).add(freeIntervals, billing_cycle.interval);
- summary.push(
- {
- title: `Charged ${chargeStartDate.format(
- "YYYY-MM-DD"
- )}`,
- amount: formatPrice(amount, currencyCode)
- },
- {
- title: `Charged ${chargeStartDate
- .add(1, billing_cycle.interval)
- .format("YYYY-MM-DD")}`,
- amount: formatPrice(planPrice, currencyCode)
- }
- );
- }
- }
- setChangeSummary(summary);
- } finally {
- setLoading(false);
- }
+ await changePlan(id);
}}
/>
)}
@@ -215,10 +285,6 @@ export const ChangePlanDialog = DialogManager.register(
</Text>
</Flex>
))}
- <Text variant="subBody">
- Note: These are estimated amounts. The real amounts could
- differ slightly.
- </Text>
</Flex>
) : null}
</Flex>
diff --git a/apps/web/src/dialogs/buy-dialog/upgrade-dialog.tsx b/apps/web/src/dialogs/buy-dialog/upgrade-dialog.tsx
index 5d45c8b11..35623f628 100644
--- a/apps/web/src/dialogs/buy-dialog/upgrade-dialog.tsx
+++ b/apps/web/src/dialogs/buy-dialog/upgrade-dialog.tsx
@@ -33,6 +33,9 @@ import { useCheckoutStore } from "./store";
import { BuyDialog } from "./buy-dialog";
import { ErrorText } from "../../components/error-text";
import { getCurrencySymbol } from "../../common/currencies";
+import { useStore as useUserStore } from "../../stores/user-store";
+import { ChangePlanDialog } from "./change-plan-dialog";
+import { Loading } from "../../components/icons";
export type UpgradeDialogProps = {
feature: FeatureResult<any>;
@@ -44,18 +47,17 @@ export const UpgradeDialog = DialogManager.register(function UpgradeDialog(
) {
const { onClose, feature } = props;
const { plans } = usePlans();
- const plan = plans.find(
+ const plan = plans?.find(
(p) =>
- (p.plan === feature.availableOn || SubscriptionPlan.PRO) &&
+ p.plan === (feature.availableOn || SubscriptionPlan.PRO) &&
p.period === "yearly"
- )!;
- const { title } = getFeature(feature.id);
+ );
+ const metadata = PLAN_METADATA[feature.availableOn || SubscriptionPlan.PRO];
+
return (
<BaseDialog
title={`Unlock this feature today`}
- description={`Upgrade to the ${
- PLAN_METADATA[plan?.plan].title
- } plan to use this feature`}
+ description={`Upgrade to the ${metadata.title} plan to use this feature`}
isOpen={true}
onClose={onClose}
textAlignment="center"
@@ -83,7 +85,7 @@ export const UpgradeDialog = DialogManager.register(function UpgradeDialog(
}}
>
<Text variant="body" sx={{ fontWeight: "bold", color: "heading" }}>
- {PLAN_METADATA[plan?.plan].title} plan
+ {metadata.title} plan
</Text>
{[
getFeature("storage"),
@@ -94,7 +96,9 @@ export const UpgradeDialog = DialogManager.register(function UpgradeDialog(
getFeature("activeReminders")
].map((f) => {
const caption =
- f.availability[planToAvailability(plan.plan)].caption;
+ f.availability[
+ planToAvailability(feature.availableOn || SubscriptionPlan.PRO)
+ ].caption;
return (
<Flex key={f.id} sx={{ justifyContent: "space-between" }}>
<Text
@@ -126,15 +130,26 @@ export const UpgradeDialog = DialogManager.register(function UpgradeDialog(
</Text>
<Button
variant="accent"
+ disabled={!plan}
onClick={() => {
+ if (!plan) return;
onClose();
- useCheckoutStore.getState().selectPlan(plan);
- BuyDialog.show({});
+ const subscription = useUserStore.getState().user?.subscription;
+ if (!subscription || subscription.plan === SubscriptionPlan.FREE) {
+ useCheckoutStore.getState().selectPlan(plan);
+ BuyDialog.show({});
+ } else ChangePlanDialog.show({ selectedPlan: plan.id });
}}
>
- Start your free trial {getCurrencySymbol(plan.currency)}
- {plan.price.gross}
- {formatRecurringPeriodShort(plan.period)}
+ {plan ? (
+ <>
+ Upgrade to {metadata.title} {getCurrencySymbol(plan.currency)}
+ {plan.price.gross}
+ {formatRecurringPeriodShort(plan.period)}
+ </>
+ ) : (
+ <Loading size={16} color="accentForeground" />
+ )}
</Button>
<Button
variant="tertiary"