Skip to content

Instantly share code, notes, and snippets.

@alexanderson1993
Created March 4, 2020 23:35
Show Gist options
  • Save alexanderson1993/7842ce95b41851328d1ab74149f3c119 to your computer and use it in GitHub Desktop.
Save alexanderson1993/7842ce95b41851328d1ab74149f3c119 to your computer and use it in GitHub Desktop.
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
var stripe = require("stripe")(process.env.STRIPE_KEY_SECRET);
exports.handler = (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
const done = (err, res) =>
callback(null, {
statusCode: err ? "400" : "200",
body: err ? err.message : JSON.stringify(res),
headers: {
"Content-Type": "application/json",
"X-Requested-With":"*",
"Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with",
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Methods":"POST,GET,OPTIONS"
}
});
switch (event.httpMethod) {
case "POST":
// Token is created using Stripe.js or Checkout!
// Charge the user's card:
var body = JSON.parse(event.body) || event.body;
var charge = stripe.charges.create(
{
amount: body.amount,
currency: "usd",
source: body.stripeToken,
description: body.description || 'Thorium Donation payment '+body.order_id,
receipt_email: body.receipt_email || null
},
function(err, charge) {
done(err, charge);
}
);
break;
default:
done(new Error(`Unsupported method "${event.httpMethod}"`));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment