148ed696b2
This commit introduces Swagger API documentation for all endpoints in the application. - Installs and . - Configures Swagger in to generate and serve API documentation at . - Adds JSDoc-style Swagger annotations to all routes in and the directory (, , , , , ). - Defines a cookie-based security scheme for authenticated routes. This allows for interactive API documentation and testing via the endpoint.
202 lines
5.4 KiB
JavaScript
202 lines
5.4 KiB
JavaScript
var express = require('express');
|
|
var router = express.Router();
|
|
|
|
const DB = require("../mongoDB.js");
|
|
const mongo = require('mongodb');
|
|
//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",
|
|
// });
|
|
// });
|
|
|
|
/**
|
|
* @swagger
|
|
* tags:
|
|
* name: Payments
|
|
* description: Payment processing
|
|
*/
|
|
|
|
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",
|
|
],
|
|
});
|
|
|
|
// check if user is email or userid
|
|
const isUserId = mongo.ObjectId.isValid(userid);
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
const isEmail = emailRegex.test(userid.trim().toLowerCase());
|
|
console.log("isUserId: ", isUserId);
|
|
console.log("isEmail: ", isEmail);
|
|
console.log("userid: ", userid);
|
|
|
|
if (isUserId) {
|
|
//Register in DB
|
|
const intent = {
|
|
paymentIntent,
|
|
userid,
|
|
price,
|
|
description,
|
|
client_secret: paymentIntent.client_secret,
|
|
};
|
|
DB.newIntent(intent);
|
|
|
|
return res.send({
|
|
clientSecret: paymentIntent.client_secret,
|
|
email: await DB.getUsernameByIdCache(userid),
|
|
price
|
|
});
|
|
}
|
|
if (isEmail) {
|
|
//Register in DB
|
|
return res.send({
|
|
clientSecret: paymentIntent.client_secret,
|
|
email: userid,
|
|
price
|
|
});
|
|
}
|
|
return res.send({
|
|
clientSecret: paymentIntent.client_secret,
|
|
email: 'guess',
|
|
price
|
|
});
|
|
};
|
|
|
|
|
|
/**
|
|
* @swagger
|
|
* /payments/create-payment-intent:
|
|
* post:
|
|
* summary: Creates a Stripe Payment Intent
|
|
* tags: [Payments]
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* userid:
|
|
* type: string
|
|
* price:
|
|
* type: number
|
|
* description:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.post("/create-payment-intent", intent);
|
|
/**
|
|
* @swagger
|
|
* /payments/intent:
|
|
* post:
|
|
* summary: Creates a Stripe Payment Intent (Alias for /create-payment-intent)
|
|
* tags: [Payments]
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* userid:
|
|
* type: string
|
|
* price:
|
|
* type: number
|
|
* description:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.post("/intent", intent);
|
|
|
|
/**
|
|
* @swagger
|
|
* /payments/register:
|
|
* post:
|
|
* summary: Registers a payment after a successful Stripe transaction
|
|
* tags: [Payments]
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* userid:
|
|
* type: string
|
|
* result:
|
|
* type: object
|
|
* responses:
|
|
* 200:
|
|
* description: OK
|
|
*/
|
|
router.post("/register", async (req, res) => {
|
|
const userid = req.body.userid;
|
|
const result = req.body.result;
|
|
|
|
//Register in DB
|
|
const payment = {
|
|
result,
|
|
userid
|
|
};
|
|
//console.log(payment);
|
|
const intent = await DB.getIntent(result.client_secret);
|
|
if (intent.description === "Subscription 1 Month") {
|
|
//update profile subscription status
|
|
const profileid = getProfileId(req);
|
|
const isSubscriptor = await DB.isSubscriptor(profileid);
|
|
const updateR = await DB.updateProfileSubscription(profileid, !isSubscriptor);
|
|
console.log(updateR);
|
|
}
|
|
await DB.newResult(payment, result.client_secret);
|
|
|
|
return res.send({
|
|
status: 'ok'
|
|
});
|
|
});
|
|
|
|
|
|
});
|
|
|
|
module.exports = router |