Skip to content

Commit

Permalink
feat: centralize Checkout utils in helpers/
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-anderson committed Feb 20, 2024
1 parent 0393abc commit ce540f8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/pages/CheckoutPage/helpers/getTotalForDisplay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isSafeInteger } from "@nerdware/ts-type-safety-utils";
import { fmt } from "@/utils/formatters";
import type { OpenApiSchemas } from "@/types/open-api";

/**
* > `The values used here are for DISPLAY PURPOSES ONLY and merely convey
* information to the user. All pricing/product info is stored and calculated
* by the backend API. Sending invalid pricing/product info to the server
* results a 400 response.`
*/
export const getPrice_FOR_DISPLAY_ONLY = <FormatOpts extends { formatAsCurrency?: boolean }>(
price: number,
discountPercentage?: OpenApiSchemas["PromoCodeInfo"]["discountPercentage"] | null, // 10 = 10% off
{ formatAsCurrency }: FormatOpts = {} as FormatOpts
) => {
const priceWithDiscount = isSafeInteger(discountPercentage)
? price - price * (discountPercentage / 100)
: price;

const returnValue =
formatAsCurrency === true ? fmt.intToCurrencyStr(priceWithDiscount) : priceWithDiscount;

return returnValue as FormatOpts["formatAsCurrency"] extends true ? string : number;
};
2 changes: 2 additions & 0 deletions src/pages/CheckoutPage/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./getTotalForDisplay";
export * from "./subPricingDisplayConfigs";
55 changes: 55 additions & 0 deletions src/pages/CheckoutPage/helpers/subPricingDisplayConfigs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { SubscriptionPriceLabel } from "@/graphql/types";

/**
* > `The values used here are for DISPLAY PURPOSES ONLY and merely convey
* information to the user. All pricing/product info is stored and calculated
* by the backend API. Sending invalid pricing/product info to the server
* results a 400 response.`
*/
export const SUB_PRICING_DISPLAY_CONFIGS = {
TRIAL: {
label: "Try Fixit",
price: 0,
trialDays: 14,
afterTrial: {
price: 500,
billingPeriod: "month",
},
},
MONTHLY: {
label: "Monthly Subscription",
price: 500,
billingPeriod: "month",
},
ANNUAL: {
label: "Annual Subscription",
price: 5000,
billingPeriod: "year",
},
} as {
readonly [Sub in SubscriptionPriceLabel]: Sub extends "TRIAL"
? TrialSubBillingDisplayConfigs
: PaidSubBillingDisplayConfigs;
};

interface BaseSubPricingDisplayConfigs {
label: string;
}

type SubBillingInfo = {
price: number;
billingPeriod: "month" | "year";
};

type PaidSubBillingDisplayConfigs = BaseSubPricingDisplayConfigs &
SubBillingInfo & {
trialDays?: never;
afterTrial?: never;
};

type TrialSubBillingDisplayConfigs = BaseSubPricingDisplayConfigs & {
price: 0;
trialDays: number;
afterTrial: SubBillingInfo;
billingPeriod?: never;
};

0 comments on commit ce540f8

Please sign in to comment.