From 2287e6dda55b2d41f67e39a4eb906b9570358ba1 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Mon, 16 May 2016 15:31:37 +0200 Subject: [PATCH] working on epay payment --- shop/urls.py | 12 +--- shop/views.py | 149 +++++++++++++++++++++++++------------------------- 2 files changed, 76 insertions(+), 85 deletions(-) diff --git a/shop/urls.py b/shop/urls.py index 1d3fa374..35c54626 100644 --- a/shop/urls.py +++ b/shop/urls.py @@ -2,16 +2,8 @@ from django.conf.urls import url from views import * urlpatterns = [ - #url( - #r'pay/credit_card/(?P[a-zA-Z0-9\-]+)/$', - #EpayView.as_view(), - #name='epay_form' - #), - #url( - #r'epay_callback/', - #EpayCallbackView, - #name='epay_callback' - #), + url(r'orders/(?P[0-9]+)/pay/creditcard/$', EpayFormView.as_view(), name='epay_form'), + url(r'epay_callback/', EpayCallbackViev.as_view(), name='epay_callback'), url(r'^$', ShopIndexView.as_view(), name='index'), url(r'products/(?P[-_\w+]+)/$', ProductDetailView.as_view(), name='product_detail'), url(r'orders/$', OrderListView.as_view(), name='order_list'), diff --git a/shop/views.py b/shop/views.py index 1c380a38..bfe99a58 100644 --- a/shop/views.py +++ b/shop/views.py @@ -88,6 +88,7 @@ class OrderDetailView(LoginRequiredMixin, DetailView): # Mark the order as closed order.open = None + order.save() reverses = { Order.CREDIT_CARD: reverse_lazy( @@ -224,78 +225,76 @@ class CoinifyRedirectView(TemplateView): return context -# class EpayView(TemplateView): -# template_name = 'tickets/epay_form.html' -# -# def get_context_data(self, **kwargs): -# ticket = Ticket.objects.get(pk=kwargs.get('ticket_id')) -# accept_url = ticket.get_absolute_url() -# amount = ticket.ticket_type.price * 100 -# order_id = str(ticket.pk) -# description = str(ticket.user.pk) -# -# hashstring = ( -# '{merchant_number}{description}11{amount}DKK' -# '{order_id}{accept_url}{md5_secret}' -# ).format( -# merchant_number=settings.EPAY_MERCHANT_NUMBER, -# description=description, -# amount=str(amount), -# order_id=str(order_id), -# accept_url=accept_url, -# md5_secret=settings.EPAY_MD5_SECRET, -# ) -# epay_hash = hashlib.md5(hashstring).hexdigest() -# -# context = super(EpayView, self).get_context_data(**kwargs) -# context['merchant_number'] = settings.EPAY_MERCHANT_NUMBER -# context['description'] = description -# context['order_id'] = order_id -# context['accept_url'] = accept_url -# context['amount'] = amount -# context['epay_hash'] = epay_hash -# return context -# -# -# class EpayCallbackView(View): -# -# def get(self, request, **kwargs): -# -# callback = EpayCallback.objects.create( -# payload=request.GET -# ) -# -# if 'orderid' in request.GET: -# ticket = Ticket.objects.get(pk=request.GET.get('order_id')) -# query = dict( -# map( -# lambda x: tuple(x.split('=')), -# request.META['QUERY_STRING'].split('&') -# ) -# ) -# -# hashstring = ( -# '{merchant_number}{description}11{amount}DKK' -# '{order_id}{accept_url}{md5_secret}' -# ).format( -# merchant_number=query.get('merchantnumber'), -# description=query.get('description'), -# amount=query.get('amount'), -# order_id=query.get('orderid'), -# accept_url=query.get('accepturl'), -# md5_secret=settings.EPAY_MD5_SECRET, -# ) -# epay_hash = hashlib.md5(hashstring).hexdigest() -# -# if not epay_hash == request.GET['hash']: -# return HttpResponse(status=400) -# -# EpayPayment.objects.create( -# ticket=ticket, -# callback=callback, -# txnid=request.GET['txnid'], -# ) -# else: -# return HttpResponse(status=400) -# -# return HttpResponse('OK') +class EpayFormView(TemplateView): + template_name = 'epay_form.html' + + def get_context_data(self, **kwargs): + ticket = Ticket.objects.get(pk=kwargs.get('ticket_id')) + accept_url = ticket.get_absolute_url() + amount = ticket.ticket_type.price * 100 + order_id = str(ticket.pk) + description = str(ticket.user.pk) + + hashstring = ( + '{merchant_number}{description}11{amount}DKK' + '{order_id}{accept_url}{md5_secret}' + ).format( + merchant_number=settings.EPAY_MERCHANT_NUMBER, + description=description, + amount=str(amount), + order_id=str(order_id), + accept_url=accept_url, + md5_secret=settings.EPAY_MD5_SECRET, + ) + epay_hash = hashlib.md5(hashstring).hexdigest() + + context = super(EpayFormView, self).get_context_data(**kwargs) + context['merchant_number'] = settings.EPAY_MERCHANT_NUMBER + context['description'] = description + context['order_id'] = order_id + context['accept_url'] = accept_url + context['amount'] = amount + context['epay_hash'] = epay_hash + return context + + +class EpayCallbackView(View): + def get(self, request, **kwargs): + callback = EpayCallback.objects.create( + payload=request.GET + ) + + if 'orderid' in request.GET: + order = Order.objects.get(pk=request.GET.get('order_id')) + query = dict( + map( + lambda x: tuple(x.split('=')), + request.META['QUERY_STRING'].split('&') + ) + ) + + hashstring = ( + '{merchant_number}{description}11{amount}DKK' + '{order_id}{accept_url}{md5_secret}' + ).format( + merchant_number=query.get('merchantnumber'), + description=query.get('description'), + amount=query.get('amount'), + order_id=query.get('orderid'), + accept_url=query.get('accepturl'), + md5_secret=settings.EPAY_MD5_SECRET, + ) + epay_hash = hashlib.md5(hashstring).hexdigest() + + if not epay_hash == request.GET['hash']: + return HttpResponse(status=400) + + EpayPayment.objects.create( + ticket=ticket, + callback=callback, + txnid=request.GET['txnid'], + ) + else: + return HttpResponse(status=400) + + return HttpResponse('OK')