payments v1
This commit is contained in:
24
Payments.js
24
Payments.js
@@ -1,21 +1,19 @@
|
|||||||
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 = {
|
||||||
|
async addNewCustomer(email, profile){
|
||||||
const customer = await Stripe.customers.create({
|
const customer = await Stripe.customers.create({
|
||||||
email,
|
email,
|
||||||
description: profile ? profile.firstName + " " + profile.lastName : "new costumer",
|
description: profile ? profile.firstName + " " + profile.lastName : "new costumer",
|
||||||
});
|
});
|
||||||
return customer;
|
return customer;
|
||||||
}
|
},
|
||||||
|
async getCustomerByID(id){
|
||||||
const getCustomerByID = async (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,
|
||||||
@@ -29,16 +27,16 @@ const addNewCustomerCard = async (customer, cardInfo) => {
|
|||||||
"name": cardInfo.name,
|
"name": cardInfo.name,
|
||||||
};
|
};
|
||||||
const card = await stripe.customers.createSource(
|
const card = await stripe.customers.createSource(
|
||||||
'cus_KKsGI28Nrbm61K',
|
customer,
|
||||||
template
|
template
|
||||||
);
|
);
|
||||||
return card;
|
return card;
|
||||||
}
|
},
|
||||||
|
async getCustomerCard(customer, cardId){
|
||||||
const getCustomerCard = async (customer, card) => {
|
|
||||||
const card = await stripe.customers.retrieveSource(
|
const card = await stripe.customers.retrieveSource(
|
||||||
customer,
|
customer,
|
||||||
card
|
cardId
|
||||||
);
|
);
|
||||||
return card;
|
return card;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
7
index.js
7
index.js
@@ -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) {
|
||||||
|
|||||||
@@ -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
routes/payments.js
Normal file
63
routes/payments.js
Normal 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
|
||||||
Reference in New Issue
Block a user