working on epay payment

This commit is contained in:
Thomas Steen Rasmussen 2016-05-17 07:06:25 +02:00
parent ebec7265df
commit 0e3fe85dfe
4 changed files with 37 additions and 34 deletions

View file

@ -85,9 +85,15 @@ class Order(CreatedUpdatedModel):
) )
)['sum'] )['sum']
def get_absolute_url(self): def get_epay_accept_url(self, request):
return reverse_lazy('shop:order_detail', kwargs={'pk': self.pk}) return 'https://' + request.get_host() + str(reverse_lazy('epay_thanks', kwargs={'pk': self.pk}))
def get_epay_cancel_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('order_detail', kwargs={'pk': self.pk}))
@property
def description(self):
return "BornHack 2016 order #%s" % self.pk
class ProductCategory(CreatedUpdatedModel, UUIDModel): class ProductCategory(CreatedUpdatedModel, UUIDModel):
class Meta: class Meta:

View file

@ -13,6 +13,7 @@
'currency': "DKK", 'currency': "DKK",
'orderid': "{{ order_id }}", 'orderid': "{{ order_id }}",
'accepturl': "{{ accept_url }}", 'accepturl': "{{ accept_url }}",
'cancelurl': "{{ cancel_url }}",
'hash': "{{ epay_hash }}", 'hash': "{{ epay_hash }}",
}); });
</script> </script>

19
shop/utils.py Normal file
View file

@ -0,0 +1,19 @@
import hashlib
from django.conf import settings
def calculate_epay_hash(order, request):
hashstring = (
'{merchant_number}{description}11{amount}DKK'
'{order_id}{accept_url}{cancel_url}{md5_secret}'
).format(
merchant_number=settings.EPAY_MERCHANT_NUMBER,
description=order.description,
amount=order.amount*100,
order_id=order.pk,
accept_url = order.get_epay_accept_url(request)
cancel_url = order.get_epay_cancel_url(request)
md5_secret=settings.EPAY_MD5_SECRET,
)
epay_hash = hashlib.md5(hashstring).hexdigest()
return epay_hash

View file

@ -23,7 +23,7 @@ from shop.models import (
EpayCallback, EpayCallback,
) )
from .forms import AddToOrderForm from .forms import AddToOrderForm
import hashlib from epay.utils import calculate_epay_hash
class EnsureUserOwnsOrderMixin(SingleObjectMixin): class EnsureUserOwnsOrderMixin(SingleObjectMixin):
@ -247,30 +247,19 @@ class EpayFormView(LoginRequiredMixin, EnsureUserOwnsOrderMixin, DetailView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
order = self.get_object() order = self.get_object()
accept_url = 'https://' + self.request.get_host() + str(reverse_lazy('epay_thanks', kwargs={'pk': order.pk})) accept_url = order.get_epay_accept_url(request)
cancel_url = order.get_epay_cancel_url(request)
amount = order.total * 100 amount = order.total * 100
order_id = str(order.pk)
description = "BornHack 2016 order #%s" % order.pk
hashstring = ( epay_hash = calculate_epay_hash(order, request)
'{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=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 = super(EpayFormView, self).get_context_data(**kwargs)
context['merchant_number'] = settings.EPAY_MERCHANT_NUMBER context['merchant_number'] = settings.EPAY_MERCHANT_NUMBER
context['description'] = description context['description'] = order.description
context['order_id'] = order_id
context['accept_url'] = accept_url
context['amount'] = amount context['amount'] = amount
context['order_id'] = order.pk
context['accept_url'] = accept_url
context['cancel_url'] = cancel_url
context['epay_hash'] = epay_hash context['epay_hash'] = epay_hash
return context return context
@ -290,19 +279,7 @@ class EpayCallbackView(View):
) )
order = get_object_or_404(Order, pk=query.get('orderid')) order = get_object_or_404(Order, pk=query.get('orderid'))
hashstring = ( epay_hash = calculate_epay_hash(order, request)
'{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 == query.get('hash'): if not epay_hash == query.get('hash'):
return HttpResponse(status=400) return HttpResponse(status=400)