-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,242 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ test.db | |
.idea | ||
*.iml | ||
|
||
# Code | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pytest-django | ||
flake8 | ||
mypy | ||
python-language-server | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Netaxept | ||
|
||
## Configuration | ||
|
||
In the PAYMENT_GATEWAYS setting, configure the netaxept connection params: | ||
|
||
`merchant_id`, `secret`, `base_url`, and `after_terminal_url`. | ||
|
||
The production base_url is: | ||
|
||
`https://epayment.nets.eu/` | ||
|
||
|
||
## Design | ||
|
||
Netaxept works by taking the user to a hosted page and then redirecting the user to the merchant in order to finish | ||
processing the payment. | ||
We chose not to provide such a view in the payment application (we do provide an example view in the example_project), | ||
This means a project that uses netaxept payment will have to implement its own after_terminal view. | ||
|
||
- The first reason is that it's not possible to design a simple, generic response that we can show to users of the | ||
application (because we don't know anything about the application) | ||
- The second reason is that after a successful payment something more than just acknowledging the payment | ||
usually needs to happen in the application (for instance setting the status of an order, sending an email, etc). | ||
|
||
It's not impossible to solve those two problems with configuration, application-provided functions, and signals | ||
but it doesn't seem like all this complexity is worth it, compared to reimplementing a simple, straightforward webhook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<html> | ||
<body> | ||
|
||
<p>Annulled: {{ query_response.annulled }}</p> | ||
|
||
<p>Authorized: {{ query_response.authorized }}</p> | ||
|
||
<p>Status code: {{ query_response.raw_response.status_code }}</p> | ||
|
||
<p>Raw response: | ||
<pre> {{ query_response.raw_response.text }} </pre> | ||
</p> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
|
||
<body> | ||
|
||
<H3>Payments</H3> | ||
|
||
{% for payment in payments %} | ||
<div><a href="{%url 'view_payment' payment.id %}">Payment {{payment.id}} - {{payment.total}} ({{payment.gateway}}) </a></div> | ||
{% endfor %} | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
from django.http import HttpRequest, HttpResponse | ||
from django.shortcuts import get_object_or_404, redirect | ||
from django.shortcuts import get_object_or_404 | ||
from django.template.response import TemplateResponse | ||
from structlog import get_logger | ||
|
||
from payment.models import Payment | ||
from payment.utils import gateway_capture | ||
|
||
logger = get_logger() | ||
|
||
|
||
def view_payment(request: HttpRequest, payment_id: int) -> HttpResponse: | ||
payment = get_object_or_404(Payment, id=payment_id) | ||
return TemplateResponse(request, 'operation_list.html', {'payment': payment}) | ||
def list_payments(request: HttpRequest) -> HttpResponse: | ||
payments = Payment.objects.all().order_by('-id') | ||
return TemplateResponse(request, 'payment_list.html', {'payments': payments}) | ||
|
||
|
||
def capture(request: HttpRequest, payment_id: int) -> HttpResponse: | ||
def view_payment(request: HttpRequest, payment_id: int) -> HttpResponse: | ||
payment = get_object_or_404(Payment, id=payment_id) | ||
capture_result = gateway_capture(payment=payment) | ||
logger.info('capture', payment=payment, capture_result=capture_result) | ||
return redirect('view_payment', payment_id=payment_id) | ||
return TemplateResponse(request, 'view_payment.html', {'payment': payment}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
""" | ||
Example views for interactive testing of payment with netaxept. | ||
""" | ||
|
||
from django.http import HttpRequest | ||
from django.http import HttpResponse | ||
from django.shortcuts import redirect, get_object_or_404 | ||
from django.template.response import TemplateResponse | ||
from django.urls import path | ||
from django.views.decorators.http import require_GET | ||
from structlog import get_logger | ||
|
||
from payment import get_payment_gateway | ||
from payment.gateways.netaxept import actions | ||
from payment.gateways.netaxept import gateway_to_netaxept_config | ||
from payment.gateways.netaxept import netaxept_protocol | ||
from payment.models import Payment | ||
from payment.utils import gateway_authorize | ||
|
||
logger = get_logger() | ||
|
||
|
||
@require_GET | ||
def register_and_goto_terminal(request: HttpRequest, payment_id: int) -> HttpResponse: | ||
""" | ||
Register the payment with netaxept, and take the user to the terminal page for payment authorization. | ||
""" | ||
logger.info('netaxept-register-and-goto-terminal', payment_id=payment_id) | ||
|
||
payment = get_object_or_404(Payment, id=payment_id) | ||
|
||
transaction_id = actions.register_payment(payment) | ||
|
||
payment_gateway, gateway_config = get_payment_gateway(payment.gateway) | ||
netaxept_config = gateway_to_netaxept_config(gateway_config) | ||
return redirect(netaxept_protocol.get_payment_terminal_url(config=netaxept_config, transaction_id=transaction_id)) | ||
|
||
|
||
@require_GET | ||
def after_terminal(request): | ||
""" | ||
The browser gets redirected here when the user finishes interacting with the netaxept terminal pages. | ||
We expect query-string parameters: transactionId and responseCode. | ||
https://shop.nets.eu/web/partners/response-codes | ||
We opened the terminal with AutoAuth set to True, so we know that the payment was not merely verified but rather | ||
authorized (we check the veracity of that fact below) | ||
""" | ||
transaction_id = request.GET['transactionId'] | ||
response_code = request.GET['responseCode'] | ||
logger.info('netaxept-after-terminal', transaction_id=transaction_id, response_code=response_code) | ||
|
||
if response_code == 'OK': | ||
payment = Payment.objects.get(token=transaction_id) | ||
try: | ||
# Here we query netaxept to verify if the payment was indeed authorized: because this endpoint is not | ||
# secured this notification cannot be trusted. | ||
gateway_authorize(payment=payment, payment_token=payment.token) | ||
except Exception as exc: | ||
logger.error('netaxept-after-terminal-error', exc_info=exc) | ||
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)) | ||
|
||
|
||
def query(request: HttpRequest, transaction_id: str) -> HttpResponse: | ||
""" | ||
Retries the status of the given transaction from netaxept. | ||
""" | ||
logger.info('netaxept-query', transaction_id=transaction_id) | ||
payment_gateway, gateway_config = get_payment_gateway('netaxept') | ||
netaxept_config = gateway_to_netaxept_config(gateway_config) | ||
query_response = netaxept_protocol.query(config=netaxept_config, transaction_id=transaction_id) | ||
return TemplateResponse(request, 'netaxept/query_result.html', {'query_response': query_response}) | ||
|
||
|
||
urls = [ | ||
path('register_and_goto_terminal/<payment_id>', register_and_goto_terminal, | ||
name='netaxept_register_and_goto_terminal'), | ||
path('after_terminal', after_terminal, name='netaxept_after_terminal'), | ||
path('query/<transaction_id>', query, name='netaxept_query'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.