Skip to content

Commit

Permalink
fix: Update embedded checkout example to use +page.server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
joshnuss committed Nov 16, 2023
1 parent c6c89e6 commit f597644
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { json } from '@sveltejs/kit'
import Stripe from 'stripe'
import { SECRET_STRIPE_KEY, DOMAIN } from '$env/static/private'

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

export async function POST() {
export async function load() {
const session = await stripe.checkout.sessions.create({
ui_mode: 'embedded',
line_items: [
Expand All @@ -18,7 +17,7 @@ export async function POST() {
return_url
})

return json({
return {
clientSecret: session.client_secret
})
}
}
20 changes: 3 additions & 17 deletions src/routes/examples/embedded-checkout/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
<script>
import { goto } from '$app/navigation'
import { onMount } from 'svelte'
import { loadStripe } from '@stripe/stripe-js'
import { PUBLIC_STRIPE_KEY } from '$env/static/public'
import { EmbeddedCheckout } from '$lib'
export let data
let stripe = null
let clientSecret = null
onMount(async () => {
stripe = await loadStripe(PUBLIC_STRIPE_KEY)
clientSecret = await createCheckoutSession()
})
async function createCheckoutSession() {
const response = await fetch('/examples/embedded-checkout/checkout-session', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({})
})
const { clientSecret } = await response.json()
return clientSecret
}
</script>

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

<EmbeddedCheckout {stripe} {clientSecret}/>
<EmbeddedCheckout {stripe} clientSecret={data.clientSecret}/>

1 comment on commit f597644

@vercel
Copy link

@vercel vercel bot commented on f597644 Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.