Skip to content

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

web: fix checkout

Abdullah Atta6921fcb3ae97
diff --git a/apps/web/src/dialogs/buy-dialog/buy-dialog.tsx b/apps/web/src/dialogs/buy-dialog/buy-dialog.tsx
index 766a54650..aa6378197 100644
--- a/apps/web/src/dialogs/buy-dialog/buy-dialog.tsx
+++ b/apps/web/src/dialogs/buy-dialog/buy-dialog.tsx
@@ -34,7 +34,7 @@ import {
PlansList
} from "./plan-list";
import { useCheckoutStore } from "./store";
-import { getCurrencySymbol } from "./helpers";
+import { getCurrencySymbol, toPricingInfo } from "./helpers";
import { isMacStoreApp } from "../../utils/platform";
import { isUserSubscribed } from "../../hooks/use-is-user-premium";
import { SUBSCRIPTION_STATUS } from "../../common/constants";
@@ -413,32 +413,7 @@ type SelectedPlanProps = {
onChangePlan?: () => void;
};
function SelectedPlan(props: SelectedPlanProps) {
- const {
- plan,
- pricingInfo = {
- country: plan.country,
- period: plan.period,
- price: {
- currency: plan.currency,
- id: plan.id,
- period: plan.period,
- subtotal: `${getCurrencySymbol(plan.currency)}${plan.price.net}`,
- tax: `${getCurrencySymbol(plan.currency)}${plan.price.tax}`,
- total: `${getCurrencySymbol(plan.currency)}${plan.price.gross}`
- },
- discount: plan.discount,
- coupon: plan.discount?.code,
- recurringPrice: {
- currency: plan.currency,
- id: plan.id,
- period: plan.period,
- subtotal: `${getCurrencySymbol(plan.currency)}${plan.price.net}`,
- tax: `${getCurrencySymbol(plan.currency)}${plan.price.tax}`,
- total: `${getCurrencySymbol(plan.currency)}${plan.price.gross}`
- }
- },
- onChangePlan
- } = props;
+ const { plan, pricingInfo = toPricingInfo(plan), onChangePlan } = props;
const selectPlan = useCheckoutStore((store) => store.selectPlan);
const upsellDetails = usePromise(() => getUpsellDetails(plan), [plan]);
diff --git a/apps/web/src/dialogs/buy-dialog/helpers.ts b/apps/web/src/dialogs/buy-dialog/helpers.ts
index 750d0cd79..7f1b30a57 100644
--- a/apps/web/src/dialogs/buy-dialog/helpers.ts
+++ b/apps/web/src/dialogs/buy-dialog/helpers.ts
@@ -22,6 +22,8 @@ import {
ICurrencySymbols
} from "@brixtol/currency-symbols";
import { getFeature } from "@notesnook/common";
+import { Plan } from "@notesnook/core";
+import { PricingInfo } from "./types";
export const IS_DEV = import.meta.env.DEV || IS_TESTING;
export function getCurrencySymbol(currency: string) {
@@ -41,6 +43,33 @@ export function parseAmount(amount: string) {
export const FEATURE_HIGHLIGHTS = [
getFeature("storage"),
getFeature("fileSize"),
+ getFeature("colors"),
+ getFeature("notebooks"),
getFeature("fullQualityImages"),
getFeature("appLock")
];
+
+export function toPricingInfo(plan: Plan): PricingInfo {
+ return {
+ country: plan.country,
+ period: plan.period,
+ price: {
+ currency: plan.currency,
+ id: plan.id,
+ period: plan.period,
+ subtotal: `${getCurrencySymbol(plan.currency)}${plan.price.net}`,
+ tax: `${getCurrencySymbol(plan.currency)}${plan.price.tax}`,
+ total: `${getCurrencySymbol(plan.currency)}${plan.price.gross}`
+ },
+ discount: plan.discount,
+ coupon: plan.discount?.code,
+ recurringPrice: {
+ currency: plan.currency,
+ id: plan.id,
+ period: plan.period,
+ subtotal: `${getCurrencySymbol(plan.currency)}${plan.price.net}`,
+ tax: `${getCurrencySymbol(plan.currency)}${plan.price.tax}`,
+ total: `${getCurrencySymbol(plan.currency)}${plan.price.gross}`
+ }
+ };
+}
diff --git a/apps/web/src/dialogs/buy-dialog/plans.ts b/apps/web/src/dialogs/buy-dialog/plans.ts
index 8464e80dc..bdc5377e9 100644
--- a/apps/web/src/dialogs/buy-dialog/plans.ts
+++ b/apps/web/src/dialogs/buy-dialog/plans.ts
@@ -18,21 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { useEffect, useState } from "react";
-import { Period, Plan, PlanMetadata, Price } from "./types";
+import { Plan, PlanMetadata } from "./types";
import { IS_DEV } from "./helpers";
-import { SubscriptionPlan } from "@notesnook/core";
+import { Period, SubscriptionPlan } from "@notesnook/core";
import { strings } from "@notesnook/intl";
-
-// function createPrice(id: string, period: Period, subtotal: number): Price {
-// return {
-// id,
-// period,
-// subtotal: `${subtotal.toFixed(2)}USD`,
-// total: `0.00USD`,
-// tax: `0.00USD`,
-// currency: "USD"
-// };
-// }
+import { db } from "../../common/db";
function createPlan(
id: string,
@@ -172,10 +162,7 @@ export async function getPlans(): Promise<Plan[] | null> {
if (IS_TESTING || import.meta.env.DEV) return DEFAULT_PLANS;
if (CACHED_PLANS) return CACHED_PLANS;
- const url = `http://localhost:8788/api/v2/prices/products/web`;
- const response = await fetch(url);
- if (!response.ok) return null;
- const plans = (await response.json()) as Plan[];
+ const plans = await db.pricing.products();
CACHED_PLANS = plans.sort((a, b) => a.plan - b.plan);
return plans;
}
@@ -192,8 +179,8 @@ export function usePlans() {
const plans = await getPlans();
if (!plans) return;
setPlans(plans);
- // setDiscount(Math.max(...plans.map((p) => p.discount?.amount || 0)));
- // setCountry(plans[0].country);
+ setDiscount(Math.max(...plans.map((p) => p.discount?.amount || 0)));
+ setCountry(plans[0].country);
} catch (e) {
console.error(e);
} finally {
diff --git a/apps/web/src/dialogs/buy-dialog/types.ts b/apps/web/src/dialogs/buy-dialog/types.ts
index a320ba431..226143f4f 100644
--- a/apps/web/src/dialogs/buy-dialog/types.ts
+++ b/apps/web/src/dialogs/buy-dialog/types.ts
@@ -17,8 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { FeatureId } from "@notesnook/common";
-import { SubscriptionPlan } from "@notesnook/core";
+import { Discount, Period, PlanPrice, SubscriptionPlan } from "@notesnook/core";
import {
CheckoutEventNames,
CheckoutEventsCustomer,
@@ -27,8 +26,6 @@ import {
PaddleEventData
} from "@paddle/paddle-js";
-export type Period = "monthly" | "yearly" | "5-year";
-
// export interface CallbackData {
// checkout?: Checkout;
// }
@@ -62,13 +59,6 @@ export type PlanMetadata = Record<
}
>;
-export interface PlanPrice {
- gross: number;
- net: number;
- tax: number;
- currency?: string;
-}
-
export interface Plan {
plan: SubscriptionPlan;
recurring: boolean;
@@ -107,13 +97,6 @@ export type PricingInfo = {
invalidCoupon?: boolean;
};
-export type Discount = {
- type: "regional" | "promo";
- code?: string;
- recurring: boolean;
- amount: number;
-};
-
export interface Customer {
// id: number;
// email: string;
diff --git a/apps/web/src/navigation/hash-routes.tsx b/apps/web/src/navigation/hash-routes.tsx
index c19b0f1af..ed861e5c6 100644
--- a/apps/web/src/navigation/hash-routes.tsx
+++ b/apps/web/src/navigation/hash-routes.tsx
@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { hashNavigate } from ".";
import { defineHashRoutes } from "./types";
-import { useEditorStore } from "../stores/editor-store";
import {
AddNotebookDialog,
EditNotebookDialog
@@ -59,13 +58,7 @@ const hashroutes = defineHashRoutes({
BuyDialog.show({}).then(afterAction);
},
"/buy/:code": ({ code }: { code: string }) => {
- BuyDialog.show({ couponCode: code, plan: "monthly" }).then(afterAction);
- },
- "/buy/:plan/:code": ({ plan, code }) => {
- BuyDialog.show({
- plan: plan === "monthly" ? "monthly" : "yearly",
- couponCode: code
- }).then(afterAction);
+ BuyDialog.show({ couponCode: code }).then(afterAction);
},
"/welcome": () => {
OnboardingDialog.show({ type: "new" })?.then(afterAction);
diff --git a/apps/web/src/views/checkout.tsx b/apps/web/src/views/checkout.tsx
index 8d5d206b3..127e523a0 100644
--- a/apps/web/src/views/checkout.tsx
+++ b/apps/web/src/views/checkout.tsx
@@ -21,7 +21,7 @@ import "../app.css";
import { useEffect, useState } from "react";
import { Box, Button, Flex, Text } from "@theme-ui/components";
import { hardNavigate, useQueryParams } from "../navigation";
-import { Check, Coupon, Support } from "../components/icons";
+import { Support } from "../components/icons";
import { HeadlessAuth } from "./auth";
import {
CheckoutCompleted,
@@ -31,16 +31,22 @@ import {
import { useCheckoutStore } from "../dialogs/buy-dialog/store";
import { useStore as useUserStore } from "../stores/user-store";
import { z } from "zod";
-import { PricingInfo } from "../dialogs/buy-dialog/types";
-import IconTag from "../components/icon-tag";
-import { SUBSCRIPTION_STATUS } from "../common/constants";
+import {
+ FEATURE_HIGHLIGHTS,
+ toPricingInfo
+} from "../dialogs/buy-dialog/helpers";
+import { isUserSubscribed } from "../hooks/use-is-user-premium";
+import { PLAN_METADATA } from "../dialogs/buy-dialog/plans";
+import { planToAvailability } from "@notesnook/common";
+import { FeatureCaption } from "../dialogs/buy-dialog/feature-caption";
-export type Period = "yearly" | "monthly" | "education";
export type Plan = z.infer<typeof PlanSchema>;
const PlanSchema = z.object({
id: z.string(),
- period: z.enum(["yearly", "monthly", "education"]),
+ period: z.enum(["yearly", "monthly", "5-year"]),
+ plan: z.number(),
+ recurring: z.boolean(),
price: z.object({
gross: z.number(),
net: z.number(),
@@ -63,36 +69,22 @@ const PlanSchema = z.object({
amount: z.number()
})
.optional(),
- country: z.string()
+ country: z.string(),
+ customer: z
+ .object({
+ id: z.string(),
+ email: z.string()
+ })
+ .optional()
});
-function toPricingInfo(plan: Plan): PricingInfo {
- return {
- country: plan.country,
- currency: plan.currency,
- discount: plan.discount
- ? {
- amount: plan.originalPrice.gross - plan.price.gross,
- recurring: plan.discount.recurring,
- type: plan.discount.type,
- code: plan.discount.code
- }
- : { amount: 0, recurring: false, type: "promo" },
- period: plan.period,
- price: plan.originalPrice,
- recurringPrice: plan.originalPrice,
- coupon: plan.discount?.code,
- invalidCoupon: false
- };
-}
-
const CHECKOUT_STEPS = ["Account", "Payment", "Complete"];
function Checkout() {
const [{ plan }] = useQueryParams();
const [currentStep, setCurrentStep] = useState(0);
const [error, setError] = useState<string>();
- const user = useUserStore((store) => store.user);
+ const [customer, setCustomer] = useState<{ id: string; email: string }>();
const isCheckoutCompleted = useCheckoutStore((store) => store.isCompleted);
useEffect(() => {
@@ -116,6 +108,10 @@ function Checkout() {
useCheckoutStore.getState().selectPlan(pricingInfo.data);
useCheckoutStore.getState().updatePrice(toPricingInfo(pricingInfo.data));
useCheckoutStore.getState().applyCoupon(pricingInfo.data.discount?.code);
+ if (pricingInfo.data.customer) {
+ setCustomer(pricingInfo.data.customer);
+ setCurrentStep(1);
+ }
}, [plan]);
if (!plan) {
@@ -242,11 +238,8 @@ function Checkout() {
if (ctx?.authenticated) {
await useUserStore.getState().init();
const user = useUserStore.getState().user;
- setCurrentStep(
- user?.subscription.type === SUBSCRIPTION_STATUS.PREMIUM
- ? 2
- : 1
- );
+ setCustomer(user);
+ setCurrentStep(isUserSubscribed(user) ? 2 : 1);
} else setError("Failed to create account.");
}}
/>
@@ -270,7 +263,7 @@ function Checkout() {
You are one step away from unlocking the full potential of
Notesnook.
</Text>
- <CheckoutDetails user={user} />
+ <CheckoutDetails user={customer} />
</Flex>
) : currentStep === 2 ? (
<Flex
@@ -331,9 +324,7 @@ export default Checkout;
function CheckoutSummaryMobile() {
const selectedPlan = useCheckoutStore((state) => state.selectedPlan);
const pricingInfo = useCheckoutStore((state) => state.pricingInfo);
- const couponCode = useCheckoutStore((store) => store.couponCode);
- console.log(pricingInfo);
if (!selectedPlan || !pricingInfo) return null;
return (
<>
@@ -347,13 +338,6 @@ function CheckoutSummaryMobile() {
</Text>
<Box sx={{ my: 2, height: 1, bg: "separator" }} />
<CheckoutPricing pricingInfo={pricingInfo} />
- {couponCode && pricingInfo.discount.amount > 0 ? (
- <IconTag
- icon={Coupon}
- text={couponCode}
- styles={{ container: { alignSelf: "end", mt: 2 } }}
- />
- ) : null}
</Flex>
</>
);
@@ -362,9 +346,7 @@ function CheckoutSummaryMobile() {
function CheckoutSummary() {
const selectedPlan = useCheckoutStore((state) => state.selectedPlan);
const pricingInfo = useCheckoutStore((state) => state.pricingInfo);
- const couponCode = useCheckoutStore((store) => store.couponCode);
- console.log(pricingInfo);
if (!selectedPlan || !pricingInfo) return null;
return (
<>
@@ -378,78 +360,31 @@ function CheckoutSummary() {
</Text>
<Box sx={{ my: 2, height: 1, bg: "separator" }} />
<Text variant="subtitle" sx={{}}>
- Notesnook Pro
+ {PLAN_METADATA[selectedPlan.plan].title} plan
</Text>
- <Box as="ul" sx={{ paddingInlineStart: 30, mt: 1 }}>
- {[
- "Unlimited real time sync",
- "Unlimited storage",
- "Exports in PDF, HTML & Markdown",
- "Unlimited devices",
- "Recurring reminders",
- "Unlimited notebooks & tags",
- "Automatic backups"
- ].map((feature) => (
- <Text
- as="li"
- key={feature}
- variant="body"
- sx={{ color: "paragraph-secondary", mt: 1 }}
- >
- {feature}
- </Text>
- ))}
- </Box>
- <Box sx={{ my: 2, height: 1, bg: "separator" }} />
- <CheckoutPricing pricingInfo={pricingInfo} />
-
- {couponCode && pricingInfo.discount.amount > 0 ? (
- <IconTag
- icon={Coupon}
- text={couponCode}
- styles={{ container: { alignSelf: "end", mt: 2 } }}
- />
- ) : null}
- </Flex>
- <Flex sx={{ flexDirection: "column", gap: 2, mt: 2 }}>
- <Flex
- sx={{
- p: 2,
- bg: "background-secondary",
- borderRadius: "default",
- border: "1px solid var(--border)",
- flexDirection: "column"
- }}
- >
- <Text variant="body" sx={{ fontWeight: "bold" }}>
- Privacy seems expensive until you get hacked!
- </Text>
- <Text variant="subBody">
- Hey there! We&apos;re a small team of developers focused on bringing
- you the best note-taking experience. Your support helps us keep the
- lights on.
- </Text>
- </Flex>
<Flex
sx={{
- p: 2,
- bg: "black",
- borderRadius: 10,
- border: "1px solid var(--border)",
- gap: 2
+ flexDirection: "column",
+ mt: 2,
+ gap: 1
}}
>
- <Check size={16} color="white" />
- <Text
- variant="body"
- sx={{
- color: "white"
- }}
- >
- {pricingInfo.period === "yearly" ? "30" : "7"}-day money-back
- guarantee
- </Text>
+ {FEATURE_HIGHLIGHTS.map((feature) => {
+ const caption =
+ feature.availability[planToAvailability(selectedPlan.plan)]
+ .caption;
+ return (
+ <Flex key={feature.id} sx={{ justifyContent: "space-between" }}>
+ <Text variant="body">{feature.title}</Text>
+ <Text variant="body" sx={{ color: "paragraph-secondary" }}>
+ <FeatureCaption caption={caption} />
+ </Text>
+ </Flex>
+ );
+ })}
</Flex>
+ <Box sx={{ my: 2, height: 1, bg: "separator" }} />
+ <CheckoutPricing pricingInfo={pricingInfo} />
</Flex>
</>
);