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.

185 lines
5.7 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. error = sub.update_payment_method(nonce)
  76. if error:
  77. return log_and_bail(request, error)
  78. request.session["payment_method_status"] = "success"
  79. return redirect("hc-billing")
  80. if plan_id not in ("", "P20", "P80", "Y192", "Y768", "S5", "S48"):
  81. return HttpResponseBadRequest()
  82. # Cancel the previous plan and reset limits:
  83. sub.cancel()
  84. profile = request.user.profile
  85. profile.ping_log_limit = 100
  86. profile.check_limit = 20
  87. profile.team_limit = 2
  88. profile.sms_limit = 5
  89. profile.save()
  90. if plan_id == "":
  91. request.session["set_plan_status"] = "success"
  92. return redirect("hc-billing")
  93. result = sub.setup(plan_id, nonce)
  94. if not result.is_success:
  95. return log_and_bail(request, result)
  96. # Update user's profile
  97. profile = request.user.profile
  98. if plan_id in ("S5", "S48"):
  99. profile.check_limit = 20
  100. profile.team_limit = 2
  101. profile.ping_log_limit = 1000
  102. profile.sms_limit = 5
  103. profile.sms_sent = 0
  104. profile.save()
  105. elif plan_id in ("P20", "Y192"):
  106. profile.check_limit = 100
  107. profile.team_limit = 9
  108. profile.ping_log_limit = 1000
  109. profile.sms_limit = 50
  110. profile.sms_sent = 0
  111. profile.save()
  112. elif plan_id in ("P80", "Y768"):
  113. profile.check_limit = 1000
  114. profile.team_limit = 500
  115. profile.ping_log_limit = 1000
  116. profile.sms_limit = 500
  117. profile.sms_sent = 0
  118. profile.save()
  119. request.session["set_plan_status"] = "success"
  120. return redirect("hc-billing")
  121. @login_required
  122. def address(request):
  123. sub = Subscription.objects.for_user(request.user)
  124. if request.method == "POST":
  125. error = sub.update_address(request.POST)
  126. if error:
  127. return log_and_bail(request, error)
  128. request.session["address_status"] = "success"
  129. return redirect("hc-billing")
  130. ctx = {"a": sub.address, "email": request.user.email}
  131. return render(request, "payments/address.html", ctx)
  132. @login_required
  133. def payment_method(request):
  134. sub = get_object_or_404(Subscription, user=request.user)
  135. ctx = {"sub": sub, "pm": sub.payment_method}
  136. return render(request, "payments/payment_method.html", ctx)
  137. @login_required
  138. def billing_history(request):
  139. try:
  140. sub = Subscription.objects.get(user=request.user)
  141. transactions = sub.transactions
  142. except Subscription.DoesNotExist:
  143. transactions = []
  144. ctx = {"transactions": transactions}
  145. return render(request, "payments/billing_history.html", ctx)