You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
5.1 KiB
147 lines
5.1 KiB
# -*- 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 ..models import License, LicensePrice
|
|
from ..consts import PAYFORMS
|
|
from ..forms import LicenseForm
|
|
|
|
|
|
@login_required
|
|
@csrf_protect
|
|
def order_license(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(user=request.user,
|
|
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':
|
|
sMerchantLogin = settings.ROBOKASSA_LOGIN
|
|
nOutSum = form.cleaned_data['term'].price
|
|
nInvId = new_license.id
|
|
sInvDesc = u"Оплата лицензии Документор.ру"
|
|
sign_str = u"%s:%s:%s:%s" % (sMerchantLogin, nOutSum, nInvId, settings.ROBOKASSA_PASSWORD1)
|
|
sSignatureValue = hashlib.md5(sign_str).hexdigest()
|
|
sEmail = request.user.email
|
|
|
|
robokassa = '%sIndex.aspx?MrchLogin=%s&OutSum=%s&InvId=%s&Desc=%s&SignatureValue=%s&Email=%s&Culture=ru&Encoding=utf-8' % (settings.ROBOKASSA_ADDR, sMerchantLogin, nOutSum, nInvId, urlquote(sInvDesc), sSignatureValue, sEmail)
|
|
return redirect(robokassa)
|
|
return redirect(reverse('customer_license_list'))
|
|
|
|
return render(request, template_name, dictionary)
|
|
|
|
|
|
@login_required
|
|
def license_list(request):
|
|
"""Список счетов на лицензии
|
|
"""
|
|
template_name = 'customer/profile/license_list.html'
|
|
licenses = License.objects.filter(user=request.user, deleted=False, status__gt=-1).order_by('-id')
|
|
dictionary = {
|
|
'licenses': licenses,
|
|
}
|
|
return render(request, template_name, dictionary)
|
|
|
|
|
|
@login_required
|
|
def paid_list(request):
|
|
"""Оплаченные лицензии
|
|
"""
|
|
template_name = 'customer/profile/paid_list.html'
|
|
licenses = License.objects.filter(user=request.user,
|
|
status__in=[-1, 1, 2, 3], 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()
|
|
|
|
try:
|
|
license = License.objects.get(pk=pk, status=0, user=request.user)
|
|
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):
|
|
if request.method == 'POST':
|
|
try:
|
|
nInvId = request.POST.get('InvId')
|
|
order = License.objects.get(pk=nInvId)
|
|
|
|
context = {'success': True, 'order_num': nInvId}
|
|
return TemplateResponse(request, 'customer/profile/end_order.html', context)
|
|
except License.DoesNotExist:
|
|
return HttpResponseForbidden()
|
|
else:
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
@csrf_exempt
|
|
def payment_fail(request):
|
|
if request.method == 'POST':
|
|
try:
|
|
nInvId = request.POST.get('InvId')
|
|
|
|
message = u"Возникла проблема. Ваш Заказ не оплачен. Попробуйте оформить заявку снова, или позвоните по номеру."
|
|
|
|
return TemplateResponse(request, 'customer/profile/end_order.html', {'message': message, 'success': False})
|
|
except Order.DoesNotExist:
|
|
return HttpResponseForbidden()
|
|
else:
|
|
return HttpResponseForbidden()
|
|
|