-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe.ts
69 lines (60 loc) · 1.93 KB
/
stripe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"use server";
import type { Stripe } from "stripe";
import { headers } from "next/headers";
import { CURRENCY } from "@/config";
import { formatAmountForStripe } from "@/utils/stripe-helpers";
import { stripe } from "@/lib/stripe";
export async function createCheckoutSession(
data: FormData,
): Promise<{ client_secret: string | null; url: string | null }> {
const ui_mode = data.get(
"uiMode",
) as Stripe.Checkout.SessionCreateParams.UiMode;
const origin: string = headers().get("origin") as string;
const checkoutSession: Stripe.Checkout.Session =
await stripe.checkout.sessions.create({
mode: "payment",
submit_type: "donate",
line_items: [
{
quantity: 1,
price_data: {
currency: CURRENCY,
product_data: {
name: "Custom amount donation",
},
unit_amount: formatAmountForStripe(
Number(data.get("customDonation") as string),
CURRENCY,
),
},
},
],
...(ui_mode === "hosted" && {
success_url: `${origin}/donate-with-checkout/result?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${origin}/donate-with-checkout`,
}),
...(ui_mode === "embedded" && {
return_url: `${origin}/donate-with-embedded-checkout/result?session_id={CHECKOUT_SESSION_ID}`,
}),
ui_mode,
});
return {
client_secret: checkoutSession.client_secret,
url: checkoutSession.url,
};
}
export async function createPaymentIntent(
data: FormData,
): Promise<{ client_secret: string }> {
const paymentIntent: Stripe.PaymentIntent =
await stripe.paymentIntents.create({
amount: formatAmountForStripe(
Number(data.get("customDonation") as string),
CURRENCY,
),
automatic_payment_methods: { enabled: true },
currency: CURRENCY,
});
return { client_secret: paymentIntent.client_secret as string };
}