Skip to content

Commit

Permalink
⚡️ Remove currency parameter from billing API
Browse files Browse the repository at this point in the history
It let Stripe figure it out
  • Loading branch information
baptisteArno committed Nov 19, 2024
1 parent 05cc23e commit 6088a56
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const createCheckoutSession = authenticatedProcedure
email: z.string(),
company: z.string(),
workspaceId: z.string(),
currency: z.enum(["usd", "eur"]),
plan: z.enum([Plan.STARTER, Plan.PRO]),
returnUrl: z.string(),
vat: z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const updateSubscription = authenticatedProcedure
returnUrl: z.string(),
workspaceId: z.string(),
plan: z.enum([Plan.STARTER, Plan.PRO]),
currency: z.enum(["usd", "eur"]),
}),
)
.output(
Expand Down
21 changes: 2 additions & 19 deletions apps/builder/src/features/billing/components/ChangePlanForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ export const ChangePlanForm = ({
const newSubscription = {
plan,
workspaceId: workspace.id,
currency:
data?.subscription?.currency ??
(guessIfUserIsEuropean() ? "eur" : "usd"),
} as const;
if (workspace.stripeId) {
updateSubscription({
Expand All @@ -82,15 +79,6 @@ export const ChangePlanForm = ({
}
};

const updateCurrency = (currency: "eur" | "usd") => {
if (preCheckoutPlan) {
setPreCheckoutPlan({
...preCheckoutPlan,
currency,
});
}
};

if (
data?.subscription?.cancelDate ||
data?.subscription?.status === "past_due"
Expand Down Expand Up @@ -129,7 +117,6 @@ export const ChangePlanForm = ({
existingEmail={user?.email ?? undefined}
existingCompany={user?.company ?? undefined}
onClose={() => setPreCheckoutPlan(undefined)}
onCurrencyChange={updateCurrency}
/>
</ParentModalProvider>
)}
Expand All @@ -141,9 +128,7 @@ export const ChangePlanForm = ({
currentPlan={workspace.plan}
onPayClick={() => handlePayClick(Plan.STARTER)}
isLoading={isUpdatingSubscription}
currency={
preCheckoutPlan?.currency ?? data.subscription?.currency
}
currency={data.subscription?.currency}
/>
)}

Expand All @@ -152,9 +137,7 @@ export const ChangePlanForm = ({
currentPlan={workspace.plan}
onPayClick={() => handlePayClick(Plan.PRO)}
isLoading={isUpdatingSubscription}
currency={
preCheckoutPlan?.currency ?? data.subscription?.currency
}
currency={data.subscription?.currency}
/>
)}
</HStack>
Expand Down
17 changes: 0 additions & 17 deletions apps/builder/src/features/billing/components/PreCheckoutModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ export type PreCheckoutModalProps = {
| {
plan: "STARTER" | "PRO";
workspaceId: string;
currency: "eur" | "usd";
}
| undefined;
existingCompany?: string;
existingEmail?: string;
onCurrencyChange: (currency: "eur" | "usd") => void;
onClose: () => void;
};

Expand All @@ -48,7 +46,6 @@ export const PreCheckoutModal = ({
selectedSubscription,
existingCompany,
existingEmail,
onCurrencyChange,
onClose,
}: PreCheckoutModalProps) => {
const { t } = useTranslate();
Expand Down Expand Up @@ -127,10 +124,6 @@ export const PreCheckoutModal = ({
});
};

const updateCurrency = (currency: "EUR" | "USD") => {
onCurrencyChange(currency.toLowerCase() as "eur" | "usd");
};

return (
<Modal isOpen={isDefined(selectedSubscription)} onClose={onClose}>
<ModalOverlay />
Expand Down Expand Up @@ -172,16 +165,6 @@ export const PreCheckoutModal = ({
/>
</HStack>
</FormControl>
<FormControl as={HStack} justify="space-between">
<FormLabel>Currency:</FormLabel>
{selectedSubscription && (
<DropdownList
items={["USD", "EUR"]}
currentItem={selectedSubscription.currency.toUpperCase()}
onItemSelect={updateCurrency}
/>
)}
</FormControl>

<Button
type="submit"
Expand Down
12 changes: 0 additions & 12 deletions apps/builder/src/features/dashboard/components/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { useWorkspace } from "@/features/workspace/WorkspaceProvider";
import { trpc } from "@/lib/trpc";
import { Spinner, Stack, Text, VStack } from "@chakra-ui/react";
import { useTranslate } from "@tolgee/react";
import { guessIfUserIsEuropean } from "@typebot.io/billing/helpers/guessIfUserIsEuropean";
import type { Plan } from "@typebot.io/prisma/enum";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
Expand Down Expand Up @@ -51,20 +50,10 @@ export const DashboardPage = () => {
setPreCheckoutPlan({
plan: subscribePlan as "PRO" | "STARTER",
workspaceId: workspace.id,
currency: guessIfUserIsEuropean() ? "eur" : "usd",
});
}
}, [createCustomCheckoutSession, router.query, user, workspace]);

const updateCurrency = (currency: "eur" | "usd") => {
if (preCheckoutPlan) {
setPreCheckoutPlan({
...preCheckoutPlan,
currency,
});
}
};

return (
<Stack minH="100vh">
<Seo title={workspace?.name ?? t("dashboard.title")} />
Expand All @@ -76,7 +65,6 @@ export const DashboardPage = () => {
existingEmail={user?.email ?? undefined}
existingCompany={workspace?.name ?? undefined}
onClose={() => setPreCheckoutPlan(undefined)}
onCurrencyChange={updateCurrency}
/>
</ParentModalProvider>
)}
Expand Down
18 changes: 1 addition & 17 deletions apps/docs/openapi/builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -9309,13 +9309,6 @@
"workspaceId": {
"type": "string"
},
"currency": {
"type": "string",
"enum": [
"usd",
"eur"
]
},
"plan": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -9346,7 +9339,6 @@
"email",
"company",
"workspaceId",
"currency",
"plan",
"returnUrl"
]
Expand Down Expand Up @@ -9447,20 +9439,12 @@
"STARTER",
"PRO"
]
},
"currency": {
"type": "string",
"enum": [
"usd",
"eur"
]
}
},
"required": [
"returnUrl",
"workspaceId",
"plan",
"currency"
"plan"
]
}
}
Expand Down
3 changes: 0 additions & 3 deletions packages/billing/src/api/createCheckoutSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type Props = {
email: string;
company: string;
plan: "STARTER" | "PRO";
currency: "usd" | "eur";
vat?: {
type: string;
value: string;
Expand All @@ -28,7 +27,6 @@ export const createCheckoutSession = async ({
email,
company,
plan,
currency,
vat,
}: Props) => {
if (!env.STRIPE_SECRET_KEY)
Expand Down Expand Up @@ -92,7 +90,6 @@ export const createCheckoutSession = async ({
customerId: customer.id,
userId: user.id,
workspaceId,
currency,
plan,
returnUrl,
});
Expand Down
3 changes: 0 additions & 3 deletions packages/billing/src/api/updateSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ type Props = {
user: Pick<User, "email" | "id">;
plan: "STARTER" | "PRO";
returnUrl: string;
currency: "usd" | "eur";
};

export const updateSubscription = async ({
workspaceId,
user,
plan,
returnUrl,
currency,
}: Props) => {
if (!env.STRIPE_SECRET_KEY)
throw new TRPCError({
Expand Down Expand Up @@ -125,7 +123,6 @@ export const updateSubscription = async ({
customerId: workspace.stripeId,
userId: user.id,
workspaceId,
currency,
plan,
returnUrl,
});
Expand Down
4 changes: 1 addition & 3 deletions packages/billing/src/helpers/createCheckoutSessionUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import type Stripe from "stripe";
type Props = {
customerId: string;
workspaceId: string;
currency: "usd" | "eur";
plan: "STARTER" | "PRO";
returnUrl: string;
userId: string;
};

export const createCheckoutSessionUrl =
(stripe: Stripe) =>
async ({ customerId, workspaceId, currency, plan, returnUrl }: Props) => {
async ({ customerId, workspaceId, plan, returnUrl }: Props) => {
const session = await stripe.checkout.sessions.create({
success_url: `${returnUrl}?stripe=${plan}&success=true`,
cancel_url: `${returnUrl}?stripe=cancel`,
Expand All @@ -27,7 +26,6 @@ export const createCheckoutSessionUrl =
workspaceId,
plan,
},
currency,
billing_address_collection: "required",
automatic_tax: { enabled: true },
line_items: [
Expand Down

0 comments on commit 6088a56

Please sign in to comment.