From 2d8f85f77c49a04b7997ead86c3875945336b400 Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Wed, 7 Apr 2021 22:51:33 +0100 Subject: [PATCH] SatsPayServer works apart from updating charges --- lnbits/extensions/satspay/config.json | 2 +- lnbits/extensions/satspay/crud.py | 18 +- lnbits/extensions/satspay/migrations.py | 2 + lnbits/extensions/satspay/models.py | 2 + .../satspay/templates/satspay/_api_docs.html | 4 +- .../satspay/templates/satspay/display.html | 12 +- .../satspay/templates/satspay/index.html | 189 +++++++++--------- lnbits/extensions/satspay/views_api.py | 39 +++- 8 files changed, 151 insertions(+), 117 deletions(-) diff --git a/lnbits/extensions/satspay/config.json b/lnbits/extensions/satspay/config.json index beb0071c..b8cd185a 100644 --- a/lnbits/extensions/satspay/config.json +++ b/lnbits/extensions/satspay/config.json @@ -1,5 +1,5 @@ { - "name": "SatsPay Server", + "name": "SatsPayServer", "short_description": "Create onchain and LN charges", "icon": "payment", "contributors": [ diff --git a/lnbits/extensions/satspay/crud.py b/lnbits/extensions/satspay/crud.py index b31ac255..c1dbdaa1 100644 --- a/lnbits/extensions/satspay/crud.py +++ b/lnbits/extensions/satspay/crud.py @@ -9,17 +9,19 @@ from lnbits.helpers import urlsafe_short_hash from quart import jsonify import httpx from lnbits.core.services import create_invoice, check_invoice_status -from ..watchonly.crud import get_watch_wallet, get_derive_address, get_mempool +from ..watchonly.crud import get_watch_wallet, get_derive_address, get_mempool, update_watch_wallet ###############CHARGES########################## -async def create_charge(user: str, description: Optional[str] = None, onchainwallet: Optional[str] = None, lnbitswallet: Optional[str] = None, webhook: Optional[str] = None, time: Optional[int] = None, amount: Optional[int] = None) -> Charges: +async def create_charge(user: str, description: str = None, onchainwallet: Optional[str] = None, lnbitswallet: Optional[str] = None, webhook: Optional[str] = None, completelink: Optional[str] = None, completelinktext: Optional[str] = None, time: Optional[int] = None, amount: Optional[int] = None) -> Charges: charge_id = urlsafe_short_hash() if onchainwallet: wallet = await get_watch_wallet(onchainwallet) - onchainaddress = await get_derive_address(onchainwallet, wallet[4] + 1) + onchainaddress = await get_derive_address(onchainwallet, int(wallet[4]) + 1) + await update_watch_wallet(wallet_id=onchainwallet, address_no=int(wallet[4]) + 1) + print(onchainaddress) else: onchainaddress = None if lnbitswallet: @@ -42,14 +44,16 @@ async def create_charge(user: str, description: Optional[str] = None, onchainwal payment_request, payment_hash, webhook, + completelink, + completelinktext, time, amount, balance ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, (charge_id, user, description, onchainwallet, onchainaddress, lnbitswallet, - payment_request, payment_hash, webhook, time, amount, 0), + payment_request, payment_hash, webhook, completelink, completelinktext, time, amount, 0), ) return await get_charge(charge_id) @@ -77,7 +81,6 @@ async def delete_charge(charge_id: str) -> None: async def check_address_balance(charge_id: str) -> List[Charges]: charge = await get_charge(charge_id) - print(charge.balance) if not charge.paid: if charge.onchainaddress: mempool = await get_mempool(charge.user) @@ -85,16 +88,13 @@ async def check_address_balance(charge_id: str) -> List[Charges]: async with httpx.AsyncClient() as client: r = await client.get(mempool.endpoint + "/api/address/" + charge.onchainaddress) respAmount = r.json()['chain_stats']['funded_txo_sum'] - print(respAmount) if respAmount >= charge.balance: await update_charge(charge_id=charge_id, balance=respAmount) except Exception: pass if charge.lnbitswallet: invoice_status = await check_invoice_status(charge.lnbitswallet, charge.payment_hash) - print(invoice_status) if invoice_status.paid: - print("paid") return await update_charge(charge_id=charge_id, balance=charge.amount) row = await db.fetchone("SELECT * FROM charges WHERE id = ?", (charge_id,)) return Charges.from_row(row) if row else None diff --git a/lnbits/extensions/satspay/migrations.py b/lnbits/extensions/satspay/migrations.py index 6fdb29c3..79048f9e 100644 --- a/lnbits/extensions/satspay/migrations.py +++ b/lnbits/extensions/satspay/migrations.py @@ -15,6 +15,8 @@ async def m001_initial(db): payment_request TEXT, payment_hash TEXT, webhook TEXT, + completelink TEXT, + completelinktext TEXT, time INTEGER, amount INTEGER, balance INTEGER DEFAULT 0, diff --git a/lnbits/extensions/satspay/models.py b/lnbits/extensions/satspay/models.py index 145164c4..a7bfa14f 100644 --- a/lnbits/extensions/satspay/models.py +++ b/lnbits/extensions/satspay/models.py @@ -13,6 +13,8 @@ class Charges(NamedTuple): payment_request: str payment_hash: str webhook: str + completelink: str + completelinktext: str time: int amount: int balance: int diff --git a/lnbits/extensions/satspay/templates/satspay/_api_docs.html b/lnbits/extensions/satspay/templates/satspay/_api_docs.html index d8df4d5b..0c27ab7b 100644 --- a/lnbits/extensions/satspay/templates/satspay/_api_docs.html +++ b/lnbits/extensions/satspay/templates/satspay/_api_docs.html @@ -1,7 +1,9 @@

- SatsPay: Create Onchain/LN charges. Includes webhooks!
+ SatsPayServer, create Onchain/LN charges.
WARNING: If using with the + WatchOnly extension, we highly reccomend using a fresh extended public Key + specifically for SatsPayServer!
Created by, Ben Arc diff --git a/lnbits/extensions/satspay/templates/satspay/display.html b/lnbits/extensions/satspay/templates/satspay/display.html index 7f5d5efc..16538a7c 100644 --- a/lnbits/extensions/satspay/templates/satspay/display.html +++ b/lnbits/extensions/satspay/templates/satspay/display.html @@ -81,6 +81,7 @@

+
@@ -115,6 +116,7 @@
+
@@ -217,7 +219,11 @@ timerCount: function () { self = this - setInterval(function () { + var refreshIntervalId = setInterval(function () { + if(self.charge_paid == "True"){ + console.log("did this work?") + clearInterval(refreshIntervalId) + } self.getTheTime() self.getThePercentage() self.counter++ @@ -228,6 +234,10 @@ } }, created: function () { + if('{{ charge.lnbitswallet }}' == 'None'){ + this.lnbtc = false + this.onbtc = true + } this.getTheTime() this.getThePercentage() var timerCount = this.timerCount diff --git a/lnbits/extensions/satspay/templates/satspay/index.html b/lnbits/extensions/satspay/templates/satspay/index.html index 0a7f49b7..74070a29 100644 --- a/lnbits/extensions/satspay/templates/satspay/index.html +++ b/lnbits/extensions/satspay/templates/satspay/index.html @@ -104,11 +104,10 @@ size="xs" icon="cached" flat - @click="getBalance(props.row.id)" :color="($q.dark.isActive) ? 'blue' : 'blue'" > - Check balance + Processing + > + + Edit charge + + + > + + Delete charge + + @@ -179,7 +186,7 @@ dense v-model.trim="formDialogCharge.data.description" type="text" - label="Description" + label="*Description" > + + +
@@ -263,9 +285,9 @@ formDialogCharge.data.time == null || formDialogCharge.data.amount == null" type="submit" - >Create PaylinkCreate Charge - Cancel
@@ -283,30 +305,9 @@ diff --git a/lnbits/extensions/satspay/views_api.py b/lnbits/extensions/satspay/views_api.py index 70ba78e4..e7c0f14c 100644 --- a/lnbits/extensions/satspay/views_api.py +++ b/lnbits/extensions/satspay/views_api.py @@ -28,7 +28,9 @@ from .crud import ( "onchainwallet": {"type": "string"}, "lnbitswallet": {"type": "string"}, "description": {"type": "string", "empty": False, "required": True}, - "webhook": {"type": "string", "empty": False, "required": True}, + "webhook": {"type": "string"}, + "completelink": {"type": "string"}, + "completelinktext": {"type": "string"}, "time": {"type": "integer", "min": 1, "required": True}, "amount": {"type": "integer", "min": 1, "required": True}, } @@ -85,15 +87,36 @@ async def api_charges_balance(charge_id): if not charge: return jsonify({"message": "charge does not exist"}), HTTPStatus.NOT_FOUND - else: - return jsonify(charge._asdict()), HTTPStatus.OK + if charge.paid and charge.webhook: + async with httpx.AsyncClient() as client: + try: + r = await client.post( + charge.webhook, + json={ + "id": charge.id, + "description": charge.description, + "onchainaddress": charge.onchainaddress, + "payment_request": charge.payment_request, + "payment_hash": charge.payment_hash, + "time": charge.time, + "amount": charge.amount, + "balance": charge.balance, + "paid": charge.paid, + "timestamp": charge.timestamp, + "completelink": charge.completelink, + }, + timeout=40, + ) + except AssertionError: + charge.webhook = None + return jsonify(charge._asdict()), HTTPStatus.OK #############################MEMPOOL########################## -@satspay_ext.route("/api/v1/mempool", methods=["PUT"]) -@api_check_wallet_key("invoice") -@api_validate_post_request( +@ satspay_ext.route("/api/v1/mempool", methods=["PUT"]) +@ api_check_wallet_key("invoice") +@ api_validate_post_request( schema={ "endpoint": {"type": "string", "empty": False, "required": True}, } @@ -103,8 +126,8 @@ async def api_update_mempool(): return jsonify(mempool._asdict()), HTTPStatus.OK -@satspay_ext.route("/api/v1/mempool", methods=["GET"]) -@api_check_wallet_key("invoice") +@ satspay_ext.route("/api/v1/mempool", methods=["GET"]) +@ api_check_wallet_key("invoice") async def api_get_mempool(): mempool = await get_mempool(g.wallet.user) if not mempool: