Last active
March 13, 2019 02:19
-
-
Save talkaboutdesign/9ea2508e4305eacd8f5313e45a7a1768 to your computer and use it in GitHub Desktop.
Serverless Stripe payment processing. Compatible with Zeit.co Now 2.0.
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')('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