Skip to content

Commit 5111b74

Browse files
Add methods get_payment_form and send_payment_form (#89)
* Add the type PaymentForm * Add the bound method Message.pay Co-authored-by: KurimuzonAkuma <[email protected]>
1 parent a8f4703 commit 5111b74

File tree

12 files changed

+461
-25
lines changed

12 files changed

+461
-25
lines changed

compiler/docs/compiler.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ def get_title_list(s: str) -> list:
393393
refund_star_payment
394394
get_business_connection
395395
get_collectible_item_info
396+
get_payment_form
397+
send_payment_form
396398
""",
397399
advanced="""
398400
Advanced
@@ -651,6 +653,7 @@ def get_title_list(s: str) -> list:
651653
ShippingAddress
652654
OrderInfo
653655
ShippingOption
656+
PaymentForm
654657
SuccessfulPayment
655658
RefundedPayment
656659
ShippingQuery
@@ -726,6 +729,7 @@ def get_title_list(s: str) -> list:
726729
Message.read
727730
Message.view
728731
Message.translate
732+
Message.pay
729733
""",
730734
chat="""
731735
Chat

compiler/errors/source/400_BAD_REQUEST.tsv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ AUTH_TOKEN_EXPIRED The authorization token has expired.
2121
AUTH_TOKEN_INVALID The specified auth token is invalid.
2222
AUTH_TOKEN_INVALIDX The specified auth token is invalid.
2323
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.
24+
BALANCE_TOO_LOW Your stars balance too low
2425
BANK_CARD_NUMBER_INVALID The specified card number is invalid.
2526
BANNED_RIGHTS_INVALID You provided some invalid flags in the banned rights.
2627
BASE_PORT_LOC_INVALID The base port location is invalid

compiler/errors/source/406_NOT_ACCEPTABLE.tsv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
id message
22
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
33
BANNED_RIGHTS_INVALID You provided some invalid flags in the banned rights.
4+
BOT_PRECHECKOUT_FAILED Bot precheckout failed.
45
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.
56
CHANNEL_PRIVATE You haven't joined this channel/supergroup.
67
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).
@@ -22,4 +23,4 @@ TOPIC_CLOSED This topic was closed, you can't send messages to it anymore.
2223
TOPIC_DELETED The specified topic was deleted.
2324
USERPIC_PRIVACY_REQUIRED You need to disable privacy settings for your profile picture in order to make your geolocation public.
2425
USERPIC_UPLOAD_REQUIRED You must have a profile picture to publish your geolocation.
25-
USER_RESTRICTED You're spamreported, you can't create channels or chats.
26+
USER_RESTRICTED You're spamreported, you can't create channels or chats.

pyrogram/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class Client(Methods):
237237

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

pyrogram/methods/business/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from .get_collectible_item_info import GetCollectibleItemInfo
2424
from .refund_star_payment import RefundStarPayment
2525
from .send_invoice import SendInvoice
26+
from .get_payment_from import GetPaymentForm
27+
from .send_payment_from import SendPaymentForm
2628

2729

