From cb056a078a80b3d2696fc8d220bfb766965e6d67 Mon Sep 17 00:00:00 2001 From: aeroreyna Date: Sun, 23 Jan 2022 20:16:50 -0800 Subject: [PATCH] Record Payments Intents --- dbTools/payments.js | 22 ++++++++++++++++++++++ mongoDB.js | 2 ++ routes/payments.js | 42 +++++++++++++++++++++++++++++++++++------- 3 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 dbTools/payments.js diff --git a/dbTools/payments.js b/dbTools/payments.js new file mode 100644 index 0000000..af7f116 --- /dev/null +++ b/dbTools/payments.js @@ -0,0 +1,22 @@ +const DBName = "EMI_SOCIAL"; + +postDB = (DB)=>{ + DB.paymentsCols = DB.db.db(DBName).collection("payments"); + + DB.newIntent = (intent) => { + return DB.paymentsCols.insertOne(intent).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.newResult = (paymentResult) => { + return DB.paymentsCols.insertOne(paymentResult).catch((err)=>{ + console.log(err); + return false; + }); + } + +} + +module.exports = postDB; \ No newline at end of file diff --git a/mongoDB.js b/mongoDB.js index 16adc0a..9993a54 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -5,6 +5,7 @@ const DBName = "EMI_SOCIAL"; const mongoUrl = process.env.MONGO_URL; const postDB = require("./dbTools/post.js"); const profileDB = require("./dbTools/profile.js"); +const paymentDB = require("./dbTools/payments.js") const getDB = new Promise((resolve, reject) => { @@ -107,6 +108,7 @@ const getDB = new Promise((resolve, reject) => { postDB(DB); profileDB(DB); + paymentDB(DB); resolve(DB); }); diff --git a/routes/payments.js b/routes/payments.js index 504c85a..32a83c6 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -1,5 +1,5 @@ -var express = require('express') -var router = express.Router() +var express = require('express'); +var router = express.Router(); const DB = require("../mongoDB.js"); //const Payments = require("../payments.js"); @@ -37,12 +37,11 @@ DB.getDB.then((DB) => { // status: "ok", // }); // }); - - router.post("/create-payment-intent", async (req, res) => { + let intent = async (req, res) => { const userid = req.body.userid; const price = req.body.price || 500; - console.log(req.body) + const description = req.body.description; // Create a PaymentIntent with the order amount and currency const paymentIntent = await stripe.paymentIntents.create({ @@ -53,11 +52,40 @@ DB.getDB.then((DB) => { ], }); - res.send({ + //Register in DB + const intent = { + paymentIntent, + userid, + price, + description + }; + DB.newIntent(intent); + + return res.send({ clientSecret: paymentIntent.client_secret, - email: await DB.getUsernameByIdCache(req.body.userid), + email: await DB.getUsernameByIdCache(userid), price }); + }; + + + router.post("/create-payment-intent", intent); + router.post("/intent", intent); + + router.post("/register", async (req, res) => { + const userid = req.body.userid; + const result = req.body.result; + + //Register in DB + const payment = { + result, + userid + }; + DB.newResult(payment); + + return res.send({ + status: 'ok' + }); });