|
| 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 |
| 23 | + |
| 24 | + |
| 25 | +class SendPaymentForm: |
| 26 | + async def send_payment_form( |
| 27 | + self: "pyrogram.Client", *, |
| 28 | + chat_id: Union[int, str] = None, |
| 29 | + message_id: int = None, |
| 30 | + invoice_link: str = None |
| 31 | + ) -> bool: |
| 32 | + """Pay an invoice. |
| 33 | +
|
| 34 | + .. note:: |
| 35 | +
|
| 36 | + For now only stars invoices are supported. |
| 37 | +
|
| 38 | + .. include:: /_includes/usable-by/users.rst |
| 39 | +
|
| 40 | + Parameters: |
| 41 | + chat_id (``int`` | ``str``): |
| 42 | + Unique identifier (int) or username (str) of the target chat. |
| 43 | + of the target channel/supergroup (in the format @username). |
| 44 | +
|
| 45 | + message_id (``int``): |
| 46 | + Pass a message identifier or to get the invoice from message. |
| 47 | +
|
| 48 | + invoice_link (``str``): |
| 49 | + Pass a invoice link in form of a *t.me/$...* link or slug itself to pay this invoice. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + ``bool``: On success, True is returned. |
| 53 | +
|
| 54 | + Example: |
| 55 | + .. code-block:: python |
| 56 | +
|
| 57 | + # Pay invoice from message |
| 58 | + app.send_payment_form(chat_id=chat_id, message_id=123) |
| 59 | +
|
| 60 | + # Pay invoice form from link |
| 61 | + app.send_payment_form(invoice_link="https://t.me/$xvbzUtt5sUlJCAAATqZrWRy9Yzk") |
| 62 | + """ |
| 63 | + if not any((all((chat_id, message_id)), invoice_link)): |
| 64 | + raise ValueError("You should pass at least one parameter to this method.") |
| 65 | + |
| 66 | + form = None |
| 67 | + invoice = None |
| 68 | + |
| 69 | + if message_id: |
| 70 | + invoice = raw.types.InputInvoiceMessage( |
| 71 | + peer=await self.resolve_peer(chat_id), |
| 72 | + msg_id=message_id |
| 73 | + ) |
| 74 | + elif invoice_link: |
| 75 | + match = self.INVOICE_LINK_RE.match(invoice_link) |
| 76 | + if match: |
| 77 | + slug = match.group(1) |
| 78 | + else: |
| 79 | + slug = invoice_link |
| 80 | + |
| 81 | + invoice = raw.types.InputInvoiceSlug( |
| 82 | + slug=slug |
| 83 | + ) |
| 84 | + |
| 85 | + form = await self.get_payment_form(chat_id=chat_id, message_id=message_id, invoice_link=invoice_link) |
| 86 | + |
| 87 | + # if form.invoice.currency == "XTR": |
| 88 | + await self.invoke( |
| 89 | + raw.functions.payments.SendStarsForm( |
| 90 | + form_id=form.id, |
| 91 | + invoice=invoice |
| 92 | + ) |
| 93 | + ) |
| 94 | + # TODO: Add support for regular invoices (credentials) |
| 95 | + # else: |
| 96 | + # r = await self.invoke( |
| 97 | + # raw.functions.payments.SendPaymentForm( |
| 98 | + # form_id=form.id, |
| 99 | + # invoice=invoice, |
| 100 | + # credentials=raw.types.InputPaymentCredentials(data=raw.types.DataJSON(data={})) |
| 101 | + # ) |
| 102 | + # ) |
| 103 | + |
| 104 | + return True |
0 commit comments