You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.5 KiB
Python

8 years ago
from datetime import timedelta
from django.shortcuts import render, redirect
from django.urls import reverse
8 years ago
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound, Http404
8 years ago
from django.views.decorators.csrf import csrf_exempt
from django.utils import timezone
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
8 years ago
from .forms import NewPaymentForm
from .models import Payment, Subscription, BACKENDS, ACTIVE_BACKENDS
8 years ago
def require_backend(name):
backend = BACKENDS.get(name)
if not backend:
raise Http404()
if not backend.backend_enabled:
raise Http404()
return backend
8 years ago
@login_required
def new(request):
if request.method != 'POST':
return redirect('account:index')
form = NewPaymentForm(request.POST)
if not form.is_valid():
return redirect('account:index')
if request.user.vpnuser.get_subscription() is not None:
return redirect('account:index')
subscr = form.cleaned_data['subscr'] == '1'
backend_id = form.cleaned_data['method']
8 years ago
months = int(form.cleaned_data['time'])
if backend_id not in ACTIVE_BACKENDS:
8 years ago
return HttpResponseNotFound()
if subscr:
if months not in (3, 6, 12):
return redirect('account:index')
8 years ago
rps = Subscription(
user=request.user,
backend_id=backend_id,
period=str(months) + 'm',
)
rps.save()
r = rps.backend.new_subscription(rps)
else:
payment = Payment.create_payment(backend_id, request.user, months)
payment.save()
r = payment.backend.new_payment(payment)
8 years ago
if not r:
payment.status = 'error'
payment.save()
raise Exception("Failed to initialize payment #%d" % payment.id)
if isinstance(r, str):
return render(request, 'payments/form.html', dict(html=r))
elif r is None:
return redirect('payments:view', payment.id)
return r
@csrf_exempt
def callback_paypal(request, id):
""" PayPal IPN """
backend = require_backend('paypal')
8 years ago
p = Payment.objects.get(id=id)
if backend.callback(p, request):
8 years ago
return HttpResponse()
else:
return HttpResponseBadRequest()
@csrf_exempt
@login_required
def callback_stripe(request, id):
""" Stripe button POST """
backend = require_backend('stripe')
8 years ago
p = Payment.objects.get(id=id)
backend.callback(p, request)
8 years ago
return redirect(reverse('payments:view', args=(id,)))
6 years ago
@csrf_exempt
def callback_coingate(request, id):
""" CoinGate payment callback """
backend = require_backend('coingate')
6 years ago
p = Payment.objects.get(id=id)
if backend.callback(p, request):
6 years ago
return HttpResponse()
else:
return HttpResponseBadRequest()
8 years ago
@csrf_exempt
def callback_coinbase(request):
backend = require_backend('coinbase')
8 years ago
if backend.callback(Payment, request):
8 years ago
return HttpResponse()
else:
return HttpResponseBadRequest()
@csrf_exempt
def callback_paypal_subscr(request, id):
""" PayPal Subscription IPN """
backend = require_backend('paypal')
p = Subscription.objects.get(id=id)
if paypal.callback_subscr(p, request):
return HttpResponse()
else:
return HttpResponseBadRequest()
@csrf_exempt
@login_required
def callback_stripe_subscr(request, id):
""" Stripe subscription form target """
backend = require_backend('stripe')
p = Subscription.objects.get(id=id)
backend.callback_subscr(p, request)
if p.status == 'error' or p.status == 'cancelled':
messages.add_message(request, messages.ERROR,
_("Error subscribing. It usually means you don't"
" have enough money available."))
else:
messages.add_message(request, messages.INFO, _("Subscribed!"))
return redirect(reverse('account:index'))
@csrf_exempt
def stripe_hook(request):
backend = require_backend('stripe')
if backend.webhook(request):
return HttpResponse()
else:
return HttpResponseBadRequest()
8 years ago
@login_required
@csrf_exempt
def view(request, id):
p = Payment.objects.get(id=id, user=request.user)
return render(request, 'payments/view.html', dict(payment=p))
@login_required
def cancel(request, id):
p = Payment.objects.get(id=id, user=request.user)
if p.status == 'new':
p.status = 'cancelled'
p.save()
return render(request, 'payments/view.html', dict(payment=p))
@login_required
def cancel_subscr(request, id):
if request.method != 'POST':
return redirect('account:index')
p = Subscription.objects.get(id=id, user=request.user)
try:
p.backend.cancel_subscription(p)
messages.add_message(request, messages.INFO, _("Subscription cancelled!"))
except NotImplementedError:
pass
return redirect('account:index')
@login_required
def return_subscr(request, id):
p = Subscription.objects.get(id=id, user=request.user)
if p.status == 'new':
p.status = 'unconfirmed'
p.save()
return redirect('account:index')
8 years ago
@login_required
def list_payments(request):
# Only show recent cancelled payments
cancelled_limit = timezone.now() - timedelta(days=3)
objects = request.user.payment_set.exclude(status='cancelled',
created__lte=cancelled_limit)
return render(request, 'payments/list.html', dict(payments=objects))