|
| 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 | + ) |
0 commit comments