Skip to content

Commit 02aa82d

Browse files
committed
Update stripe redirect links to handle subdomains
1 parent 708c353 commit 02aa82d

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

apps/dashboard/src/@/actions/billing.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use server";
22

33
import "server-only";
4-
import { API_SERVER_URL, getAbsoluteUrlFromPath } from "@/constants/env";
4+
import { API_SERVER_URL } from "@/constants/env";
55
import { redirect } from "next/navigation";
66
import { getAuthToken } from "../../app/api/lib/getAuthToken";
77
import type { ProductSKU } from "../lib/billing";
88

99
export type RedirectCheckoutOptions = {
1010
teamSlug: string;
1111
sku: ProductSKU;
12-
redirectPath?: string;
12+
redirectUrl: string;
1313
metadata?: Record<string, string>;
1414
};
1515
export async function redirectToCheckout(
@@ -34,10 +34,7 @@ export async function redirectToCheckout(
3434
method: "POST",
3535
body: JSON.stringify({
3636
sku: options.sku,
37-
redirectTo: getAbsoluteUrlFromPath(
38-
options.redirectPath ||
39-
`/team/${options.teamSlug}/~/settings/billing`,
40-
).toString(),
37+
redirectTo: options.redirectUrl,
4138
metadata: options.metadata || {},
4239
}),
4340
headers: {
@@ -64,7 +61,7 @@ export async function redirectToCheckout(
6461

6562
export type BillingPortalOptions = {
6663
teamSlug: string | undefined;
67-
redirectPath?: string;
64+
redirectUrl: string;
6865
};
6966
export async function redirectToBillingPortal(
7067
options: BillingPortalOptions,
@@ -86,10 +83,7 @@ export async function redirectToBillingPortal(
8683
{
8784
method: "POST",
8885
body: JSON.stringify({
89-
redirectTo: getAbsoluteUrlFromPath(
90-
options.redirectPath ||
91-
`/team/${options.teamSlug}/~/settings/billing`,
92-
).toString(),
86+
redirectTo: options.redirectUrl,
9387
}),
9488
headers: {
9589
"Content-Type": "application/json",

apps/dashboard/src/@/components/billing.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
} from "../actions/billing";
99
import { Button, type ButtonProps } from "./ui/button";
1010

11-
type CheckoutButtonProps = RedirectCheckoutOptions & ButtonProps;
11+
type CheckoutButtonProps = Omit<RedirectCheckoutOptions, "redirectUrl"> &
12+
ButtonProps & {
13+
redirectPath: string;
14+
};
15+
1216
export function CheckoutButton({
1317
onClick,
1418
teamSlug,
@@ -27,7 +31,7 @@ export function CheckoutButton({
2731
teamSlug,
2832
sku,
2933
metadata,
30-
redirectPath,
34+
redirectUrl: getRedirectUrl(redirectPath),
3135
});
3236
}}
3337
>
@@ -36,7 +40,10 @@ export function CheckoutButton({
3640
);
3741
}
3842

39-
type BillingPortalButtonProps = BillingPortalOptions & ButtonProps;
43+
type BillingPortalButtonProps = Omit<BillingPortalOptions, "redirectUrl"> &
44+
ButtonProps & {
45+
redirectPath: string;
46+
};
4047
export function BillingPortalButton({
4148
onClick,
4249
teamSlug,
@@ -51,11 +58,17 @@ export function BillingPortalButton({
5158
onClick?.(e);
5259
await redirectToBillingPortal({
5360
teamSlug,
54-
redirectPath,
61+
redirectUrl: getRedirectUrl(redirectPath),
5562
});
5663
}}
5764
>
5865
{children}
5966
</Button>
6067
);
6168
}
69+
70+
function getRedirectUrl(path: string) {
71+
const url = new URL(window.location.origin);
72+
url.pathname = path;
73+
return url.toString();
74+
}

apps/dashboard/src/@/constants/env.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,3 @@ export const BASE_URL = isProd
3939
: (process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL
4040
? `https://${process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL}`
4141
: "http://localhost:3000") || "https://thirdweb-dev.com";
42-
43-
export function getAbsoluteUrlFromPath(path: string) {
44-
const url = new URL(BASE_URL);
45-
46-
url.pathname = path;
47-
return url;
48-
}

apps/dashboard/src/app/team/[team_slug]/(team)/~/settings/billing/components/PlanInfoCard.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export function PlanInfoCard(props: {
4949

5050
<div className="flex flex-row gap-2">
5151
{/* manage team billing */}
52-
<BillingPortalButton teamSlug={team.slug} variant="outline">
52+
<BillingPortalButton
53+
teamSlug={team.slug}
54+
variant="outline"
55+
redirectPath={`/team/${team.slug}/~/settings/billing`}
56+
>
5357
Manage Billing
5458
</BillingPortalButton>
5559

0 commit comments

Comments
 (0)