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.

196 lines
6.0 KiB

9 years ago
9 years ago
9 years ago
  1. from django.contrib import messages
  2. from django.contrib.auth.decorators import login_required
  3. from django.http import Http404, HttpResponseBadRequest, JsonResponse
  4. from django.shortcuts import get_object_or_404, redirect, render
  5. from django.views.decorators.http import require_POST
  6. from hc.front.views import _get_project_for_user
  7. from hc.payments.forms import InvoiceEmailingForm
  8. from hc.payments.models import Subscription
  9. @login_required
  10. def token(request):
  11. sub = Subscription.objects.for_user(request.user)
  12. return JsonResponse({"client_token": sub.get_client_token()})
  13. def pricing(request, code=None):
  14. project = None
  15. if code:
  16. if not request.user.is_authenticated:
  17. raise Http404()
  18. project, rw = _get_project_for_user(request, code)
  19. if project.owner != request.user:
  20. ctx = {"page": "pricing", "project": project}
  21. return render(request, "payments/pricing_not_owner.html", ctx)
  22. sub = None
  23. if request.user.is_authenticated:
  24. # Don't use Subscription.objects.for_user method here, so a
  25. # subscription object is not created just by viewing a page.
  26. sub = Subscription.objects.filter(user_id=request.user.id).first()
  27. ctx = {"page": "pricing", "project": project, "sub": sub}
  28. return render(request, "payments/pricing.html", ctx)
  29. @login_required
  30. def billing(request):
  31. # Don't use Subscription.objects.for_user method here, so a
  32. # subscription object is not created just by viewing a page.
  33. sub = Subscription.objects.filter(user_id=request.user.id).first()
  34. if sub is None:
  35. sub = Subscription(user=request.user)
  36. send_invoices_status = "default"
  37. if request.method == "POST":
  38. form = InvoiceEmailingForm(request.POST)
  39. if form.is_valid():
  40. sub = Subscription.objects.for_user(request.user)
  41. form.update_subscription(sub)
  42. send_invoices_status = "success"
  43. ctx = {
  44. "page": "billing",
  45. "profile": request.profile,
  46. "sub": sub,
  47. "send_invoices_status": send_invoices_status,
  48. "set_plan_status": "default",
  49. "address_status": "default",
  50. "payment_method_status": "default",
  51. }
  52. if "set_plan_status" in request.session:
  53. ctx["set_plan_status"] = request.session.pop("set_plan_status")
  54. if "address_status" in request.session:
  55. ctx["address_status"] = request.session.pop("address_status")
  56. if "payment_method_status" in request.session:
  57. ctx["payment_method_status"] = request.session.pop("payment_method_status")
  58. return render(request, "accounts/billing.html", ctx)
  59. def log_and_bail(request, result):
  60. logged_deep_error = False
  61. for error in result.errors.deep_errors:
  62. messages.error(request, error.message)
  63. logged_deep_error = True
  64. if not logged_deep_error:
  65. messages.error(request, result.message)
  66. return redirect("hc-billing")
  67. @login_required
  68. @require_POST
  69. def update(request):
  70. plan_id = request.POST["plan_id"]
  71. nonce = request.POST["nonce"]
  72. sub = Subscription.objects.for_user(request.user)
  73. # If plan_id has not changed then just update the payment method:
  74. if plan_id == sub.plan_id:
  75. if not sub.subscription_id:
  76. error = sub.setup(plan_id, nonce)
  77. else:
  78. error = sub.update_payment_method(nonce)
  79. if error:
  80. return log_and_bail(request, error)
  81. request.session["payment_method_status"] = "success"
  82. return redirect("hc-billing")
  83. if plan_id not in ("", "P20", "P80", "Y192", "Y768", "S5", "S48"):
  84. return HttpResponseBadRequest()
  85. # Cancel the previous plan and reset limits:
  86. sub.cancel()
  87. profile = request.user.profile
  88. profile.ping_log_limit = 100
  89. profile.check_limit = 20
  90. profile.team_limit = 2
  91. profile.sms_limit = 5
  92. profile.call_limit = 0
  93. profile.save()
  94. if plan_id == "":
  95. request.session["set_plan_status"] = "success"
  96. return redirect("hc-billing")
  97. error = sub.setup(plan_id, nonce)
  98. if error:
  99. return log_and_bail(request, error)
  100. # Update user's profile
  101. profile = request.user.profile
  102. if plan_id in ("S5", "S48"):
  103. profile.check_limit = 20
  104. profile.team_limit = 2
  105. profile.ping_log_limit = 1000
  106. profile.sms_limit = 5
  107. profile.sms_sent = 0
  108. profile.call_limit = 5
  109. profile.calls_sent = 0
  110. profile.save()
  111. elif plan_id in ("P20", "Y192"):
  112. profile.check_limit = 100
  113. profile.team_limit = 9
  114. profile.ping_log_limit = 1000
  115. profile.sms_limit = 50
  116. profile.sms_sent = 0
  117. profile.call_limit = 20
  118. profile.calls_sent = 0
  119. profile.save()
  120. elif plan_id in ("P80", "Y768"):
  121. profile.check_limit = 1000
  122. profile.team_limit = 500
  123. profile.ping_log_limit = 1000
  124. profile.sms_limit = 500
  125. profile.sms_sent = 0
  126. profile.call_limit = 100
  127. profile.calls_sent = 0
  128. profile.save()
  129. request.session["set_plan_status"] = "success"
  130. return redirect("hc-billing")
  131. @login_required
  132. def address(request):
  133. sub = Subscription.objects.for_user(request.user)
  134. if request.method == "POST":
  135. error = sub.update_address(request.POST)
  136. if error:
  137. return log_and_bail(request, error)
  138. request.session["address_status"] = "success"
  139. return redirect("hc-billing")
  140. ctx = {"a": sub.address, "email": request.user.email}
  141. return render(request, "payments/address.html", ctx)
  142. @login_required
  143. def payment_method(request):
  144. sub = get_object_or_404(Subscription, user=request.user)
  145. ctx = {"sub": sub, "pm": sub.payment_method}
  146. return render(request, "payments/payment_method.html", ctx)
  147. @login_required
  148. def billing_history(request):
  149. try:
  150. sub = Subscription.objects.get(user=request.user)
  151. transactions = sub.transactions
  152. except Subscription.DoesNotExist:
  153. transactions = []
  154. ctx = {"transactions": transactions}
  155. return render(request, "payments/billing_history.html", ctx)