|
@ -4,7 +4,7 @@ from lnbits.bolt11 import decode as bolt11_decode # type: ignore |
|
|
from lnbits.helpers import urlsafe_short_hash |
|
|
from lnbits.helpers import urlsafe_short_hash |
|
|
from lnbits.settings import WALLET |
|
|
from lnbits.settings import WALLET |
|
|
|
|
|
|
|
|
from .crud import get_wallet, create_payment, delete_payment |
|
|
from .crud import get_wallet, create_payment, delete_payment, check_internal |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_invoice(*, wallet_id: str, amount: int, memo: str) -> Tuple[str, str]: |
|
|
def create_invoice(*, wallet_id: str, amount: int, memo: str) -> Tuple[str, str]: |
|
@ -26,33 +26,50 @@ def pay_invoice(*, wallet_id: str, bolt11: str, max_sat: Optional[int] = None) - |
|
|
temp_id = f"temp_{urlsafe_short_hash()}" |
|
|
temp_id = f"temp_{urlsafe_short_hash()}" |
|
|
try: |
|
|
try: |
|
|
invoice = bolt11_decode(bolt11) |
|
|
invoice = bolt11_decode(bolt11) |
|
|
|
|
|
print(invoice.payment_hash) |
|
|
|
|
|
internal = check_internal(invoice.payment_hash) |
|
|
|
|
|
|
|
|
if invoice.amount_msat == 0: |
|
|
if invoice.amount_msat == 0: |
|
|
raise ValueError("Amountless invoices not supported.") |
|
|
raise ValueError("Amountless invoices not supported.") |
|
|
|
|
|
|
|
|
if max_sat and invoice.amount_msat > max_sat * 1000: |
|
|
if max_sat and invoice.amount_msat > max_sat * 1000: |
|
|
raise ValueError("Amount in invoice is too high.") |
|
|
raise ValueError("Amount in invoice is too high.") |
|
|
|
|
|
|
|
|
fee_reserve = max(1000, int(invoice.amount_msat * 0.01)) |
|
|
fee_reserve = max(1000, int(invoice.amount_msat * 0.01)) |
|
|
create_payment( |
|
|
|
|
|
wallet_id=wallet_id, |
|
|
if not internal: |
|
|
checking_id=temp_id, |
|
|
create_payment( |
|
|
amount=-invoice.amount_msat, |
|
|
wallet_id=wallet_id, |
|
|
fee=-fee_reserve, |
|
|
checking_id=temp_id, |
|
|
memo=temp_id, |
|
|
payment_hash=invoice.payment_hash, |
|
|
) |
|
|
amount=-invoice.amount_msat, |
|
|
|
|
|
fee=-fee_reserve, |
|
|
|
|
|
memo=temp_id, |
|
|
|
|
|
) |
|
|
wallet = get_wallet(wallet_id) |
|
|
wallet = get_wallet(wallet_id) |
|
|
assert wallet, "invalid wallet id" |
|
|
assert wallet, "invalid wallet id" |
|
|
if wallet.balance_msat < 0: |
|
|
if wallet.balance_msat < 0: |
|
|
raise PermissionError("Insufficient balance.") |
|
|
raise PermissionError("Insufficient balance.") |
|
|
|
|
|
print(internal) |
|
|
|
|
|
if internal: |
|
|
|
|
|
create_payment( |
|
|
|
|
|
wallet_id=wallet_id, |
|
|
|
|
|
checking_id=temp_id, |
|
|
|
|
|
payment_hash=invoice.payment_hash, |
|
|
|
|
|
amount=-invoice.amount_msat, |
|
|
|
|
|
fee=0, |
|
|
|
|
|
pending=False, |
|
|
|
|
|
memo=invoice.description, |
|
|
|
|
|
) |
|
|
|
|
|
update_payment_status(checking_id=internal, pending=False) |
|
|
|
|
|
return temp_id |
|
|
|
|
|
|
|
|
ok, checking_id, fee_msat, error_message = WALLET.pay_invoice(bolt11) |
|
|
ok, checking_id, fee_msat, error_message = WALLET.pay_invoice(bolt11) |
|
|
|
|
|
|
|
|
if ok: |
|
|
if ok: |
|
|
create_payment( |
|
|
create_payment( |
|
|
wallet_id=wallet_id, |
|
|
wallet_id=wallet_id, |
|
|
checking_id=checking_id, |
|
|
checking_id=checking_id, |
|
|
|
|
|
payment_hash=invoice.payment_hash, |
|
|
amount=-invoice.amount_msat, |
|
|
amount=-invoice.amount_msat, |
|
|
fee=fee_msat, |
|
|
fee=fee_msat, |
|
|
memo=invoice.description, |
|
|
memo=invoice.description, |
|
|