From f3b0918766e6ba0d7640443cac5d9a3c4d3b4832 Mon Sep 17 00:00:00 2001 From: BigBossRabbit <87260669+BigBossRabbit@users.noreply.github.com> Date: Sun, 30 Jul 2023 15:22:45 +0200 Subject: [PATCH] Adding Paywall.js and Withdrawl.js files I could not get my copy of VSCodium to upload my changes via the synch functionaity so I have uploaded them directly. This is as a submission for the AI4All Bounty: 2/3 AI4ALL: Build a Kaboom Game with an AI Enemy and Bitcoin Payments using the ZBD API --- js/Paywall.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++-- js/Withdraw.js | 58 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 115 insertions(+), 4 deletions(-) diff --git a/js/Paywall.js b/js/Paywall.js index cc10a36..932276d 100644 --- a/js/Paywall.js +++ b/js/Paywall.js @@ -22,11 +22,68 @@ function startGame(){ } function createInvoice() { - + + const Http = new XMLHttpRequest(); + const url = 'https://api.zebedee.io/v0/charges'; + + Http.open("POST", url); + + Http.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + Http.setRequestHeader("apikey", config.API_KEY); + + const payload = JSON.stringify({ + "expiresIn": 300, + "amount": config.FEE * 1000, + "description": "EasySats - Bomberman Paywall", + "internalId": "11af01d092444a317cb33faa6b8304b8", + "callbackUrl": "https://your-website.com/callback" + }) + Http.send(payload); + + Http.onreadystatechange = (e) => { + + if (Http.readyState === XMLHttpRequest.DONE) { + console.log(Http.responseText) + const data = JSON.parse(Http.responseText); + currentChargeID = data.data.id; + createInvoiceQRCode(data); + } + + } } function checkPayment() { - + const Http = new XMLHttpRequest(); + const url = 'https://api.zebedee.io/v0/charges/' + currentChargeID; + + Http.open("GET", url); + + Http.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + Http.setRequestHeader("apikey", config.API_KEY); + + Http.send(); + + Http.onreadystatechange = (e) => { + + if (Http.readyState === XMLHttpRequest.DONE) { + console.log(Http.responseText); + const data = JSON.parse(Http.responseText); + const status = data.data.status; + + if (status === "pending") { + + setTimeout(function() { + checkPayment(); + }, 2000); + + } else if (status === "completed") { + + startGame(); + + } + } + } + } createInvoice(); \ No newline at end of file diff --git a/js/Withdraw.js b/js/Withdraw.js index 9194b52..1dfc922 100644 --- a/js/Withdraw.js +++ b/js/Withdraw.js @@ -33,11 +33,65 @@ function updateWithdrawUI(){ } function createWithdrawLNURL(){ - + const Http = new XMLHttpRequest(); + const url = 'https://api.zebedee.io/v0/withdrawal-requests'; + + Http.open("POST", url); + + Http.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + Http.setRequestHeader("apikey", config.API_KEY); + + const payload = JSON.stringify({ + "expiresIn": 300, + "amount": gGameEngine.totalSats * 1000, + "description": "Bomberman withdraw", + "internalId": "11af01d092444a317cb33faa6b8304b8", + "callbackUrl": "https://your-website.com/callback" + }); + + Http.send(payload); + + Http.onreadystatechange = (e) => { + + if (Http.readyState === XMLHttpRequest.DONE) { + + const data = JSON.parse(Http.responseText); + + currentWithdrawlID = data.data.id; + + makeWithdrawQRCode(data) + } + + } } function checkWithdraw() { - + const Http = new XMLHttpRequest(); + const url = 'https://api.zebedee.io/v0/withdrawal-requests/' + currentWithdrawlID; + + Http.open("GET", url); + + Http.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + Http.setRequestHeader("apikey", config.API_KEY); + + Http.send(); + + Http.onreadystatechange = (e) => { + + if (Http.readyState === XMLHttpRequest.DONE) { + + const data = JSON.parse(Http.responseText); + + const status = data.data.status; + if (status === "pending") { + setTimeout(function() { + checkWithdraw(); + }, 2000); + } else if (status === "completed") { + updateWithdrawUI(); + } + } + } } document.getElementById('withdrawButton').onclick = withdraw; \ No newline at end of file