From 67080adfa88829706f483b70f8b6455d58017b75 Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Thu, 17 Sep 2015 16:20:38 +0200 Subject: [PATCH] transaction handles remote items --- lingo/migrations/0009_auto_20150917_1456.py | 33 +++++++++++++++++ lingo/models.py | 11 +++++- lingo/templates/lingo/combo/item.html | 2 +- lingo/views.py | 41 ++++++++++++++++++--- 4 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 lingo/migrations/0009_auto_20150917_1456.py diff --git a/lingo/migrations/0009_auto_20150917_1456.py b/lingo/migrations/0009_auto_20150917_1456.py new file mode 100644 index 0000000..29f5dd9 --- /dev/null +++ b/lingo/migrations/0009_auto_20150917_1456.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + ('lingo', '0008_auto_20150908_1538'), + ] + + operations = [ + migrations.AddField( + model_name='transaction', + name='regie', + field=models.ForeignKey(to='lingo.Regie', null=True), + preserve_default=True, + ), + migrations.AddField( + model_name='transaction', + name='remote_items', + field=models.CharField(default='', max_length=512), + preserve_default=False, + ), + migrations.AlterField( + model_name='transaction', + name='user', + field=models.ForeignKey(to=settings.AUTH_USER_MODEL, null=True), + preserve_default=True, + ), + ] diff --git a/lingo/models.py b/lingo/models.py index e2d7d1d..615cf6d 100644 --- a/lingo/models.py +++ b/lingo/models.py @@ -133,6 +133,10 @@ class Regie(models.Model): return build_remote_item(item.get('data'), self) return {} + def pay_item(self, request, item): + url = self.webservice_url + '/invoice/%s/pay/' % item + return self.get_url(request, url) + def as_api_dict(self): return {'slug': self.slug, 'label': self.label, @@ -183,14 +187,19 @@ class RemoteItem(object): class Transaction(models.Model): + regie = models.ForeignKey(Regie, null=True) items = models.ManyToManyField(BasketItem, blank=True) + remote_items = models.CharField(max_length=512) start_date = models.DateTimeField(auto_now_add=True) end_date = models.DateTimeField(null=True) bank_data = JSONField(blank=True) order_id = models.CharField(max_length=200) - user = models.ForeignKey(settings.AUTH_USER_MODEL) + user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True) status = models.IntegerField(null=True) + def is_remote(self): + return self.remote_items != '' + def is_paid(self): return self.status == eopayment.PAID diff --git a/lingo/templates/lingo/combo/item.html b/lingo/templates/lingo/combo/item.html index 470409b..b7c79d6 100644 --- a/lingo/templates/lingo/combo/item.html +++ b/lingo/templates/lingo/combo/item.html @@ -32,7 +32,7 @@ {% if item.amount %} {% csrf_token %} - + {% endif %} diff --git a/lingo/views.py b/lingo/views.py index b888fad..767eebb 100644 --- a/lingo/views.py +++ b/lingo/views.py @@ -96,19 +96,41 @@ class AddBasketItemApiView(View): class PayView(View): def post(self, request, *args, **kwargs): - items = BasketItem.objects.filter(id__in=request.POST.getlist('item')) - # XXX: check all items are going to the same regie + regie_id = request.POST.get('regie') + if regie_id: + regie = Regie.objects.get(pk=regie_id) + if regie.is_remote(): + items = [] + remote_items_data = [] + # get all items data from regie webservice + for item in request.POST.getlist('item'): + remote_items_data.append(regie.get_item(request, item)) + remote_items = ','.join([x.id for x in remote_items_data]) + else: + items = BasketItem.objects.filter(id__in=request.POST.getlist('item'), regie=regie) + remote_items = '' + else: + items = BasketItem.objects.filter(id__in=request.POST.getlist('item')) + # XXX: check all items are going to the same regie + regie = items[0].regie + remote_items = '' transaction = Transaction() - transaction.user = request.user + if request.user.is_authenticated(): + transaction.user = request.user + else: + transaction.user = None transaction.save() + transaction.regie = regie transaction.items = items + transaction.remote_items = remote_items transaction.status = 0 transaction.save() - total_amount = sum([x.amount for x in items]) - - regie = items[0].regie + if remote_items: + total_amount = sum([x.amount for x in remote_items_data]) + else: + total_amount = sum([x.amount for x in items]) payment = eopayment.Payment(regie.service, regie.service_options) return_url = request.build_absolute_uri( @@ -148,6 +170,9 @@ class CallbackView(View): transaction.end_date = timezone.now() transaction.save() + # check if transaction belongs to right regie + assert transaction.regie == regie + if payment_response.result != eopayment.PAID: return HttpResponseRedirect('/') @@ -159,6 +184,10 @@ class CallbackView(View): except: # ignore errors, it will be retried later on if it fails pass + + for item in transaction.remote_items.split(','): + regie.pay_item(request, item) + return HttpResponseRedirect('/')