# -*- coding: utf-8 -*- import json import hashlib from django.shortcuts import render, redirect from django.http import Http404, HttpResponseRedirect, HttpResponseForbidden, HttpResponse from django.conf import settings from django.views.decorators.csrf import csrf_exempt from django.utils.http import urlquote from django.contrib.auth.decorators import login_required from django.template.response import TemplateResponse from django.core.urlresolvers import reverse from django.views.decorators.csrf import csrf_protect from yandex_money.models import Payment from ..models import License, LicensePrice from ..consts import PAYFORMS from ..forms import LicenseForm, YaForm from ..utils import raise_if_no_profile @login_required @csrf_protect def yandex_pay(request, payment_id): template_name = 'customer/profile/yandex.html' payment = Payment.objects.get(id=payment_id) if payment.user != request.user: raise form = YaForm(instance=payment) return render(request, template_name, {'form': form, 'ya_url': settings.YANDEX_MONEY_PAYMENT_URL}) @login_required @csrf_protect def order_license(request): """заказ лицензии """ raise_if_no_profile(request) template_name = 'customer/profile/license.html' form = LicenseForm(request.POST or None, initial = {'term': LicensePrice.objects.all()[1], 'payform': 0}) dictionary = { 'form': form, } if form.is_valid(): new_license = License(company=request.user.profile, term=form.cleaned_data['term'].term, payform=form.cleaned_data['payform'], pay_sum=form.cleaned_data['term'].price, ) new_license.save() if form.cleaned_data['payform'] == '1': payment, _ = Payment.objects.get_or_create(order_amount=form.cleaned_data['term'].price, payment_type=Payment.PAYMENT_TYPE.AC, order_number=new_license.id, ) payment.user=request.user # payment.cps_email=request.user.email payment.customer_number=request.user.email # payment.customer_number=request.user.profile.get_company_name().strip() payment.save() return redirect(reverse('yamoney_confirm', kwargs={'payment_id': payment.id})) return redirect(reverse('customer_license_list')) return render(request, template_name, dictionary) @login_required def license_list(request): """Список счетов на лицензии """ raise_if_no_profile(request) template_name = 'customer/profile/license_list.html' licenses = License.objects.filter(company=request.user.profile, deleted=False, status__gt=-1).order_by('-id') dictionary = { 'licenses': licenses, } return render(request, template_name, dictionary) @login_required def paid_list(request): """Оплаченные лицензии """ raise_if_no_profile(request) template_name = 'customer/profile/paid_list.html' licenses = License.objects.filter(company=request.user.profile, status__in=[-1, 1, 2, 3, 4], deleted=False).order_by('-id') dictionary = { 'licenses': licenses, } return render(request, template_name, dictionary) @login_required def delete_license(request, pk): if not request.is_ajax(): return HttpResponseBadRequest() raise_if_no_profile(request) try: license = License.objects.get(pk=pk, status=0, company=request.user.profile) if request.method == 'POST': license.deleted = True license.save() dictionary = {'res': 'Ok', 'id': pk} except: dictionary = {'res': 'fail'} data = json.dumps(dictionary) return HttpResponse(data, mimetype='application/json') @csrf_exempt def payment_result(request): if request.method == 'POST': try: nInvId = request.POST.get('InvId') nOutSum = request.POST.get('OutSum') sSignatureValue = request.POST.get('SignatureValue') SignatureValue = hashlib.md5('%s:%s:%s' % (nOutSum, nInvId, settings.ROBOKASSA_PASSWORD2)).hexdigest() if sSignatureValue.upper() == SignatureValue.upper(): license = License.objects.get(pk=nInvId) license.status = 1 license.save() return HttpResponse('OK%s' % nInvId) else: return HttpResponse('bad sign') except License.DoesNotExist: return HttpResponseForbidden() else: return HttpResponseForbidden() @csrf_exempt def payment_success(request): nInvId = request.GET.get('orderNumber') order = License.objects.get(pk=nInvId) context = {'success': True, 'order_num': nInvId} return TemplateResponse(request, 'customer/profile/end_order.html', context) @csrf_exempt def payment_fail(request): try: nInvId = request.GET.get('') message = u"Возникла проблема. Ваш Заказ не оплачен. Попробуйте оформить заявку снова, или позвоните по номеру." return TemplateResponse(request, 'customer/profile/end_order.html', {'message': message, 'success': False}) except Order.DoesNotExist: return HttpResponseForbidden()