mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 19:13:48 +00:00
feat(#29): Add ability to record that a user has purchased a product - part 1
This commit is contained in:
@@ -22,6 +22,8 @@ If you want a fully local development environment (other than stripe) then you c
|
|||||||
- [Local DB Url](http://localhost:54323/)
|
- [Local DB Url](http://localhost:54323/)
|
||||||
- [Local Email Monitoring Url](http://localhost:54324/)
|
- [Local Email Monitoring Url](http://localhost:54324/)
|
||||||
|
|
||||||
|
For stripe, you can forward the events to your local server using `stripe listen --forward-to localhost:5173/api/webhook/stripe --skip-verify` in a separate terminal window.
|
||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
To create a production version:
|
To create a production version:
|
||||||
|
|||||||
@@ -2,7 +2,16 @@ import { type RequestHandler, redirect } from '@sveltejs/kit';
|
|||||||
import { stripe as stripeClient } from '$lib/utils/stripe';
|
import { stripe as stripeClient } from '$lib/utils/stripe';
|
||||||
|
|
||||||
// Create a checkout session
|
// Create a checkout session
|
||||||
export const POST: RequestHandler = async ({ request, cookies }) => {
|
export const POST: RequestHandler = async ({ request, cookies, locals: { safeGetSession } }) => {
|
||||||
|
const { session } = await safeGetSession();
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
// If no session is found, the user isn't authenticated, so block the action
|
||||||
|
return new Response('Unauthorized', { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const userId = session.user.id;
|
||||||
|
|
||||||
const formData = new URLSearchParams(await request.text());
|
const formData = new URLSearchParams(await request.text());
|
||||||
const items: { priceId: string; quantity: number }[] = [];
|
const items: { priceId: string; quantity: number }[] = [];
|
||||||
|
|
||||||
@@ -16,14 +25,17 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
|
|||||||
quantity: item.quantity
|
quantity: item.quantity
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const session = await stripeClient.checkout.sessions.create({
|
const checkoutSession = await stripeClient.checkout.sessions.create({
|
||||||
line_items: lineItems,
|
line_items: lineItems,
|
||||||
mode: 'payment',
|
mode: 'payment',
|
||||||
success_url: `${request.headers.get('origin')}/checkout/success`,
|
success_url: `${request.headers.get('origin')}/checkout/success`,
|
||||||
cancel_url: `${request.headers.get('origin')}/checkout/cancelled`
|
cancel_url: `${request.headers.get('origin')}/checkout/cancelled`,
|
||||||
|
metadata: {
|
||||||
|
userId: userId
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Store the checkout session.id for access from the frontend
|
// Store the checkout session.id for access from the frontend
|
||||||
cookies.set('checkout_session_id', session.id, { path: '/' });
|
cookies.set('checkout_session_id', checkoutSession.id, { path: '/' });
|
||||||
return redirect(303, session.url as string);
|
return redirect(303, checkoutSession.url as string);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { RequestHandler } from '@sveltejs/kit';
|
import { json, type RequestHandler } from '@sveltejs/kit';
|
||||||
import stripe from 'stripe';
|
import stripe from 'stripe';
|
||||||
import { STRIPE_ENDPOINT_SECRET } from '$env/static/private';
|
import { STRIPE_ENDPOINT_SECRET } from '$env/static/private';
|
||||||
import {
|
import {
|
||||||
@@ -71,16 +71,19 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
checkoutSession.customer as string,
|
checkoutSession.customer as string,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
} else if (checkoutSession.mode === 'payment') {
|
||||||
|
// TODO Record that they purchased the product
|
||||||
|
console.log(checkoutSession);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
console.log(`Unhandled event type ${event.type}`)
|
console.log(`Unhandled event type ${event.type}`);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const message = err instanceof Error ? err.message : 'An unknown error has occurred';
|
const message = err instanceof Error ? err.message : 'An unknown error has occurred';
|
||||||
return new Response(`Webhook Error: ${message}`, { status: 500 });
|
return new Response(`Webhook Error: ${message}`, { status: 500 });
|
||||||
}
|
}
|
||||||
|
|
||||||
return json({received: true})
|
return json({ received: true });
|
||||||
}
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user