-
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
20 changed files
with
525 additions
and
3 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
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 @@ | ||
# Netaxept | ||
|
||
## Configuration | ||
|
||
In the PAYMENT_GATEWAYS setting, configure the netaxept connection params: | ||
|
||
`merchant_id`, `secret`, `wsdl_url`, `terminal_url`, and `after_terminal_url`. | ||
|
||
The production wsdl_url and terminal_url are: | ||
|
||
`https://epayment.nets.eu/netaxept.svc?wsdl` | ||
|
||
`https://epayment.nets.eu/Terminal/default.aspx` | ||
|
||
|
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,18 @@ | ||
<html> | ||
|
||
<head> | ||
<title>Netaxept - Example - {{ title }}</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<h2> {{ title }} </h2> | ||
|
||
<form method="post"> | ||
{% csrf_token %} | ||
{{ form.as_p }} | ||
<input type="submit" value="Submit"/> | ||
</form> | ||
|
||
</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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
Example views for interactive testing of payment with netaxept. | ||
You should restrict access (maybe with 'staff_member_required') if you choose to add this to your urlconf. | ||
""" | ||
from django.http import HttpRequest, HttpResponse | ||
from django.shortcuts import redirect, get_object_or_404 | ||
from django.urls import path | ||
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.netaxept_protocol import get_payment_terminal_url | ||
from payment.models import Payment | ||
|
||
logger = get_logger() | ||
|
||
|
||
def register_and_authorize(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-authorize', payment_id=payment_id) | ||
|
||
transaction_id = actions.register_payment(payment_id) | ||
|
||
payment = get_object_or_404(Payment, id=payment_id) | ||
payment_gateway, gateway_config = get_payment_gateway(payment.gateway) | ||
netaxept_config = gateway_to_netaxept_config(gateway_config) | ||
return redirect(get_payment_terminal_url(config=netaxept_config, transaction_id=transaction_id)) | ||
|
||
|
||
urls = [ | ||
path('register_and_authorize/<payment_id>', register_and_authorize, name='netaxept_register_and_authorize'), | ||
] |
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,78 @@ | ||
import zeep | ||
from structlog import get_logger | ||
|
||
from .netaxept_protocol import NetaxeptConfig, NetaxeptOperation, process | ||
from ... import OperationType | ||
from ...interface import GatewayConfig, GatewayResponse, PaymentData | ||
|
||
logger = get_logger() | ||
|
||
operation_type_to_netaxept_op = { | ||
OperationType.AUTH: NetaxeptOperation.AUTH, | ||
OperationType.CAPTURE: NetaxeptOperation.CAPTURE, | ||
OperationType.VOID: NetaxeptOperation.ANNUL, | ||
OperationType.REFUND: NetaxeptOperation.CREDIT, | ||
} | ||
|
||
|
||
def get_client_token(**_): | ||
""" Not implemented for netaxept gateway. """ | ||
pass | ||
|
||
|
||
def authorize(payment_information: PaymentData, | ||
config: GatewayConfig, | ||
should_capture: bool = False) -> GatewayResponse: | ||
raise NotImplementedError() | ||
|
||
|
||
def process_payment(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse: | ||
raise NotImplementedError() | ||
|
||
|
||
def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse: | ||
return _op(payment_information, config, OperationType.CAPTURE) | ||
|
||
|
||
def refund(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse: | ||
return _op(payment_information, config, OperationType.REFUND) | ||
|
||
|
||
def void(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse: | ||
return _op(payment_information, config, OperationType.VOID) | ||
|
||
|
||
def gateway_to_netaxept_config(gateway_config: GatewayConfig) -> NetaxeptConfig: | ||
return NetaxeptConfig(**gateway_config.connection_params) | ||
|
||
|
||
# XXX: Need to return a GatewayResponse and also handle exceptions | ||
# XXX: logging? | ||
def _op(payment_information: PaymentData, config: GatewayConfig, operation_type: OperationType) -> GatewayResponse: | ||
logger.info('netaxept-op', payment_information=payment_information, operation_type=operation_type) | ||
try: | ||
process_result = process( | ||
config=gateway_to_netaxept_config(config), | ||
transaction_id=payment_information.token, | ||
operation=operation_type_to_netaxept_op[operation_type]) | ||
print(process_result) | ||
return GatewayResponse( | ||
is_success=True, | ||
kind=operation_type.value, | ||
amount=payment_information.amount, | ||
currency=payment_information.currency, | ||
transaction_id=None, | ||
error=None, | ||
raw_response=vars(process_result) | ||
) | ||
except zeep.exceptions.Fault as exception: # XXX | ||
logger.error('netaxept-op-error', exc_info=exception) | ||
return GatewayResponse( | ||
is_success=False, | ||
kind=operation_type.value, | ||
amount=payment_information.amount, | ||
currency=payment_information.currency, | ||
transaction_id=None, | ||
error=str(exception), | ||
raw_response={} | ||
) |
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,83 @@ | ||
from django.db import transaction | ||
from django.shortcuts import get_object_or_404 | ||
from structlog import get_logger | ||
|
||
from payment import get_payment_gateway, TransactionKind | ||
from payment.gateways.netaxept import netaxept_protocol, gateway_to_netaxept_config | ||
from payment.models import Payment, Transaction | ||
|
||
logger = get_logger() | ||
|
||
|
||
class NetaxeptException(Exception): | ||
def __str__(self): | ||
return repr(self.msg) | ||
|
||
|
||
class PaymentAlreadyRegistered(NetaxeptException): | ||
msg = 'Payment already registered' | ||
|
||
|
||
class PaymentNotAuthorized(NetaxeptException): | ||
msg = 'Payment not authorized' | ||
|
||
|
||
class PaymentRegistrationNotCompleted(NetaxeptException): | ||
msg = 'Payment registration not completed' | ||
|
||
|
||
def register_payment(payment_id: int) -> str: | ||
""" | ||
- Registers the payment with netaxept. | ||
- Records a Transaction representing the registration. | ||
- Stores the newly created netaxept transaction_id in the Payment. | ||
:param payment_id: The id of a Payment object. | ||
:return: The newly created netaxept transaction_id | ||
""" | ||
payment = get_object_or_404(Payment, id=payment_id) | ||
|
||
if payment.token != '': | ||
raise PaymentAlreadyRegistered() | ||
|
||
_payment_gateway, gateway_config = get_payment_gateway(payment.gateway) | ||
netaxept_config = gateway_to_netaxept_config(gateway_config) | ||
|
||
transaction_id = netaxept_protocol.register( | ||
config=netaxept_config, | ||
order_number=payment_id, | ||
amount=payment.total, | ||
language='en') | ||
|
||
# XXX: Should also create a transaction object in case of exception (but not update the payment transaction id). | ||
|
||
with transaction.atomic(): | ||
Transaction.objects.create( | ||
payment=payment, | ||
kind=TransactionKind.REGISTER, | ||
token=transaction_id, | ||
is_success=True, | ||
amount=payment.total, | ||
error=None, | ||
gateway_response={}) | ||
|
||
payment.token = transaction_id | ||
payment.save() | ||
|
||
logger.info('netaxept-register-payment', transaction_id=transaction_id) | ||
|
||
return transaction_id | ||
|
||
|
||
def create_auth_transaction(transaction_id: str, success: bool) -> None: | ||
""" Record the outcome of a netaxept auth transaction. """ | ||
payment = Payment.objects.get(token=transaction_id) | ||
|
||
Transaction.objects.create( | ||
payment=payment, | ||
kind=TransactionKind.AUTH, | ||
token=transaction_id, | ||
is_success=success, | ||
amount=payment.total, | ||
error=None, | ||
gateway_response={}) |
Oops, something went wrong.