Created
November 11, 2019 06:37
-
-
Save rlingineni/3b606adaab8b5e3ec80bcdbf389d0388 to your computer and use it in GitHub Desktop.
AWS Lambda and Stripe Example
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
var stripe = require('stripe')('STRIPE KEY HERE'); | |
var Query = require('url-query-parser'); | |
var AWS = require('aws-sdk'); | |
Date.prototype.addDays = function(days) { | |
this.setDate(this.getDate() + parseInt(days)); | |
return this; | |
}; | |
exports.myHandler = function(event, context, callback) { | |
var query = Query.parse(event.body); | |
var randomString = shortid.generate(); | |
var token = query.get('stripeToken')[0]; | |
var email = query.get('stripeEmail')[0]; | |
console.log(email , " ", token); | |
// Create a Customer: | |
stripe.customers.create({ | |
email: email, | |
source: token, | |
},function(err,customer){ | |
if(err){ | |
console.log(err); | |
helper.sendError({msg:"Something went wrong! Send an e-mail to: [email protected]"},callback); | |
} | |
// Charge the user's card: | |
stripe.charges.create({ | |
amount: 500, | |
currency: "usd", | |
description: "isLocation API", | |
customer: customer.id | |
}, function(err, charge) { | |
if(err){ | |
console.log(err); | |
helper.sendError({msg:"Something went wrong! Send an e-mail to: [email protected]"},callback); | |
} | |
console.log(customer.id) | |
// Charge the user's card: | |
}); | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment