From a2b462abb7a4b904c087205269061ec7c153e184 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Wed, 10 Nov 2021 21:59:01 -0800 Subject: [PATCH] payments v1 --- Payments.js | 80 ++++++++++++++++++++++------------------------ index.js | 7 ++-- mongoDB.js | 5 +++ routes/payments.js | 63 ++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 43 deletions(-) create mode 100644 routes/payments.js diff --git a/Payments.js b/Payments.js index 0a3c07b..35f16f2 100644 --- a/Payments.js +++ b/Payments.js @@ -1,44 +1,42 @@ const Stripe = require('stripe'); - const stripe = Stripe(process.env.STRIPE); -const addNewCustomer = async (email, profile) => { - const customer = await Stripe.customers.create({ - email, - description: profile ? profile.firstName + " " + profile.lastName : "new costumer", - }); - return customer; -} - -const getCustomerByID = async (id) => { - const customer = await Stripe.customers.retrieve(id); - return customer; -} - -const addNewCustomerCard = async (customer, cardInfo) => { - let template = { - "address_city": cardInfo.address_city, - "address_country": cardInfo.address_country, - "address_line1": cardInfo.address_line1, - "address_line2": cardInfo.address_line2, - "address_state": cardInfo.address_state, - "address_zip": cardInfo.address_zip, - customer, - "exp_month": cardInfo.exp_month, - "exp_year": cardInfo.exp_year, - "name": cardInfo.name, - }; - const card = await stripe.customers.createSource( - 'cus_KKsGI28Nrbm61K', - template - ); - return card; -} - -const getCustomerCard = async (customer, card) => { - const card = await stripe.customers.retrieveSource( - customer, - card - ); - return card; -} +module.exports = { + async addNewCustomer(email, profile){ + const customer = await Stripe.customers.create({ + email, + description: profile ? profile.firstName + " " + profile.lastName : "new costumer", + }); + return customer; + }, + async getCustomerByID(id){ + const customer = await Stripe.customers.retrieve(id); + return customer; + }, + async addNewCustomerCard(customer, cardInfo){ + let template = { + "address_city": cardInfo.address_city, + "address_country": cardInfo.address_country, + "address_line1": cardInfo.address_line1, + "address_line2": cardInfo.address_line2, + "address_state": cardInfo.address_state, + "address_zip": cardInfo.address_zip, + customer, + "exp_month": cardInfo.exp_month, + "exp_year": cardInfo.exp_year, + "name": cardInfo.name, + }; + const card = await stripe.customers.createSource( + customer, + template + ); + return card; + }, + async getCustomerCard(customer, cardId){ + const card = await stripe.customers.retrieveSource( + customer, + cardId + ); + return card; + } +} \ No newline at end of file diff --git a/index.js b/index.js index 01a72d0..e7a7996 100644 --- a/index.js +++ b/index.js @@ -37,8 +37,10 @@ const getProfileId = function(req){ // Definitions const Post = require("./def/post.js") const Profile = require("./def/profile.js"); -var profileRoute = require('./routes/profile.js'); -var postRoute = require('./routes/post.js'); +const profileRoute = require('./routes/profile.js'); +const postRoute = require('./routes/post.js'); +const paymentsRoute = require('./routes/payments.js'); + DB.getDB.then((DB)=>{ @@ -213,6 +215,7 @@ DB.getDB.then((DB)=>{ app.use('/user', sessionChecker, profileRoute); app.use('/post', sessionChecker, postRoute); + app.use('/payments', sessionChecker, paymentsRoute); // route for handling 404 requests(unavailable routes) app.use(function (req, res, next) { diff --git a/mongoDB.js b/mongoDB.js index 7970b95..98e5f33 100644 --- a/mongoDB.js +++ b/mongoDB.js @@ -40,6 +40,11 @@ const getDB = new Promise((resolve, reject) => { .catch(console.error); } + DB.setUserCustomerId = (username, customer)=>{ + return DB.usersCol.updateOne({username}, {$set:{customer}}) + .catch(console.error); + } + DB.getUserById = (userid)=>{ const _id = new mongo.ObjectID(userid); return DB.usersCol.findOne({ _id }); diff --git a/routes/payments.js b/routes/payments.js new file mode 100644 index 0000000..a9affcb --- /dev/null +++ b/routes/payments.js @@ -0,0 +1,63 @@ +var express = require('express') +var router = express.Router() + +const DB = require("../mongoDB.js"); +const Payments = require("../payments.js"); +const Stripe = require('stripe'); +const stripe = Stripe(process.env.STRIPE); + +DB.getDB.then((DB) => { + + const getUserId = function (req) { + const user_sid = req.cookies.user_sid || req.query.user_sid || req.body.user_sid; + return DB.ObjectID(user_sid); + } + + const getProfileId = (req) => { + return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id); + } + + router.get("/", async (req, res) => { + //let profiles = await DB.getUserProfiles(userid); + //Payments.getCustomerCard() + return res.json({ + status: "ok", + }); + }); + + router.post("/card", async (req, res) => { + //get user stripe customer + //Payments.getCustomerByID + //if not customer register, make one + //Payments.addNewCustomer() + //add card to customer + const cardInfo = req.body.cardInfo; + //Payments.addNewCustomerCard() + return res.json({ + status: "ok", + }); + }); + + router.post("/create-payment-intent", async (req, res) => { + const { items } = req.body; + console.log("payments", req.body) + + // Create a PaymentIntent with the order amount and currency + const paymentIntent = await stripe.paymentIntents.create({ + amount: 500, + currency: "usd", + payment_method_types: [ + "card", + ], + }); + + res.send({ + clientSecret: paymentIntent.client_secret, + email: await DB.getUsernameByIdCache(req.body.userid) + }); + }); + + +}); + +module.exports = router \ No newline at end of file