Initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
const redis = require("../../../Miscellaneous/redis");
|
||||
const stripe = require("../../../Miscellaneous/stripe");
|
||||
|
||||
const entries = require("../../../Data/payment");
|
||||
|
||||
function badRequest(res) {
|
||||
console.trace("asd")
|
||||
res.cork(() => res
|
||||
.writeStatus("400")
|
||||
.end());
|
||||
};
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
const paymentPacketID = req.getQuery("paymentPacketId");
|
||||
console.info("payment_intent");
|
||||
const cookieHeader = req.getHeader("cookie");
|
||||
const cookie = cookieHeader.split("ps=")[1];
|
||||
|
||||
res.onAborted(() => console.log("aborted"));
|
||||
|
||||
if (!cookie) return badRequest(res);
|
||||
|
||||
const paymentPacket = entries[paymentPacketID];
|
||||
if (!paymentPacketID || !paymentPacket) return badRequest(res);
|
||||
|
||||
const formattedCookie = cookie.split(".")[0].slice(4);
|
||||
const cookieInDB = await redis.get(`sess:${formattedCookie}`);
|
||||
|
||||
if (!cookieInDB) return badRequest(res);
|
||||
|
||||
const parsedCookie = JSON.parse(cookieInDB);
|
||||
const { accountID } = parsedCookie;
|
||||
|
||||
const existingClientSecret = await redis.get(`o_${accountID}_${paymentPacketID}`);
|
||||
let clientSecret = null;
|
||||
|
||||
try {
|
||||
const { amount, unit_amount } = paymentPacket;
|
||||
if (!existingClientSecret) {
|
||||
const paymentIntent = await stripe.paymentIntents.create({
|
||||
currency: "EUR",
|
||||
amount: unit_amount,
|
||||
automatic_payment_methods: {
|
||||
enabled: true
|
||||
},
|
||||
metadata: {
|
||||
paymentPacketID,
|
||||
playerID: accountID
|
||||
}
|
||||
});
|
||||
|
||||
clientSecret = paymentIntent.client_secret;
|
||||
await redis.set(`o_${accountID}_${paymentPacketID}`, clientSecret);
|
||||
} else {
|
||||
clientSecret = existingClientSecret;
|
||||
};
|
||||
|
||||
const response = JSON.stringify({
|
||||
clientSecret,
|
||||
amount,
|
||||
price: unit_amount
|
||||
});
|
||||
|
||||
res.cork(() => {
|
||||
res
|
||||
.writeStatus("200")
|
||||
.end(response);
|
||||
});
|
||||
} catch (e) {
|
||||
console.info(e)
|
||||
return badRequest(res);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user