async def api_payments_create_invoice(): if "description_hash" in g.data: description_hash = unhexlify(g.data["description_hash"]) memo = "" else: description_hash = b"" memo = g.data["memo"] try: payment_hash, payment_request = create_invoice( wallet_id=g.wallet.id, amount=g.data["amount"], memo=memo, description_hash=description_hash) except Exception as e: g.db.rollback() return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR invoice = bolt11.decode(payment_request) return ( jsonify({ "payment_hash": invoice.payment_hash, "payment_request": payment_request, # maintain backwards compatibility with API clients: "checking_id": invoice.payment_hash, }), HTTPStatus.CREATED, )
async def api_ticket_make_ticket(event_id, sats): event = get_event(event_id) if not event: return jsonify({"message": "Event does not exist."}), HTTPStatus.NOT_FOUND try: payment_hash, payment_request = create_invoice(wallet_id=event.wallet, amount=int(sats), memo=f"{event_id}", extra={"tag": "events"}) except Exception as e: return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR ticket = create_ticket(payment_hash=payment_hash, wallet=event.wallet, event=event_id, **g.data) if not ticket: return jsonify({"message": "Event could not be fetched."}), HTTPStatus.NOT_FOUND return jsonify({ "payment_hash": payment_hash, "payment_request": payment_request }), HTTPStatus.OK
async def api_lnurl_callback(link_id): link = increment_pay_link(link_id, served_pr=1) if not link: return jsonify({ "status": "ERROR", "reason": "LNURL-pay not found." }), HTTPStatus.OK payment_hash, payment_request = create_invoice( wallet_id=link.wallet, amount=link.amount, memo=link.description, description_hash=hashlib.sha256( link.lnurlpay_metadata.encode("utf-8")).digest(), extra={"tag": "lnurlp"}, ) save_link_invoice(link_id, payment_request) resp = LnurlPayActionResponse( pr=payment_request, success_action=link.success_action(payment_hash), routes=[], ) return jsonify(resp.dict()), HTTPStatus.OK
def api_ticket_make_ticket(event_id, sats): event = get_event(event_id) if not event: return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND try: checking_id, payment_request = create_invoice( wallet_id=event.wallet, amount=int(sats), memo=f"#lnticket {event_id}") except Exception as e: return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR ticket = create_ticket(checking_id=checking_id, wallet=event.wallet, event=event_id, **g.data) if not ticket: return jsonify({"message": "LNTicket could not be fetched." }), HTTPStatus.NOT_FOUND return jsonify({ "checking_id": checking_id, "payment_request": payment_request }), HTTPStatus.OK
async def api_diagonalley_stall_order(indexer_id): product = get_diagonalleys_product(g.data["id"]) shipping = get_diagonalleys_indexer(indexer_id) if g.data["shippingzone"] == 1: shippingcost = shipping.zone1cost else: shippingcost = shipping.zone2cost checking_id, payment_request = create_invoice( wallet_id=product.wallet, amount=shippingcost + (g.data["quantity"] * product.price), memo=g.data["id"] ) selling_id = urlsafe_b64encode(uuid4().bytes_le).decode("utf-8") with open_ext_db("diagonalley") as db: db.execute( """ INSERT INTO orders (id, productid, wallet, product, quantity, shippingzone, address, email, invoiceid, paid, shipped) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( selling_id, g.data["id"], product.wallet, product.product, g.data["quantity"], g.data["shippingzone"], g.data["address"], g.data["email"], checking_id, False, False, ), ) return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK
def api_ticket_create(form_id, sats): form = get_form(form_id) try: checking_id, payment_request = create_invoice( wallet_id=form.wallet, amount=int(sats), memo=f"#lnticket {form_id}" ) except Exception as e: return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK
async def api_lnurl_callback(link_id): link = increment_pay_link(link_id, served_pr=1) if not link: return jsonify({"status": "ERROR", "reason": "LNURL-pay not found."}), HTTPStatus.OK min, max = link.min, link.max rate = await get_fiat_rate(link.currency) if link.currency else 1 if link.currency: # allow some fluctuation (as the fiat price may have changed between the calls) min = rate * 995 * link.min max = rate * 1010 * link.max else: min = link.min * 1000 max = link.max * 1000 amount_received = int(request.args.get("amount")) if amount_received < min: return ( jsonify(LnurlErrorResponse(reason=f"Amount {amount_received} is smaller than minimum {min}.").dict()), HTTPStatus.OK, ) elif amount_received > max: return ( jsonify(LnurlErrorResponse(reason=f"Amount {amount_received} is greater than maximum {max}.").dict()), HTTPStatus.OK, ) comment = request.args.get("comment") if len(comment or "") > link.comment_chars: return ( jsonify( LnurlErrorResponse( reason=f"Got a comment with {len(comment)} characters, but can only accept {link.comment_chars}" ).dict() ), HTTPStatus.OK, ) payment_hash, payment_request = create_invoice( wallet_id=link.wallet, amount=int(amount_received / 1000), memo=link.description, description_hash=hashlib.sha256(link.lnurlpay_metadata.encode("utf-8")).digest(), extra={"tag": "lnurlp", "link": link.id, "comment": comment}, ) resp = LnurlPayActionResponse( pr=payment_request, success_action=link.success_action(payment_hash), routes=[], ) return jsonify(resp.dict()), HTTPStatus.OK
async def api_tpos_create_invoice(tpos_id): tpos = get_tpos(tpos_id) if not tpos: return jsonify({"message": "TPoS does not exist."}), HTTPStatus.NOT_FOUND try: payment_hash, payment_request = create_invoice( wallet_id=tpos.wallet, amount=g.data["amount"], memo=f"{tpos.name}", extra={"tag": "tpos"} ) except Exception as e: return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR return jsonify({"payment_hash": payment_hash, "payment_request": payment_request}), HTTPStatus.CREATED
def api_tpos_create_invoice(tpos_id): tpos = get_tpos(tpos_id) if not tpos: return jsonify({"message": "TPoS does not exist."}), HTTPStatus.NOT_FOUND try: checking_id, payment_request = create_invoice( wallet_id=tpos.wallet, amount=g.data["amount"], memo=f"#tpos {tpos.name}" ) except Exception as e: return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.CREATED
def api_paywall_create_invoice(paywall_id): paywall = get_paywall(paywall_id) if g.data["amount"] < paywall.amount: return jsonify({"message": f"Minimum amount is {paywall.amount} sat."}), HTTPStatus.BAD_REQUEST try: amount = g.data["amount"] if g.data["amount"] > paywall.amount else paywall.amount checking_id, payment_request = create_invoice( wallet_id=paywall.wallet, amount=amount, memo=f"#paywall {paywall.memo}" ) except Exception as e: return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.CREATED
def api_paywall_get_invoice(paywall_id): paywall = get_paywall(paywall_id) try: checking_id, payment_request = create_invoice( wallet_id=paywall.wallet, amount=paywall.amount, memo=f"#paywall {paywall.memo}") except Exception as e: return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR return jsonify({ "checking_id": checking_id, "payment_request": payment_request }), Status.OK
def api_amilkit(amilk_id): milk = get_amilk(amilk_id) memo = milk.id try: withdraw_res = handle_lnurl(milk.lnurl, response_class=LnurlWithdrawResponse) except LnurlException: abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") print(withdraw_res.max_sats) try: checking_id, payment_request = create_invoice( wallet_id=milk.wallet, amount=withdraw_res.max_sats, memo=memo) #print(payment_request) except Exception as e: error_message = False, str(e) r = requests.get( withdraw_res.callback.base, params={ **withdraw_res.callback.query_params, **{ "k1": withdraw_res.k1, "pr": payment_request } }, ) if not r.ok: abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") for i in range(10): invoice_status = WALLET.get_invoice_status(checking_id) sleep(i) if not invoice_status.paid: continue else: return jsonify({"paid": False}), HTTPStatus.OK break return jsonify({"paid": True}), HTTPStatus.OK
async def api_amilkit(amilk_id): milk = get_amilk(amilk_id) memo = milk.id try: withdraw_res = handle_lnurl(milk.lnurl, response_class=LnurlWithdrawResponse) except LnurlException: abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") payment_hash, payment_request = create_invoice( wallet_id=milk.wallet, amount=withdraw_res.max_sats, memo=memo, extra={"tag": "amilk"}) r = requests.get( withdraw_res.callback.base, params={ **withdraw_res.callback.query_params, **{ "k1": withdraw_res.k1, "pr": payment_request } }, ) if not r.ok: abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.") for i in range(10): sleep(i) invoice_status = check_invoice_status(milk.wallet, payment_hash) if invoice_status.paid: return jsonify({"paid": True}), HTTPStatus.OK else: continue return jsonify({"paid": False}), HTTPStatus.OK
def api_ticket_make_ticket(form_id): form = get_form(form_id) if not form: return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND try: nwords = len(re.split(r"\s+", g.data["ltext"])) sats = nwords * form.costpword payment_hash, payment_request = create_invoice( wallet_id=form.wallet, amount=sats, memo=f"ticket with {nwords} words on {form_id}", extra={"tag": "lnticket"}, ) except Exception as e: return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR ticket = create_ticket(payment_hash=payment_hash, wallet=form.wallet, sats=sats, **g.data) if not ticket: return jsonify({"message": "LNTicket could not be fetched."}), HTTPStatus.NOT_FOUND return jsonify({"payment_hash": payment_hash, "payment_request": payment_request}), HTTPStatus.OK
def lndhub_addinvoice(): try: _, pr = create_invoice( wallet_id=g.wallet.id, amount=int(g.data["amt"]), memo=g.data["memo"], extra={"tag": "lndhub"}, ) except Exception as e: return jsonify({ "error": True, "code": 7, "message": "Failed to create invoice: " + str(e), }) invoice = bolt11.decode(pr) return jsonify({ "pay_req": pr, "payment_request": pr, "add_index": "500", "r_hash": to_buffer(invoice.payment_hash), "hash": invoice.payment_hash, })