Skip to content

Instantly share code, notes, and snippets.

@bluebeel
Created March 11, 2019 17:36
Show Gist options
  • Save bluebeel/7d711c5550e31a9ed4a3c9675b776c52 to your computer and use it in GitHub Desktop.
Save bluebeel/7d711c5550e31a9ed4a3c9675b776c52 to your computer and use it in GitHub Desktop.
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