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.

186 lines
5.7 KiB

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