2830
class TelegramBusiness(
@@ -33,5 +35,7 @@ class TelegramBusiness(
3335
GetCollectibleItemInfo,
3436
RefundStarPayment,
3537
SendInvoice,
38+
GetPaymentForm,
39+
SendPaymentForm,
3640
):
3741
pass
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Union
20+
21+
import pyrogram
22+
from pyrogram import raw, types
23+
24+
25+
class GetPaymentForm:
26+
async def get_payment_form(
27+
self: "pyrogram.Client", *,
28+
chat_id: Union[int, str] = None,
29+
message_id: int = None,
30+
invoice_link: str = None
31+
) -> "types.PaymentForm":
32+
"""Get information about a invoice or paid media.
33+
34+
.. include:: /_includes/usable-by/users.rst
35+
36+
Parameters:
37+
chat_id (``int`` | ``str``):
38+
Unique identifier (int) or username (str) of the target chat.
39+
of the target channel/supergroup (in the format @username).
40+
41+
message_id (``int``):
42+
Pass a message identifier or to get the invoice from message.
43+
44+
invoice_link (``str``):
45+
Pass a invoice link in form of a *t.me/$...* link or slug itself to get the payment form from link.
46+
47+
Returns:
48+
:obj:`~pyrogram.types.PaymentForm`: On success, a payment form is returned.
49+
50+
Example:
51+
.. code-block:: python
52+
53+
# get payment form from message
54+
app.get_payment_form(chat_id=chat_id, message_id=123)
55+
56+
# get payment form from link
57+
app.get_payment_form(invoice_link="https://t.me/$xvbzUtt5sUlJCAAATqZrWRy9Yzk")
58+
"""
59+
if not any((all((chat_id, message_id)), invoice_link)):
60+
raise ValueError("You should pass at least one parameter to this method.")
61+
62+
invoice = None
63+
64+
if message_id:
65+
invoice = raw.types.InputInvoiceMessage(
66+
peer=await self.resolve_peer(chat_id),
67+
msg_id=message_id
68+
)
69+
elif invoice_link:
70+
match = self.INVOICE_LINK_RE.match(invoice_link)
71+
72+
if match:
73+
slug = match.group(1)
74+
else:
75+
slug = invoice_link
76+
77+
invoice = raw.types.InputInvoiceSlug(
78+
slug=slug
79+
)
80+
81+
r = await self.invoke(
82+
raw.functions.payments.GetPaymentForm(
83+
invoice=invoice
84+
)
85+
)
86+
87+
return types.PaymentForm._parse(self, r)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Union
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
24+
25+
class SendPaymentForm:
26+
async def send_payment_form(
27+
self: "pyrogram.Client", *,
28+
chat_id: Union[int, str] = None,
29+
message_id: int = None,
30+
invoice_link: str = None
31+
) -> bool:
32+
"""Pay an invoice.
33+
34+
.. note::
35+
36+
For now only stars invoices are supported.
37+
38+
.. include:: /_includes/usable-by/users.rst
39+
40+
Parameters:
41+
chat_id (``int`` | ``str``):
42+
Unique identifier (int) or username (str) of the target chat.
43+
of the target channel/supergroup (in the format @username).
44+
45+
message_id (``int``):
46+
Pass a message identifier or to get the invoice from message.
47+
48+
invoice_link (``str``):
49+
Pass a invoice link in form of a *t.me/$...* link or slug itself to pay this invoice.
50+
51+
Returns:
52+
``bool``: On success, True is returned.
53+
54+
Example:
55+
.. code-block:: python
56+
57+
# Pay invoice from message
58+
app.send_payment_form(chat_id=chat_id, message_id=123)
59+
60+
# Pay invoice form from link
61+
app.send_payment_form(invoice_link="https://t.me/$xvbzUtt5sUlJCAAATqZrWRy9Yzk")
62+
"""
63+
if not any((all((chat_id, message_id)), invoice_link)):
64+
raise ValueError("You should pass at least one parameter to this method.")
65+
66+
form = None
67+
invoice = None
68+
69+
if message_id:
70+
invoice = raw.types.InputInvoiceMessage(
71+
peer=await self.resolve_peer(chat_id),
72+
msg_id=message_id
73+
)
74+
elif invoice_link:
75+
match = self.INVOICE_LINK_RE.match(invoice_link)
76+
if match:
77+
slug = match.group(1)
78+
else:
79+
slug = invoice_link
80+
81+
invoice = raw.types.InputInvoiceSlug(
82+
slug=slug
83+
)
84+
85+
form = await self.get_payment_form(chat_id=chat_id, message_id=message_id, invoice_link=invoice_link)
86+
87+
# if form.invoice.currency == "XTR":
88+
await self.invoke(
89+
raw.functions.payments.SendStarsForm(
90+
form_id=form.id,
91+
invoice=invoice
92+
)
93+
)
94+
# TODO: Add support for regular invoices (credentials)
95+
# else:
96+
# r = await self.invoke(
97+
# raw.functions.payments.SendPaymentForm(
98+
# form_id=form.id,
99+
# invoice=invoice,
100+
# credentials=raw.types.InputPaymentCredentials(data=raw.types.DataJSON(data={}))
101+
# )
102+
# )
103+
104+
return True

0 commit comments

Comments
 (0)