Skip to content

Commit

Permalink
Delete transaction token if transaction is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedal987 committed Jun 10, 2024
1 parent f3cc4ef commit a9ca524
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
15 changes: 14 additions & 1 deletion ebs/src/modules/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { app } from "../index";
import { parseJWT, verifyJWT } from "../util/jwt";
import { BitsTransactionPayload } from "../types";
import { getConfig } from "./config";
import { getPrepurchase, isReceiptUsed, isUserBanned, registerPrepurchase } from "../util/db";
import { getPrepurchase, isReceiptUsed, isUserBanned, registerPrepurchase, deletePrepurchase } from "../util/db";
import { logToDiscord } from "../util/logger";
import { connection } from "./game";

Expand Down Expand Up @@ -203,3 +203,16 @@ app.post("/public/transaction", async (req, res) => {

res.sendStatus(200);
});

app.post("/public/transaction/cancel", async (req, res) => {
const token = req.body as string;

// remove transaction from db
try {
await deletePrepurchase(token);

res.sendStatus(200);
} catch (error) {
res.sendStatus(404);
}
});
10 changes: 10 additions & 0 deletions ebs/src/util/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ export async function getPrepurchase(token: string): Promise<IdentifiableCart |
}
}

export async function deletePrepurchase(token: string) {
try {
await db.query("DELETE FROM prepurchases WHERE token = ?", [token]);
} catch (e: any) {
console.error("Database query failed (deletePrepurchase)");
console.error(e);
throw new Error("Database query failed");
}
}

export async function isUserBanned(userId: string): Promise<boolean> {
try {
const [rows] = (await db.query("SELECT COUNT(*) FROM bans WHERE userId = ?", [userId])) as [RowDataPacket[], any];
Expand Down
7 changes: 7 additions & 0 deletions frontend/www/src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ Twitch.ext.bits.onTransactionCancelled(async () => {
],
}).then();
}
await ebsFetch("/public/transaction/cancel", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: transactionToken,
});
hideProcessingModal();
showErrorModal("Transaction cancelled.", `Transaction ID: ${transactionToken}`);
});

0 comments on commit a9ca524

Please sign in to comment.