From c099f1c5d74bdd65fd9fe2f5c2050bd8fa6661be Mon Sep 17 00:00:00 2001 From: Skia Date: Tue, 26 Jul 2016 18:28:36 +0200 Subject: [PATCH] Make eboutic working --- core/static/core/style.css | 4 ++ counter/templates/counter/user_account.jinja | 69 ++++++++++++++++++- eboutic/migrations/0003_auto_20160726_1708.py | 29 ++++++++ eboutic/models.py | 9 ++- eboutic/views.py | 15 ++-- requirements.txt | 2 + sith/et_keys/pubkey.pem | 6 ++ 7 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 eboutic/migrations/0003_auto_20160726_1708.py create mode 100644 sith/et_keys/pubkey.pem diff --git a/core/static/core/style.css b/core/static/core/style.css index 456712d9..7ef55ccd 100644 --- a/core/static/core/style.css +++ b/core/static/core/style.css @@ -119,6 +119,10 @@ td { padding: 4px; border: solid 1px black; border-collapse: collapse; + vertical-align: top; +} +td>ul { + margin-top: 0px; } thead { font-weight: bold; diff --git a/counter/templates/counter/user_account.jinja b/counter/templates/counter/user_account.jinja index 294e6bf2..6c671aa8 100644 --- a/counter/templates/counter/user_account.jinja +++ b/counter/templates/counter/user_account.jinja @@ -7,8 +7,73 @@ {% block infos %}

{% trans %}User account{% endtrans %}

{% trans %}Amount: {% endtrans %}{{ customer.amount }} €

-

{{ customer.refillings.all() }}

-

{{ customer.buyings.all() }}

+

{% trans %}Refillings{% endtrans %}

+ + + + + + + + + + {% for i in customer.refillings.all() %} + + + + + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Amount{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.operator }}{{ i.amount }} €
+

{% trans %}Buyings{% endtrans %}

+ + + + + + + + + + + + {% for i in customer.buyings.all() %} + + + + + + + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Product{% endtrans %}{% trans %}Quantity{% endtrans %}{% trans %}Total{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.seller }}{{ i.product }}{{ i.quantity }}{{ i.quantity * i.unit_price }} €
+

{% trans %}Invoices{% endtrans %}

+ + + + + + + + + + {% for i in customer.user.invoices.all() %} + + + + + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Items{% endtrans %}{% trans %}Amount{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }} +
    + {% for it in i.items.all() %} +
  • {{ it.product_name }} - {{ it.product_unit_price }} €
  • + {% endfor %} +
