1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
from django.db import transaction
from django.shortcuts import render, get_object_or_404
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
import base64
from postgresqleu.util.decorators import global_login_exempt
from postgresqleu.util.payment.adyen import calculate_signature
from postgresqleu.util.payment.adyen import AdyenBanktransfer
from postgresqleu.invoices.models import Invoice
from postgresqleu.invoices.util import InvoiceManager
from models import RawNotification, AdyenLog, ReturnAuthorizationStatus
from util import process_raw_adyen_notification
@transaction.atomic
def adyen_return_handler(request):
sig = calculate_signature(request.GET)
if sig != request.GET['merchantSig']:
return render(request, 'adyen/sigerror.html')
# We're going to need the invoice for pretty much everything,
# so attempt to find it.
if request.GET['merchantReturnData'] != request.GET['merchantReference'] or not request.GET['merchantReturnData'].startswith(settings.ADYEN_MERCHANTREF_PREFIX):
AdyenLog(pspReference='', message='Return handler received invalid reference %s/%s' % (request.GET['merchantReturnData'], request.GET['merchantReference']), error=True).save()
return render(request, 'adyen/invalidreference.html', {
'reference': "%s//%s" % (request.GET['merchantReturnData'], request.GET['merchantReference']),
})
invoiceid = int(request.GET['merchantReturnData'][len(settings.ADYEN_MERCHANTREF_PREFIX):])
try:
invoice = Invoice.objects.get(pk=invoiceid)
except Invoice.DoesNotExist:
AdyenLog(pspReference='', message='Return handler could not find invoice for reference %s' % request.GET['merchantReturnData'], error=True).save()
return render(request, 'adyen/invalidreference.html', {
'reference': request.GET['merchantReturnData'],
})
manager = InvoiceManager()
if invoice.processor:
processor = manager.get_invoice_processor(invoice)
returnurl = processor.get_return_url(invoice)
else:
if invoice.recipient_user:
returnurl = "%s/invoices/%s/" % (settings.SITEBASE, invoice.pk)
else:
returnurl = "%s/invoices/%s/%s/" % (settings.SITEBASE, invoice.pk, invoice.recipient_secret)
AdyenLog(pspReference='', message='Return handler received %s result for %s' % (request.GET['authResult'], request.GET['merchantReturnData']), error=False).save()
if request.GET['authResult'] == 'REFUSED':
return render(request, 'adyen/refused.html', {
'url': returnurl,
})
elif request.GET['authResult'] == 'CANCELLED':
return HttpResponseRedirect(returnurl)
elif request.GET['authResult'] == 'ERROR':
return render(request, 'adyen/transerror.html', {
'url': returnurl,
})
elif request.GET['authResult'] == 'PENDING':
return render(request, 'adyen/pending.html', {
'url': returnurl,
})
elif request.GET['authResult'] == 'AUTHORISED':
# NOTE! Adyen strongly recommends not reacting on
# authorized values, but deal with them from the
# notifications instead. So we'll do that.
# However, if we reach this point and it's actually
# already dealt with by the notification arriving
# asynchronously, redirect the user properly.
if invoice.paidat:
# Yup, it's paid, so send the user off to the page
# that they came from.
return HttpResponseRedirect(returnurl)
# Show the user a pending message. The refresh time is dependent
# on how many times we've seen this one before.
status, created = ReturnAuthorizationStatus.objects.get_or_create(pspReference=request.GET['pspReference'])
status.seencount += 1
status.save()
return render(request, 'adyen/authorized.html', {
'refresh': 3**status.seencount,
'url': returnurl,
})
else:
return render(request, 'adyen/invalidresult.html', {
'result': request.GET['authResult'],
})
@global_login_exempt
@csrf_exempt
def adyen_notify_handler(request):
# Handle asynchronous notifications from the Adyen payment platform
# Authenticate with HTTP BASIC
if not 'HTTP_AUTHORIZATION' in request.META:
# Sometimes Adyen sends notifications without authorization headers.
# In this case, we request authrorization and they will try again
r = HttpResponse('Unauthorized', status=401)
r['WWW-Authenticate'] = 'Basic realm="postgresqleu adyen"'
return r
auth = request.META['HTTP_AUTHORIZATION'].split()
if len(auth) != 2:
raise Exception('Adyen notification received with invalid length authentication')
if auth[0].lower() != 'basic':
raise Exception('Adyen notification received with invalid authentication type')
user, pwd = base64.b64decode(auth[1]).split(':')
if user != settings.ADYEN_NOTIFY_USER or pwd != settings.ADYEN_NOTIFY_PASSWORD:
return HttpResponseForbidden('Invalid username or password')
# Ok, we have authentication. All our data is now available in
# request.POST
# Store the raw notification at this point, so we have it around in
# case something breaks in a way we couldn't handle
raw = RawNotification(contents=request.body)
raw.save()
if process_raw_adyen_notification(raw, request.POST):
return HttpResponse('[accepted]', content_type='text/plain')
else:
return HttpResponse('[internal error]', content_type='text/plain')
# Rendered views to do bank payment
def _invoice_payment(request, invoice):
method = AdyenBanktransfer()
paymenturl = method.build_adyen_payment_url(invoice.invoicestr, invoice.total_amount, invoice.pk)
return render(request, 'adyen/adyen_bank_payment.html', {
'available': method.available(invoice),
'unavailable_reason': method.unavailable_reason(invoice),
'paymenturl': paymenturl,
})
@login_required
def invoicepayment(request, invoiceid):
invoice = get_object_or_404(Invoice, pk=invoiceid, deleted=False, finalized=True)
if not (request.user.has_module_perms('invoices') or invoice.recipient_user == request.user):
return HttpResponseForbidden("Access denied")
return _invoice_payment(request, invoice)
def invoicepayment_secret(request, invoiceid, secret):
invoice = get_object_or_404(Invoice, pk=invoiceid, deleted=False, finalized=True, recipient_secret=secret)
return _invoice_payment(request, invoice)
|