Add new payment classes

Invoice, LabeledPrice, OrderInfo, PreCheckoutQuery, ShippingAddress, ShippingOption, ShippingQuery and SuccessfulPayment
This commit is contained in:
Jacob Bom 2017-05-19 19:41:06 +02:00
parent 9720f59d7e
commit 8e62b02ff6
9 changed files with 567 additions and 1 deletions

View file

@ -84,6 +84,14 @@ from .inputtextmessagecontent import InputTextMessageContent
from .inputlocationmessagecontent import InputLocationMessageContent
from .inputvenuemessagecontent import InputVenueMessageContent
from .inputcontactmessagecontent import InputContactMessageContent
from .labeledprice import LabeledPrice
from .successfulpayment import SuccessfulPayment
from .shippingoption import ShippingOption
from .shippingaddress import ShippingAddress
from .precheckoutquery import PreCheckoutQuery
from .orderinfo import OrderInfo
from .shippingquery import ShippingQuery
from .invoice import Invoice
from .webhookinfo import WebhookInfo
from .gamehighscore import GameHighScore
from .update import Update
@ -115,5 +123,6 @@ __all__ = [
'Video', 'Voice', 'MAX_MESSAGE_LENGTH', 'MAX_CAPTION_LENGTH', 'SUPPORTED_WEBHOOK_PORTS',
'MAX_FILESIZE_DOWNLOAD', 'MAX_FILESIZE_UPLOAD', 'MAX_MESSAGES_PER_SECOND_PER_CHAT',
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP', 'WebhookInfo', 'Animation',
'Game', 'GameHighScore'
'Game', 'GameHighScore', 'LabeledPrice', 'SuccessfulPayment', 'ShippingOption',
'ShippingAddress', 'PreCheckoutQuery', 'OrderInfo', 'Invoice', 'ShippingQuery'
]

57
telegram/invoice.py Normal file
View file

@ -0,0 +1,57 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Invoice."""
from telegram import TelegramObject
class Invoice(TelegramObject):
"""This object contains basic information about an invoice.
Attributes:
title (str): Product name
description (str): Product description
start_parameter (str): Unique bot deep-linking parameter that can
be used to generate this invoice
currency (str): Three-letter ISO 4217 currency code
total_amount (int): Total price in the smallest units of the currency (integer)
"""
def __init__(self, title, description, start_parameter, currency, total_amount):
self.title = title
self.description = description
self.start_parameter = start_parameter
self.currency = currency
self.total_amount = total_amount
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.Invoice:
"""
if not data:
return None
return Invoice(**data)

69
telegram/labeledprice.py Normal file
View file

@ -0,0 +1,69 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram LabeledPrice."""
from telegram import TelegramObject
class LabeledPrice(TelegramObject):
"""This object represents a portion of the price for goods or services.
Attributes:
label (str): Portion label
amount (int): Price of the product in the smallest units of the currency (integer)
"""
def __init__(self, label, amount):
self.label = label
self.amount = amount
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.LabeledPrice:
"""
if not data:
return None
return LabeledPrice(**data)
@staticmethod
def de_list(data, bot):
"""
Args:
data (list):
bot (telegram.Bot):
Returns:
List<telegram.PhotoSize>:
"""
if not data:
return []
labeled_prices = list()
for labeled_price in data:
labeled_prices.append(LabeledPrice.de_json(labeled_price, bot))
return labeled_prices

58
telegram/orderinfo.py Normal file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram OrderInfo."""
from telegram import TelegramObject, ShippingAddress
class OrderInfo(TelegramObject):
"""This object represents information about an order.
Attributes:
name (Optional[str]): User name
phone_number (Optional[str]): User's phone number
email (Optional[str]): User email
shipping_address (Optional[:class:`telegram.ShippingAddress`]): User shipping address
"""
def __init__(self, name=None, phone_number=None, email=None, shipping_address=None):
self.name = name
self.phone_number = phone_number
self.email = email
self.shipping_address = shipping_address
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.OrderInfo:
"""
if not data:
return None
data = super(OrderInfo, OrderInfo).de_json(data, bot)
data['shipping_address'] = ShippingAddress.de_json(data.get('shipping_address'), bot)
return OrderInfo(**data)

View file

@ -0,0 +1,90 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram PreCheckoutQuery."""
from telegram import TelegramObject, User, OrderInfo
class PreCheckoutQuery(TelegramObject):
"""This object contains information about an incoming pre-checkout query.
Note:
* In Python `from` is a reserved word, use `from_user` instead.
Attributes:
id (int): Unique query identifier
from_user (:class:`telegram.User`): User who sent the query
currency (str): Three-letter ISO 4217 currency code
total_amount (int): Total price in the smallest units of the currency (integer)
invoice_payload (str): Bot specified invoice payload
Keyword Args:
shipping_option_id (Optional[str]): Identifier of the shipping option chosen by the user
order_info (Optional[:class:`telegram.OrderInfo`]): Order info provided by the user
"""
def __init__(self,
id,
from_user,
currency,
total_amount,
invoice_payload,
shipping_option_id=None,
order_info=None):
self.id = id
self.from_user = from_user
self.currency = currency
self.total_amount = total_amount
self.invoice_payload = invoice_payload
self.shipping_option_id = shipping_option_id
self.order_info = order_info
self._id_attrs = (self.id,)
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.PreCheckoutQuery:
"""
if not data:
return None
data = super(PreCheckoutQuery, PreCheckoutQuery).de_json(data, bot)
data['from_user'] = User.de_json(data.get('from'), bot)
data['order_info'] = OrderInfo.de_json(data.get('order_info'), bot)
return PreCheckoutQuery(**data)
def to_dict(self):
"""
Returns:
dict:
"""
data = super(PreCheckoutQuery, self).to_dict()
data['from'] = data.pop('from_user', None)
return data

View file

@ -0,0 +1,61 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram ShippingAddress."""
from telegram import TelegramObject
class ShippingAddress(TelegramObject):
"""This object represents a Telegram ShippingAddress.
Attributes:
country_code (str): ISO 3166-1 alpha-2 country code
state (str): State, if applicable
city (str): City
street_line1 (str): First line for the address
street_line2 (str): Second line for the address
post_code (str): Address post code
"""
def __init__(self, country_code, state, city, street_line1, street_line2, post_code):
self.country_code = country_code
self.state = state
self.city = city
self.street_line1 = street_line1
self.street_line2 = street_line2
self.post_code = post_code
self._id_attrs = (self.country_code, self.state, self.city, self.street_line1,
self.street_line2, self.post_code)
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.ShippingAddress:
"""
if not data:
return None
return ShippingAddress(**data)

View file

@ -0,0 +1,72 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram ShippingOption."""
from telegram import TelegramObject, LabeledPrice
class ShippingOption(TelegramObject):
"""This object represents one shipping option.
Note:
* In Python `from` is a reserved word, use `from_user` instead.
Attributes:
id (int): Shipping option identifier
title (str): Option title
prices (List[:class:`telegram.LabeledPrice`]): List of price portions
"""
def __init__(self, id, title, prices):
self.id = id
self.title = title
self.prices = prices
self._id_attrs = (self.id,)
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.ShippingOption:
"""
if not data:
return None
data = super(ShippingOption, ShippingOption).de_json(data, bot)
data['prices'] = LabeledPrice.de_list(data.get('prices'), bot)
return ShippingOption(**data)
def to_dict(self):
"""
Returns:
dict:
"""
data = super(ShippingOption, self).to_dict()
data['prices'] = [p.to_dict() for p in self.prices]
return data

75
telegram/shippingquery.py Normal file
View file

@ -0,0 +1,75 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram ShippingQuery."""
from telegram import TelegramObject, User, ShippingAddress
class ShippingQuery(TelegramObject):
"""This object contains information about an incoming shipping query.
Note:
* In Python `from` is a reserved word, use `from_user` instead.
Attributes:
id (int): Unique query identifier
from_user (:class:`telegram.User`): User who sent the query
invoice_payload (str): Bot specified invoice payload
shipping_address (:class:`telegram.ShippingQuery`): User specified shipping address
"""
def __init__(self, id, from_user, invoice_payload, shipping_address):
self.id = id
self.from_user = from_user
self.invoice_payload = invoice_payload
self.shipping_address = shipping_address
self._id_attrs = (self.id,)
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.ShippingQuery:
"""
if not data:
return None
data = super(ShippingQuery, ShippingQuery).de_json(data, bot)
data['from_user'] = User.de_json(data.get('from'), bot)
data['shipping_address'] = ShippingAddress.de_json(data.get('shipping_address'), bot)
return ShippingQuery(**data)
def to_dict(self):
"""
Returns:
dict:
"""
data = super(ShippingQuery, self).to_dict()
data['from'] = data.pop('from_user', None)
return data

View file

@ -0,0 +1,75 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram SuccessfulPayment."""
from telegram import TelegramObject, OrderInfo
class SuccessfulPayment(TelegramObject):
"""This object contains basic information about a successful payment.
Note:
* In Python `from` is a reserved word, use `from_user` instead.
Attributes:
currency (str): Three-letter ISO 4217 currency code
total_amount (int): Total price in the smallest units of the currency (integer)
invoice_payload (str): Bot specified invoice payload
telegram_payment_charge_id (str): Telegram payment identifier
provider_payment_charge_id (str): Provider payment identifier
Keyword Args:
shipping_option_id (Optional[str]): Identifier of the shipping option chosen by the user
order_info (Optional[:class:`telegram.OrderInfo`]): Order info provided by the user
"""
def __init__(self,
currency,
total_amount,
invoice_payload,
telegram_payment_charge_id,
provider_payment_charge_id,
shipping_option_id=None,
order_info=None):
self.currency = currency
self.total_amount = total_amount
self.invoice_payload = invoice_payload
self.shipping_option_id = shipping_option_id
self.order_info = order_info
self.telegram_payment_charge_id = telegram_payment_charge_id
self.provider_payment_charge_id = provider_payment_charge_id
@staticmethod
def de_json(data, bot):
"""
Args:
data (dict):
bot (telegram.Bot):
Returns:
telegram.SuccessfulPayment:
"""
if not data:
return None
data = super(SuccessfulPayment, SuccessfulPayment).de_json(data, bot)
data['order_info'] = OrderInfo.de_json(data.get('order_info'), bot)
return SuccessfulPayment(**data)