Skip to content

Commit 9a7dea0

Browse files
fix: Correctly handle different invoice types to prevent crashes
Resolved a bug where the `Invoice` class would raise an `AttributeError` when parsing invoice data from payment links. This was due to the rigid expectation of `title`, `description`, and `start_parameter` attributes, which are not always present in the raw `Invoice` object returned by the Telegram API. - Modified the `Invoice` class in `pyrogram/types/payments/invoice.py` to make `title`, `description`, and `start_parameter` optional. - Updated the `_parse` method to handle both `raw.types.MessageMediaInvoice` and `raw.types.Invoice` types, ensuring that missing attributes are handled gracefully.
1 parent cb38d6a commit 9a7dea0

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

pyrogram/types/payments/invoice.py

Lines changed: 24 additions & 12 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 Pyrofork. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Union
20+
1921
from pyrogram import raw
2022
from ..object import Object
2123

@@ -52,11 +54,11 @@ class Invoice(Object):
5254
def __init__(
5355
self,
5456
*,
55-
title: str,
56-
description : str,
5757
currency: str,
5858
total_amount: int,
59-
start_parameter: str,
59+
title: str = None,
60+
description : str = None,
61+
start_parameter: str = None,
6062
shipping_address_requested: bool = None,
6163
test: bool = None,
6264
receipt_message_id: int = None,
@@ -75,15 +77,25 @@ def __init__(
7577

7678
@staticmethod
7779
def _parse(
78-
message_invoice: "raw.types.MessageMediaInvoice"
80+
invoice: Union["raw.types.MessageMediaInvoice", "raw.types.Invoice"]
7981
) -> "Invoice":
82+
if isinstance(invoice, raw.types.MessageMediaInvoice):
83+
return Invoice(
84+
title=invoice.title,
85+
description=invoice.description,
86+
currency=invoice.currency,
87+
total_amount=invoice.total_amount,
88+
start_parameter=invoice.start_param,
89+
shipping_address_requested=getattr(invoice, 'shipping_address_requested', None),
90+
test=getattr(invoice, 'test', None),
91+
receipt_message_id=getattr(invoice, 'receipt_msg_id', None)
92+
)
93+
94+
total_amount = sum(p.amount for p in invoice.prices)
95+
8096
return Invoice(
81-
title=message_invoice.title,
82-
description=message_invoice.description,
83-
currency=message_invoice.currency,
84-
total_amount=message_invoice.total_amount,
85-
start_parameter=message_invoice.start_param,
86-
shipping_address_requested=message_invoice.shipping_address_requested,
87-
test=message_invoice.test,
88-
receipt_message_id=message_invoice.receipt_msg_id
97+
currency=invoice.currency,
98+
total_amount=total_amount,
99+
shipping_address_requested=getattr(invoice, 'shipping_address_requested', None),
100+
test=getattr(invoice, 'test', None)
89101
)

0 commit comments

Comments
 (0)