Skip to content

Instantly share code, notes, and snippets.

@AlbionaHoti
Last active October 11, 2020 21:14
Show Gist options
  • Save AlbionaHoti/a2355927e3a79f2db0bd6c78c47a929d to your computer and use it in GitHub Desktop.
Save AlbionaHoti/a2355927e3a79f2db0bd6c78c47a929d to your computer and use it in GitHub Desktop.
webiny-starter-e-commerce-nextjs-stripe
import React from 'react'
import Stripe from 'stripe'
import { parseCookies, setCookie } from 'nookies'
import { loadStripe } from '@stripe/stripe-js'
import { Elements } from '@stripe/react-stripe-js'
// Components
import CheckoutForm from '../components/CheckoutForm'
// initialize the stripe library and create a payment intent
const stripePromise = new loadStripe(
'STRIPE_PUBLISHABLE_KEY_HERE'
)
export const getServerSideProps = async ({ ctx }) => {
const stripe = new Stripe(
'STRIPE_SECRET_KEY_HERE'
)
let paymentIntent
const { paymentIntentId } = await parseCookies(ctx)
if (paymentIntentId) {
paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId)
return {
props: {
paymentIntent,
},
}
}
// create a payment intent
paymentIntent = await stripe.paymentIntents.create({
amount: 1000,
currency: 'usd',
})
// pass the context which is giving to us be serversideprops.
setCookie(ctx, 'paymentIntentId', paymentIntent.id)
return {
props: {
paymentIntent,
},
}
}
const CheckoutPage = ({ paymentIntent }) => {
return (
<Elements stripe={stripePromise}>
<CheckoutForm paymentIntent={paymentIntent} />
</Elements>
)
}
export default CheckoutPage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment