Permalink
Browse files

Implement the test mode differently.

Instead of defining a ``testmode`` parameter for several methods, have
one ``TESTMODE`` flag for the utility.

This should make it a lot easier when developing/testing an iDeal
integration using this package.
  • Loading branch information...
1 parent 66e5fc7 commit adeb8167d489cd40abbbf66c2e01e2357efbb2c9 Mark van Lent committed Apr 3, 2012
View
@@ -36,17 +36,27 @@ before you do the payment::
>>> ideal_wrapper.get_banks()
[('0031', 'ABN AMRO'), ...]
+If the ``TESTMODE`` flag is set, you can retrieve a test bank::
-Alternatively you can retrieve a test bank by adding a ``testmode`` flag::
-
- >>> ideal_wrapper.get_banks(testmode=True)
+ >>> ideal_wrapper.TESTMODE = True
+ >>> ideal_wrapper.get_banks()
[('9999', 'TBM Bank')]
+ >>> ideal_wrapper.TESTMODE = False
+ >>> ideal_wrapper.get_banks()
+ [('0031', 'ABN AMRO'), ...]
The result of the call is a list of tuples. Each tuple consists of a
bank ID and name. The name can be used to present to the customer so
he/she can choose which bank to use. The ID is needed in the next
step.
+.. note ::
+
+ If you want to use the test mode from Molllie to test payments,
+ please **also** switch your Mollie account to test mode otherwise
+ Mollie will not accept a test payment, even though the URL might
+ point to the test bank.
+
Request a payment
-----------------
@@ -58,8 +68,8 @@ payment with the API::
... bank_id='9999', amount='123', message='The message',
... report_url='http://example.com/report',
... return_url='http://example.com/return',
- ... profile_key='999999', testmode=False)
- ('123...123, 'http://...')
+ ... profile_key='999999')
+ ('123...123', 'http://...')
The result is a ``transaction_id``, and a URL to send the customer to
to perform the payment.
@@ -72,11 +82,6 @@ to perform the payment.
to use it if you want to use a different payment profile than the
default for the specified account.
- Again, the ``testmode`` flag is optional and defaults to False. Be
- sure to also switch your Mollie account to test mode otherwise
- Mollie will not accept a test payment, even though the URL might
- point to the test bank.
-
Have the customer do the payment
--------------------------------
@@ -160,7 +165,7 @@ And we can request banks, a payment URL and the payment status::
... bank_id='9999', amount='123', message='The message',
... report_url='http://example.com/report',
... return_url='http://example.com/return',
- ... profile_key='999999', testmode=False)
+ ... profile_key='999999')
'http://....'
>>> foo_payment.get_payment_status()
'Success'
View
@@ -18,53 +18,51 @@ class MollieIdeal(object):
API_HOST = 'secure.mollie.nl'
BASE_PATH = '/xml/ideal'
+ TESTMODE = False
- def _do_request(self, data={}, testmode=False):
+ def _do_request(self, data={}):
"""Return XML after performing the actual call to the Mollie API.
The ``data`` parameter is a dictionary with parameters to send
to the Mollie API.
- If ``testmode`` is True, a flag is sent along with the request
- to signal this is a test.
+ If the TESTMODE flag is set to True, the request to Mollie
+ will also contain the 'testmode' parameter (which will be set
+ to 'true') to signal this is a test.
The return value is a string.
"""
connection = httplib.HTTPSConnection(self.API_HOST)
- if testmode:
+ if self.TESTMODE:
data['testmode'] = 'true'
encoded_data = urllib.urlencode(data)
connection.request(
'POST', self.BASE_PATH, encoded_data,
{'Content-type': 'application/x-www-form-urlencoded'})
return connection.getresponse().read()
- def _call_mollie(self, data, testmode=False):
+ def _call_mollie(self, data,):
"""Call the Mollie API and return a dict with the result.
The ``data`` dict should contain all the parameters we will
send to Mollie.
- If ``testmode`` is True, we set the test mode flag.
"""
- result_str = self._do_request(data, testmode)
+ result_str = self._do_request(data)
result_dict = xml_string_to_dict(result_str)
if 'item' in result_dict and \
result_dict['item'].get('type') == 'error':
raise MollieAPIError(result_dict['item']['errorcode'],
result_dict['item']['message'])
return result_dict
- def get_banks(self, testmode=False):
+ def get_banks(self):
"""Return a list of bank id and name tuples.
Example: [('0031, 'ABN AMRO'), ('0721', 'Postbank')]
-
- The ``testmode`` determines whether we get the actual list of
- banks or only the test bank 'The Big Mollie Bank'.
"""
data = {'a': 'banklist'}
- answer = self._call_mollie(data, testmode)
+ answer = self._call_mollie(data)
banks = answer.get('bank', None)
if not banks:
return []
@@ -73,7 +71,7 @@ def get_banks(self, testmode=False):
return [(b['bank_id'], b['bank_name']) for b in banks]
def request_payment(self, partner_id, bank_id, amount, message, report_url,
- return_url, profile_key=None, testmode=False):
+ return_url, profile_key=None):
"""Return transaction ID and URL to visit.
To send the request, a ``partner_id``, the Mollie account number,
@@ -103,15 +101,15 @@ def request_payment(self, partner_id, bank_id, amount, message, report_url,
}
if profile_key:
data['profile_key'] = profile_key
- answer = self._call_mollie(data, testmode=testmode)
+ answer = self._call_mollie(data)
order = answer.get('order')
if order.get('amount') != amount:
raise ValueError('The amount for the payment is incorrect.')
if order.get('currency') != 'EUR':
raise ValueError('The currency for the payment is incorrect.')
return order.get('transaction_id'), order.get('URL')
- def check_payment(self, partner_id, transaction_id, testmode=False):
+ def check_payment(self, partner_id, transaction_id):
"""Check the status of the payment and return a dict with infomation.
With a Mollie account number, ``partner_id``, and the ID of a
@@ -137,7 +135,7 @@ def check_payment(self, partner_id, transaction_id, testmode=False):
'partnerid': partner_id,
'transaction_id': transaction_id,
}
- answer = self._call_mollie(data, testmode=testmode)
+ answer = self._call_mollie(data)
order = answer['order']
if order.get('payed') == 'true':
order['payed'] = True
@@ -4,17 +4,14 @@
class IMollieIdeal(Interface):
"""A utility that wraps the Mollie iDeal API."""
- def get_banks(testmode=False):
+ def get_banks():
"""Return a list of bank id and name tuples.
E.g.: [('0031, 'ABN AMRO'), ('0721', 'Postbank')]
-
- @testmode determines whether we get the actual list of banks
- or only the test bank 'The Big Mollie Bank'.
"""
def request_payment(partner_id, bank_id, amount, message, report_url,
- return_url, profile_key=None, testmode=False):
+ return_url, profile_key=None):
"""Return transaction ID and URL to visit.
To send the request, a ``partner_id``, the Mollie account number,
@@ -34,7 +31,7 @@ def request_payment(partner_id, bank_id, amount, message, report_url,
profile than the default profile for the ``partnerid``.
"""
- def check_payment(partner_id, transaction_id, testmode=False):
+ def check_payment(partner_id, transaction_id):
"""Check the status of the payment and return a dict with infomation.
With a Mollie account number, ``partner_id``, and the ID of a
@@ -73,9 +70,6 @@ def get_banks():
"""Return a list of bank id and name tuples.
E.g.: [('0031, 'ABN AMRO'), ('0721', 'Postbank')]
-
- @testmode determines whether we get the actual list of banks
- or only the test bank 'The Big Mollie Bank'.
"""
def get_payment_url(partner_id, bank_id, amount, message,
@@ -12,8 +12,9 @@ class TestIdealWrapper(unittest.TestCase):
def setUp(self):
self.ideal = getUtility(IMollieIdeal)
+ self.ideal.TESTMODE = True
def test_connection(self):
"""Check the Mollie connection by retrieving list of banks."""
- banks = self.ideal.get_banks(testmode=True)
+ banks = self.ideal.get_banks()
self.assertTrue(('9999', 'TBM Bank') in banks)
@@ -55,7 +55,7 @@ def side_effect(*args, **kwargs):
self.ideal._do_request = MagicMock(
side_effect=side_effect)
self.ideal.get_banks()
- self.ideal._do_request.assert_called_with({'a': 'banklist'}, False)
+ self.ideal._do_request.assert_called_with({'a': 'banklist'})
def test_banklist(self):
"""Check the list of banks."""
@@ -79,9 +79,7 @@ def side_effect(*args, **kwargs):
{'a': 'fetch', 'partnerid': self.partner_id, 'amount': self.amount,
'bank_id': self.bank_id, 'description': self.message,
'reporturl': self.report_url, 'returnurl': self.return_url
- },
- False
- )
+ })
def test_basic_payment_request(self):
"""Check basic (successfull) payment request."""
@@ -144,9 +142,7 @@ def side_effect(*args, **kwargs):
{'a': 'check',
'partnerid': self.partner_id,
'transaction_id': self.transaction_id,
- },
- False
- )
+ })
def test_check_payment_success(self):
"""Check the best case: a successfull payment."""

0 comments on commit adeb816

Please sign in to comment.