Created
March 27, 2020 06:34
-
-
Save imprakharshukla/1e2315615983e0e9d492d2288e159832 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 express = require('express'); | |
const app = express(); | |
const bodyParser = require('body-parser'); | |
//Removed for sec reasons | |
const stripe = require('stripe')('sk_test_xxxxx'); | |
const endpointSecret = 'whsec_xxxxx'; | |
// set the port of our application | |
// process.env.PORT lets the port be set by Heroku | |
var port = process.env.PORT || 8080; | |
app.post('/', bodyParser.raw({type: 'application/json'}), (request, response) => { | |
const sig = request.headers['stripe-signature']; | |
let event; | |
try { | |
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret); | |
} catch (err) { | |
return response.status(400).send(`Webhook Error: ${err.message}`); | |
} | |
function handleCheckoutSession(uid) { | |
// Here we are getting the session obj and we can process it to check for the things we need | |
console.log("UID is " + uid); | |
} | |
// Handle the checkout.session.completed event | |
if (event.type === 'checkout.session.completed') { | |
const session = event.data.object; | |
------------------------------------------ | |
let uid = request.data.metadata.uid; | |
THE METADATA IS UNDEFINED HERE | |
---------------------------------------- | |
// Fulfill the purchase... | |
handleCheckoutSession(uid); | |
} | |
// Return a response to acknowledge receipt of the event | |
response.json({received: true}); | |
}); | |
app.listen(port, function () { | |
console.log('Our app is running on http://localhost:' + port); | |
}); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment