Render or poll invoice status
This commit is contained in:
parent
b2c7f28200
commit
a01d4488cc
@ -39,9 +39,9 @@
|
||||
</code>
|
||||
<div class="font-mono mb-1">
|
||||
<div class="mb-1">Payment Required</div>
|
||||
<div id="paid" class="yes" hidden>
|
||||
<div>Paid</div>
|
||||
<div id="countdown">Redirecting in 3 ...</div>
|
||||
<div id="status" hidden>
|
||||
<div id="status-label"></div>
|
||||
<div id="countdown" hidden>Redirecting in 3 ...</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="qr">
|
||||
@ -60,28 +60,54 @@
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
{{ if .RedirectAfterPayment }}
|
||||
<script>
|
||||
const invoiceId = "{{.Invoice.Id}}"
|
||||
const paid = document.querySelector("#paid")
|
||||
const countdown = document.querySelector("#countdown")
|
||||
const interval = setInterval(async () => {
|
||||
const body = await fetch(`/api/invoice/${invoiceId}`)
|
||||
.then((r) => r.json())
|
||||
.catch(console.error)
|
||||
if (body.ConfirmedAt) {
|
||||
paid.removeAttribute("hidden")
|
||||
const statusElement = document.querySelector("#status")
|
||||
const label = document.querySelector("#status-label")
|
||||
const status = "{{.Status}}"
|
||||
const redirectUrl = "{{.RedirectURL}}"
|
||||
function poll() {
|
||||
const invoiceId = "{{.Invoice.Id}}"
|
||||
const countdown = document.querySelector("#countdown")
|
||||
const redirect = () => {
|
||||
clearInterval(interval)
|
||||
countdown.removeAttribute("hidden")
|
||||
let timer = 2
|
||||
const redirect = setInterval(() => {
|
||||
countdown.textContent = `Redirecting in ${timer--} ...`
|
||||
if (timer === -1) {
|
||||
window.location.href = "https://{{.PUBLIC_URL}}/market/{{.MarketId}}";
|
||||
window.location.href = redirectURL;
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
}, 1000)
|
||||
const interval = setInterval(async () => {
|
||||
const body = await fetch(`/api/invoice/${invoiceId}`)
|
||||
.then((r) => r.json())
|
||||
.catch(console.error)
|
||||
if (body.ConfirmedAt) {
|
||||
statusElement.removeAttribute("hidden")
|
||||
statusElement.classList.add("yes")
|
||||
label.textContent = "Paid"
|
||||
if (redirectURL) redirect()
|
||||
} else if (new Date(body.ExpiresAt) <= new Date()) {
|
||||
statusElement.removeAttribute("hidden")
|
||||
statusElement.classList.add("no")
|
||||
label.textContent = "Expired"
|
||||
if (redirectURL) redirect()
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
if (status) {
|
||||
console.log(status)
|
||||
statusElement.removeAttribute("hidden")
|
||||
label.textContent = status
|
||||
if (status === "Paid") {
|
||||
statusElement.classList.add("yes")
|
||||
}
|
||||
else if (status === "Expired") {
|
||||
statusElement.classList.add("no")
|
||||
}
|
||||
}
|
||||
else poll()
|
||||
</script>
|
||||
{{ end }}
|
||||
|
||||
</html>
|
@ -118,9 +118,16 @@ func invoice(c echo.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
status := ""
|
||||
if invoice.ConfirmedAt.Valid {
|
||||
status = "Paid"
|
||||
} else if time.Now().After(invoice.ExpiresAt) {
|
||||
status = "Expired"
|
||||
}
|
||||
data := map[string]any{
|
||||
"session": c.Get("session"),
|
||||
"Invoice": invoice,
|
||||
"Status": status,
|
||||
"lnurl": invoice.PaymentRequest,
|
||||
"qr": qr,
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@ -91,14 +92,12 @@ func order(c echo.Context) error {
|
||||
}
|
||||
go lnd.CheckInvoice(invoice.PaymentHash)
|
||||
data := map[string]any{
|
||||
"session": c.Get("session"),
|
||||
"ENV": ENV,
|
||||
"lnurl": invoice.PaymentRequest,
|
||||
"qr": qr,
|
||||
"Invoice": invoice,
|
||||
"RedirectAfterPayment": true,
|
||||
"PUBLIC_URL": PUBLIC_URL,
|
||||
"MarketId": marketId,
|
||||
"session": c.Get("session"),
|
||||
"ENV": ENV,
|
||||
"lnurl": invoice.PaymentRequest,
|
||||
"qr": qr,
|
||||
"Invoice": invoice,
|
||||
"RedirectURL": fmt.Sprintf("https://%s/market/%s", PUBLIC_URL, marketId),
|
||||
}
|
||||
return c.Render(http.StatusPaymentRequired, "invoice.html", data)
|
||||
// Step 2: After payment, confirm order if no matching order was found
|
||||
|
Loading…
x
Reference in New Issue
Block a user