Skip to content

Commit

Permalink
Add methods get_payment_form and send_payment_form (#89)
Browse files Browse the repository at this point in the history
* Add the type PaymentForm
* Add the bound method Message.pay

Co-authored-by: KurimuzonAkuma <arturgayauskas@gmail.com>
  • Loading branch information
yehuda-lev and KurimuzonAkuma authored Sep 3, 2024
1 parent a8f4703 commit 5111b74
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 25 deletions.
4 changes: 4 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ def get_title_list(s: str) -> list:
refund_star_payment
get_business_connection
get_collectible_item_info
get_payment_form
send_payment_form
""",
advanced="""
Advanced
Expand Down Expand Up @@ -651,6 +653,7 @@ def get_title_list(s: str) -> list:
ShippingAddress
OrderInfo
ShippingOption
PaymentForm
SuccessfulPayment
RefundedPayment
ShippingQuery
Expand Down Expand Up @@ -726,6 +729,7 @@ def get_title_list(s: str) -> list:
Message.read
Message.view
Message.translate
Message.pay
""",
chat="""
Chat
Expand Down
1 change: 1 addition & 0 deletions compiler/errors/source/400_BAD_REQUEST.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ AUTH_TOKEN_EXPIRED The authorization token has expired.
AUTH_TOKEN_INVALID The specified auth token is invalid.
AUTH_TOKEN_INVALIDX The specified auth token is invalid.
AUTOARCHIVE_NOT_AVAILABLE The autoarchive setting is not available at this time: please check the value of the [autoarchive_setting_available field in client config](https://core.telegram.org/api/config#client-configuration) before calling this method.
BALANCE_TOO_LOW Your stars balance too low
BANK_CARD_NUMBER_INVALID The specified card number is invalid.
BANNED_RIGHTS_INVALID You provided some invalid flags in the banned rights.
BASE_PORT_LOC_INVALID The base port location is invalid
Expand Down
3 changes: 2 additions & 1 deletion compiler/errors/source/406_NOT_ACCEPTABLE.tsv
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
id message
AUTH_KEY_DUPLICATED The same authorization key (session file) was used in more than one place simultaneously. You must delete your session file and log in again with your phone number or bot token
BANNED_RIGHTS_INVALID You provided some invalid flags in the banned rights.
BOT_PRECHECKOUT_FAILED Bot precheckout failed.
CALL_PROTOCOL_COMPAT_LAYER_INVALID The other side of the call does not support any of the VoIP protocols supported by the local client, as specified by the `protocol.layer` and `protocol.library_versions` fields.
CHANNEL_PRIVATE You haven't joined this channel/supergroup.
CHANNEL_TOO_LARGE Channel is too large to be deleted; this error is issued when trying to delete channels with more than 1000 members (subject to change).
Expand All @@ -22,4 +23,4 @@ TOPIC_CLOSED This topic was closed, you can't send messages to it anymore.
TOPIC_DELETED The specified topic was deleted.
USERPIC_PRIVACY_REQUIRED You need to disable privacy settings for your profile picture in order to make your geolocation public.
USERPIC_UPLOAD_REQUIRED You must have a profile picture to publish your geolocation.
USER_RESTRICTED You're spamreported, you can't create channels or chats.
USER_RESTRICTED You're spamreported, you can't create channels or chats.
1 change: 1 addition & 0 deletions pyrogram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class Client(Methods):

INVITE_LINK_RE = re.compile(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/(?:joinchat/|\+))([\w-]+)$")
TME_PUBLIC_LINK_RE = re.compile(r"^(?:https?://)?(?:www|([\w-]+)\.)?(?:t(?:elegram)?\.(?:org|me|dog))/?([\w-]+)?$")
INVOICE_LINK_RE = re.compile(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/\$)([\w-]+)$")
WORKERS = min(32, (os.cpu_count() or 0) + 4) # os.cpu_count() can be None
WORKDIR = PARENT_DIR

Expand Down
4 changes: 4 additions & 0 deletions pyrogram/methods/business/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from .get_collectible_item_info import GetCollectibleItemInfo
from .refund_star_payment import RefundStarPayment
from .send_invoice import SendInvoice
from .get_payment_from import GetPaymentForm
from .send_payment_from import SendPaymentForm


class TelegramBusiness(
Expand All @@ -33,5 +35,7 @@ class TelegramBusiness(
GetCollectibleItemInfo,
RefundStarPayment,
SendInvoice,
GetPaymentForm,
SendPaymentForm,
):
pass
87 changes: 87 additions & 0 deletions pyrogram/methods/business/get_payment_from.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from typing import Union

import pyrogram
from pyrogram import raw, types


class GetPaymentForm:
async def get_payment_form(
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.
.. include:: /_includes/usable-by/users.rst
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
of the target channel/supergroup (in the format @username).
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:
:obj:`~pyrogram.types.PaymentForm`: On success, a payment form is returned.
Example:
.. code-block:: python
# get payment form from message
app.get_payment_form(chat_id=chat_id, message_id=123)
# get payment form from link
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 message_id:
invoice = raw.types.InputInvoiceMessage(
peer=await self.resolve_peer(chat_id),
msg_id=message_id
)
elif invoice_link:
match = self.INVOICE_LINK_RE.match(invoice_link)

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

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

r = await self.invoke(
raw.functions.payments.GetPaymentForm(
invoice=invoice
)
)

return types.PaymentForm._parse(self, r)
104 changes: 104 additions & 0 deletions pyrogram/methods/business/send_payment_from.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from typing import Union

import pyrogram
from pyrogram import raw


class SendPaymentForm:
async def send_payment_form(
self: "pyrogram.Client", *,
chat_id: Union[int, str] = None,
message_id: int = None,
invoice_link: str = None
) -> bool:
"""Pay an invoice.
.. note::
For now only stars invoices are supported.
.. include:: /_includes/usable-by/users.rst
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
of the target channel/supergroup (in the format @username).
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 pay this invoice.
Returns:
``bool``: On success, True is returned.
Example:
.. code-block:: python
# Pay invoice from message
app.send_payment_form(chat_id=chat_id, message_id=123)
# Pay invoice form from link
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 message_id:
invoice = raw.types.InputInvoiceMessage(
peer=await self.resolve_peer(chat_id),
msg_id=message_id
)
elif invoice_link:
match = self.INVOICE_LINK_RE.match(invoice_link)
if match:
slug = match.group(1)
else:
slug = invoice_link

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

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(
raw.functions.payments.SendStarsForm(
form_id=form.id,
invoice=invoice
)
)
# TODO: Add support for regular invoices (credentials)
# else:
# r = await self.invoke(
# raw.functions.payments.SendPaymentForm(
# form_id=form.id,
# invoice=invoice,
# credentials=raw.types.InputPaymentCredentials(data=raw.types.DataJSON(data={}))
# )
# )

return True
Loading

1 comment on commit 5111b74

@KurimuzonAkuma
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

Please sign in to comment.