Created
June 29, 2022 12:46
-
-
Save cjavilla-stripe/b063e2dd9796ed645eb85324d37a9364 to your computer and use it in GitHub Desktop.
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
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); | |
/* | |
* Next available dates loaded from some database | |
*/ | |
const calendar = require('./data/calendar.json'); | |
exports.handler = async (event) => { | |
const nextDates = getNextDates(calendar) | |
const { startDate, endDate } = nextDates; | |
const session = await stripe.checkout.sessions.create({ | |
mode: 'payment', | |
/* | |
* This env var is set by Netlify and inserts the live site URL. If you want | |
* to use a different URL, you can hard-code it here or check out the | |
* other environment variables Netlify exposes: | |
* https://docs.netlify.com/configure-builds/environment-variables/ | |
*/ | |
success_url: `${process.env.URL}/success.html`, | |
cancel_url: process.env.URL, | |
line_items: [ | |
{ | |
price_data: { | |
currency: 'cad', | |
unit_amount: 100000, // in cents | |
product_data: { | |
name: 'Product Coaching', | |
description: dateDescription(nextDates), | |
}, | |
}, | |
quantity: 1, | |
}, | |
], | |
// We are using the metadata to track which items were purchased. | |
// We can access this meatadata in our webhook handler to then handle | |
// the fulfillment process. | |
// In a real application you would track this in an order object in your database. | |
metadata: { | |
start_date: startDate, | |
end_date: endDate | |
}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment