feat: wait for paid invoce
This commit is contained in:
parent
3aa4875558
commit
527afa0c8c
2 changed files with 60 additions and 9 deletions
25
tasks.py
25
tasks.py
|
|
@ -1,8 +1,10 @@
|
|||
import asyncio
|
||||
import json
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from lnbits.core.models import Payment
|
||||
from lnbits.core.services import websocketUpdater
|
||||
from lnbits.helpers import get_current_extension_name
|
||||
from lnbits.tasks import register_invoice_listener
|
||||
|
||||
|
|
@ -25,27 +27,36 @@ async def on_invoice_paid(payment: Payment):
|
|||
|
||||
relay_id = payment.extra.get("relay_id")
|
||||
pubkey = payment.extra.get("pubkey")
|
||||
hash = payment.payment_hash
|
||||
|
||||
if not relay_id or not pubkey:
|
||||
logger.warning(
|
||||
f"Invoice extra data missing for 'relay_id' and 'pubkey'. Payment hash: {payment.payment_hash}"
|
||||
)
|
||||
message = f"Invoice extra data missing for 'relay_id' and 'pubkey'. Payment hash: {hash}"
|
||||
logger.warning(message)
|
||||
await websocketUpdater(hash, json.dumps({"success": False, "message": message}))
|
||||
return
|
||||
|
||||
if payment.extra.get("action") == "join":
|
||||
action = payment.extra.get("action")
|
||||
if action == "join":
|
||||
await invoice_paid_to_join(relay_id, pubkey, payment.amount)
|
||||
await websocketUpdater(hash, json.dumps({"success": True}))
|
||||
return
|
||||
|
||||
if payment.extra.get("action") == "storage":
|
||||
if action == "storage":
|
||||
storage_to_buy = payment.extra.get("storage_to_buy")
|
||||
if not storage_to_buy:
|
||||
logger.warning(
|
||||
f"Invoice extra data missing for 'storage_to_buy'. Payment hash: {payment.payment_hash}"
|
||||
message = (
|
||||
f"Invoice extra data missing for 'storage_to_buy'. Payment hash: {hash}"
|
||||
)
|
||||
logger.warning(message)
|
||||
return
|
||||
await invoice_paid_for_storage(relay_id, pubkey, storage_to_buy, payment.amount)
|
||||
await websocketUpdater(hash, json.dumps({"success": True}))
|
||||
return
|
||||
|
||||
await websocketUpdater(
|
||||
hash, json.dumps({"success": False, "message": f"Bad action name: '{action}'"})
|
||||
)
|
||||
|
||||
|
||||
async def invoice_paid_to_join(relay_id: str, pubkey: str, amount: int):
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -144,8 +144,9 @@
|
|||
</h5>
|
||||
</q-badge>
|
||||
</q-card-section>
|
||||
<q-card-section v-if="invoice">
|
||||
<q-card-section>
|
||||
<q-expansion-item
|
||||
v-if="invoice"
|
||||
group="join-invoice"
|
||||
label="Invoice"
|
||||
:content-inset-level="0.5"
|
||||
|
|
@ -174,6 +175,20 @@
|
|||
<div class="col-3"></div>
|
||||
</div>
|
||||
</q-expansion-item>
|
||||
<q-expansion-item v-else-if="invoiceResponse">
|
||||
<div class="row">
|
||||
<div class="col-3"></div>
|
||||
<div class="col-6">
|
||||
<q-icon
|
||||
v-if="invoiceResponse.success"
|
||||
name="check"
|
||||
style="color: green; font-size: 21.4em"
|
||||
></q-icon>
|
||||
<span v-else v-text="invoiceResponse.message"></span>
|
||||
</div>
|
||||
<div class="col-3"></div>
|
||||
</div>
|
||||
</q-expansion-item>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</div>
|
||||
|
|
@ -192,6 +207,7 @@
|
|||
relay: JSON.parse('{{relay | tojson | safe}}'),
|
||||
pubkey: '',
|
||||
invoice: '',
|
||||
invoiceResponse: null,
|
||||
unitsToBuy: 0
|
||||
}
|
||||
},
|
||||
|
|
@ -210,7 +226,6 @@
|
|||
},
|
||||
methods: {
|
||||
createInvoice: async function (action) {
|
||||
console.log('### action', action)
|
||||
if (!action) return
|
||||
this.invoice = ''
|
||||
if (!this.pubkey) {
|
||||
|
|
@ -235,9 +250,34 @@
|
|||
reqData
|
||||
)
|
||||
this.invoice = data.invoice
|
||||
const paymentHashTag = decode(data.invoice).data.tags.find(
|
||||
t => t.description === 'payment_hash'
|
||||
)
|
||||
if (paymentHashTag) {
|
||||
await this.waitForPaidInvoice(paymentHashTag.value)
|
||||
}
|
||||
} catch (error) {
|
||||
LNbits.utils.notifyApiError(error)
|
||||
}
|
||||
},
|
||||
waitForPaidInvoice: function (paymentHash) {
|
||||
try {
|
||||
const scheme = location.protocol === 'http:' ? 'ws' : 'wss'
|
||||
const wsUrl = `${scheme}://${document.domain}:${location.port}/api/v1/ws/${paymentHash}`
|
||||
const wsConnection = new WebSocket(wsUrl)
|
||||
wsConnection.onmessage = e => {
|
||||
this.invoiceResponse = JSON.parse(e.data)
|
||||
this.invoice = null
|
||||
wsConnection.close()
|
||||
}
|
||||
} catch (error) {
|
||||
this.$q.notify({
|
||||
timeout: 5000,
|
||||
type: 'warning',
|
||||
message: 'Failed to get invoice status',
|
||||
caption: `${error}`
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
created: function () {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue