Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ async def get_hits(cards_ids: list[str]) -> list[Hit]:
)


async def get_hits_today(card_id: str) -> list[Hit]:
async def get_spent_hits_today(card_id: str) -> list[Hit]:
rows = await db.fetchall(
"SELECT * FROM boltcards.hits WHERE card_id = :id",
{"id": card_id},
"SELECT * FROM boltcards.hits WHERE card_id = :card_id AND spent = :spent",
{"card_id": card_id, "spent": True},
Hit,
)
updatedrow = []
Expand All @@ -182,6 +182,14 @@ async def spend_hit(card_id: str, amount: int):
return await get_hit(card_id)


async def unspend_hit(hit_id: str):
await db.execute(
"UPDATE boltcards.hits SET spent = ? WHERE id = ?",
{"spent": False, "id": hit_id},
)
return await get_hit(hit_id)


async def create_hit(card_id, ip, useragent, old_ctr, new_ctr) -> Hit:
hit_id = urlsafe_short_hash()
await db.execute(
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "lnbits-boltcards"
version = "0.0.0"
description = "LNbits, free and open-source Lightning wallet and accounts system."
authors = ["Alan Bits <alan@lnbits.com>"]
package-mode = false

[tool.poetry.dependencies]
python = "^3.10 | ^3.9"
Expand Down
8 changes: 4 additions & 4 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ window.app = Vue.createApp({
.then(response => {
this.hits = response.data.map(obj => {
obj.card_name = this.cards.find(d => d.id == obj.card_id).card_name
obj.amount = obj.spent ? obj.amount : '(' + obj.amount + ')'
return mapCards(obj)
})
})
Expand Down Expand Up @@ -433,10 +434,9 @@ window.app = Vue.createApp({
message: 'NFC is supported on this device. You can now read NFC tags.'
})
} catch (error) {
Quasar.Notify.create({
type: 'negative',
message: error ? error.toString() : 'An unexpected error has occurred.'
})
console.error(
error ? error.toString() : 'An unexpected error has occurred.'
)
}
}
})
5 changes: 3 additions & 2 deletions templates/boltcards/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,19 @@ <h6 class="text-subtitle1 q-my-none">
>
</q-input>
<div class="row">
<div class="col-10">
<div :class="disableNfcButton ? 'col-12' : 'col-10'">
<q-input
filled
dense
emit-value
v-model="cardDialog.data.uid"
type="text"
label="Card UID "
:hint="disableNfcButton ? 'NFC reader is not available on this device. Input UID manually.' : ''"
>
</q-input>
</div>
<div class="col-2 q-pl-sm">
<div v-if="!disableNfcButton" class="col-2 q-pl-sm">
<q-btn
outline
color="grey"
Expand Down
22 changes: 20 additions & 2 deletions views_lnurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
get_card_by_otp,
get_card_by_uid,
get_hit,
get_hits_today,
get_spent_hits_today,
spend_hit,
unspend_hit,
update_card_counter,
update_card_otp,
)
Expand Down Expand Up @@ -67,7 +68,7 @@ async def api_scan(p, c, request: Request, external_id: str):
ip = request.headers["x-forwarded-for"]

agent = request.headers["user-agent"] if "user-agent" in request.headers else ""
todays_hits = await get_hits_today(card.id)
todays_hits = await get_spent_hits_today(card.id)

hits_amount = 0
for hit in todays_hits:
Expand Down Expand Up @@ -144,6 +145,23 @@ async def lnurl_callback(
except Exception as exc:
return {"status": "ERROR", "reason": f"Payment failed - {exc}"}

if invoice.amount_msat <= card.tx_limit * 1000:
hit = await spend_hit(id=hit.id, amount=int(invoice.amount_msat / 1000))
assert hit
try:
await pay_invoice(
wallet_id=card.wallet,
payment_request=pr,
max_sat=card.tx_limit,
extra={"tag": "boltcard", "hit": hit.id},
)
return {"status": "OK"}
except Exception as exc:
await unspend_hit(id=hit.id) # consider it unspent
return {"status": "ERROR", "reason": f"Payment failed - {exc}"}
else:
return {"status": "ERROR", "reason": "Amount exceeds card limit"}


# /boltcards/api/v1/auth?a=00000000000000000000000000000000
@boltcards_lnurl_router.get("/api/v1/auth")
Expand Down