|
| 1 | +""" |
| 2 | +Example views for interactive testing of payment with netaxept. |
| 3 | +""" |
| 4 | + |
| 5 | +from django.http import HttpRequest |
| 6 | +from django.http import HttpResponse |
| 7 | +from django.shortcuts import redirect, get_object_or_404 |
| 8 | +from django.template.response import TemplateResponse |
| 9 | +from django.urls import path |
| 10 | +from django.views.decorators.http import require_GET |
| 11 | +from structlog import get_logger |
| 12 | + |
| 13 | +from payment import get_payment_gateway |
| 14 | +from payment.gateways.netaxept import actions |
| 15 | +from payment.gateways.netaxept import gateway_to_netaxept_config |
| 16 | +from payment.gateways.netaxept import netaxept_protocol |
| 17 | +from payment.models import Payment |
| 18 | +from payment.utils import gateway_authorize |
| 19 | + |
| 20 | +logger = get_logger() |
| 21 | + |
| 22 | + |
| 23 | +@require_GET |
| 24 | +def register_and_goto_terminal(request: HttpRequest, payment_id: int) -> HttpResponse: |
| 25 | + """ |
| 26 | + Register the payment with netaxept, and take the user to the terminal page for payment authorization |
| 27 | + """ |
| 28 | + logger.info('netaxept-register-and-goto-terminal', payment_id=payment_id) |
| 29 | + |
| 30 | + transaction_id = actions.register_payment(payment_id) |
| 31 | + |
| 32 | + payment = get_object_or_404(Payment, id=payment_id) |
| 33 | + payment_gateway, gateway_config = get_payment_gateway(payment.gateway) |
| 34 | + netaxept_config = gateway_to_netaxept_config(gateway_config) |
| 35 | + return redirect(netaxept_protocol.get_payment_terminal_url(config=netaxept_config, transaction_id=transaction_id)) |
| 36 | + |
| 37 | + |
| 38 | +@require_GET |
| 39 | +def after_terminal(request): |
| 40 | + """ |
| 41 | + The browser gets redirected here when the user finishes interacting with the netaxept terminal pages. |
| 42 | + We expect query-string parameters: transactionId and responseCode. |
| 43 | +
|
| 44 | + We opened the terminal with AutoAuth set to True, so we know that the payment was not merely verified but rather |
| 45 | + authorized (we check the veracity of that fact below) |
| 46 | + """ |
| 47 | + transaction_id = request.GET['transactionId'] |
| 48 | + response_code = request.GET['responseCode'] |
| 49 | + logger.info('netaxept-after-terminal', transaction_id=transaction_id, response_code=response_code) |
| 50 | + |
| 51 | + payment = Payment.objects.get(token=transaction_id) |
| 52 | + |
| 53 | + try: |
| 54 | + # Here we query netaxept to verify if the payment was indeed authorized: because this endpoint is not secured |
| 55 | + # this notification cannot be trusted. |
| 56 | + gateway_authorize(payment=payment, payment_token=payment.token) |
| 57 | + except Exception as exc: |
| 58 | + logger.error('netaxept-after-terminal-error', exc_info=exc) |
| 59 | + return HttpResponse('Error authorizing {}: {}'.format(payment.id, exc)) |
| 60 | + else: |
| 61 | + return redirect('view_payment', payment_id=payment.id) |
| 62 | + |
| 63 | + |
| 64 | +def query(request: HttpRequest, transaction_id: str) -> HttpResponse: |
| 65 | + """ |
| 66 | + Retries the status of the given transaction from netaxept. |
| 67 | + """ |
| 68 | + logger.info('netaxept-query', transaction_id=transaction_id) |
| 69 | + payment_gateway, gateway_config = get_payment_gateway('netaxept') |
| 70 | + netaxept_config = gateway_to_netaxept_config(gateway_config) |
| 71 | + query_response = netaxept_protocol.query(config=netaxept_config, transaction_id=transaction_id) |
| 72 | + return TemplateResponse(request, 'netaxept/query_result.html', {'query_response': query_response}) |
| 73 | + |
| 74 | + |
| 75 | +urls = [ |
| 76 | + path('register_and_goto_terminal/<payment_id>', register_and_goto_terminal, |
| 77 | + name='netaxept_register_and_goto_terminal'), |
| 78 | + path('after_terminal', after_terminal, name='netaxept_after_terminal'), |
| 79 | + path('query/<transaction_id>', query, name='netaxept_query'), |
| 80 | +] |
0 commit comments