feat(*): Add stripe checkout support

This commit is contained in:
Josh Creek
2024-04-30 21:41:05 +01:00
parent 04290f547f
commit fcf6f1fcb7
6 changed files with 82 additions and 28 deletions
+29
View File
@@ -0,0 +1,29 @@
import { type RequestHandler, redirect } from '@sveltejs/kit';
// This is a public sample test API key.
// Dont 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 stripe from 'stripe';
const stripeSecretKey =
'sk_test_51PBMpZLiKlsU6INBHEsWwU2ekDfmotWTZghSl0XRLhtWp6mU7cBIfyBs6eBnG5kgC6MUhnvH5lUm2AbSWxgGfnKm00qTpaUnAP';
const stripeClient = new stripe(stripeSecretKey);
// 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 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
}
],
mode: 'payment',
success_url: `${request.headers.get('origin')}/checkout/success`,
cancel_url: `${request.headers.get('origin')}/checkout/cancel`
});
return redirect(303, session.url as string);
};
+14
View File
@@ -0,0 +1,14 @@
<script src="https://js.stripe.com/v3/"></script>
<section>
<div class="product">
<img src="https://i.imgur.com/EHyR2nP.png" alt="The cover of Stubborn Attachments" />
<div class="description">
<h3>Stubborn Attachments</h3>
<h5>$20.00</h5>
</div>
</div>
<form action="/api/checkout" method="POST">
<button type="submit" id="checkout-button">Checkout</button>
</form>
</section>
@@ -0,0 +1,3 @@
<section>
<p>Forgot to add something to your cart? Shop around then come back to pay!</p>
</section>
+6
View File
@@ -0,0 +1,6 @@
<section>
<p>
We appreciate your business! If you have any questions, please email
<a href="mailto:orders@example.com">orders@example.com</a>.
</p>
</section>