Untitled
unknown
plain_text
9 months ago
1.8 kB
8
Indexable
app.post("/generate-payment", async (req, res) => {
const { userWalletAddress, amountInSOL = 0.015 } = req.body;
if (!userWalletAddress) {
return res.status(400).json({ success: false, error: "User wallet address is required" });
}
try {
new PublicKey(userWalletAddress);
new PublicKey(YOUR_WALLET_ADDRESS);
} catch {
return res.status(400).json({ success: false, error: "Invalid wallet address" });
}
const amountInLamports = amountInSOL * LAMPORTS_PER_SOL;
res.json({
success: true,
paymentDetails: {
receiver: YOUR_WALLET_ADDRESS,
userWalletAddress,
amountInSOL,
amountInLamports,
},
});
});
app.post("/verify-payment", async (req, res) => {
const { transactionSignature } = req.body;
const { latestBlockhash } = req.body;
if (!transactionSignature) {
return res.status(400).json({ success: false, error: "Transaction signature is required" });
}
let retries = 0;
const maxRetries = 10;
while (retries < maxRetries) {
try {
console.log(`Transaction Signature: ${transactionSignature}`);
console.log(`Blockhash : ${latestBlockhash}`);
console.log(`Checking transaction attempt ${retries + 1}...`);
const status = await connection.getSignatureStatus(transactionSignature, { searchTransactionHistory: true });
if (status?.value?.confirmationStatus === "confirmed") {
return res.json({ success: true, message: "Payment verified" });
}
} catch (error) {
console.error("Error verifying transaction:", error);
}
retries++;
await new Promise((resolve) => setTimeout(resolve, 5000));
}
res.status(500).json({ success: false, error: "Transaction not found or not confirmed" });
});Editor is loading...
Leave a Comment