From cdee589e4167621322c89f09d6a186e96860374a Mon Sep 17 00:00:00 2001 From: ArturBaybulatov Date: Thu, 18 Aug 2016 17:20:14 +0300 Subject: [PATCH] Yandex Money into Wallets integration --- wallets/migrations/0006_transaction.py | 31 ++++++++++++++ wallets/models.py | 26 ++++++++++++ wallets/templates/score-detail.html | 39 +++++++++++++++++- .../tmp_yandex_money_request_example.html | 3 +- ...tmp_yandex_money_responses_sumulation.html | 2 +- wallets/urls.py | 5 ++- wallets/views.py | 40 +++++++++++++++++-- 7 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 wallets/migrations/0006_transaction.py diff --git a/wallets/migrations/0006_transaction.py b/wallets/migrations/0006_transaction.py new file mode 100644 index 0000000..50d8e47 --- /dev/null +++ b/wallets/migrations/0006_transaction.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-08-18 14:18 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('wallets', '0005_auto_20160809_1727'), + ] + + operations = [ + migrations.CreateModel( + name='Transaction', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('complete', models.BooleanField(default=False, editable=False)), + ('created_at', models.DateTimeField(default=django.utils.timezone.now, editable=False)), + ('voids_at', models.DateTimeField()), + ('sum', models.DecimalField(decimal_places=0, max_digits=20)), + ('type', models.CharField(choices=[('add', 'Пополнение'), ('reservation', 'Резервирование')], max_length=20)), + ('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='customer_transactions', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/wallets/models.py b/wallets/models.py index 06b01c7..8dd688d 100644 --- a/wallets/models.py +++ b/wallets/models.py @@ -48,3 +48,29 @@ class WithDraw(models.Model): verbose_name = 'Заявка на вывод средств' verbose_name_plural = 'Заявки на вывод средств' ordering = ('-created',) + + +class Transaction(models.Model): + TYPES = ( + ('add', 'Пополнение'), + ('reservation', 'Резервирование'), + ) + + complete = models.BooleanField(default=False, editable=False) # Not editable in admin + created_at = models.DateTimeField(default=timezone.now, editable=False) + customer = models.ForeignKey(User, related_name='customer_transactions') + voids_at = models.DateTimeField() + sum = models.DecimalField(max_digits=20, decimal_places=0) + type = models.CharField(max_length=20, choices=TYPES) + + def save(self, *args, **kwargs): + if not self.pk and self.created_at: + self.voids_at = self.created_at + timezone.timedelta(minutes=9000) + + super().save(*args, **kwargs) + + def is_void(self): + return timezone.now() > self.voids_at + + def __str__(self): + return str(self.pk) diff --git a/wallets/templates/score-detail.html b/wallets/templates/score-detail.html index 8e679fe..6eb9a50 100644 --- a/wallets/templates/score-detail.html +++ b/wallets/templates/score-detail.html @@ -18,7 +18,7 @@ {% if user_score.is_customer %}
- пополнить + пополнить
{% endif %} @@ -62,6 +62,43 @@ + + diff --git a/wallets/templates/tmp_yandex_money_request_example.html b/wallets/templates/tmp_yandex_money_request_example.html index 37182a5..0dbfe09 100755 --- a/wallets/templates/tmp_yandex_money_request_example.html +++ b/wallets/templates/tmp_yandex_money_request_example.html @@ -9,9 +9,10 @@

scid:

sum:

customerNumber:

-

paymentType:

+

transactionId:

+

diff --git a/wallets/templates/tmp_yandex_money_responses_sumulation.html b/wallets/templates/tmp_yandex_money_responses_sumulation.html index 16b200b..2db2eb7 100755 --- a/wallets/templates/tmp_yandex_money_responses_sumulation.html +++ b/wallets/templates/tmp_yandex_money_responses_sumulation.html @@ -44,7 +44,7 @@

orderSumBankPaycash:

shopPassword:

-

orderNumber:

+

transactionId:

diff --git a/wallets/urls.py b/wallets/urls.py index 3f71a17..082abeb 100755 --- a/wallets/urls.py +++ b/wallets/urls.py @@ -1,13 +1,14 @@ from django.conf import urls, settings from django.views.generic import TemplateView -from .views import ScoreDetailView, WithDrawCreate +from .views import ScoreDetailView, WithDrawCreate, ScoreView app_name = 'wallets' urlpatterns = [ - urls.url(r'^score/(?P\d+)/$', ScoreDetailView.as_view(), name='score-detail'), + # urls.url(r'^score/(?P\d+)/$', ScoreDetailView.as_view(), name='score-detail'), + urls.url(r'^score/(?P\d+)/$', ScoreView.as_view(), name='score-detail'), urls.url(r'^withdraw/create/$', WithDrawCreate.as_view(), name='withdraw-create'), urls.url( diff --git a/wallets/views.py b/wallets/views.py index 2baa619..c5c7fec 100644 --- a/wallets/views.py +++ b/wallets/views.py @@ -1,6 +1,8 @@ +from django.conf import urls, settings from django.contrib import messages from django.db.models import Sum -from django.http import HttpResponse, JsonResponse +from django.http import HttpResponse, JsonResponse, HttpResponseForbidden +from django.shortcuts import render, get_object_or_404 from django.utils import timezone from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt @@ -9,7 +11,7 @@ from django.views.generic.base import View from pprint import pprint, pformat from .forms import WithDrawForm, TmpCheckOrderForm, TmpPaymentAvisoForm -from .models import InvoiceHistory, WithDraw +from .models import InvoiceHistory, WithDraw, Transaction from users.mixins import CheckForUserMixin from users.models import User @@ -18,7 +20,37 @@ class ScoreDetailView(CheckForUserMixin, DetailView): model = User template_name = 'score-detail.html' context_object_name = 'user_score' + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + user_score_balance = InvoiceHistory.objects.filter(user=self.get_object()).aggregate(Sum('sum')) + context['user_score_balance'] = user_score_balance['sum__sum'] or 0 + context['form'] = WithDrawForm + return context + + +class ScoreView(View): + template_name = 'score-detail.html' + + def dispatch(self, request, *args, **kwargs): + if request.user.is_authenticated() and request.user.is_customer(): + return super().dispatch(request, *args, **kwargs) + else: + return HttpResponseForbidden('403 Forbidden') + + def get(self, request, *args, **kwargs): + + # transaction = Transaction.objects.create(customer=request.user) + user_score = get_object_or_404(User.customer_objects, pk=kwargs.get('pk')) + + return render(request, self.template_name, { + # 'transaction': transaction, + 'YANDEX_MONEY': settings.YANDEX_MONEY, + 'user_score': user_score, + }) + + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) user_score_balance = InvoiceHistory.objects.filter(user=self.get_object()).aggregate(Sum('sum')) @@ -90,7 +122,9 @@ class TmpCheckOrderView(View): def post(self, request, *args, **kwargs): form = self.form_class(request.POST) - if form.is_valid(): + trans = form.cleaned_data.get('transaction') + + if form.is_valid() and not trans.is_void(): res = """