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.

177 lines
5.4 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 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.payments.forms import InvoiceEmailingForm
  9. from hc.payments.models import Subscription
  10. @login_required
  11. def token(request):
  12. sub = Subscription.objects.for_user(request.user)
  13. return JsonResponse({"client_token": sub.get_client_token()})
  14. def pricing(request):
  15. if request.user.is_authenticated:
  16. if request.project and request.project.owner != request.user:
  17. ctx = {"page": "pricing"}
  18. return render(request, "payments/pricing_not_owner.html", ctx)
  19. # Don't use Subscription.objects.for_user method here, so a
  20. # subscription object is not created just by viewing a page.
  21. sub = Subscription.objects.filter(user_id=request.user.id).first()
  22. ctx = {
  23. "page": "pricing",
  24. "sub": sub,
  25. "enable_whatsapp": settings.TWILIO_USE_WHATSAPP,
  26. }
  27. return render(request, "payments/pricing.html", ctx)
  28. @login_required
  29. def billing(request):
  30. # Don't use Subscription.objects.for_user method here, so a
  31. # subscription object is not created just by viewing a page.
  32. sub = Subscription.objects.filter(user_id=request.user.id).first()
  33. if sub is None:
  34. sub = Subscription(user=request.user)
  35. send_invoices_status = "default"
  36. if request.method == "POST":
  37. form = InvoiceEmailingForm(request.POST)
  38. if form.is_valid():
  39. sub = Subscription.objects.for_user(request.user)
  40. form.update_subscription(sub)
  41. send_invoices_status = "success"
  42. ctx = {
  43. "page": "billing",
  44. "profile": request.profile,
  45. "sub": sub,
  46. "num_checks": Check.objects.filter(project__owner=request.user).count(),
  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"):
  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 = 0
  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 ("P20", "Y192"):
  99. profile.ping_log_limit = 1000
  100. profile.check_limit = 100
  101. profile.team_limit = 9
  102. profile.sms_limit = 50
  103. profile.sms_sent = 0
  104. profile.save()
  105. elif plan_id in ("P80", "Y768"):
  106. profile.ping_log_limit = 1000
  107. profile.check_limit = 1000
  108. profile.team_limit = 500
  109. profile.sms_limit = 500
  110. profile.sms_sent = 0
  111. profile.save()
  112. request.session["set_plan_status"] = "success"
  113. return redirect("hc-billing")
  114. @login_required
  115. def address(request):
  116. sub = Subscription.objects.for_user(request.user)
  117. if request.method == "POST":
  118. error = sub.update_address(request.POST)
  119. if error:
  120. return log_and_bail(request, error)
  121. request.session["address_status"] = "success"
  122. return redirect("hc-billing")
  123. ctx = {"a": sub.address, "email": request.user.email}
  124. return render(request, "payments/address.html", ctx)
  125. @login_required
  126. def payment_method(request):
  127. sub = get_object_or_404(Subscription, user=request.user)
  128. ctx = {"sub": sub, "pm": sub.payment_method}
  129. return render(request, "payments/payment_method.html", ctx)
  130. @login_required
  131. def billing_history(request):
  132. try:
  133. sub = Subscription.objects.get(user=request.user)
  134. transactions = sub.transactions
  135. except Subscription.DoesNotExist:
  136. transactions = []
  137. ctx = {"transactions": transactions}
  138. return render(request, "payments/billing_history.html", ctx)