Coded Geekery

BlogWritingAboutContact
BlogWritingAboutContact

Stripe

Accept a payment.

const stripe = require('stripe')(process.env.STRIPE_API_KEY); export default async (req, res) => { const session = await stripe.checkout.sessions.create({ payment_method_types: ['card'], line_items: [ { name: 'Serverless Functions – The Complete Guide', description: '100 page e-book on serverless functions.', images: ['https://site.com/image.png'], amount: '5000', // Cents currency: 'usd', quantity: 1 } ], success_url: 'https://site.com/success?session_id={CHECKOUT_SESSION_ID}', cancel_url: 'https://site.com' }); return res.status(200).json(session); };

Usage#

  • Create Checkout Session

  • Using the function above, create a checkout session returning CHECKOUT_SESSION_ID. Upon success, it will redirect to your success_url.

  • Redirect to Checkout

  • Follow Stripe's documentation to complete the client-side.

  • Add Environment Variables

  • To securely access the API, we need to include the secret with each request.

    Remember: never commit secrets to git. Thus, we should use an environment variable.

    Not Playing

    Spotify

    Coded Geekery

    TwitterGitHubInstagramgumroadbuymeacoffee

    Stuff

    HomeAbout MeContact Me

    © 2022 Roger Stringer. All rights reserved.