payments v1

This commit is contained in:
Adolfo Reyna
2021-11-10 21:59:01 -08:00
parent 687be41e3e
commit a2b462abb7
4 changed files with 112 additions and 43 deletions
+39 -41
View File
@@ -1,44 +1,42 @@
const Stripe = require('stripe'); const Stripe = require('stripe');
const stripe = Stripe(process.env.STRIPE); const stripe = Stripe(process.env.STRIPE);
const addNewCustomer = async (email, profile) => { module.exports = {
const customer = await Stripe.customers.create({ async addNewCustomer(email, profile){
email, const customer = await Stripe.customers.create({
description: profile ? profile.firstName + " " + profile.lastName : "new costumer", email,
}); description: profile ? profile.firstName + " " + profile.lastName : "new costumer",
return customer; });
} return customer;
},
const getCustomerByID = async (id) => { async getCustomerByID(id){
const customer = await Stripe.customers.retrieve(id); const customer = await Stripe.customers.retrieve(id);
return customer; return customer;
} },
async addNewCustomerCard(customer, cardInfo){
const addNewCustomerCard = async (customer, cardInfo) => { let template = {
let template = { "address_city": cardInfo.address_city,
"address_city": cardInfo.address_city, "address_country": cardInfo.address_country,
"address_country": cardInfo.address_country, "address_line1": cardInfo.address_line1,
"address_line1": cardInfo.address_line1, "address_line2": cardInfo.address_line2,
"address_line2": cardInfo.address_line2, "address_state": cardInfo.address_state,
"address_state": cardInfo.address_state, "address_zip": cardInfo.address_zip,
"address_zip": cardInfo.address_zip, customer,
customer, "exp_month": cardInfo.exp_month,
"exp_month": cardInfo.exp_month, "exp_year": cardInfo.exp_year,
"exp_year": cardInfo.exp_year, "name": cardInfo.name,
"name": cardInfo.name, };
}; const card = await stripe.customers.createSource(
const card = await stripe.customers.createSource( customer,
'cus_KKsGI28Nrbm61K', template
template );
); return card;
return card; },
} async getCustomerCard(customer, cardId){
const card = await stripe.customers.retrieveSource(
const getCustomerCard = async (customer, card) => { customer,
const card = await stripe.customers.retrieveSource( cardId
customer, );
card return card;
); }
return card; }
}
+5 -2
View File
@@ -37,8 +37,10 @@ const getProfileId = function(req){
// Definitions // Definitions
const Post = require("./def/post.js") const Post = require("./def/post.js")
const Profile = require("./def/profile.js"); const Profile = require("./def/profile.js");
var profileRoute = require('./routes/profile.js'); const profileRoute = require('./routes/profile.js');
var postRoute = require('./routes/post.js'); const postRoute = require('./routes/post.js');
const paymentsRoute = require('./routes/payments.js');
DB.getDB.then((DB)=>{ DB.getDB.then((DB)=>{
@@ -213,6 +215,7 @@ DB.getDB.then((DB)=>{
app.use('/user', sessionChecker, profileRoute); app.use('/user', sessionChecker, profileRoute);
app.use('/post', sessionChecker, postRoute); app.use('/post', sessionChecker, postRoute);
app.use('/payments', sessionChecker, paymentsRoute);
// route for handling 404 requests(unavailable routes) // route for handling 404 requests(unavailable routes)
app.use(function (req, res, next) { app.use(function (req, res, next) {
+5
View File
@@ -40,6 +40,11 @@ const getDB = new Promise((resolve, reject) => {
.catch(console.error); .catch(console.error);
} }
DB.setUserCustomerId = (username, customer)=>{
return DB.usersCol.updateOne({username}, {$set:{customer}})
.catch(console.error);
}
DB.getUserById = (userid)=>{ DB.getUserById = (userid)=>{
const _id = new mongo.ObjectID(userid); const _id = new mongo.ObjectID(userid);
return DB.usersCol.findOne({ _id }); return DB.usersCol.findOne({ _id });
+63
View File
@@ -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