Skip to content

Commit

Permalink
Add RefundedPayment
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe committed Jul 7, 2024
1 parent 671f73a commit 5f73557
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ def get_title_list(s: str) -> list:
ShippingOption
ShippingQuery
SuccessfulPayment
RefundedPayment
""",
users_chats="""
Users & Chats
Expand Down
2 changes: 2 additions & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
| Scheme layer used: 184 |
+------------------------+

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


Expand Down
3 changes: 3 additions & 0 deletions pyrogram/enums/message_service_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,6 @@ class MessageServiceType(AutoName):

SUCCESSFUL_PAYMENT = auto()
"Successful payment"

REFUNDED_PAYMENT = auto()
"Refunded payment"
2 changes: 2 additions & 0 deletions pyrogram/types/business/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .shipping_option import ShippingOption
from .shipping_query import ShippingQuery
from .successful_payment import SuccessfulPayment
from .refunded_payment import RefundedPayment

__all__ = [
"BusinessConnection",
Expand All @@ -46,4 +47,5 @@
"ShippingOption",
"ShippingQuery",
"SuccessfulPayment",
"RefundedPayment",
]
88 changes: 88 additions & 0 deletions pyrogram/types/business/refunded_payment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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

from ..object import Object


class RefundedPayment(Object):
"""This object contains basic information about a refunded payment.
Parameters:
currency (``str``):
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>`_.
total_amount (``int``):
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).
invoice_payload (``str``):
Bot specified invoice payload. Only available to the bot that received the payment.
telegram_payment_charge_id (``str``):
Telegram payment identifier. Only available to the bot that received the payment.
provider_payment_charge_id (``str``):
Provider payment identifier. Only available to the bot that received the payment.
"""

def __init__(
self,
*,
currency: str,
total_amount: str,
invoice_payload: str,
telegram_payment_charge_id: str,
provider_payment_charge_id: str
):
super().__init__()

self.currency = currency
self.total_amount = total_amount
self.invoice_payload = invoice_payload
self.telegram_payment_charge_id = telegram_payment_charge_id
self.provider_payment_charge_id = provider_payment_charge_id

@staticmethod
def _parse(
client: "pyrogram.Client",
refunded_payment: "raw.types.MessageActionPaymentRefunded"
) -> "RefundedPayment":
invoice_payload = None

# Try to decode invoice payload into string. If that fails, fallback to bytes instead of decoding by
# ignoring/replacing errors, this way, button clicks will still work.
try:
invoice_payload = refunded_payment.payload.decode()
except (UnicodeDecodeError, AttributeError):
invoice_payload = getattr(refunded_payment, "payload", None)

telegram_payment_charge_id = refunded_payment.charge.id
provider_payment_charge_id = refunded_payment.charge.provider_charge_id

return RefundedPayment(
currency=successful_payment.currency,
total_amount=successful_payment.total_amount,
invoice_payload=invoice_payload,
telegram_payment_charge_id=telegram_payment_charge_id,
provider_payment_charge_id=shipping_option_id
)
2 changes: 1 addition & 1 deletion pyrogram/types/business/successful_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _parse(
total_amount=successful_payment.total_amount,
invoice_payload=invoice_payload,
telegram_payment_charge_id=telegram_payment_charge_id,
provider_payment_charge_id=shipping_option_id,
provider_payment_charge_id=provider_payment_charge_id,
shipping_option_id=shipping_option_id,
order_info=order_info,
is_recurring=getattr(successful_payment, "recurring_used", None),
Expand Down
10 changes: 10 additions & 0 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ class Message(Object, Update):
successful_payment (:obj:`~pyrogram.types.SuccessfulPayment`, *optional*):
Message is a service message about a successful payment, information about the payment. `More about payments »<https://core.telegram.org/bots/api#payments>`_
refunded_payment (:obj:`~pyrogram.types.RefundedPayment`, *optional*):
Message is a service message about a refunded payment, information about the payment. `More about payments »<https://core.telegram.org/bots/api#payments>`_
users_shared (:obj:`~pyrogram.types.UsersShared`, *optional*):
Service message: users were shared with the bot
Expand Down Expand Up @@ -466,6 +469,7 @@ def __init__(
pinned_message: "Message" = None,
invoice: "types.Invoice" = None,
successful_payment: "types.SuccessfulPayment" = None,
refunded_payment: "types.RefundedPayment" = None,
users_shared: "types.UsersShared" = None,
chat_shared: "types.ChatShared" = None,

Expand Down Expand Up @@ -617,6 +621,7 @@ def __init__(
self.business_connection_id = business_connection_id
self.successful_payment = successful_payment
self.paid_media = paid_media
self.refunded_payment = refunded_payment
self._raw = _raw

@staticmethod
Expand Down Expand Up @@ -699,6 +704,7 @@ async def _parse(
general_forum_topic_hidden = None
general_forum_topic_unhidden = None
successful_payment = None
refunded_payment = None

service_type = None

Expand Down Expand Up @@ -891,6 +897,10 @@ async def _parse(
elif isinstance(action, (raw.types.MessageActionPaymentSent, raw.types.MessageActionPaymentSentMe)):
successful_payment = types.SuccessfulPayment._parse(client, action)
service_type = enums.MessageServiceType.SUCCESSFUL_PAYMENT

elif isinstance(action, raw.types.MessageActionPaymentRefunded):
refunded_payment = types.RefundedPayment._parse(client, action)
service_type = enums.MessageServiceType.REFUNDED_PAYMENT

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

0 comments on commit 5f73557

Please sign in to comment.