mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 02:53:50 +00:00
feat(*): Add basket functionality
This commit is contained in:
@@ -1,28 +1,29 @@
|
||||
import { type RequestHandler, redirect } from '@sveltejs/kit';
|
||||
|
||||
// This is a public sample test API key.
|
||||
// Don’t submit any personally identifiable information in requests made with this key.
|
||||
// Sign in to see your own test API key embedded in code samples.
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public';
|
||||
import stripe from 'stripe';
|
||||
const stripeSecretKey =
|
||||
'sk_test_51PBMpZLiKlsU6INBHEsWwU2ekDfmotWTZghSl0XRLhtWp6mU7cBIfyBs6eBnG5kgC6MUhnvH5lUm2AbSWxgGfnKm00qTpaUnAP';
|
||||
const stripeClient = new stripe(stripeSecretKey);
|
||||
|
||||
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
|
||||
|
||||
// Create a checkout session
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
// const { priceId } = await request.json(); // Assuming you'll send the price ID from the client
|
||||
const formData = new URLSearchParams(await request.text());
|
||||
const items: { priceId: string; quantity: number }[] = [];
|
||||
|
||||
// Iterate over form data to extract basket items and their details
|
||||
for (const [key, value] of formData) {
|
||||
items.push({ priceId: key, quantity: parseInt(value, 10) });
|
||||
}
|
||||
|
||||
const lineItems = items.map((item) => ({
|
||||
price: item.priceId,
|
||||
quantity: item.quantity
|
||||
}));
|
||||
|
||||
const session = await stripeClient.checkout.sessions.create({
|
||||
line_items: [
|
||||
{
|
||||
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell
|
||||
price: 'price_1PBNHRLiKlsU6INBjXMcRxxS', //priceId,
|
||||
quantity: 1
|
||||
}
|
||||
],
|
||||
line_items: lineItems,
|
||||
mode: 'payment',
|
||||
success_url: `${request.headers.get('origin')}/checkout/success`,
|
||||
cancel_url: `${request.headers.get('origin')}/checkout/cancel`
|
||||
cancel_url: `${request.headers.get('origin')}/checkout/cancelled`
|
||||
});
|
||||
|
||||
return redirect(303, session.url as string);
|
||||
|
||||
Reference in New Issue
Block a user