|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import "server-only"; |
| 4 | +import { API_SERVER_URL, getAbsoluteUrlFromPath } from "@/constants/env"; |
| 5 | +import { redirect } from "next/navigation"; |
| 6 | +import { getAuthToken } from "../../app/api/lib/getAuthToken"; |
| 7 | +import type { ProductSKU } from "../lib/billing"; |
| 8 | + |
| 9 | +export type RedirectCheckoutOptions = { |
| 10 | + teamSlug: string; |
| 11 | + sku: ProductSKU; |
| 12 | + redirectPath?: string; |
| 13 | + metadata?: Record<string, string>; |
| 14 | +}; |
| 15 | +export async function redirectToCheckout( |
| 16 | + options: RedirectCheckoutOptions, |
| 17 | +): Promise<{ status: number }> { |
| 18 | + if (!options.teamSlug) { |
| 19 | + return { |
| 20 | + status: 400, |
| 21 | + }; |
| 22 | + } |
| 23 | + const token = await getAuthToken(); |
| 24 | + |
| 25 | + if (!token) { |
| 26 | + return { |
| 27 | + status: 401, |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + const res = await fetch( |
| 32 | + `${API_SERVER_URL}/v1/teams/${options.teamSlug}/checkout/create-link`, |
| 33 | + { |
| 34 | + method: "POST", |
| 35 | + body: JSON.stringify({ |
| 36 | + sku: options.sku, |
| 37 | + redirectTo: getAbsoluteUrlFromPath( |
| 38 | + options.redirectPath || |
| 39 | + `/team/${options.teamSlug}/~/settings/billing`, |
| 40 | + ).toString(), |
| 41 | + metadata: options.metadata || {}, |
| 42 | + }), |
| 43 | + headers: { |
| 44 | + "Content-Type": "application/json", |
| 45 | + Authorization: `Bearer ${token}`, |
| 46 | + }, |
| 47 | + }, |
| 48 | + ); |
| 49 | + if (!res.ok) { |
| 50 | + return { |
| 51 | + status: res.status, |
| 52 | + }; |
| 53 | + } |
| 54 | + const json = await res.json(); |
| 55 | + if (!json.result) { |
| 56 | + return { |
| 57 | + status: 500, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + // redirect to the stripe checkout session |
| 62 | + redirect(json.result); |
| 63 | +} |
| 64 | + |
| 65 | +export type BillingPortalOptions = { |
| 66 | + teamSlug: string | undefined; |
| 67 | + redirectPath?: string; |
| 68 | +}; |
| 69 | +export async function redirectToBillingPortal( |
| 70 | + options: BillingPortalOptions, |
| 71 | +): Promise<{ status: number }> { |
| 72 | + if (!options.teamSlug) { |
| 73 | + return { |
| 74 | + status: 400, |
| 75 | + }; |
| 76 | + } |
| 77 | + const token = await getAuthToken(); |
| 78 | + if (!token) { |
| 79 | + return { |
| 80 | + status: 401, |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + const res = await fetch( |
| 85 | + `${API_SERVER_URL}/v1/teams/${options.teamSlug}/checkout/create-session-link`, |
| 86 | + { |
| 87 | + method: "POST", |
| 88 | + body: JSON.stringify({ |
| 89 | + redirectTo: getAbsoluteUrlFromPath( |
| 90 | + options.redirectPath || |
| 91 | + `/team/${options.teamSlug}/~/settings/billing`, |
| 92 | + ).toString(), |
| 93 | + }), |
| 94 | + headers: { |
| 95 | + "Content-Type": "application/json", |
| 96 | + Authorization: `Bearer ${token}`, |
| 97 | + }, |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + if (!res.ok) { |
| 102 | + return { |
| 103 | + status: res.status, |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + const json = await res.json(); |
| 108 | + |
| 109 | + if (!json.result) { |
| 110 | + return { |
| 111 | + status: 500, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + // redirect to the stripe billing portal |
| 116 | + redirect(json.result); |
| 117 | +} |
0 commit comments