"use server"; import { Stripe } from "stripe"; import { headers } from "next/headers"; 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; console.log('DATA', data) 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: 'eur', product_data: { name: "Custom amount donation", }, unit_amount: formatAmountForStripe( Number(data.get("customDonation") as string), 'eur', ), }, }, ], ...(ui_mode === "hosted" && { success_url: `${origin}/payment/with-checkout/result?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${origin}/with-checkout`, }), ...(ui_mode === "embedded" && { return_url: `${origin}/payment/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: { amount: number, sessionId?: string }, ): Promise<{ client_secret: string }> { const params = { amount: formatAmountForStripe( data.amount, 'eur', ), automatic_payment_methods: { enabled: true }, currency: 'eur', } as Stripe.PaymentIntentCreateParams; if (data?.sessionId){ params.metadata = { sessionId : data.sessionId } } const paymentIntent: Stripe.PaymentIntent = await stripe.paymentIntents.create(params); return { client_secret: paymentIntent.client_secret as string }; } export const getStripePaymentStatus = async (payment_intent: string): Promise => await stripe.paymentIntents.retrieve(payment_intent);