Skip to content

Commit

Permalink
Refactor *_payment_form
Browse files Browse the repository at this point in the history
  • Loading branch information
KurimuzonAkuma committed Aug 22, 2024
1 parent bed1244 commit 981ec3f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
27 changes: 16 additions & 11 deletions pyrogram/methods/payments/get_payment_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

class GetPaymentForm:
async def get_payment_form(
self: "pyrogram.Client",
chat_id: Union[int, str],
message_id: Union[int, str],
self: "pyrogram.Client", *,
chat_id: Union[int, str] = None,
message_id: int = None,
invoice_link: str = None
) -> "types.PaymentForm":
"""Get information about a invoice or paid media.
Expand All @@ -36,11 +37,12 @@ async def get_payment_form(
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
Unique identifier for the target chat in form of a *t.me/joinchat/* link, identifier (int) or username
of the target channel/supergroup (in the format @username).
message_id (``int`` | ``str``):
message_id (``int``):
Pass a message identifier or to get the invoice from message.
invoice_link (``str``):
Pass a invoice link in form of a *t.me/$...* link or slug itself to get the payment form from link.
Returns:
Expand All @@ -50,25 +52,28 @@ async def get_payment_form(
.. code-block:: python
# get payment form from message
app.get_payment_form(chat_id, 123)
app.get_payment_form(chat_id=chat_id, message_id=123)
# get payment form from link
app.get_payment_form(chat_id, "https://t.me/$xvbzUtt5sUlJCAAATqZrWRy9Yzk")
app.get_payment_form(invoice_link="https://t.me/$xvbzUtt5sUlJCAAATqZrWRy9Yzk")
"""
if not any((all((chat_id, message_id)), invoice_link)):
raise ValueError("You should pass at least one parameter to this method.")

invoice = None

if isinstance(message_id, int):
if message_id:
invoice = raw.types.InputInvoiceMessage(
peer=await self.resolve_peer(chat_id),
msg_id=message_id
)
elif isinstance(message_id, str):
match = re.match(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/\$)([\w-]+)$", message_id)
elif invoice_link:
match = re.match(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/\$)([\w-]+)$", invoice_link)

if match:
slug = match.group(1)
else:
slug = message_id
slug = invoice_link

invoice = raw.types.InputInvoiceSlug(
slug=slug
Expand Down
33 changes: 19 additions & 14 deletions pyrogram/methods/payments/send_payment_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

class SendPaymentForm:
async def send_payment_form(
self: "pyrogram.Client",
chat_id: Union[int, str],
message_id: Union[int, str],
self: "pyrogram.Client", *,
chat_id: Union[int, str] = None,
message_id: int = None,
invoice_link: str = None
) -> bool:
"""Pay an invoice.
Expand All @@ -40,12 +41,13 @@ async def send_payment_form(
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
Unique identifier for the target chat in form of a *t.me/joinchat/* link, identifier (int) or username
of the target channel/supergroup (in the format @username).
message_id (``int`` | ``str``):
message_id (``int``):
Pass a message identifier or to get the invoice from message.
Pass a invoice link in form of a *t.me/$...* link or slug itself to get the payment form from link.
invoice_link (``str``):
Pass a invoice link in form of a *t.me/$...* link or slug itself to pay this invoice.
Returns:
``bool``: On success, True is returned.
Expand All @@ -54,32 +56,35 @@ async def send_payment_form(
.. code-block:: python
# Pay invoice from message
app.send_payment_form(chat_id, 123)
app.send_payment_form(chat_id=chat_id, message_id=123)
# Pay invoice form from link
# Chat id can be None
app.send_payment_form(chat_id, "https://t.me/$xvbzUtt5sUlJCAAATqZrWRy9Yzk")
app.send_payment_form(invoice_link="https://t.me/$xvbzUtt5sUlJCAAATqZrWRy9Yzk")
"""
if not any((all((chat_id, message_id)), invoice_link)):
raise ValueError("You should pass at least one parameter to this method.")

form = None
invoice = None

if isinstance(message_id, int):
if message_id:
invoice = raw.types.InputInvoiceMessage(
peer=await self.resolve_peer(chat_id),
msg_id=message_id
)
elif isinstance(message_id, str):
match = re.match(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/\$)([\w-]+)$", message_id)
elif invoice_link:
match = re.match(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/\$)([\w-]+)$", invoice_link)

if match:
slug = match.group(1)
else:
slug = message_id
slug = invoice_link

invoice = raw.types.InputInvoiceSlug(
slug=slug
)

form = await self.get_payment_form(chat_id=chat_id, message_id=message_id)
form = await self.get_payment_form(chat_id=chat_id, message_id=message_id, invoice_link=invoice_link)

# if form.invoice.currency == "XTR":
await self.invoke(
Expand Down

0 comments on commit 981ec3f

Please sign in to comment.