Skip to content

Commit 1708407

Browse files
Fix PaymentForm parsing
1 parent 98b9331 commit 1708407

4 files changed

Lines changed: 124 additions & 45 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ def get_title_list(s: str) -> list:
10701070
MediaAreaType
10711071
PrivacyRuleType
10721072
GiftForResaleOrder
1073+
PaymentFormType
10731074
""",
10741075
)
10751076

pyrogram/enums/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from .next_code_type import NextCodeType
3434
from .paid_reaction_privacy import PaidReactionPrivacy
3535
from .parse_mode import ParseMode
36+
from .payment_form_type import PaymentFormType
3637
from .phone_call_discard_reason import PhoneCallDiscardReason
3738
from .poll_type import PollType
3839
from .privacy_key import PrivacyKey
@@ -64,6 +65,7 @@
6465
'NextCodeType',
6566
'PaidReactionPrivacy',
6667
'ParseMode',
68+
'PaymentFormType',
6769
'PhoneCallDiscardReason',
6870
'PollType',
6971
'PrivacyKey',
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 enum import auto
20+
21+
from .auto_name import AutoName
22+
23+
24+
class PaymentFormType(AutoName):
25+
"""Describes type of payment form."""
26+
27+
REGULAR = auto()
28+
"The payment form is for a regular payment"
29+
30+
STARS = auto()
31+
"The payment form is for a payment in Telegram Stars"
32+
33+
STAR_SUBSCRIPTION = auto()
34+
"The payment form is for a payment in Telegram Stars for subscription"

pyrogram/types/messages_and_media/payment_form.py

Lines changed: 87 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,53 @@
1919
from typing import Optional
2020

2121
import pyrogram
22-
from pyrogram import types, raw
22+
from pyrogram import enums, raw, types
23+
2324
from ..object import Object
2425

2526

2627
class PaymentForm(Object):
27-
"""This object contains basic information about an payment form.
28+
"""Contains information about an invoice payment form.
2829
2930
Parameters:
3031
id (``int``):
3132
Form id.
3233
33-
bot (``str``):
34-
Bot.
34+
type (:obj:`~pyrogram.enums.PaymentFormType`):
35+
Type of the payment form.
3536
36-
title (``str``):
37+
title (``str``, *optional*):
3738
Form title.
3839
39-
description (``str``):
40+
description (``str``, *optional*):
4041
Form description.
4142
42-
invoice (``str``):
43-
Invoice.
43+
photo (:obj:`~pyrogram.types.Photo`, *optional*):
44+
Product photo.
45+
46+
seller_bot_user_id (``int``, *optional*):
47+
User identifier of the seller bot.
48+
49+
seller_bot (:obj:`~pyrogram.types.User`, *optional*):
50+
Information about the seller bot.
4451
45-
provider (``str``, *optional*):
46-
Payment provider.
52+
payment_provider_user_id (``int``, *optional*):
53+
User identifier of the payment provider bot.
54+
55+
payment_provider (:obj:`~pyrogram.types.User`, *optional*):
56+
Information about the payment provider.
57+
58+
invoice (``str``, *optional*):
59+
Invoice.
4760
4861
url (``str``, *optional*):
4962
Payment form URL.
5063
5164
can_save_credentials (``str``, *optional*):
52-
Whether the user can choose to save credentials.
65+
True, if the user can choose to save credentials.
5366
54-
is_password_missing (``str``, *optional*):
55-
Indicates that the user can save payment credentials,
56-
but only after setting up a 2FA password
57-
(currently the account doesn't have a 2FA password).
67+
need_password (``str``, *optional*):
68+
True, if the user will be able to save credentials, if sets up a 2-step verification password.
5869
5970
native_provider (``str``, *optional*):
6071
Payment provider name.
@@ -68,50 +79,81 @@ def __init__(
6879
*,
6980
client: "pyrogram.Client" = None,
7081
id: int,
71-
bot: "types.User",
72-
title: str,
73-
description: str,
74-
invoice: "types.Invoice",
75-
provider: Optional["types.User"] = None,
82+
type: "enums.PaymentFormType",
83+
title: Optional[str] = None,
84+
description: Optional[str] = None,
85+
photo: Optional["types.Photo"] = None,
86+
seller_bot_user_id: Optional[int] = None,
87+
seller_bot: Optional["types.User"] = None,
88+
payment_provider_user_id: Optional[int] = None,
89+
payment_provider: Optional["types.User"] = None,
90+
invoice: Optional["types.Invoice"] = None,
7691
url: Optional[str] = None,
7792
can_save_credentials: Optional[bool] = None,
78-
is_password_missing: Optional[bool] = None,
93+
need_password: Optional[bool] = None,
7994
native_provider: Optional[str] = None,
8095
raw: "raw.base.payments.PaymentForm" = None,
81-
# TODO: Add support for other params:
82-
# native_params
83-
# additional_params
84-
# saved_info
85-
# saved_credentials
8696
):
8797
super().__init__(client)
8898

8999
self.id = id
90-
self.bot = bot
100+
self.type = type
101+
self.seller_bot_user_id = seller_bot_user_id
102+
self.seller_bot = seller_bot
103+
self.payment_provider_user_id = payment_provider_user_id
104+
self.payment_provider = payment_provider
91105
self.title = title
92106
self.description = description
107+
self.photo = photo
93108
self.invoice = invoice
94-
self.provider = provider
95109
self.url = url
96110
self.can_save_credentials = can_save_credentials
97-
self.is_password_missing = is_password_missing
111+
self.need_password = need_password
98112
self.native_provider = native_provider
99113
self.raw = raw
100114

101115
@staticmethod
102-
def _parse(client, payment_form: "raw.base.payments.PaymentForm") -> "PaymentForm":
103-
users = {i.id: i for i in payment_form.users}
104-
105-
return PaymentForm(
106-
id=payment_form.form_id,
107-
bot=types.User._parse(client, users.get(payment_form.bot_id)),
108-
title=payment_form.title,
109-
description=payment_form.description,
110-
invoice=types.Invoice._parse(client, payment_form.invoice),
111-
provider=types.User._parse(client, users.get(getattr(payment_form, "provider_id", None))),
112-
url=getattr(payment_form, "url", None),
113-
can_save_credentials=getattr(payment_form, "can_save_credentials", None),
114-
is_password_missing=getattr(payment_form, "password_missing", None),
115-
native_provider=getattr(payment_form, "native_provider", None),
116-
raw=payment_form
117-
)
116+
def _parse(client, form: "raw.base.payments.PaymentForm") -> "PaymentForm":
117+
users = {i.id: i for i in getattr(form, "users", [])}
118+
119+
if isinstance(form, raw.types.payments.PaymentForm):
120+
return PaymentForm(
121+
id=form.form_id,
122+
type=enums.PaymentFormType.REGULAR,
123+
title=form.title,
124+
description=form.description,
125+
photo=types.Photo._parse(client, form.photo),
126+
seller_bot_user_id=form.bot_id,
127+
seller_bot=types.User._parse(client, users.get(form.bot_id)),
128+
payment_provider_user_id=form.provider_id,
129+
payment_provider=types.User._parse(client, users.get(form.provider_id)),
130+
invoice=types.Invoice._parse(client, form.invoice),
131+
url=form.url,
132+
can_save_credentials=form.can_save_credentials,
133+
need_password=form.password_missing,
134+
native_provider=form.native_provider,
135+
# native_params,
136+
# additional_methods,
137+
# saved_info,
138+
# saved_credentials,
139+
raw=form
140+
)
141+
elif isinstance(form, raw.types.payments.PaymentFormStarGift):
142+
return PaymentForm(
143+
id=form.form_id,
144+
type=enums.PaymentFormType.STAR_SUBSCRIPTION,
145+
invoice=types.Invoice._parse(client, form.invoice),
146+
raw=form
147+
)
148+
elif isinstance(form, raw.types.payments.PaymentFormStars):
149+
return PaymentForm(
150+
id=form.form_id,
151+
type=enums.PaymentFormType.STARS,
152+
title=form.title,
153+
description=form.description,
154+
photo=types.Photo._parse(client, form.photo),
155+
seller_bot_user_id=form.bot_id,
156+
seller_bot=types.User._parse(client, users.get(form.bot_id)),
157+
invoice=types.Invoice._parse(client, form.invoice),
158+
raw=form
159+
)

0 commit comments

Comments
 (0)