mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 10:33:01 +00:00
stripe frontend
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { getServerURL } from "@/lib/utils";
|
||||
|
||||
interface CreateCheckoutParams {
|
||||
billingPeriod: "monthly" | "annual";
|
||||
csrfToken: string;
|
||||
onSuccess?: (url: string) => void;
|
||||
onError?: (error: string) => void;
|
||||
}
|
||||
|
||||
export async function createCheckoutSession({
|
||||
billingPeriod,
|
||||
csrfToken,
|
||||
onSuccess,
|
||||
onError,
|
||||
}: CreateCheckoutParams) {
|
||||
try {
|
||||
const response = await fetch(`${getServerURL()}/subscription/create-checkout-session`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRF-Token": csrfToken,
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify({ billingPeriod }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
onError?.(data.error || "Failed to create checkout session");
|
||||
return;
|
||||
}
|
||||
|
||||
onSuccess?.(data.url);
|
||||
} catch (error) {
|
||||
onError?.("Network error");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { getServerURL } from "@/lib/utils";
|
||||
|
||||
interface CreatePortalParams {
|
||||
csrfToken: string;
|
||||
onSuccess?: (url: string) => void;
|
||||
onError?: (error: string) => void;
|
||||
}
|
||||
|
||||
export async function createPortalSession({ csrfToken, onSuccess, onError }: CreatePortalParams) {
|
||||
try {
|
||||
const response = await fetch(`${getServerURL()}/subscription/create-portal-session`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRF-Token": csrfToken,
|
||||
},
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
onError?.(data.error || "Failed to create portal session");
|
||||
return;
|
||||
}
|
||||
|
||||
onSuccess?.(data.url);
|
||||
} catch (error) {
|
||||
onError?.("Network error");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { SubscriptionRecord } from "@sprint/shared";
|
||||
import { getServerURL } from "@/lib/utils";
|
||||
|
||||
interface GetSubscriptionParams {
|
||||
onSuccess?: (subscription: SubscriptionRecord | null) => void;
|
||||
onError?: (error: string) => void;
|
||||
}
|
||||
|
||||
export async function getSubscription({ onSuccess, onError }: GetSubscriptionParams) {
|
||||
try {
|
||||
const response = await fetch(`${getServerURL()}/subscription/get`, {
|
||||
method: "GET",
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
onError?.(data.error || "Failed to fetch subscription");
|
||||
return;
|
||||
}
|
||||
|
||||
onSuccess?.(data.subscription);
|
||||
} catch (error) {
|
||||
onError?.("Network error");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user