+
{{ i.get_total() }} €
{% endblock %} diff --git a/eboutic/migrations/0003_auto_20160726_1708.py b/eboutic/migrations/0003_auto_20160726_1708.py new file mode 100644 index 00000000..5f7a7d0e --- /dev/null +++ b/eboutic/migrations/0003_auto_20160726_1708.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('eboutic', '0002_auto_20160722_0100'), + ] + + operations = [ + migrations.DeleteModel( + name='Eboutic', + ), + migrations.AddField( + model_name='basketitem', + name='type', + field=models.CharField(default='GUY', verbose_name='product type', max_length=255), + preserve_default=False, + ), + migrations.AddField( + model_name='invoiceitem', + name='type', + field=models.CharField(default='GUY', verbose_name='product type', max_length=255), + preserve_default=False, + ), + ] diff --git a/eboutic/models.py b/eboutic/models.py index 2d83e1eb..dd0eb7d9 100644 --- a/eboutic/models.py +++ b/eboutic/models.py @@ -28,7 +28,6 @@ class Invoice(models.Model): max_length=20, verbose_name=_('payment method')) validated = models.BooleanField(_("validated"), default=False) - def get_total(self): total = 0 for i in self.items.all(): @@ -41,13 +40,17 @@ class Invoice(models.Model): if self.payment_method == "SITH_ACCOUNT": self.user.customer.amount -= self.get_total() self.user.customer.save() + else: + for i in self.items.filter(type="REFILLING").all(): + self.user.customer.amount += i.product_unit_price * i.quantity + self.user.customer.save() + self.validated = True self.save() - - class AbstractBaseItem(models.Model): product_name = models.CharField(_('product name'), max_length=255) + type = models.CharField(_('product type'), max_length=255) product_unit_price = CurrencyField(_('unit price')) quantity = models.IntegerField(_('quantity')) diff --git a/eboutic/views.py b/eboutic/views.py index 9cebc8e9..b70b5956 100644 --- a/eboutic/views.py +++ b/eboutic/views.py @@ -1,5 +1,6 @@ from collections import OrderedDict from datetime import datetime +import pytz import hmac import base64 from OpenSSL import crypto @@ -84,6 +85,8 @@ class EbouticCommand(TemplateView): return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs)) def post(self, request, *args, **kwargs): + if not request.user.is_authenticated(): + return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs)) if 'basket' not in request.session.keys(): return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs)) if self.make_basket(request): @@ -102,7 +105,8 @@ class EbouticCommand(TemplateView): request.session.modified = True b.items.all().delete() for pid,infos in request.session['basket'].items(): - BasketItem(basket=b, product_name=Product.objects.filter(id=int(pid)).first().name, + p = Product.objects.filter(id=int(pid)).first() + BasketItem(basket=b, product_name=p.name, type=p.product_type.name, quantity=infos['qty'], product_unit_price=infos['price']/100).save() self.basket = b return True @@ -115,10 +119,12 @@ class EbouticCommand(TemplateView): kwargs['et_request']['PBX_IDENTIFIANT'] = settings.SITH_EBOUTIC_PBX_IDENTIFIANT kwargs['et_request']['PBX_TOTAL'] = int(self.basket.get_total()*100) kwargs['et_request']['PBX_DEVISE'] = 978 # This is Euro. ET support only this value anyway - kwargs['et_request']['PBX_CMD'] = "CMD_"+str(self.basket.id) + kwargs['et_request']['PBX_CMD'] = self.basket.id kwargs['et_request']['PBX_PORTEUR'] = self.basket.user.email kwargs['et_request']['PBX_RETOUR'] = "Amount:M;BasketID:R;Auto:A;Error:E;Sig:K" kwargs['et_request']['PBX_HASH'] = "SHA512" + kwargs['et_request']['PBX_TYPEPAIEMENT'] = "CARTE" + kwargs['et_request']['PBX_TYPECARTE'] = "CB" kwargs['et_request']['PBX_TIME'] = str(datetime.now().replace(microsecond=0).isoformat('T')) kwargs['et_request']['PBX_HMAC'] = hmac.new(settings.SITH_EBOUTIC_HMAC_KEY, bytes("&".join(["%s=%s"%(k,v) for k,v in kwargs['et_request'].items()]), 'utf-8'), @@ -148,7 +154,7 @@ class EbouticPayWithSith(TemplateView): i.payment_method = "SITH_ACCOUNT" i.save() for it in b.items.all(): - InvoiceItem(invoice=i, product_name=it.product_name, + InvoiceItem(invoice=i, product_name=it.product_name, type=it.type, product_unit_price=it.product_unit_price, quantity=it.quantity).save() i.validate() kwargs['not_enough'] = False @@ -184,9 +190,10 @@ class EtransactionAutoAnswer(View): i.payment_method = "CREDIT_CARD" i.save() for it in b.items.all(): - InvoiceItem(invoice=i, product_name=it.product_name, + InvoiceItem(invoice=i, product_name=it.product_name, type=it.type, product_unit_price=it.product_unit_price, quantity=it.quantity).save() i.validate() + b.delete() return HttpResponse("Payment validated") else: return HttpResponse("Payment failed with error: "+request.GET['Error']) diff --git a/requirements.txt b/requirements.txt index 59855ecd..9a948359 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ Django >=1.8,<1.9 Pillow mistune django-jinja +pyopenssl +pytz diff --git a/sith/et_keys/pubkey.pem b/sith/et_keys/pubkey.pem new file mode 100644 index 00000000..55dc28b5 --- /dev/null +++ b/sith/et_keys/pubkey.pem @@ -0,0 +1,6 @@ +-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDe+hkicNP7ROHUssGNtHwiT2Ew +HFrSk/qwrcq8v5metRtTTFPE/nmzSkRnTs3GMpi57rBdxBBJW5W9cpNyGUh0jNXc +VrOSClpD5Ri2hER/GcNrxVRP7RlWOqB1C03q4QYmwjHZ+zlM4OUhCCAtSWflB4wC +Ka1g88CjFwRw/PB9kwIDAQAB +-----END PUBLIC KEY-----