diff --git a/lnbits/extensions/satspay/crud.py b/lnbits/extensions/satspay/crud.py
index bd156ae4..b31ac255 100644
--- a/lnbits/extensions/satspay/crud.py
+++ b/lnbits/extensions/satspay/crud.py
@@ -11,7 +11,6 @@ import httpx
from lnbits.core.services import create_invoice, check_invoice_status
from ..watchonly.crud import get_watch_wallet, get_derive_address, get_mempool
-import time
###############CHARGES##########################
@@ -45,13 +44,12 @@ async def create_charge(user: str, description: Optional[str] = None, onchainwal
webhook,
time,
amount,
- balance,
- paid
+ balance
)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(charge_id, user, description, onchainwallet, onchainaddress, lnbitswallet,
- payment_request, payment_hash, webhook, time, amount, 0, False),
+ payment_request, payment_hash, webhook, time, amount, 0),
)
return await get_charge(charge_id)
@@ -79,21 +77,24 @@ async def delete_charge(charge_id: str) -> None:
async def check_address_balance(charge_id: str) -> List[Charges]:
charge = await get_charge(charge_id)
- if charge.onchainaddress:
- mempool = await get_mempool(charge.user)
- try:
- async with httpx.AsyncClient() as client:
- r = await client.get(mempool.endpoint + "/api/address/" + charge.onchainaddress)
- respAmount = r.json()['chain_stats']['funded_txo_sum']
- if (charge.balance + respAmount) >= charge.balance:
- return await update_charge(charge_id=charge_id, balance=(charge.balance + respAmount), paid=True)
- else:
- return await update_charge(charge_id=charge_id, balance=(charge.balance + respAmount), paid=False)
- except Exception:
- pass
- if charge.lnbitswallet:
- invoice_status = await check_invoice_status(charge.lnbitswallet, charge.payment_hash)
- if invoice_status.paid:
- return await update_charge(charge_id=charge_id, balance=charge.balance, paid=True)
+ print(charge.balance)
+ if not charge.paid:
+ if charge.onchainaddress:
+ mempool = await get_mempool(charge.user)
+ try:
+ 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 044eed13..6fdb29c3 100644
--- a/lnbits/extensions/satspay/migrations.py
+++ b/lnbits/extensions/satspay/migrations.py
@@ -3,7 +3,6 @@ async def m001_initial(db):
Initial wallet table.
"""
-
await db.execute(
"""
CREATE TABLE IF NOT EXISTS charges (
@@ -19,8 +18,7 @@ async def m001_initial(db):
time INTEGER,
amount INTEGER,
balance INTEGER DEFAULT 0,
- paid BOOLEAN,
timestamp TIMESTAMP NOT NULL DEFAULT (strftime('%s', 'now'))
);
"""
- )
\ No newline at end of file
+ )
diff --git a/lnbits/extensions/satspay/models.py b/lnbits/extensions/satspay/models.py
index 5eee1061..145164c4 100644
--- a/lnbits/extensions/satspay/models.py
+++ b/lnbits/extensions/satspay/models.py
@@ -1,5 +1,6 @@
from sqlite3 import Row
from typing import NamedTuple
+import time
class Charges(NamedTuple):
@@ -12,12 +13,25 @@ class Charges(NamedTuple):
payment_request: str
payment_hash: str
webhook: str
- time: str
+ time: int
amount: int
balance: int
- paid: bool
timestamp: int
@classmethod
def from_row(cls, row: Row) -> "Charges":
return cls(**dict(row))
+
+ @property
+ def time_elapsed(self):
+ if (self.timestamp + (self.time * 60)) >= time.time():
+ return False
+ else:
+ return True
+
+ @property
+ def paid(self):
+ if self.balance >= self.amount:
+ return True
+ else:
+ return False
diff --git a/lnbits/extensions/satspay/templates/satspay/_api_docs.html b/lnbits/extensions/satspay/templates/satspay/_api_docs.html
index 3c0bc720..d8df4d5b 100644
--- a/lnbits/extensions/satspay/templates/satspay/_api_docs.html
+++ b/lnbits/extensions/satspay/templates/satspay/_api_docs.html
@@ -117,80 +117,6 @@
- POST /pay/api/v1/links
- Headers
- {"X-Api-Key": <invoice_key>}
-
- Body (application/json)
-
- {"description": <string> "amount": <integer>}
-
- Returns 201 CREATED (application/json)
-
-
- { "deliveryId": <string>, "description": <string>,
- "webhookId": <string>, "originalDeliveryId": <string>,
- "isRedelivery": <boolean>, "type": <string>,
- "timestamp": <int>, "paytime": <int>, "storeId":
- <string>, "invoiceId": <string>, "manuallyMarked":
- <boolean>, "overPaid": <boolean>, "afterExpiration":
- <boolean>, "partiallyPaid": <boolean> }
-
"type" can be InvoiceReceivedPayment, InvoicePaidInFull,
- InvoiceExpired, InvoiceConfirmed, and InvoiceInvalid
- Curl example
- curl -X POST {{ request.url_root }}pay/api/v1/links -d
- '{"description": <string>, "amount": <integer>}' -H
- "Content-type: application/json" -H "X-Api-Key: {{
- g.user.wallets[0].adminkey }}"
-
- PUT
- /pay/api/v1/links/<pay_id>
- Headers
- {"X-Api-Key": <admin_key>}
-
- Body (application/json)
-
- {"description": <string>, "amount": <integer>}
-
- Returns 200 OK (application/json)
-
- {"lnurl": <string>}
- Curl example
- curl -X PUT {{ request.url_root }}pay/api/v1/links/<pay_id>
- -d '{"description": <string>, "amount": <integer>}' -H
- "Content-type: application/json" -H "X-Api-Key: {{
- g.user.wallets[0].adminkey }}"
-
- DELETE
- /pay/api/v1/links/<pay_id>
Headers
{"X-Api-Key": <admin_key>}
@@ -211,11 +137,36 @@
Curl example
curl -X DELETE {{ request.url_root
- }}pay/api/v1/links/<pay_id> -H "X-Api-Key: {{
+ }}api/v1/charge/<charge_id> -H "X-Api-Key: {{
g.user.wallets[0].adminkey }}"
GET
+ /satspay/api/v1/charges/balance/<charge_id>
+ Headers
+ {"X-Api-Key": <admin_key>}
+
+ Body (application/json)
+
+
+ Returns 200 OK (application/json)
+
+ [<charge_object>, ...]
+ Curl example
+ curl -X GET {{ request.url_root
+ }}api/v1/charges/balance/<charge_id> -H "X-Api-Key: {{
+ g.user.wallets[0].inkey }}"
+
+