94 lines
2.4 KiB
JavaScript
94 lines
2.4 KiB
JavaScript
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",
|
|
// });
|
|
// });
|
|
|
|
let intent = async (req, res) => {
|
|
const userid = req.body.userid;
|
|
const price = req.body.price || 500;
|
|
const description = req.body.description;
|
|
|
|
// Create a PaymentIntent with the order amount and currency
|
|
const paymentIntent = await stripe.paymentIntents.create({
|
|
amount: price,
|
|
currency: "usd",
|
|
payment_method_types: [
|
|
"card",
|
|
],
|
|
});
|
|
|
|
//Register in DB
|
|
const intent = {
|
|
paymentIntent,
|
|
userid,
|
|
price,
|
|
description
|
|
};
|
|
DB.newIntent(intent);
|
|
|
|
return res.send({
|
|
clientSecret: paymentIntent.client_secret,
|
|
email: await DB.getUsernameByIdCache(userid),
|
|
price
|
|
});
|
|
};
|
|
|
|
|
|
router.post("/create-payment-intent", intent);
|
|
router.post("/intent", intent);
|
|
|
|
router.post("/register", async (req, res) => {
|
|
const userid = req.body.userid;
|
|
const result = req.body.result;
|
|
|
|
//Register in DB
|
|
const payment = {
|
|
result,
|
|
userid
|
|
};
|
|
DB.newResult(payment);
|
|
|
|
return res.send({
|
|
status: 'ok'
|
|
});
|
|
});
|
|
|
|
|
|
});
|
|
|
|
module.exports = router |