2018-01-12 00:07:25 +00:00
|
|
|
"""Payment
|
|
|
|
|
|
|
|
Here are defined some views dedicated to online payement.
|
|
|
|
"""
|
2018-04-14 13:39:51 +00:00
|
|
|
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
2018-01-12 00:07:25 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
from django.shortcuts import redirect, get_object_or_404
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.utils.datastructures import MultiValueDictKeyError
|
2018-03-31 13:43:46 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-01-12 00:07:25 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseBadRequest
|
|
|
|
|
2018-01-15 00:06:54 +00:00
|
|
|
from preferences.models import AssoOption
|
2018-01-12 00:07:25 +00:00
|
|
|
from .models import Facture
|
|
|
|
from .payment_utils.comnpay import Payment as ComnpayPayment
|
|
|
|
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2018-01-12 00:07:25 +00:00
|
|
|
@csrf_exempt
|
|
|
|
@login_required
|
|
|
|
def accept_payment(request, factureid):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
The view called when an online payment has been accepted.
|
|
|
|
"""
|
2018-06-17 14:31:47 +00:00
|
|
|
invoice = get_object_or_404(Facture, id=factureid)
|
2018-01-12 00:07:25 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
2018-03-31 13:43:46 +00:00
|
|
|
_("The payment of %(amount)s € has been accepted.") % {
|
2018-06-17 15:44:38 +00:00
|
|
|
'amount': invoice.prix_total()
|
2018-03-31 13:43:46 +00:00
|
|
|
}
|
2018-01-12 00:07:25 +00:00
|
|
|
)
|
2018-06-17 14:31:47 +00:00
|
|
|
# In case a cotisation was bought, inform the user, the
|
|
|
|
# cotisation time has been extended too
|
|
|
|
if any(purchase.type_cotisation for purchase in invoice.vente_set.all()):
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The cotisation of %(member_name)s has been \
|
|
|
|
extended to %(end_date)s.") % {
|
|
|
|
'member_name': request.user.pseudo,
|
|
|
|
'end_date': request.user.end_adhesion()
|
|
|
|
}
|
|
|
|
)
|
2018-04-13 18:58:29 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': request.user.id}
|
|
|
|
))
|
2018-01-12 00:07:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
@login_required
|
|
|
|
def refuse_payment(request):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
The view called when an online payment has been refused.
|
|
|
|
"""
|
2018-01-12 00:07:25 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
2018-03-31 13:43:46 +00:00
|
|
|
_("The payment has been refused.")
|
2018-01-12 00:07:25 +00:00
|
|
|
)
|
2018-04-13 18:58:29 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': request.user.id}
|
|
|
|
))
|
|
|
|
|
2018-01-12 00:07:25 +00:00
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def ipn(request):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
The view called by Comnpay server to validate the transaction.
|
|
|
|
Verify that we can firmly save the user's action and notify
|
|
|
|
Comnpay with 400 response if not or with a 200 response if yes
|
|
|
|
"""
|
2018-01-12 00:07:25 +00:00
|
|
|
p = ComnpayPayment()
|
|
|
|
order = ('idTpe', 'idTransaction', 'montant', 'result', 'sec', )
|
|
|
|
try:
|
|
|
|
data = OrderedDict([(f, request.POST[f]) for f in order])
|
|
|
|
except MultiValueDictKeyError:
|
|
|
|
return HttpResponseBadRequest("HTTP/1.1 400 Bad Request")
|
|
|
|
|
2018-01-31 02:27:59 +00:00
|
|
|
if not p.validSec(data, AssoOption.get_cached_value('payment_pass')):
|
2018-01-12 00:07:25 +00:00
|
|
|
return HttpResponseBadRequest("HTTP/1.1 400 Bad Request")
|
|
|
|
|
|
|
|
result = True if (request.POST['result'] == 'OK') else False
|
|
|
|
idTpe = request.POST['idTpe']
|
|
|
|
idTransaction = request.POST['idTransaction']
|
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
# Checking that the payment is actually for us.
|
2018-01-31 02:27:59 +00:00
|
|
|
if not idTpe == AssoOption.get_cached_value('payment_id'):
|
2018-01-12 00:07:25 +00:00
|
|
|
return HttpResponseBadRequest("HTTP/1.1 400 Bad Request")
|
|
|
|
|
|
|
|
try:
|
|
|
|
factureid = int(idTransaction)
|
|
|
|
except ValueError:
|
|
|
|
return HttpResponseBadRequest("HTTP/1.1 400 Bad Request")
|
|
|
|
|
|
|
|
facture = get_object_or_404(Facture, id=factureid)
|
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
# Checking that the payment is valid
|
2018-01-12 00:07:25 +00:00
|
|
|
if not result:
|
2018-04-09 17:40:46 +00:00
|
|
|
# Payment failed: Cancelling the invoice operation
|
2018-01-12 00:07:25 +00:00
|
|
|
facture.delete()
|
2018-04-09 17:40:46 +00:00
|
|
|
# And send the response to Comnpay indicating we have well
|
|
|
|
# received the failure information.
|
2018-01-12 00:07:25 +00:00
|
|
|
return HttpResponse("HTTP/1.1 200 OK")
|
|
|
|
|
|
|
|
facture.valid = True
|
|
|
|
facture.save()
|
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
# Everything worked we send a reponse to Comnpay indicating that
|
|
|
|
# it's ok for them to proceed
|
2018-01-12 00:07:25 +00:00
|
|
|
return HttpResponse("HTTP/1.0 200 OK")
|
|
|
|
|
|
|
|
|
2018-01-14 19:15:21 +00:00
|
|
|
def comnpay(facture, request):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Build a request to start the negociation with Comnpay by using
|
|
|
|
a facture id, the price and the secret transaction data stored in
|
|
|
|
the preferences.
|
|
|
|
"""
|
2018-01-14 19:15:21 +00:00
|
|
|
host = request.get_host()
|
2018-01-12 00:07:25 +00:00
|
|
|
p = ComnpayPayment(
|
2018-01-31 02:27:59 +00:00
|
|
|
str(AssoOption.get_cached_value('payment_id')),
|
|
|
|
str(AssoOption.get_cached_value('payment_pass')),
|
2018-01-15 00:06:54 +00:00
|
|
|
'https://' + host + reverse(
|
|
|
|
'cotisations:accept_payment',
|
2018-04-13 18:58:29 +00:00
|
|
|
kwargs={'factureid': facture.id}
|
2018-01-15 00:06:54 +00:00
|
|
|
),
|
2018-01-12 00:07:25 +00:00
|
|
|
'https://' + host + reverse('cotisations:refuse_payment'),
|
|
|
|
'https://' + host + reverse('cotisations:ipn'),
|
|
|
|
"",
|
|
|
|
"D"
|
|
|
|
)
|
|
|
|
r = {
|
2018-04-13 18:58:29 +00:00
|
|
|
'action': 'https://secure.homologation.comnpay.com',
|
|
|
|
'method': 'POST',
|
|
|
|
'content': p.buildSecretHTML(
|
2018-06-17 15:38:01 +00:00
|
|
|
"Paiement de la facture "+str(facture.id),
|
|
|
|
facture.prix_total(),
|
2018-01-15 00:06:54 +00:00
|
|
|
idTransaction=str(facture.id)
|
|
|
|
),
|
2018-06-17 15:38:01 +00:00
|
|
|
'amount': facture.prix_total(),
|
2018-01-12 00:07:25 +00:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
# The payment systems supported by re2o
|
2018-01-12 00:07:25 +00:00
|
|
|
PAYMENT_SYSTEM = {
|
2018-04-13 18:58:29 +00:00
|
|
|
'COMNPAY': comnpay,
|
|
|
|
'NONE': None
|
2018-01-12 00:07:25 +00:00
|
|
|
}
|