CCrypto VPN public website https://vpn.ccrypto.org/
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.
 
 
 
 
 
 

194 lines
5.4 KiB

  1. from datetime import timedelta
  2. from django.shortcuts import render, redirect
  3. from django.core.urlresolvers import reverse
  4. from django.conf import settings
  5. from django.contrib.auth.decorators import login_required
  6. from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
  7. from django.views.decorators.csrf import csrf_exempt
  8. from django.utils import timezone
  9. from django.contrib import messages
  10. from django.utils.translation import ugettext_lazy as _
  11. from .forms import NewPaymentForm
  12. from .models import Payment, Subscription, BACKENDS, ACTIVE_BACKENDS
  13. @login_required
  14. def new(request):
  15. if request.method != 'POST':
  16. return redirect('account:index')
  17. form = NewPaymentForm(request.POST)
  18. if not form.is_valid():
  19. return redirect('account:index')
  20. if request.user.vpnuser.get_subscription() is not None:
  21. return redirect('account:index')
  22. subscr = form.cleaned_data['subscr'] == '1'
  23. backend_id = form.cleaned_data['method']
  24. months = int(form.cleaned_data['time'])
  25. if backend_id not in ACTIVE_BACKENDS:
  26. return HttpResponseNotFound()
  27. if subscr:
  28. if months not in (3, 6, 12):
  29. return redirect('account:index')
  30. rps = Subscription(
  31. user=request.user,
  32. backend_id=backend_id,
  33. period=str(months) + 'm',
  34. )
  35. rps.save()
  36. r = rps.backend.new_subscription(rps)
  37. else:
  38. payment = Payment.create_payment(backend_id, request.user, months)
  39. payment.save()
  40. r = payment.backend.new_payment(payment)
  41. if not r:
  42. payment.status = 'error'
  43. payment.save()
  44. raise Exception("Failed to initialize payment #%d" % payment.id)
  45. if isinstance(r, str):
  46. return render(request, 'payments/form.html', dict(html=r))
  47. elif r is None:
  48. return redirect('payments:view', payment.id)
  49. return r
  50. @csrf_exempt
  51. def callback_paypal(request, id):
  52. """ PayPal IPN """
  53. if not BACKENDS['paypal'].backend_enabled:
  54. return HttpResponseNotFound()
  55. p = Payment.objects.get(id=id)
  56. if BACKENDS['paypal'].callback(p, request):
  57. return HttpResponse()
  58. else:
  59. return HttpResponseBadRequest()
  60. @csrf_exempt
  61. @login_required
  62. def callback_stripe(request, id):
  63. """ Stripe button POST """
  64. if not BACKENDS['stripe'].backend_enabled:
  65. return HttpResponseNotFound()
  66. p = Payment.objects.get(id=id)
  67. BACKENDS['stripe'].callback(p, request)
  68. return redirect(reverse('payments:view', args=(id,)))
  69. @csrf_exempt
  70. def callback_coinbase(request):
  71. if not BACKENDS['coinbase'].backend_enabled:
  72. return HttpResponseNotFound()
  73. if BACKENDS['coinbase'].callback(Payment, request):
  74. return HttpResponse()
  75. else:
  76. return HttpResponseBadRequest()
  77. @csrf_exempt
  78. def callback_paypal_subscr(request, id):
  79. """ PayPal Subscription IPN """
  80. if not BACKENDS['paypal'].backend_enabled:
  81. return HttpResponseNotFound()
  82. p = Subscription.objects.get(id=id)
  83. if BACKENDS['paypal'].callback_subscr(p, request):
  84. return HttpResponse()
  85. else:
  86. return HttpResponseBadRequest()
  87. @csrf_exempt
  88. @login_required
  89. def callback_stripe_subscr(request, id):
  90. """ Stripe subscription form target """
  91. if not BACKENDS['stripe'].backend_enabled:
  92. return HttpResponseNotFound()
  93. p = Subscription.objects.get(id=id)
  94. BACKENDS['stripe'].callback_subscr(p, request)
  95. if p.status == 'error' or p.status == 'cancelled':
  96. messages.add_message(request, messages.ERROR,
  97. _("Error subscribing. It usually means you don't"
  98. " have enough money available."))
  99. else:
  100. messages.add_message(request, messages.INFO, _("Subscribed!"))
  101. return redirect(reverse('account:index'))
  102. @csrf_exempt
  103. def stripe_hook(request):
  104. if not BACKENDS['stripe'].backend_enabled:
  105. return HttpResponseNotFound()
  106. if BACKENDS['stripe'].webhook(request):
  107. return HttpResponse()
  108. else:
  109. return HttpResponseBadRequest()
  110. @login_required
  111. @csrf_exempt
  112. def view(request, id):
  113. p = Payment.objects.get(id=id, user=request.user)
  114. return render(request, 'payments/view.html', dict(payment=p))
  115. @login_required
  116. def cancel(request, id):
  117. p = Payment.objects.get(id=id, user=request.user)
  118. if p.status == 'new':
  119. p.status = 'cancelled'
  120. p.save()
  121. return render(request, 'payments/view.html', dict(payment=p))
  122. @login_required
  123. def cancel_subscr(request, id):
  124. if request.method != 'POST':
  125. return redirect('account:index')
  126. p = Subscription.objects.get(id=id, user=request.user)
  127. try:
  128. p.backend.cancel_subscription(p)
  129. messages.add_message(request, messages.INFO, _("Subscription cancelled!"))
  130. except NotImplementedError:
  131. pass
  132. return redirect('account:index')
  133. @login_required
  134. def return_subscr(request, id):
  135. p = Subscription.objects.get(id=id, user=request.user)
  136. if p.status == 'new':
  137. p.status = 'unconfirmed'
  138. p.save()
  139. return redirect('account:index')
  140. @login_required
  141. def list_payments(request):
  142. # Only show recent cancelled payments
  143. cancelled_limit = timezone.now() - timedelta(days=3)
  144. objects = request.user.payment_set.exclude(status='cancelled',
  145. created__lte=cancelled_limit)
  146. return render(request, 'payments/list.html', dict(payments=objects))