document.addEventListener('DOMContentLoaded', function () {
const buyButton = document.getElementById("buy-button");
const receipt = document.getElementById("receipt");
if (buyButton) {
buyButton.addEventListener("click", async () => {
try {
const walletAddress = prompt("Enter your XRP wallet address:");
const amount = prompt("Enter the amount of LYL you want to purchase:");
if (!walletAddress || !amount || isNaN(amount) || amount <= 0) {
receipt.innerText = "Invalid wallet address or amount.";
receipt.style.color = 'red';
receipt.style.display = 'block';
return;
}
const response = await fetch("/api/buy", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
wallet_address: walletAddress.trim(),
amount: parseFloat(amount)
})
});
const data = await response.json();
if (response.ok && data.tx_url) {
// Show the transaction receipt
receipt.innerHTML = `
Transaction Created:
Amount: ${amount} LYL
Wallet: ${walletAddress}
Complete the transaction in XUMM
`;
receipt.style.color = 'green';
receipt.style.display = 'block';
} else {
receipt.innerText = data.message || "Transaction failed. Please try again.";
receipt.style.color = 'red';
receipt.style.display = 'block';
}
} catch (error) {
console.error("Error buying LYL:", error);
receipt.innerText = "Purchase failed. Please try again.";
receipt.style.color = 'red';
receipt.style.display = 'block';
}
});
}
});