Finalize Payment API

bugfixes
added payment-related handlers
This commit is contained in:
Jeff 2017-05-23 02:19:23 +08:00 committed by GitHub
commit 1210e4ef04
6 changed files with 56 additions and 10 deletions

View file

@ -29,7 +29,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Hugo Damer <https://github.com/HakimusGIT>`_
- `Jacob Bom <https://github.com/bomjacob>`_
- `JASON0916 <https://github.com/JASON0916>`_
- `jeffffc <https://github.com/jeffffc`_
- `jeffffc <https://github.com/jeffffc>`_
- `jh0ker <https://github.com/jh0ker>`_
- `John Yong <https://github.com/whipermr5>`_
- `jossalgon <https://github.com/jossalgon>`_

View file

@ -1926,7 +1926,9 @@ class Bot(TelegramObject):
shipping_query_id,
ok,
shipping_options=None,
error_message=None):
error_message=None,
timeout=None,
**kwargs):
"""
If you sent an invoice requesting a shipping address and the parameter is_flexible was
specified, the Bot API will send an Update with a shipping_query field to the bot. Use
@ -1943,6 +1945,7 @@ class Bot(TelegramObject):
that explains why it is impossible to complete the order (e.g. "Sorry, delivery
to your desired address is unavailable'). Telegram will display this message
to the user.
**kwargs (dict): Arbitrary keyword arguments.
Returns:
bool: On success, `True` is returned.
@ -1951,18 +1954,32 @@ class Bot(TelegramObject):
:class:`telegram.TelegramError`
"""
url = '{0]/answerShippingQuery'.format(self.base_url)
if ok is True and (shipping_options is None or error_message is not None):
raise TelegramError(
'answerShippingQuery: If ok is True, shipping_options '
'should not be empty and there should not be error_message')
if ok is False and (shipping_options is not None or error_message is None):
raise TelegramError(
'answerShippingQuery: If ok is False, error_message '
'should not be empty and there should not be shipping_options')
url_ = '{0}/answerShippingQuery'.format(self.base_url)
data = {'shipping_query_id': shipping_query_id, 'ok': ok}
if shipping_options:
if ok is True:
data['shipping_options'] = shipping_options
if error_message:
data['error_message'] = error_message
return url, data
result = self._request.post(url_, data, timeout=timeout)
def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=None):
return result
def answer_pre_checkout_query(self, pre_checkout_query_id, ok,
error_message=None, timeout=None, **kwargs):
"""
If you sent an invoice requesting a shipping address and the parameter is_flexible was
specified, the Bot API will send an Update with a shipping_query field to the bot.
@ -1977,6 +1994,7 @@ class Bot(TelegramObject):
just bought the last of our amazing black T-shirts while you were busy filling out
your payment details. Please choose a different color or garment!"). Telegram will
display this message to the user.
**kwargs (dict): Arbitrary keyword arguments.
Returns:
bool: On success, `True` is returned.
@ -1985,14 +2003,23 @@ class Bot(TelegramObject):
:class:`telegram.TelegramError`
"""
url = '{0]/answerPreCheckoutQuery'.format(self.base_url)
if (ok is not True and error_message is None) or (ok is True and error_message is not None):
raise TelegramError(
'answerPreCheckoutQuery: If ok is True, there should '
'not be error_message; if ok is False, error_message '
'should not be empty')
url_ = '{0}/answerPreCheckoutQuery'.format(self.base_url)
data = {'pre_checkout_query_id': pre_checkout_query_id, 'ok': ok}
if error_message:
data['error_message'] = error_message
return url, data
result = self._request.post(url_, data, timeout=timeout)
return result
@staticmethod
def de_json(data, bot):

View file

@ -238,6 +238,13 @@ class Filters(object):
game = _Game()
class _SuccessfulPayment(BaseFilter):
def filter(self, message):
return bool(message.successful_payment)
successful_payment = _SuccessfulPayment()
class entity(BaseFilter):
"""Filters messages to only allow those which have a :class:`telegram.MessageEntity`
where their `type` matches `entity_type`.

View file

@ -47,7 +47,9 @@ class PreCheckoutQuery(TelegramObject):
total_amount,
invoice_payload,
shipping_option_id=None,
order_info=None):
order_info=None,
bot=None,
**kwargs):
self.id = id
self.from_user = from_user
self.currency = currency
@ -56,6 +58,8 @@ class PreCheckoutQuery(TelegramObject):
self.shipping_option_id = shipping_option_id
self.order_info = order_info
self.bot = bot
self._id_attrs = (self.id,)
@staticmethod

View file

@ -35,12 +35,14 @@ class ShippingQuery(TelegramObject):
"""
def __init__(self, id, from_user, invoice_payload, shipping_address):
def __init__(self, id, from_user, invoice_payload, shipping_address, bot=None, **kwargs):
self.id = id
self.from_user = from_user
self.invoice_payload = invoice_payload
self.shipping_address = shipping_address
self.bot = bot
self._id_attrs = (self.id,)
@staticmethod

View file

@ -118,6 +118,12 @@ class FiltersTest(BaseTest, unittest.TestCase):
self.message.game = None
self.assertFalse(Filters.game(self.message))
def test_filters_successful_payment(self):
self.message.successful_payment = 'test'
self.assertTrue(Filters.successful_payment(self.message))
self.message.successful_payment = None
self.assertFalse(Filters.successful_payment(self.message))
def test_filters_status_update(self):
self.assertFalse(Filters.status_update(self.message))