From 0fce04933a2cd555b53da7798eec6bc451e711c9 Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Thu, 11 Oct 2018 18:57:54 +0200 Subject: [PATCH] paybox: implement transaction validation (#27270) --- eopayment/paybox.py | 12 ++++- tests/test_paybox.py | 114 ++++++++++++++++++++++++------------------- 2 files changed, 74 insertions(+), 52 deletions(-) diff --git a/eopayment/paybox.py b/eopayment/paybox.py index 44de769..937dc06 100644 --- a/eopayment/paybox.py +++ b/eopayment/paybox.py @@ -333,11 +333,11 @@ class Payment(PaymentCommon): result=result, bank_status=bank_status) - def cancel(self, amount, bank_data, **kwargs): + def perform(self, amount, bank_data, operation): logger = logging.getLogger(__name__) url = DIRECT_URLS[self.platform] params = {'VERSION': '00104', # protocol version - 'TYPE': '00005', # cancelling operation code + 'TYPE': operation, 'SITE': force_text(self.site), 'RANG': self.rang.strip(), 'CLE': force_text(self.cle), @@ -356,3 +356,11 @@ class Payment(PaymentCommon): if data.get('CODEREPONSE') != '00000': raise ResponseError(data.get('COMMENTAIRE')) return data + + def validate(self, amount, bank_data, **kwargs): + # code for validation operation is 00002 + return self.perform(amount, bank_data, '00002') + + def cancel(self, amount, bank_data, **kwargs): + # code for cancel operation is 00005 + return self.perform(amount, bank_data, '00005') diff --git a/tests/test_paybox.py b/tests/test_paybox.py index b692d7d..c601e54 100644 --- a/tests/test_paybox.py +++ b/tests/test_paybox.py @@ -136,7 +136,70 @@ class PayboxTests(TestCase): with self.assertRaisesRegexp(eopayment.ResponseError, 'missing erreur or reference'): backend.response('foo=bar') - def test_cancel_payment(self): + def test_perform_operations(self): + operations = {'validate': '00002', 'cancel': '00005'} + for operation_name, operation_code in operations.items(): + params = BACKEND_PARAMS.copy() + params['cle'] = 'cancelling_key' + backend = eopayment.Payment('paybox', params) + bank_data = {'numero_transaction': ['13957441'], + 'numero_appel': ['30310733'], + 'reference': ['830657461681'] + } + backend_raw_response = """NUMTRANS=0013989865&NUMAPPEL=0030378572&NUMQUESTION=0013989862&SITE=1999888&RANG=32&AUTORISATION=XXXXXX&CODEREPONSE=00000&COMMENTAIRE=Demande traitée avec succès&REFABONNE=&PORTEUR=""" + backend_expected_response = {"CODEREPONSE": "00000", + "RANG": "32", + "AUTORISATION": "XXXXXX", + "NUMTRANS": "0013989865", + "PORTEUR": "", + "COMMENTAIRE": "Demande traitée avec succès", + "SITE": "1999888", + "NUMAPPEL": "0030378572", + "REFABONNE": "", + "NUMQUESTION": "0013989862"} + + with mock.patch('eopayment.paybox.requests.post') as requests_post: + response = mock.Mock(status_code=200, content=backend_raw_response) + requests_post.return_value = response + backend_response = getattr(backend, operation_name)(Decimal('10'), bank_data) + self.assertEqual(requests_post.call_args[0][0], 'https://preprod-ppps.paybox.com/PPPS.php') + params_sent = requests_post.call_args[0][1] + # make sure the date parameter is present + assert 'DATEQ' in params_sent + # don't care about its value + params_sent.pop('DATEQ') + expected_params = {'CLE': 'cancelling_key', + 'VERSION': '00104', + 'TYPE': operation_code, + 'MONTANT': Decimal('1000'), + 'NUMAPPEL': '30310733', + 'NUMTRANS': '13957441', + 'NUMQUESTION': '0013957441', + 'REFERENCE': '830657461681', + 'RANG': backend.backend.rang, + 'SITE': backend.backend.site, + 'DEVISE': backend.backend.devise + } + self.assertEqual(params_sent, expected_params) + self.assertEqual(backend_response, backend_expected_response) + + params['platform'] = 'prod' + backend = eopayment.Payment('paybox', params) + with mock.patch('eopayment.paybox.requests.post') as requests_post: + response = mock.Mock(status_code=200, content=backend_raw_response) + requests_post.return_value = response + getattr(backend, operation_name)(Decimal('10'), bank_data) + self.assertEqual(requests_post.call_args[0][0], 'https://ppps.paybox.com/PPPS.php') + + with mock.patch('eopayment.paybox.requests.post') as requests_post: + error_response = """CODEREPONSE=00015&COMMENTAIRE=PAYBOX : Transaction non trouvée""" + response = mock.Mock(status_code=200, content=error_response) + requests_post.return_value = response + self.assertRaisesRegexp(eopayment.ResponseError, 'Transaction non trouvée', getattr(backend, operation_name), + Decimal('10'), bank_data) + + + def test_validate_payment(self): params = BACKEND_PARAMS.copy() params['cle'] = 'cancelling_key' backend = eopayment.Payment('paybox', params) @@ -145,55 +208,6 @@ class PayboxTests(TestCase): 'reference': ['830657461681'] } backend_raw_response = """NUMTRANS=0013989865&NUMAPPEL=0030378572&NUMQUESTION=0013989862&SITE=1999888&RANG=32&AUTORISATION=XXXXXX&CODEREPONSE=00000&COMMENTAIRE=Demande traitée avec succès&REFABONNE=&PORTEUR=""" - backend_expected_response = {"CODEREPONSE": "00000", - "RANG": "32", - "AUTORISATION": "XXXXXX", - "NUMTRANS": "0013989865", - "PORTEUR": "", - "COMMENTAIRE": "Demande traitée avec succès", - "SITE": "1999888", - "NUMAPPEL": "0030378572", - "REFABONNE": "", - "NUMQUESTION": "0013989862"} - - with mock.patch('eopayment.paybox.requests.post') as requests_post: - response = mock.Mock(status_code=200, content=backend_raw_response) - requests_post.return_value = response - backend_response = backend.cancel(Decimal('10'), bank_data) - self.assertEqual(requests_post.call_args[0][0], 'https://preprod-ppps.paybox.com/PPPS.php') - params_sent = requests_post.call_args[0][1] - # make sure the date parameter is present - assert 'DATEQ' in params_sent - # don't care about its value - params_sent.pop('DATEQ') - expected_params = {'CLE': 'cancelling_key', - 'VERSION': '00104', - 'TYPE': '00005', - 'MONTANT': Decimal('1000'), - 'NUMAPPEL': '30310733', - 'NUMTRANS': '13957441', - 'NUMQUESTION': '0013957441', - 'REFERENCE': '830657461681', - 'RANG': backend.backend.rang, - 'SITE': backend.backend.site, - 'DEVISE': backend.backend.devise - } - self.assertEqual(params_sent, expected_params) - self.assertEqual(backend_response, backend_expected_response) - - params['platform'] = 'prod' - backend = eopayment.Payment('paybox', params) - with mock.patch('eopayment.paybox.requests.post') as requests_post: - response = mock.Mock(status_code=200, content=backend_raw_response) - requests_post.return_value = response - backend.cancel(Decimal('10'), bank_data) - self.assertEqual(requests_post.call_args[0][0], 'https://ppps.paybox.com/PPPS.php') - - with mock.patch('eopayment.paybox.requests.post') as requests_post: - error_response = """CODEREPONSE=00015&COMMENTAIRE=PAYBOX : Transaction non trouvée""" - response = mock.Mock(status_code=200, content=error_response) - requests_post.return_value = response - self.assertRaisesRegexp(eopayment.ResponseError, 'Transaction non trouvée', backend.cancel, Decimal('10'), bank_data) def test_rsa_signature_validation(self): -- 2.19.1