Stripe

Accept a payment using Stripe’s Checkout flow.

S
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);
};
const stripe = require('stripe')(process.env.STRIPE_API_KEY);

exports.handler = async (event, context, callback) => {
  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 {
    statusCode: 200,
    body: JSON.stringify(session)
  };
};

Usage#

1

Create Checkout Session

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

2

Redirect to Checkout

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

3

Add Environment Variables

To securely access the API, we need to include the secret with each request. We also do not want to commit secrets to git. Thus, we should use an environment variable.

  • Netlify: Settings -> Build & deploy -> Environment -> Environment variables.
  • Vercel: Settings -> General -> Environment Variables.

Alternatively, you can add the enviornment variable to your vercel.json or netlify.toml file.

New functions in your inbox

Get access to functions when they're added.