Last active
July 5, 2020 16:14
-
-
Save ferreiro/c15ff1a7e8f390dde7c469e0e169f463 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 admin = require("firebase-admin"); | |
const bodyParser = require("body-parser"); | |
const cors = require("cors"); | |
const express = require("express"); | |
const stripeSdk = require("stripe"); | |
const { body } = require("express-validator"); | |
const get = require("lodash/get"); | |
const merge = require("lodash/merge"); | |
const { isEmpty } = require("lodash"); | |
const serviceAccount = require("../serviceAccountKey.json"); | |
const stripeKeys = require("../stripeKeys.json"); | |
const STRIPE_SECRET_KEY = stripeKeys.private; | |
const STRIPE_ENDPOINT_SECRET = stripeKeys.endpointSecret; | |
const stripe = stripeSdk(STRIPE_SECRET_KEY, { apiVersion: "" }); | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
}); | |
const db = admin.firestore(); | |
const app = express(); | |
const whitelist = ["http://localhost:8000", "*"]; | |
const corsOptions = { | |
origin: function (origin, callback) { | |
if (whitelist.indexOf(origin) !== -1) { | |
return callback(null, true); | |
} else { | |
return callback(null, true); | |
// return callback(new Error(`Not allowed by CORS ${origin}`)); | |
} | |
}, | |
}; | |
app.use(cors(corsOptions)); | |
const AVAILABLE_PRICING_IDS = [ | |
"price_1GxfxwFCA2zjcDcHnoM5pVjd", | |
"price_1GxfyzFCA2zjcDcHimmfy74C", | |
"price_1GxfwoFCA2zjcDcHQMFsTuV5", | |
]; | |
function getProducts() { | |
return new Promise((resolve, reject) => { | |
stripe.products.list({ limit: 5 }, (err, products) => { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(products); | |
}); | |
}); | |
} | |
function getPrices() { | |
return new Promise((resolve, reject) => { | |
stripe.prices.list({ limit: 5 }, (err, prices) => { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(prices); | |
}); | |
}); | |
} | |
app.post( | |
"/webhook-wuGHR59CbVRN", | |
bodyParser.raw({ type: "application/json" }), | |
(request, response) => { | |
const signature = request.headers["stripe-signature"]; | |
const stripeEndpointSecret = STRIPE_ENDPOINT_SECRET; | |
let event; | |
try { | |
event = stripe.webhooks.constructEvent( | |
request.body, | |
signature, | |
"whsec_XZJTKxeBwBUgeV1aAItpOEa3sOf50Ue4" | |
); | |
} catch (error) { | |
console.log(error); | |
return response.status(400).send(`Webhook Error: ${error.message}`); | |
} | |
const session = event.data.object; | |
console.log("STRIPE WEBHOOK SIGNATURE VALIDATED"); | |
console.log(session); | |
switch (event.type) { | |
case "checkout.session.completed": | |
updateUserSubscription(session); | |
break; | |
case "customer.subscription.trial_will_end": | |
break; | |
case "customer.subscription.deleted": | |
break; | |
case "customer.subscription.updated": | |
break; | |
} | |
// Return a response to acknowledge receipt of the event | |
return response.json({ received: true }); | |
} | |
); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment