Created
March 11, 2019 17:36
-
-
Save bluebeel/7d711c5550e31a9ed4a3c9675b776c52 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')('secret-code-here') | |
const micro = require("micro"); | |
const isPost = (req) => { | |
if (!isPostRequest(req)) { | |
throw micro.createError(400, "POST request is required"); | |
} | |
return micro.json(req); | |
}; | |
const isPostRequest = (req) => { | |
return req.method.toLowerCase() === "POST".toLowerCase(); | |
}; | |
module.exports = (req, res) => { | |
const body = isPost(req) | |
console.log(body) | |
const { tokenId, email, name, description, amount } = body | |
stripe.charges | |
.create({ | |
amount, | |
currency: 'usd', | |
description, | |
source: tokenId | |
}) | |
.then(charge => { | |
return res.status(200).send({ credits: 100 }) | |
}) | |
.catch(err => { | |
console.log(err) | |
return res.send(err.message) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment