-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path+page.svelte
48 lines (39 loc) · 998 Bytes
/
+page.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<script>
import { PUBLIC_STRIPE_KEY } from '$env/static/public'
import { onMount } from 'svelte'
import { loadStripe } from '@stripe/stripe-js'
import { Elements, PaymentElement } from 'svelte-stripe'
export let data
let stripe
let elements
$: ({ clientSecret, returnUrl } = data)
// load Stripe client from CDN
onMount(async () => {
stripe = await loadStripe(PUBLIC_STRIPE_KEY)
})
// handle form submission
async function submit() {
const { error } = await stripe.confirmPayment({
// pass instance that was used to create the Payment Element
elements,
confirmParams: {
return_url: returnUrl
}
})
if (error) {
// handle error
console.error(error)
}
}
</script>
<h1>Payment</h1>
{#if stripe}
<form on:submit|preventDefault={submit}>
<Elements {stripe} {clientSecret} bind:elements>
<PaymentElement />
</Elements>
<button>Pay</button>
</form>
{:else}
Loading Stripe...
{/if}