Skip to content

Commit f597644

Browse files
committed
fix: Update embedded checkout example to use +page.server.js
1 parent c6c89e6 commit f597644

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

src/routes/examples/embedded-checkout/checkout-session/+server.js renamed to src/routes/examples/embedded-checkout/+page.server.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { json } from '@sveltejs/kit'
21
import Stripe from 'stripe'
32
import { SECRET_STRIPE_KEY, DOMAIN } from '$env/static/private'
43

54
const stripe = new Stripe(SECRET_STRIPE_KEY)
65
const return_url = new URL('/examples/embedded-checkout/thanks?session_id={CHECKOUT_SESSION_ID}', DOMAIN).toString()
76

8-
export async function POST() {
7+
export async function load() {
98
const session = await stripe.checkout.sessions.create({
109
ui_mode: 'embedded',
1110
line_items: [
@@ -18,7 +17,7 @@ export async function POST() {
1817
return_url
1918
})
2019

21-
return json({
20+
return {
2221
clientSecret: session.client_secret
23-
})
22+
}
2423
}
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
<script>
2-
import { goto } from '$app/navigation'
32
import { onMount } from 'svelte'
43
import { loadStripe } from '@stripe/stripe-js'
54
import { PUBLIC_STRIPE_KEY } from '$env/static/public'
65
import { EmbeddedCheckout } from '$lib'
76
7+
export let data
8+
89
let stripe = null
9-
let clientSecret = null
1010
1111
onMount(async () => {
1212
stripe = await loadStripe(PUBLIC_STRIPE_KEY)
13-
clientSecret = await createCheckoutSession()
1413
})
15-
16-
async function createCheckoutSession() {
17-
const response = await fetch('/examples/embedded-checkout/checkout-session', {
18-
method: 'POST',
19-
headers: {
20-
'content-type': 'application/json'
21-
},
22-
body: JSON.stringify({})
23-
})
24-
const { clientSecret } = await response.json()
25-
26-
return clientSecret
27-
}
2814
</script>
2915

3016
<h1>Embedded Checkout Example</h1>
@@ -33,4 +19,4 @@
3319
<a href="https://github.com/joshnuss/svelte-stripe/tree/main/src/routes/examples/embedded-checkout">View code</a>
3420
</nav>
3521

36-
<EmbeddedCheckout {stripe} {clientSecret}/>
22+
<EmbeddedCheckout {stripe} clientSecret={data.clientSecret}/>

0 commit comments

Comments
 (0)