Initial commit
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
const RewardPacket = require("../../../Packets/Reward");
|
||||
|
||||
const Game = require("../../../Game/Game");
|
||||
|
||||
const stripe = require("../../../Miscellaneous/stripe");
|
||||
const redis = require("../../../Miscellaneous/redis");
|
||||
|
||||
const entries = require("../../../Data/payment");
|
||||
const STRIPE_IP_COLLECTION = require("../../../Data/stripe");
|
||||
|
||||
require("dotenv").config({ path: "../.env" });
|
||||
|
||||
const textDecoder = new TextDecoder();
|
||||
|
||||
const PAYMENT_INTENT_SUCCEEDED = "payment_intent.succeeded";
|
||||
|
||||
function badRequest(res) {
|
||||
res
|
||||
.writeStatus("400")
|
||||
.end();
|
||||
};
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
const sig = req.getHeader("stripe-signature");
|
||||
if (!sig) return badRequest(res);
|
||||
|
||||
const realIP = req.getHeader("x-real-ip");
|
||||
if (!STRIPE_IP_COLLECTION[realIP]) return badRequest(res);
|
||||
|
||||
res.onAborted(() => console.log("/webhook path aborted connection!"));
|
||||
|
||||
let data;
|
||||
|
||||
// it works because there is no multiple chunks, but should address this issue
|
||||
res.onData(async (raw, isLast) => {
|
||||
const stringData = textDecoder.decode(raw);
|
||||
|
||||
try {
|
||||
data = stripe.webhooks.constructEvent(stringData, sig, process.env.STRIPE_WEBHOOK_ENDPOINT_SECRET);
|
||||
} catch (err) {
|
||||
console.log(err.message)
|
||||
return badRequest(res);
|
||||
};
|
||||
|
||||
if (data.type === PAYMENT_INTENT_SUCCEEDED) {
|
||||
const { paymentPacketID, playerID } = data.data.object.metadata;
|
||||
if (!paymentPacketID || !playerID) return badRequest(res);
|
||||
|
||||
const paymentPackage = entries[paymentPacketID];
|
||||
const { amount } = paymentPackage;
|
||||
|
||||
const player = Game.players[playerID];
|
||||
if (player) {
|
||||
const boughtItem = {
|
||||
categoryID: 1,
|
||||
id: 4,
|
||||
amount
|
||||
};
|
||||
|
||||
player.inventory.refreshSingle(boughtItem);
|
||||
|
||||
const initializedPacket = RewardPacket.init(true, 1);
|
||||
initializedPacket.update(boughtItem);
|
||||
|
||||
const packet = initializedPacket.get();
|
||||
player.map.registerIndividualNetworkPacket(player.id, packet);
|
||||
};
|
||||
|
||||
await redis.del(`o_${playerID}_${paymentPacketID}`);
|
||||
await execute("UPDATE economy SET corsaireCoins = corsaireCoins + ? WHERE playerID = ?", [amount, playerID]);
|
||||
};
|
||||
|
||||
res.cork(() => res.writeStatus("200").end());
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user