-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<script> | ||
import { mount } from './util' | ||
import { onMount, getContext, createEventDispatcher } from 'svelte' | ||
/** @typedef { import('@stripe/stripe-js').StripeExpressCheckoutElementOptions } Options */ | ||
/** @type {Options['buttonHeight']} */ | ||
export let buttonHeight = undefined | ||
/** @type {Options['buttonTheme']} */ | ||
export let buttonTheme = undefined | ||
/** @type {Options['buttonType']} */ | ||
export let buttonType = undefined | ||
/** @type {Options['layout']} */ | ||
export let layout = undefined | ||
/** @type {Options['paymentMethodOrder']} */ | ||
export let paymentMethodOrder = undefined | ||
/** @type {Options['wallets']} */ | ||
export let wallets = undefined | ||
/** @type {import('@stripe/stripe-js').StripeElementBase?} */ | ||
export let element = null | ||
/** @type {HTMLElement?} */ | ||
let wrapper | ||
const dispatch = createEventDispatcher() | ||
/** @type {import("./types").ElementsContext} */ | ||
const { elements } = getContext('stripe') | ||
onMount(() => { | ||
const options = { | ||
buttonHeight, | ||
buttonTheme, | ||
buttonType, | ||
layout, | ||
paymentMethodOrder, | ||
wallets | ||
} | ||
element = mount(wrapper, 'expressCheckout', elements, dispatch, options) | ||
element.on('click', (e) => dispatch('click', e)) | ||
element.on('cancel', (e) => dispatch('cancel', e)) | ||
element.on('confirm', (e) => dispatch('confirm', e)) | ||
element.on('shippingaddresschange', (e) => dispatch('shippingaddresschange', e)) | ||
element.on('shippingratechange', (e) => dispatch('shippingratechange', e)) | ||
return () => element.destroy() | ||
}) | ||
export function blur() { | ||
element.blur() | ||
} | ||
export function clear() { | ||
element.clear() | ||
} | ||
export function destroy() { | ||
element.destroy() | ||
} | ||
export function focus() { | ||
element.focus() | ||
} | ||
</script> | ||
|
||
<div bind:this={wrapper} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<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 { Elements, ExpressCheckout } from '$lib' | ||
let stripe = null | ||
let error = null | ||
let elements | ||
let processing = false | ||
onMount(async () => { | ||
stripe = await loadStripe(PUBLIC_STRIPE_KEY) | ||
}) | ||
async function createPaymentIntent() { | ||
const response = await fetch('/examples/express-checkout/payment-intent', { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
body: JSON.stringify({}) | ||
}) | ||
const { clientSecret } = await response.json() | ||
return clientSecret | ||
} | ||
async function click(event) { | ||
const options = { | ||
emailRequired: true, | ||
phoneNumberRequired: true, | ||
lineItems: [ | ||
{ | ||
name: 'Rad T-Shirt', | ||
amount: 1099 | ||
} | ||
] | ||
} | ||
event.detail.resolve(options) | ||
} | ||
async function confirm() { | ||
// avoid processing duplicates | ||
if (processing) return | ||
processing = true | ||
let result = await elements.submit() | ||
if (result.error) { | ||
// validation failed, notify user | ||
error = result.error | ||
processing = false | ||
return | ||
} | ||
// create payment intent server side | ||
const clientSecret = await createPaymentIntent() | ||
const return_url = new URL('/examples/express-checkout/thanks', window.location.origin).toString() | ||
// confirm payment with stripe | ||
result = await stripe.confirmPayment({ | ||
elements, | ||
clientSecret, | ||
confirmParams: { | ||
return_url | ||
} | ||
}) | ||
// log results, for debugging | ||
console.log({ result }) | ||
if (result.error) { | ||
// payment failed, notify user | ||
error = result.error | ||
processing = false | ||
} else { | ||
// payment succeeded, redirect to "thank you" page | ||
goto('/examples/express-checkout/thanks') | ||
} | ||
} | ||
</script> | ||
|
||
<h1>Express Checkout Example</h1> | ||
|
||
<nav> | ||
<a href="https://github.com/joshnuss/svelte-stripe/tree/main/src/routes/examples/express-checkout">View code</a> | ||
</nav> | ||
|
||
{#if error} | ||
<p class="error">{error.message} Please try again.</p> | ||
{/if} | ||
|
||
{#if stripe} | ||
<Elements | ||
{stripe} | ||
mode="payment" | ||
currency="usd" | ||
amount={1099} | ||
bind:elements> | ||
|
||
<ExpressCheckout | ||
on:confirm={confirm} | ||
on:click={click} | ||
buttonHeight={50} | ||
buttonTheme={{googlePay: 'white'}} | ||
buttonType={{googlePay: 'donate'}} | ||
paymentMethodOrder={['googlePay', 'link']}/> | ||
|
||
</Elements> | ||
{:else} | ||
Loading... | ||
{/if} | ||
|
||
<style> | ||
.error { | ||
color: tomato; | ||
margin: 2rem 0 0; | ||
} | ||
</style> |
19 changes: 19 additions & 0 deletions
19
src/routes/examples/express-checkout/payment-intent/+server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { json } from '@sveltejs/kit' | ||
import Stripe from 'stripe' | ||
import { SECRET_STRIPE_KEY } from '$env/static/private' | ||
|
||
const stripe = new Stripe(SECRET_STRIPE_KEY) | ||
|
||
export async function POST() { | ||
const paymentIntent = await stripe.paymentIntents.create({ | ||
amount: 1099, | ||
currency: 'usd', | ||
automatic_payment_methods: { | ||
enabled: true | ||
} | ||
}) | ||
|
||
return json({ | ||
clientSecret: paymentIntent.client_secret | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Success!</h1> | ||
<p>Payment was successfully processed.</p> |
62ace4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
svelte-stripe-js – ./
svelte-stripe-js-git-main-joshnuss.vercel.app
svelte-stripe-js.vercel.app
svelte-stripe-js-joshnuss.vercel.app
sveltestripe.com
www.sveltestripe.com