From 4831ae523c1b86b28b0bfe1bef2ecc420fc57dda Mon Sep 17 00:00:00 2001 From: Leon Xu Date: Sat, 7 May 2022 14:42:13 -0700 Subject: [PATCH] doc: add express, cors, and stripe along with middleware and routes to index.js --- functions/index.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/functions/index.js b/functions/index.js index 081873b..4dc38f3 100644 --- a/functions/index.js +++ b/functions/index.js @@ -1,9 +1,26 @@ const functions = require("firebase-functions"); +const express = require("express"); +const cors = require("cors"); +const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY); -// // Create and Deploy Your First Cloud Functions -// // https://firebase.google.com/docs/functions/write-firebase-functions -// -// exports.helloWorld = functions.https.onRequest((request, response) => { -// functions.logger.info("Hello logs!", {structuredData: true}); -// response.send("Hello from Firebase!"); -// }); +// App Config +const app = express(); + +// Middleware +app.use(cors({ origin: true })); +app.use(express.json()); + +// API Routes +app.post("/paymens/create", async (req, res) => { + const total = request.query.total; + const paymentIntent = await stripe.paymentIntents.create({ + amount: total, // subunits of the currency + currency: "usd", + }); + res.status(201).send({ + clientSecret: paymentIntent.client_secret, + }); +}); + +// Listen command +exports.api = functions.https.onRequest(app);