Projet

Général

Profil

0001-paybox-implement-transaction-validation-27270.patch

Serghei Mihai, 11 octobre 2018 19:28

Télécharger (8,99 ko)

Voir les différences:

Subject: [PATCH] paybox: implement transaction validation (#27270)

 eopayment/paybox.py  |  12 ++++-
 tests/test_paybox.py | 114 ++++++++++++++++++++++++-------------------
 2 files changed, 74 insertions(+), 52 deletions(-)
eopayment/paybox.py
333 333
            result=result,
334 334
            bank_status=bank_status)
335 335

  
336
    def cancel(self, amount, bank_data, **kwargs):
336
    def perform(self, amount, bank_data, operation):
337 337
        logger = logging.getLogger(__name__)
338 338
        url = DIRECT_URLS[self.platform]
339 339
        params = {'VERSION': '00104', # protocol version
340
                  'TYPE': '00005', # cancelling operation code
340
                  'TYPE': operation,
341 341
                  'SITE': force_text(self.site),
342 342
                  'RANG': self.rang.strip(),
343 343
                  'CLE': force_text(self.cle),
......
356 356
        if data.get('CODEREPONSE') != '00000':
357 357
            raise ResponseError(data.get('COMMENTAIRE'))
358 358
        return data
359

  
360
    def validate(self, amount, bank_data, **kwargs):
361
        # code for validation operation is 00002
362
        return self.perform(amount, bank_data, '00002')
363

  
364
    def cancel(self, amount, bank_data, **kwargs):
365
        # code for cancel operation is 00005
366
        return self.perform(amount, bank_data, '00005')
tests/test_paybox.py
136 136
        with self.assertRaisesRegexp(eopayment.ResponseError, 'missing erreur or reference'):
137 137
            backend.response('foo=bar')
138 138

  
139
    def test_cancel_payment(self):
139
    def test_perform_operations(self):
140
        operations = {'validate': '00002', 'cancel': '00005'}
141
        for operation_name, operation_code in operations.items():
142
            params = BACKEND_PARAMS.copy()
143
            params['cle'] = 'cancelling_key'
144
            backend = eopayment.Payment('paybox', params)
145
            bank_data = {'numero_transaction': ['13957441'],
146
                         'numero_appel': ['30310733'],
147
                         'reference': ['830657461681']
148
            }
149
            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="""
150
            backend_expected_response = {"CODEREPONSE": "00000",
151
                                         "RANG": "32",
152
                                         "AUTORISATION": "XXXXXX",
153
                                         "NUMTRANS": "0013989865",
154
                                         "PORTEUR": "",
155
                                         "COMMENTAIRE": "Demande traitée avec succès",
156
                                         "SITE": "1999888",
157
                                         "NUMAPPEL": "0030378572",
158
                                         "REFABONNE": "",
159
                                         "NUMQUESTION": "0013989862"}
160

  
161
            with mock.patch('eopayment.paybox.requests.post') as requests_post:
162
                response = mock.Mock(status_code=200, content=backend_raw_response)
163
                requests_post.return_value = response
164
                backend_response = getattr(backend, operation_name)(Decimal('10'), bank_data)
165
                self.assertEqual(requests_post.call_args[0][0], 'https://preprod-ppps.paybox.com/PPPS.php')
166
                params_sent = requests_post.call_args[0][1]
167
                # make sure the date parameter is present
168
                assert 'DATEQ' in params_sent
169
                # don't care about its value
170
                params_sent.pop('DATEQ')
171
                expected_params = {'CLE': 'cancelling_key',
172
                                   'VERSION': '00104',
173
                                   'TYPE': operation_code,
174
                                   'MONTANT': Decimal('1000'),
175
                                   'NUMAPPEL': '30310733',
176
                                   'NUMTRANS': '13957441',
177
                                   'NUMQUESTION': '0013957441',
178
                                   'REFERENCE': '830657461681',
179
                                   'RANG': backend.backend.rang,
180
                                   'SITE': backend.backend.site,
181
                                   'DEVISE': backend.backend.devise
182
                }
183
                self.assertEqual(params_sent, expected_params)
184
                self.assertEqual(backend_response, backend_expected_response)
185

  
186
                params['platform'] = 'prod'
187
                backend = eopayment.Payment('paybox', params)
188
                with mock.patch('eopayment.paybox.requests.post') as requests_post:
189
                    response = mock.Mock(status_code=200, content=backend_raw_response)
190
                    requests_post.return_value = response
191
                    getattr(backend, operation_name)(Decimal('10'), bank_data)
192
                    self.assertEqual(requests_post.call_args[0][0], 'https://ppps.paybox.com/PPPS.php')
193

  
194
                with mock.patch('eopayment.paybox.requests.post') as requests_post:
195
                    error_response = """CODEREPONSE=00015&COMMENTAIRE=PAYBOX : Transaction non trouvée"""
196
                    response = mock.Mock(status_code=200, content=error_response)
197
                    requests_post.return_value = response
198
                    self.assertRaisesRegexp(eopayment.ResponseError, 'Transaction non trouvée', getattr(backend, operation_name),
199
                                            Decimal('10'), bank_data)
200

  
201

  
202
    def test_validate_payment(self):
140 203
        params = BACKEND_PARAMS.copy()
141 204
        params['cle'] = 'cancelling_key'
142 205
        backend = eopayment.Payment('paybox', params)
......
145 208
                     'reference': ['830657461681']
146 209
        }
147 210
        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="""
148
        backend_expected_response = {"CODEREPONSE": "00000",
149
                    "RANG": "32",
150
                    "AUTORISATION": "XXXXXX",
151
                    "NUMTRANS": "0013989865",
152
                    "PORTEUR": "",
153
                    "COMMENTAIRE": "Demande traitée avec succès",
154
                    "SITE": "1999888",
155
                    "NUMAPPEL": "0030378572",
156
                    "REFABONNE": "",
157
                    "NUMQUESTION": "0013989862"}
158

  
159
        with mock.patch('eopayment.paybox.requests.post') as requests_post:
160
            response = mock.Mock(status_code=200, content=backend_raw_response)
161
            requests_post.return_value = response
162
            backend_response = backend.cancel(Decimal('10'), bank_data)
163
            self.assertEqual(requests_post.call_args[0][0], 'https://preprod-ppps.paybox.com/PPPS.php')
164
            params_sent = requests_post.call_args[0][1]
165
            # make sure the date parameter is present
166
            assert 'DATEQ' in params_sent
167
            # don't care about its value
168
            params_sent.pop('DATEQ')
169
            expected_params = {'CLE': 'cancelling_key',
170
                'VERSION': '00104',
171
                'TYPE': '00005',
172
                'MONTANT': Decimal('1000'),
173
                'NUMAPPEL': '30310733',
174
                'NUMTRANS': '13957441',
175
                'NUMQUESTION': '0013957441',
176
                'REFERENCE': '830657461681',
177
                'RANG': backend.backend.rang,
178
                'SITE': backend.backend.site,
179
                'DEVISE': backend.backend.devise
180
            }
181
            self.assertEqual(params_sent, expected_params)
182
            self.assertEqual(backend_response, backend_expected_response)
183

  
184
        params['platform'] = 'prod'
185
        backend = eopayment.Payment('paybox', params)
186
        with mock.patch('eopayment.paybox.requests.post') as requests_post:
187
            response = mock.Mock(status_code=200, content=backend_raw_response)
188
            requests_post.return_value = response
189
            backend.cancel(Decimal('10'), bank_data)
190
            self.assertEqual(requests_post.call_args[0][0], 'https://ppps.paybox.com/PPPS.php')
191

  
192
        with mock.patch('eopayment.paybox.requests.post') as requests_post:
193
            error_response = """CODEREPONSE=00015&COMMENTAIRE=PAYBOX : Transaction non trouvée"""
194
            response = mock.Mock(status_code=200, content=error_response)
195
            requests_post.return_value = response
196
            self.assertRaisesRegexp(eopayment.ResponseError, 'Transaction non trouvée', backend.cancel, Decimal('10'), bank_data)
197 211

  
198 212

  
199 213
    def test_rsa_signature_validation(self):
200
-