payments v1

This commit is contained in:
Adolfo Reyna
2021-11-10 22:03:49 -08:00
parent 687be41e3e
commit 364f38a28d
4 changed files with 112 additions and 43 deletions

View File

@@ -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;
}
}

View File

@@ -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) {

View File

@@ -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 });

63
routes/payments.js Normal file
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