mirror of
https://github.com/lnbits/lnbits.git
synced 2025-04-09 12:30:13 +02:00
Merge pull request #938 from lnbits/boltcardfix
Fixes daily limit issue
This commit is contained in:
commit
3080094273
@ -6,6 +6,7 @@ from lnbits.helpers import urlsafe_short_hash
|
||||
from . import db
|
||||
from .models import Card, CreateCardData, Hit, Refund
|
||||
|
||||
from datetime import date, datetime
|
||||
|
||||
async def create_card(data: CreateCardData, wallet_id: str) -> Card:
|
||||
card_id = urlsafe_short_hash().upper()
|
||||
@ -178,20 +179,24 @@ async def get_hits(cards_ids: Union[str, List[str]]) -> List[Hit]:
|
||||
return [Hit(**row) for row in rows]
|
||||
|
||||
|
||||
async def get_hits_today(card_id: Union[str, List[str]]) -> List[Hit]:
|
||||
async def get_hits_today(card_id: str) -> Optional[Hit]:
|
||||
rows = await db.fetchall(
|
||||
f"SELECT * FROM boltcards.hits WHERE card_id = ? AND time >= DATE('now') AND time < DATE('now', '+1 day')",
|
||||
(card_id,),
|
||||
f"SELECT * FROM boltcards.hits WHERE card_id = ?", (card_id,),
|
||||
)
|
||||
updatedrow = []
|
||||
for row in rows:
|
||||
if datetime.now().date() == datetime.fromtimestamp(row.time).date():
|
||||
updatedrow.append(row)
|
||||
|
||||
return [Hit(**row) for row in rows]
|
||||
return [Hit(**row) for row in updatedrow]
|
||||
|
||||
|
||||
async def spend_hit(id: str):
|
||||
async def spend_hit(id: str, amount: int):
|
||||
await db.execute(
|
||||
"UPDATE boltcards.hits SET spent = ? WHERE id = ?",
|
||||
(True, id),
|
||||
"UPDATE boltcards.hits SET spent = ?, amount = ? WHERE id = ?",
|
||||
(True, amount, id),
|
||||
)
|
||||
return await get_hit(id)
|
||||
|
||||
|
||||
async def create_hit(card_id, ip, useragent, old_ctr, new_ctr) -> Hit:
|
||||
|
@ -20,6 +20,8 @@ from starlette.exceptions import HTTPException
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import HTMLResponse
|
||||
|
||||
from lnbits import bolt11
|
||||
|
||||
from lnbits.core.services import create_invoice
|
||||
from lnbits.core.views.api import pay_invoice
|
||||
|
||||
@ -96,7 +98,6 @@ async def api_scan(p, c, request: Request, external_id: str = None):
|
||||
"defaultDescription": f"Boltcard (refund address lnurl://{lnurlpay})",
|
||||
}
|
||||
|
||||
|
||||
@boltcards_ext.get(
|
||||
"/api/v1/lnurl/cb/{hitid}",
|
||||
status_code=HTTPStatus.OK,
|
||||
@ -111,23 +112,23 @@ async def lnurl_callback(
|
||||
card = await get_card(hit.card_id)
|
||||
if not hit:
|
||||
return {"status": "ERROR", "reason": f"LNURL-pay record not found."}
|
||||
if hit.id != k1:
|
||||
return {"status": "ERROR", "reason": "Bad K1"}
|
||||
if hit.spent:
|
||||
return {"status": "ERROR", "reason": f"Payment already claimed"}
|
||||
invoice = bolt11.decode(pr)
|
||||
hit = await spend_hit(id=hit.id, amount=int(invoice.amount_msat / 1000))
|
||||
try:
|
||||
if hit.id != k1:
|
||||
return {"status": "ERROR", "reason": "Bad K1"}
|
||||
if hit.spent:
|
||||
return {"status": "ERROR", "reason": f"Payment already claimed"}
|
||||
hit = await spend_hit(hit.id)
|
||||
await pay_invoice(
|
||||
wallet_id=card.wallet,
|
||||
payment_request=pr,
|
||||
max_sat=card.tx_limit,
|
||||
extra={"tag": "boltcard"},
|
||||
extra={"tag": "boltcard", "tag": hit.id},
|
||||
)
|
||||
return {"status": "OK"}
|
||||
except:
|
||||
return {"status": "ERROR", "reason": f"Payment failed"}
|
||||
|
||||
|
||||
# /boltcards/api/v1/auth?a=00000000000000000000000000000000
|
||||
@boltcards_ext.get("/api/v1/auth")
|
||||
async def api_auth(a, request: Request):
|
||||
|
@ -104,6 +104,12 @@ new Vue({
|
||||
label: 'Card name',
|
||||
field: 'card_name'
|
||||
},
|
||||
{
|
||||
name: 'amount',
|
||||
align: 'left',
|
||||
label: 'Amount',
|
||||
field: 'amount'
|
||||
},
|
||||
{
|
||||
name: 'old_ctr',
|
||||
align: 'left',
|
||||
|
Loading…
x
Reference in New Issue
Block a user