2021-03-07 00:08:36 -03:00
|
|
|
import time
|
|
|
|
from quart import g, render_template, request
|
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
from lnbits.decorators import check_user_exists, validate_uuids
|
|
|
|
from lnbits.core.models import Payment
|
|
|
|
from lnbits.core.crud import get_standalone_payment
|
|
|
|
|
|
|
|
from . import offlineshop_ext
|
|
|
|
from .crud import get_item, get_shop
|
|
|
|
|
|
|
|
|
|
|
|
@offlineshop_ext.route("/")
|
|
|
|
@validate_uuids(["usr"], required=True)
|
|
|
|
@check_user_exists()
|
|
|
|
async def index():
|
|
|
|
return await render_template("offlineshop/index.html", user=g.user)
|
|
|
|
|
|
|
|
|
2021-03-07 16:13:20 -03:00
|
|
|
@offlineshop_ext.route("/print")
|
|
|
|
async def print_qr_codes():
|
|
|
|
items = []
|
|
|
|
for item_id in request.args.get("items").split(","):
|
|
|
|
item = await get_item(item_id)
|
|
|
|
if item:
|
|
|
|
items.append({"lnurl": item.lnurl, "name": item.name, "price": f"{item.price} {item.unit}"})
|
|
|
|
|
|
|
|
return await render_template("offlineshop/print.html", items=items)
|
|
|
|
|
|
|
|
|
2021-03-07 00:08:36 -03:00
|
|
|
@offlineshop_ext.route("/confirmation")
|
|
|
|
async def confirmation_code():
|
2021-03-07 16:13:20 -03:00
|
|
|
style = "<style>* { font-size: 100px}</style>"
|
|
|
|
|
2021-03-07 00:08:36 -03:00
|
|
|
payment_hash = request.args.get("p")
|
|
|
|
payment: Payment = await get_standalone_payment(payment_hash)
|
|
|
|
if not payment:
|
2021-03-07 16:13:20 -03:00
|
|
|
return f"Couldn't find the payment {payment_hash}." + style, HTTPStatus.NOT_FOUND
|
2021-03-07 00:08:36 -03:00
|
|
|
if payment.pending:
|
2021-03-07 16:13:20 -03:00
|
|
|
return (
|
|
|
|
f"Payment {payment_hash} wasn't received yet. Please try again in a minute." + style,
|
|
|
|
HTTPStatus.PAYMENT_REQUIRED,
|
|
|
|
)
|
2021-03-07 00:08:36 -03:00
|
|
|
|
|
|
|
if payment.time + 60 * 15 < time.time():
|
2021-03-07 16:13:20 -03:00
|
|
|
return "too much time has passed." + style
|
2021-03-07 00:08:36 -03:00
|
|
|
|
|
|
|
item = await get_item(payment.extra.get("item"))
|
|
|
|
shop = await get_shop(item.shop)
|
2021-03-07 16:13:20 -03:00
|
|
|
|
|
|
|
return shop.get_word(payment_hash) + style
|