mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2024-11-19 18:11:30 +01:00
d3fc52cd49
a big refactor that: - fixes some issues that might have happened (or not) with asynchronous reactions to payments; - paves the way to https://github.com/lnbits/lnbits/issues/121; - uses more async/await notation which just looks nice; and - makes it simple(r?) for one extension to modify stuff from other extensions.
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import json
|
|
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode, ParseResult
|
|
from quart import url_for
|
|
from typing import NamedTuple, Optional, Dict
|
|
from sqlite3 import Row
|
|
from lnurl import Lnurl, encode as lnurl_encode # type: ignore
|
|
from lnurl.types import LnurlPayMetadata # type: ignore
|
|
from lnurl.models import LnurlPaySuccessAction, MessageAction, UrlAction # type: ignore
|
|
|
|
|
|
class PayLink(NamedTuple):
|
|
id: int
|
|
wallet: str
|
|
description: str
|
|
min: int
|
|
served_meta: int
|
|
served_pr: int
|
|
webhook_url: str
|
|
success_text: str
|
|
success_url: str
|
|
currency: str
|
|
comment_chars: int
|
|
max: int
|
|
|
|
@classmethod
|
|
def from_row(cls, row: Row) -> "PayLink":
|
|
data = dict(row)
|
|
return cls(**data)
|
|
|
|
@property
|
|
def lnurl(self) -> Lnurl:
|
|
url = url_for("lnurlp.api_lnurl_response", link_id=self.id, _external=True)
|
|
return lnurl_encode(url)
|
|
|
|
@property
|
|
def lnurlpay_metadata(self) -> LnurlPayMetadata:
|
|
return LnurlPayMetadata(json.dumps([["text/plain", self.description]]))
|
|
|
|
def success_action(self, payment_hash: str) -> Optional[LnurlPaySuccessAction]:
|
|
if self.success_url:
|
|
url: ParseResult = urlparse(self.success_url)
|
|
qs: Dict = parse_qs(url.query)
|
|
qs["payment_hash"] = payment_hash
|
|
url = url._replace(query=urlencode(qs, doseq=True))
|
|
raw: str = urlunparse(url)
|
|
return UrlAction(url=raw, description=self.success_text)
|
|
elif self.success_text:
|
|
return MessageAction(message=self.success_text)
|
|
else:
|
|
return None
|