pay lnurl

This commit is contained in:
Tiago vasconcelos 2022-06-21 10:31:18 +01:00
parent f6d922e1b7
commit 10e1bbcb66
4 changed files with 46 additions and 78 deletions

View File

@ -65,3 +65,17 @@ async def update_scrub_link(link_id: int, **kwargs) -> Optional[ScrubLink]:
async def delete_scrub_link(link_id: int) -> None:
await db.execute("DELETE FROM scrub.scrub_links WHERE id = ?", (link_id,))
async def get_scrub_by_wallet(wallet_id) -> Optional[ScrubLink]:
row = await db.fetchone(
"SELECT * from scrub.scrub_links WHERE wallet = ?",
(wallet_id,),
)
return ScrubLink(**row) if row else None
async def unique_scrubed_wallet(wallet_id):
row, = await db.fetchone(
"SELECT COUNT(wallet) FROM scrub.scrub_links WHERE wallet = ?",
(wallet_id,),
)
return row

View File

@ -26,8 +26,6 @@ new Vue({
mixins: [windowMixin],
data() {
return {
currencies: [],
fiatRates: {},
checker: null,
payLinks: [],
payLinksTable: {
@ -37,7 +35,6 @@ new Vue({
},
formDialog: {
show: false,
fixedAmount: true,
data: {}
},
qrCodeDialog: {
@ -65,42 +62,11 @@ new Vue({
closeFormDialog() {
this.resetFormData()
},
openQrCodeDialog(linkId) {
var link = _.findWhere(this.payLinks, {id: linkId})
if (link.currency) this.updateFiatRate(link.currency)
this.qrCodeDialog.data = {
id: link.id,
amount:
(link.min === link.max ? link.min : `${link.min} - ${link.max}`) +
' ' +
(link.currency || 'sat'),
currency: link.currency,
comments: link.comment_chars
? `${link.comment_chars} characters`
: 'no',
webhook: link.webhook_url || 'nowhere',
success:
link.success_text || link.success_url
? 'Display message "' +
link.success_text +
'"' +
(link.success_url ? ' and URL "' + link.success_url + '"' : '')
: 'do nothing',
lnurl: link.lnurl,
pay_url: link.pay_url,
print_url: link.print_url
}
this.qrCodeDialog.show = true
},
openUpdateDialog(linkId) {
const link = _.findWhere(this.payLinks, {id: linkId})
if (link.currency) this.updateFiatRate(link.currency)
this.formDialog.data = _.clone(link._data)
this.formDialog.show = true
this.formDialog.fixedAmount =
this.formDialog.data.min === this.formDialog.data.max
},
sendFormData() {
const wallet = _.findWhere(this.g.user.wallets, {
@ -166,35 +132,12 @@ new Vue({
LNbits.utils.notifyApiError(err)
})
})
},
updateFiatRate(currency) {
LNbits.api
.request('GET', '/scrub/api/v1/rate/' + currency, null)
.then(response => {
let rates = _.clone(this.fiatRates)
rates[currency] = response.data.rate
this.fiatRates = rates
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
}
},
created() {
if (this.g.user.wallets.length) {
var getScrubLinks = this.getScrubLinks
getScrubLinks()
// this.checker = setInterval(() => {
// getScrubLinks()
// }, 20000)
}
LNbits.api
.request('GET', '/scrub/api/v1/currencies')
.then(response => {
this.currencies = ['satoshis', ...response.data]
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
}
})

View File

@ -1,12 +1,15 @@
import asyncio
import json
import httpx
from lnbits.core import db as core_db
from .models import ScrubLink
from lnbits.core.crud import get_wallet
from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener
from .crud import get_scrub_link
from .crud import get_scrub_by_wallet, get_scrub_link
from .models import ScrubLink
async def wait_for_paid_invoices():
@ -18,26 +21,34 @@ async def wait_for_paid_invoices():
await on_invoice_paid(payment)
async def on_invoice_paid(payment: ScrubLink) -> None:
if "scrub" != payment.extra.get("tag"):
# not an scrub invoice
async def on_invoice_paid(payment: Payment) -> None:
# (avoid loops)
if "scrub" == payment.extra.get("tag"):
# already scrubbed
return
if payment.extra.get("wh_status"):
# this webhook has already been sent
scrub_link = await get_scrub_by_wallet(payment.wallet_id)
if not scrub_link:
return
pay_link = await get_pay_link(payment.extra.get("link", -1))
# PAY LNURLP AND LNADDRESS
async def mark_webhook_sent(payment: ScrubLink, status: int) -> None:
payment.extra["wh_status"] = status
await core_db.execute(
"""
UPDATE apipayments SET extra = ?
WHERE hash = ?
""",
(json.dumps(payment.extra), payment.payment_hash),
from lnbits.core.views.api import (
CreateLNURLData,
api_lnurlscan,
api_payments_pay_lnurl,
)
wallet = await get_wallet(wallet_id=payment.wallet_id)
assert wallet
# PAY LNURLP AND LNADDRESS
invoice = await api_lnurlscan(scrub_link.payoraddress)
invoice_data = CreateLNURLData(
callback = invoice["callback"],
description_hash = invoice["description_hash"],
amount = payment.amount
)
print("INV", invoice_data, wallet)
invoice_paid = await api_payments_pay_lnurl(data=invoice_data, wallet=wallet.adminkey)
print("PAID", invoice_paid)

View File

@ -97,7 +97,7 @@ async def api_scrub_create_or_update(
@scrub_ext.delete("/api/v1/links/{link_id}")
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(require_admin_key)):
link = await get_scrub_link(link_id)
if not link: