Skip to content

Commit 1210e4e

Browse files
authored
Finalize Payment API
bugfixes added payment-related handlers
2 parents a78f72c + 05ed693 commit 1210e4e

File tree

6 files changed

+56
-10
lines changed

6 files changed

+56
-10
lines changed

AUTHORS.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The following wonderful people contributed directly or indirectly to this projec
2929
- `Hugo Damer <https://github.com/HakimusGIT>`_
3030
- `Jacob Bom <https://github.com/bomjacob>`_
3131
- `JASON0916 <https://github.com/JASON0916>`_
32-
- `jeffffc <https://github.com/jeffffc`_
32+
- `jeffffc <https://github.com/jeffffc>`_
3333
- `jh0ker <https://github.com/jh0ker>`_
3434
- `John Yong <https://github.com/whipermr5>`_
3535
- `jossalgon <https://github.com/jossalgon>`_

telegram/bot.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,9 @@ def answer_shipping_query(self,
19261926
shipping_query_id,
19271927
ok,
19281928
shipping_options=None,
1929-
error_message=None):
1929+
error_message=None,
1930+
timeout=None,
1931+
**kwargs):
19301932
"""
19311933
If you sent an invoice requesting a shipping address and the parameter is_flexible was
19321934
specified, the Bot API will send an Update with a shipping_query field to the bot. Use
@@ -1943,6 +1945,7 @@ def answer_shipping_query(self,
19431945
that explains why it is impossible to complete the order (e.g. "Sorry, delivery
19441946
to your desired address is unavailable'). Telegram will display this message
19451947
to the user.
1948+
**kwargs (dict): Arbitrary keyword arguments.
19461949
19471950
Returns:
19481951
bool: On success, `True` is returned.
@@ -1951,18 +1954,32 @@ def answer_shipping_query(self,
19511954
:class:`telegram.TelegramError`
19521955
19531956
"""
1954-
url = '{0]/answerShippingQuery'.format(self.base_url)
1957+
1958+
if ok is True and (shipping_options is None or error_message is not None):
1959+
raise TelegramError(
1960+
'answerShippingQuery: If ok is True, shipping_options '
1961+
'should not be empty and there should not be error_message')
1962+
1963+
if ok is False and (shipping_options is not None or error_message is None):
1964+
raise TelegramError(
1965+
'answerShippingQuery: If ok is False, error_message '
1966+
'should not be empty and there should not be shipping_options')
1967+
1968+
url_ = '{0}/answerShippingQuery'.format(self.base_url)
19551969

19561970
data = {'shipping_query_id': shipping_query_id, 'ok': ok}
19571971

1958-
if shipping_options:
1972+
if ok is True:
19591973
data['shipping_options'] = shipping_options
19601974
if error_message:
19611975
data['error_message'] = error_message
19621976

1963-
return url, data
1977+
result = self._request.post(url_, data, timeout=timeout)
19641978

1965-
def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=None):
1979+
return result
1980+
1981+
def answer_pre_checkout_query(self, pre_checkout_query_id, ok,
1982+
error_message=None, timeout=None, **kwargs):
19661983
"""
19671984
If you sent an invoice requesting a shipping address and the parameter is_flexible was
19681985
specified, the Bot API will send an Update with a shipping_query field to the bot.
@@ -1977,6 +1994,7 @@ def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=Non
19771994
just bought the last of our amazing black T-shirts while you were busy filling out
19781995
your payment details. Please choose a different color or garment!"). Telegram will
19791996
display this message to the user.
1997+
**kwargs (dict): Arbitrary keyword arguments.
19801998
19811999
Returns:
19822000
bool: On success, `True` is returned.
@@ -1985,14 +2003,23 @@ def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=Non
19852003
:class:`telegram.TelegramError`
19862004
19872005
"""
1988-
url = '{0]/answerPreCheckoutQuery'.format(self.base_url)
2006+
2007+
if (ok is not True and error_message is None) or (ok is True and error_message is not None):
2008+
raise TelegramError(
2009+
'answerPreCheckoutQuery: If ok is True, there should '
2010+
'not be error_message; if ok is False, error_message '
2011+
'should not be empty')
2012+
2013+
url_ = '{0}/answerPreCheckoutQuery'.format(self.base_url)
19892014

19902015
data = {'pre_checkout_query_id': pre_checkout_query_id, 'ok': ok}
19912016

19922017
if error_message:
19932018
data['error_message'] = error_message
19942019

1995-
return url, data
2020+
result = self._request.post(url_, data, timeout=timeout)
2021+
2022+
return result
19962023

19972024
@staticmethod
19982025
def de_json(data, bot):

telegram/ext/filters.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ def filter(self, message):
238238

239239
game = _Game()
240240

241+
class _SuccessfulPayment(BaseFilter):
242+
243+
def filter(self, message):
244+
return bool(message.successful_payment)
245+
246+
successful_payment = _SuccessfulPayment()
247+
241248
class entity(BaseFilter):
242249
"""Filters messages to only allow those which have a :class:`telegram.MessageEntity`
243250
where their `type` matches `entity_type`.

telegram/precheckoutquery.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def __init__(self,
4747
total_amount,
4848
invoice_payload,
4949
shipping_option_id=None,
50-
order_info=None):
50+
order_info=None,
51+
bot=None,
52+
**kwargs):
5153
self.id = id
5254
self.from_user = from_user
5355
self.currency = currency
@@ -56,6 +58,8 @@ def __init__(self,
5658
self.shipping_option_id = shipping_option_id
5759
self.order_info = order_info
5860

61+
self.bot = bot
62+
5963
self._id_attrs = (self.id,)
6064

6165
@staticmethod

telegram/shippingquery.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ class ShippingQuery(TelegramObject):
3535
3636
"""
3737

38-
def __init__(self, id, from_user, invoice_payload, shipping_address):
38+
def __init__(self, id, from_user, invoice_payload, shipping_address, bot=None, **kwargs):
3939
self.id = id
4040
self.from_user = from_user
4141
self.invoice_payload = invoice_payload
4242
self.shipping_address = shipping_address
4343

44+
self.bot = bot
45+
4446
self._id_attrs = (self.id,)
4547

4648
@staticmethod

tests/test_filters.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def test_filters_game(self):
118118
self.message.game = None
119119
self.assertFalse(Filters.game(self.message))
120120

121+
def test_filters_successful_payment(self):
122+
self.message.successful_payment = 'test'
123+
self.assertTrue(Filters.successful_payment(self.message))
124+
self.message.successful_payment = None
125+
self.assertFalse(Filters.successful_payment(self.message))
126+
121127
def test_filters_status_update(self):
122128
self.assertFalse(Filters.status_update(self.message))
123129

0 commit comments

Comments
 (0)