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.

189 lines
5.8 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 = _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.save()
  93. if plan_id == "":
  94. request.session["set_plan_status"] = "success"
  95. return redirect("hc-billing")
  96. error = sub.setup(plan_id, nonce)
  97. if error:
  98. return log_and_bail(request, error)
  99. # Update user's profile
  100. profile = request.user.profile
  101. if plan_id in ("S5", "S48"):
  102. profile.check_limit = 20
  103. profile.team_limit = 2
  104. profile.ping_log_limit = 1000
  105. profile.sms_limit = 5
  106. profile.sms_sent = 0
  107. profile.save()
  108. elif plan_id in ("P20", "Y192"):
  109. profile.check_limit = 100
  110. profile.team_limit = 9
  111. profile.ping_log_limit = 1000
  112. profile.sms_limit = 50
  113. profile.sms_sent = 0
  114. profile.save()
  115. elif plan_id in ("P80", "Y768"):
  116. profile.check_limit = 1000
  117. profile.team_limit = 500
  118. profile.ping_log_limit = 1000
  119. profile.sms_limit = 500
  120. profile.sms_sent = 0
  121. profile.save()
  122. request.session["set_plan_status"] = "success"
  123. return redirect("hc-billing")
  124. @login_required
  125. def address(request):
  126. sub = Subscription.objects.for_user(request.user)
  127. if request.method == "POST":
  128. error = sub.update_address(request.POST)
  129. if error:
  130. return log_and_bail(request, error)
  131. request.session["address_status"] = "success"
  132. return redirect("hc-billing")
  133. ctx = {"a": sub.address, "email": request.user.email}
  134. return render(request, "payments/address.html", ctx)
  135. @login_required
  136. def payment_method(request):
  137. sub = get_object_or_404(Subscription, user=request.user)
  138. ctx = {"sub": sub, "pm": sub.payment_method}
  139. return render(request, "payments/payment_method.html", ctx)
  140. @login_required
  141. def billing_history(request):
  142. try:
  143. sub = Subscription.objects.get(user=request.user)
  144. transactions = sub.transactions
  145. except Subscription.DoesNotExist:
  146. transactions = []
  147. ctx = {"transactions": transactions}
  148. return render(request, "payments/billing_history.html", ctx)