diff --git a/example_project/views/netaxept.py b/example_project/views/netaxept.py index 073336d..04d878c 100644 --- a/example_project/views/netaxept.py +++ b/example_project/views/netaxept.py @@ -53,7 +53,9 @@ def after_terminal(request): response_code = request.GET['responseCode'] logger.info('netaxept-after-terminal', transaction_id=transaction_id, response_code=response_code) - if response_code == 'OK': + if response_code == 'Cancel': + return HttpResponse('Payment cancelled') + elif response_code == 'OK': payment = Payment.objects.get(token=transaction_id) try: # This will verify if the payment was indeed authorized. @@ -63,10 +65,13 @@ def after_terminal(request): return HttpResponse('Error authorizing {}: {}'.format(payment.id, exc)) else: return redirect('view_payment', payment_id=payment.id) - elif response_code == 'Cancel': - return HttpResponse('Payment cancelled') - else: - return HttpResponse('Payment error {}'.format(response_code)) + else: # The error case + payment = Payment.objects.get(token=transaction_id) + try: + # This will query the state of the payment in netaxept, and create a transaction object with all details + gateway_authorize(payment=payment, payment_token=payment.token) + finally: + return HttpResponse('Payment error {}'.format(response_code)) def query(request: HttpRequest, transaction_id: str) -> HttpResponse: diff --git a/payment/gateways/netaxept/netaxept_protocol.py b/payment/gateways/netaxept/netaxept_protocol.py index 89f8196..4e4e918 100644 --- a/payment/gateways/netaxept/netaxept_protocol.py +++ b/payment/gateways/netaxept/netaxept_protocol.py @@ -191,7 +191,7 @@ def query(config: NetaxeptConfig, transaction_id: str) -> QueryResponse: summary = d['PaymentInfo']['Summary'] annulled = summary['Annulled'] == 'true' authorized = summary['Authorized'] == 'true' - authorization_id = summary['AuthorizationId'] + authorization_id = summary.get('AuthorizationId') # AuthorizationId may be absent from the response return QueryResponse( annulled=annulled, authorized=authorized, diff --git a/setup.py b/setup.py index d71e12e..ec2f5ca 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='django-payment', - version='1.2', + version='1.4', description='', long_description='', author='Nicholas Wolff', diff --git a/tests/gateways/bob.xml b/tests/gateways/bob.xml new file mode 100644 index 0000000..a81055f --- /dev/null +++ b/tests/gateways/bob.xml @@ -0,0 +1,144 @@ + + 12002913 + 2019-11-03T09:35:00.1029756+01:00 + + 2778ffe955754b3ca217697982bb5272 + + + + 1000 + NOK + 32 + + + 0 + + 0 + 1000 + + 2019-11-03T09:34:21.273 + + + + + + + 2019-11-03T09:34:26.210 + + 2019-11-03T09:34:40.227 + + Chrome-Mozilla/5.0 (X11; Linux x86_64) + AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36 + + + + + + + nwolff@gmail.com + 85.218.56.162 + + + + + + + + + + + + + + + + + + + + 0 + 0 + + false + + false + false + + + + + + Visa + NO + + 492500******0087 + + Visa + 2002 + + 3 + + + + + + + + + 2019-11-03T09:34:21.273 + + + Register + + + + + + + + + + + + 2019-11-03T09:34:40.18 + Auth + + 99 + + Netaxept + Auth Reg Comp + Failure) + + + + + + + + + + + + 2019-11-03T09:34:40.18 + Auth + + 99 + + Netaxept + Auth Reg Comp + Failure) + + + + + + + CH + + false + + + + + + \ No newline at end of file diff --git a/tests/gateways/test_netaxept.py b/tests/gateways/test_netaxept.py index 9e2a158..e00a0a9 100644 --- a/tests/gateways/test_netaxept.py +++ b/tests/gateways/test_netaxept.py @@ -265,6 +265,101 @@ def it_should_query(requests_post): raw_response=asdict(mock_response)) +@patch('requests.post') +def it_should_handle_query_response_without_authorization_id(requests_post): + mock_response = MockResponse( + status_code=200, + url='https://test.epayment.nets.eu/Netaxept/Query.aspx', + encoding='ISO-8859-1', + reason='OK', + text=""" + + 11111111 + 2019-10-14T10:15:07.2677951+02:00 + 1111111111114cf693a1cf86123e0d8f + + 700 + NOK + 7 + + 0 + 0 + 700 + 2019-09-11T16:30:06.967 + + + 2019-09-11T16:30:08.513 + 2019-09-11T16:30:24.903 + Chrome-Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36 + + + + 85.218.56.162 + + + + + + + + + + + + + + + 700 + 0 + false + false + true + + + Visa + NO + 492500******0004 + Visa + 2301 + 3 + + + + 2019-09-11T16:30:06.967 + Register + + + 2019-09-11T16:30:24.81 + 127.0.0.1: Auto AUTH + Auth + 672 + + + + + 2019-11-03T09:34:40.18 + Auth + 99 + Netaxept + Auth Reg Comp Failure) + + + + + + CH + false + + """) + requests_post.return_value = mock_response + query_response = query(config=_netaxept_config, transaction_id='233abb21f18b47dc98469fb9000b1f21') + assert query_response == QueryResponse( + annulled=False, + authorized=True, + authorization_id=None, + raw_response=asdict(mock_response)) + + ############################################################################## # SPI tests