Skip to content

Instantly share code, notes, and snippets.

@talkaboutdesign
Last active March 13, 2019 02:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talkaboutdesign/9ea2508e4305eacd8f5313e45a7a1768 to your computer and use it in GitHub Desktop.
Save talkaboutdesign/9ea2508e4305eacd8f5313e45a7a1768 to your computer and use it in GitHub Desktop.
Serverless Stripe payment processing. Compatible with Zeit.co Now 2.0.
const stripe = require('stripe')('your-stripe-secret')
const { text } = require('micro')
module.exports = async (req, res) => {
// Need to process body from request coming in.
const body = JSON.parse(await text(req))
// Process payment using details from incoming request. You might also optionally want to create a customer before initiating a payment.
const charge = await stripe.charges
.create({
amount: body.amount,
currency: 'usd',
source: body.tokenId,
description: body.description
})
.then(charge => {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end('Payment success')
})
.catch(err => {
res.writeHead(err.statusCode, { 'Content-Type': 'application/json' })
res.end(err)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment