Skip to content

Commit f027117

Browse files
committed
feat: add payment-related classes and methods for bank card data and receipts
1 parent 78677fb commit f027117

16 files changed

Lines changed: 908 additions & 6 deletions

pyrogram/methods/payments/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
from .apply_gift_code import ApplyGiftCode
2121
from .buy_gift_upgrade import BuyGiftUpgrade
2222
from .check_gift_code import CheckGiftCode
23+
from .clear_saved_payment_info import ClearSavedPaymentInfo
2324
from .convert_gift_to_stars import ConvertGiftToStars
2425
from .craft_gift import CraftGift
2526
from .create_gift_collection import CreateGiftCollection
2627
from .delete_gift_collection import DeleteGiftCollection
2728
from .drop_gift_original_details import DropGiftOriginalDetails
2829
from .edit_star_subscription import EditStarSubscription
2930
from .get_available_gifts import GetAvailableGifts
31+
from .get_bank_card_data import GetBankCardData
3032
from .get_chat_gifts import GetChatGifts
3133
from .get_gift_auction_state import GetGiftAuctionState
3234
from .get_chat_gifts_count import GetChatGiftsCount
@@ -35,6 +37,8 @@
3537
from .get_gift_upgrade_variants import GetGiftUpgradeVariants
3638
from .get_gifts_for_crafting import GetGiftsForCrafting
3739
from .get_payment_form import GetPaymentForm
40+
from .get_payment_receipt import GetPaymentReceipt
41+
from .get_saved_payment_info import GetSavedPaymentInfo
3842
from .get_stars_balance import GetStarsBalance
3943
from .get_ton_balance import GetTonBalance
4044
from .get_upgraded_gift import GetUpgradedGift
@@ -60,20 +64,23 @@
6064
from .suggest_birthday import SuggestBirthday
6165
from .transfer_gift import TransferGift
6266
from .upgrade_gift import UpgradeGift
67+
from .validate_requested_info import ValidateRequestedInfo
6368

6469

6570
class Payments(
6671
AddCollectionGifts,
6772
ApplyGiftCode,
6873
BuyGiftUpgrade,
6974
CheckGiftCode,
75+
ClearSavedPaymentInfo,
7076
ConvertGiftToStars,
7177
CraftGift,
7278
CreateGiftCollection,
7379
DeleteGiftCollection,
7480
DropGiftOriginalDetails,
7581
EditStarSubscription,
7682
GetAvailableGifts,
83+
GetBankCardData,
7784
GetChatGifts,
7885
GetGiftAuctionState,
7986
GetChatGiftsCount,
@@ -82,6 +89,8 @@ class Payments(
8289
GetGiftUpgradeVariants,
8390
GetGiftsForCrafting,
8491
GetPaymentForm,
92+
GetPaymentReceipt,
93+
GetSavedPaymentInfo,
8594
GetStarsBalance,
8695
GetTonBalance,
8796
GetUpgradedGift,
@@ -106,6 +115,7 @@ class Payments(
106115
ShowGift,
107116
SuggestBirthday,
108117
TransferGift,
109-
UpgradeGift
118+
UpgradeGift,
119+
ValidateRequestedInfo
110120
):
111121
pass
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 Optional
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
24+
25+
class ClearSavedPaymentInfo:
26+
async def clear_saved_payment_info(
27+
self: "pyrogram.Client",
28+
credentials: Optional[bool] = None,
29+
info: Optional[bool] = None
30+
) -> bool:
31+
"""Clear saved payment information.
32+
33+
.. include:: /_includes/usable-by/users.rst
34+
35+
Parameters:
36+
credentials (``bool``, *optional*):
37+
Pass True to remove saved payment credentials.
38+
39+
info (``bool``, *optional*):
40+
Pass True to remove saved order information.
41+
42+
Returns:
43+
``bool``: True on success.
44+
45+
Example:
46+
.. code-block:: python
47+
48+
# Clear both credentials and info
49+
await app.clear_saved_payment_info(
50+
credentials=True,
51+
info=True
52+
)
53+
54+
# Clear only saved credentials
55+
await app.clear_saved_payment_info(credentials=True)
56+
"""
57+
r = await self.invoke(
58+
raw.functions.payments.ClearSavedInfo(
59+
credentials=credentials,
60+
info=info
61+
)
62+
)
63+
64+
return bool(r)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
import pyrogram
20+
from pyrogram import raw, types
21+
22+
23+
class GetBankCardData:
24+
async def get_bank_card_data(
25+
self: "pyrogram.Client",
26+
number: str
27+
) -> "types.BankCardData":
28+
"""Get information about a bank card by its number.
29+
30+
.. include:: /_includes/usable-by/users.rst
31+
32+
Parameters:
33+
number (``str``):
34+
The bank card number.
35+
36+
Returns:
37+
:obj:`~pyrogram.types.BankCardData`: On success, the bank card data is returned.
38+
39+
Example:
40+
.. code-block:: python
41+
42+
data = await app.get_bank_card_data("1234567890123456")
43+
44+
print(data.title)
45+
for link in data.open_urls:
46+
print(link.name, link.url)
47+
"""
48+
r = await self.invoke(
49+
raw.functions.payments.GetBankCardData(
50+
number=number
51+
)
52+
)
53+
54+
return types.BankCardData._parse(r)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 GetPaymentReceipt:
26+
async def get_payment_receipt(
27+
self: "pyrogram.Client",
28+
chat_id: Union[int, str],
29+
message_id: int
30+
) -> "types.PaymentReceipt":
31+
"""Get a payment receipt.
32+
33+
.. include:: /_includes/usable-by/users.rst
34+
35+
Parameters:
36+
chat_id (``int`` | ``str``):
37+
Unique identifier (int) or username (str) of the target chat.
38+
39+
message_id (``int``):
40+
Identifier of the message with the successful payment service message.
41+
42+
Returns:
43+
:obj:`~pyrogram.types.PaymentReceipt`: On success, the payment receipt is returned.
44+
45+
Example:
46+
.. code-block:: python
47+
48+
receipt = await app.get_payment_receipt(
49+
chat_id=chat_id,
50+
message_id=123
51+
)
52+
53+
print(receipt.total_amount, receipt.currency)
54+
"""
55+
r = await self.invoke(
56+
raw.functions.payments.GetPaymentReceipt(
57+
peer=await self.resolve_peer(chat_id),
58+
msg_id=message_id
59+
)
60+
)
61+
62+
return types.PaymentReceipt._parse(self, r)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import pyrogram
20+
from pyrogram import raw, types
21+
22+
23+
class GetSavedPaymentInfo:
24+
async def get_saved_payment_info(
25+
self: "pyrogram.Client",
26+
) -> "types.SavedPaymentInfo":
27+
"""Get saved payment information.
28+
29+
.. include:: /_includes/usable-by/users.rst
30+
31+
Returns:
32+
:obj:`~pyrogram.types.SavedPaymentInfo`: On success, the saved payment info is returned.
33+
34+
Example:
35+
.. code-block:: python
36+
37+
info = await app.get_saved_payment_info()
38+
39+
if info.has_saved_credentials:
40+
print("Has saved credentials")
41+
42+
if info.order_info:
43+
print(info.order_info.name)
44+
"""
45+
r = await self.invoke(
46+
raw.functions.payments.GetSavedInfo()
47+
)
48+
49+
return types.SavedPaymentInfo._parse(r)

pyrogram/methods/payments/send_payment_form.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Optional
20+
1921
import pyrogram
2022
from pyrogram import raw, types
2123

@@ -25,7 +27,10 @@ async def send_payment_form(
2527
self: "pyrogram.Client",
2628
payment_form_id: int,
2729
input_invoice: "types.InputInvoice",
28-
credentials: "types.InputCredentials" = None
30+
credentials: "types.InputCredentials" = None,
31+
requested_info_id: Optional[str] = None,
32+
shipping_option_id: Optional[str] = None,
33+
tip_amount: Optional[int] = None
2934
) -> "types.PaymentResult":
3035
"""Send a filled-out payment form to the bot for final verification.
3136
@@ -42,6 +47,16 @@ async def send_payment_form(
4247
The credentials chosen by user for payment.
4348
Pass None for a payment in Telegram Stars.
4449
50+
requested_info_id (``str``, *optional*):
51+
Identifier of a saved order info, returned by
52+
:meth:`~pyrogram.Client.validate_requested_info`.
53+
54+
shipping_option_id (``str``, *optional*):
55+
Identifier of the chosen shipping option.
56+
57+
tip_amount (``int``, *optional*):
58+
The amount of tip in the smallest units of the currency.
59+
4560
Returns:
4661
:obj:`~pyrogram.types.PaymentResult`: On success, the payment result is returned.
4762
@@ -60,7 +75,7 @@ async def send_payment_form(
6075
payment_form_id=form.id,
6176
input_invoice=invoice,
6277
credentials=types.InputCredentialsNew(
63-
data=json.dumps({"token": "...", "type": "card"}), # Pass the token received from the payment provider
78+
data=json.dumps({"token": "...", "type": "card"}),
6479
)
6580
)
6681
@@ -89,7 +104,10 @@ async def send_payment_form(
89104
raw.functions.payments.SendPaymentForm(
90105
form_id=payment_form_id,
91106
invoice=await input_invoice.write(self),
92-
credentials=await credentials.write(self)
107+
credentials=await credentials.write(self),
108+
requested_info_id=requested_info_id,
109+
shipping_option_id=shipping_option_id,
110+
tip_amount=tip_amount
93111
)
94112
)
95113

0 commit comments

Comments
 (0)