Skip to content

Commit 5f73557

Browse files
committed
Add RefundedPayment
1 parent 671f73a commit 5f73557

File tree

7 files changed

+107
-1
lines changed

7 files changed

+107
-1
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ def get_title_list(s: str) -> list:
604604
ShippingOption
605605
ShippingQuery
606606
SuccessfulPayment
607+
RefundedPayment
607608
""",
608609
users_chats="""
609610
Users & Chats

docs/source/releases/changes-in-this-fork.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
1414
| Scheme layer used: 184 |
1515
+------------------------+
1616

17+
- Added the class :obj:`~pyrogram.types.RefundedPayment`, containing information about a refunded payment.
18+
- Added the field ``refunded_payment`` to the class :obj:`~pyrogram.types.Message`, describing a service message about a refunded payment.
1719
- `View new and changed raw API methods <https://telegramplayground.github.io/TG-APIs/TL/diff/tdesktop.html?from=183&to=184>`__.
1820

1921

pyrogram/enums/message_service_type.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,6 @@ class MessageServiceType(AutoName):
122122

123123
SUCCESSFUL_PAYMENT = auto()
124124
"Successful payment"
125+
126+
REFUNDED_PAYMENT = auto()
127+
"Refunded payment"

pyrogram/types/business/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .shipping_option import ShippingOption
3131
from .shipping_query import ShippingQuery
3232
from .successful_payment import SuccessfulPayment
33+
from .refunded_payment import RefundedPayment
3334

3435
__all__ = [
3536
"BusinessConnection",
@@ -46,4 +47,5 @@
4647
"ShippingOption",
4748
"ShippingQuery",
4849
"SuccessfulPayment",
50+
"RefundedPayment",
4951
]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
from ..object import Object
25+
26+
27+
class RefundedPayment(Object):
28+
"""This object contains basic information about a refunded payment.
29+
30+
Parameters:
31+
currency (``str``):
32+
Three-letter ISO 4217 `currency <https://core.telegram.org/bots/payments#supported-currencies>`_ code, or ``XTR`` for payments in `Telegram Stars <https://t.me/BotNews/90>`_.
33+
34+
total_amount (``int``):
35+
Total price in the smallest units of the currency (integer, **not** float/double). For example, for a price of ``US$ 1.45`` pass ``amount = 145``. See the __exp__ parameter in `currencies.json <https://core.telegram.org/bots/payments/currencies.json>`_, it shows the number of digits past the decimal point for each currency (2 for the majority of currencies).
36+
37+
invoice_payload (``str``):
38+
Bot specified invoice payload. Only available to the bot that received the payment.
39+
40+
telegram_payment_charge_id (``str``):
41+
Telegram payment identifier. Only available to the bot that received the payment.
42+
43+
provider_payment_charge_id (``str``):
44+
Provider payment identifier. Only available to the bot that received the payment.
45+
46+
"""
47+
48+
def __init__(
49+
self,
50+
*,
51+
currency: str,
52+
total_amount: str,
53+
invoice_payload: str,
54+
telegram_payment_charge_id: str,
55+
provider_payment_charge_id: str
56+
):
57+
super().__init__()
58+
59+
self.currency = currency
60+
self.total_amount = total_amount
61+
self.invoice_payload = invoice_payload
62+
self.telegram_payment_charge_id = telegram_payment_charge_id
63+
self.provider_payment_charge_id = provider_payment_charge_id
64+
65+
@staticmethod
66+
def _parse(
67+
client: "pyrogram.Client",
68+
refunded_payment: "raw.types.MessageActionPaymentRefunded"
69+
) -> "RefundedPayment":
70+
invoice_payload = None
71+
72+
# Try to decode invoice payload into string. If that fails, fallback to bytes instead of decoding by
73+
# ignoring/replacing errors, this way, button clicks will still work.
74+
try:
75+
invoice_payload = refunded_payment.payload.decode()
76+
except (UnicodeDecodeError, AttributeError):
77+
invoice_payload = getattr(refunded_payment, "payload", None)
78+
79+
telegram_payment_charge_id = refunded_payment.charge.id
80+
provider_payment_charge_id = refunded_payment.charge.provider_charge_id
81+
82+
return RefundedPayment(
83+
currency=successful_payment.currency,
84+
total_amount=successful_payment.total_amount,
85+
invoice_payload=invoice_payload,
86+
telegram_payment_charge_id=telegram_payment_charge_id,
87+
provider_payment_charge_id=shipping_option_id
88+
)

pyrogram/types/business/successful_payment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _parse(
134134
total_amount=successful_payment.total_amount,
135135
invoice_payload=invoice_payload,
136136
telegram_payment_charge_id=telegram_payment_charge_id,
137-
provider_payment_charge_id=shipping_option_id,
137+
provider_payment_charge_id=provider_payment_charge_id,
138138
shipping_option_id=shipping_option_id,
139139
order_info=order_info,
140140
is_recurring=getattr(successful_payment, "recurring_used", None),

pyrogram/types/messages_and_media/message.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ class Message(Object, Update):
269269
successful_payment (:obj:`~pyrogram.types.SuccessfulPayment`, *optional*):
270270
Message is a service message about a successful payment, information about the payment. `More about payments »<https://core.telegram.org/bots/api#payments>`_
271271
272+
refunded_payment (:obj:`~pyrogram.types.RefundedPayment`, *optional*):
273+
Message is a service message about a refunded payment, information about the payment. `More about payments »<https://core.telegram.org/bots/api#payments>`_
274+
272275
users_shared (:obj:`~pyrogram.types.UsersShared`, *optional*):
273276
Service message: users were shared with the bot
274277
@@ -466,6 +469,7 @@ def __init__(
466469
pinned_message: "Message" = None,
467470
invoice: "types.Invoice" = None,
468471
successful_payment: "types.SuccessfulPayment" = None,
472+
refunded_payment: "types.RefundedPayment" = None,
469473
users_shared: "types.UsersShared" = None,
470474
chat_shared: "types.ChatShared" = None,
471475

@@ -617,6 +621,7 @@ def __init__(
617621
self.business_connection_id = business_connection_id
618622
self.successful_payment = successful_payment
619623
self.paid_media = paid_media
624+
self.refunded_payment = refunded_payment
620625
self._raw = _raw
621626

622627
@staticmethod
@@ -699,6 +704,7 @@ async def _parse(
699704
general_forum_topic_hidden = None
700705
general_forum_topic_unhidden = None
701706
successful_payment = None
707+
refunded_payment = None
702708

703709
service_type = None
704710

@@ -891,6 +897,10 @@ async def _parse(
891897
elif isinstance(action, (raw.types.MessageActionPaymentSent, raw.types.MessageActionPaymentSentMe)):
892898
successful_payment = types.SuccessfulPayment._parse(client, action)
893899
service_type = enums.MessageServiceType.SUCCESSFUL_PAYMENT
900+
901+
elif isinstance(action, raw.types.MessageActionPaymentRefunded):
902+
refunded_payment = types.RefundedPayment._parse(client, action)
903+
service_type = enums.MessageServiceType.REFUNDED_PAYMENT
894904

895905
elif isinstance(action, raw.types.MessageActionTopicEdit):
896906
title = getattr(action, "title", None)

0 commit comments

Comments
 (0)