diff --git a/index.js b/index.js index b6021c4..87883d0 100644 --- a/index.js +++ b/index.js @@ -54,7 +54,7 @@ const sessionChecker = require('./middleware/sessionChecker'); // -- Private Routes app.use('/user', sessionChecker, profileRoute); app.use('/post', sessionChecker, postRoute); -app.use('/payments', sessionChecker, paymentsRoute); +app.use('/payments', paymentsRoute); app.use('/bible', sessionChecker, bibleRoute); app.use('/songs', sessionChecker, songsRoute); // -- Public Routes diff --git a/routes/payments.js b/routes/payments.js index f3b902d..3f2b129 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -2,6 +2,7 @@ var express = require('express'); var router = express.Router(); const DB = require("../mongoDB.js"); +const mongo = require('mongodb'); //const Payments = require("../payments.js"); const Stripe = require('stripe'); const stripe = Stripe(process.env.STRIPE); @@ -52,23 +53,46 @@ DB.getDB.then((DB) => { ], }); - //Register in DB - const intent = { - paymentIntent, - userid, - price, - description, - client_secret: paymentIntent.client_secret, - }; - DB.newIntent(intent); + // check if user is email or userid + const isUserId = mongo.ObjectId.isValid(userid); + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + const isEmail = emailRegex.test(userid.trim().toLowerCase()); + console.log("isUserId: ", isUserId); + console.log("isEmail: ", isEmail); + console.log("userid: ", userid); + if (isUserId) { + //Register in DB + const intent = { + paymentIntent, + userid, + price, + description, + client_secret: paymentIntent.client_secret, + }; + DB.newIntent(intent); + + return res.send({ + clientSecret: paymentIntent.client_secret, + email: await DB.getUsernameByIdCache(userid), + price + }); + } + if (isEmail) { + //Register in DB + return res.send({ + clientSecret: paymentIntent.client_secret, + email: userid, + price + }); + } return res.send({ clientSecret: paymentIntent.client_secret, - email: await DB.getUsernameByIdCache(userid), + email: 'guess', price }); }; - + router.post("/create-payment-intent", intent); router.post("/intent", intent); @@ -84,7 +108,7 @@ DB.getDB.then((DB) => { }; //console.log(payment); const intent = await DB.getIntent(result.client_secret); - if(intent.description === "Subscription 1 Month"){ + if (intent.description === "Subscription 1 Month") { //update profile subscription status const profileid = getProfileId(req); const isSubscriptor = await DB.isSubscriptor(profileid);