Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 3x 3x 2x 3x 1x 1x 1x 1x 3x 3x 1x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x | import Stripe from "stripe";
export default defineEventHandler(async event => {
const user = await requireUser(event);
if (user.role === 'demo') throw createError({statusCode: 403, statusMessage: 'Le paiement est indisponible avec un compte de démonstration'});
const config = useRuntimeConfig(event);
if (!config.stripeSecretKey) throw createError({
statusCode: 503,
statusMessage: "Stripe n’est pas encore configuré"
});
const body = await readBody(event);
const {lines, promoError} = await checkoutLines(event, body.lines || [], body.promoCode);
if (promoError) throw createError({statusCode: 400, statusMessage: promoError});
const stripe = new Stripe(config.stripeSecretKey);
const origin = config.public.siteUrl || getRequestURL(event).origin;
const session = await stripe.checkout.sessions.create({
mode: "payment",
customer_email: user.email,
line_items: lines.map(l => ({
quantity: l.quantity,
price_data: {
currency: "eur",
unit_amount: l.price,
product_data: {name: l.name + (l.originalPrice !== l.price ? ` (remisé)` : '')}
}
})),
success_url: `${origin}/commande?success=stripe&session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${origin}/panier?cancelled=1`,
billing_address_collection: "required",
shipping_address_collection: {allowed_countries: ["FR", "BE", "LU", "DE", "ES", "IT", "NL"]}
});
return {url: session.url};
});
|