Last active
October 11, 2020 21:14
-
-
Save AlbionaHoti/a2355927e3a79f2db0bd6c78c47a929d to your computer and use it in GitHub Desktop.
webiny-starter-e-commerce-nextjs-stripe
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
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