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.

225 lines
7.0 KiB

7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. from django.contrib import messages
  2. from django.contrib.auth.decorators import login_required
  3. from django.http import (HttpResponseBadRequest, HttpResponseForbidden,
  4. JsonResponse, HttpResponse)
  5. from django.shortcuts import get_object_or_404, redirect, render
  6. from django.views.decorators.csrf import csrf_exempt
  7. from django.views.decorators.http import require_POST
  8. import six
  9. from hc.api.models import Check
  10. from hc.lib import emails
  11. from hc.payments.forms import InvoiceEmailingForm
  12. from hc.payments.invoices import PdfInvoice
  13. from hc.payments.models import Subscription
  14. @login_required
  15. def get_client_token(request):
  16. sub = Subscription.objects.for_user(request.user)
  17. return JsonResponse({"client_token": sub.get_client_token()})
  18. def pricing(request):
  19. if request.user.is_authenticated and request.profile != request.team:
  20. ctx = {"page": "pricing"}
  21. return render(request, "payments/pricing_not_owner.html", ctx)
  22. # Don't use Subscription.objects.for_user method here, so a
  23. # subscription object is not created just by viewing a page.
  24. sub = Subscription.objects.filter(user_id=request.user.id).first()
  25. ctx = {"page": "pricing", "sub": sub}
  26. return render(request, "payments/pricing.html", ctx)
  27. @login_required
  28. def billing(request):
  29. if request.team != request.profile:
  30. request.team = request.profile
  31. request.profile.current_team = request.profile
  32. request.profile.save()
  33. # Don't use Subscription.objects.for_user method here, so a
  34. # subscription object is not created just by viewing a page.
  35. sub = Subscription.objects.filter(user_id=request.user.id).first()
  36. if sub is None:
  37. sub = Subscription(user=request.user)
  38. send_invoices_status = "default"
  39. if request.method == "POST":
  40. form = InvoiceEmailingForm(request.POST)
  41. if form.is_valid():
  42. sub = Subscription.objects.for_user(request.user)
  43. form.update_subscription(sub)
  44. send_invoices_status = "success"
  45. ctx = {
  46. "page": "billing",
  47. "profile": request.profile,
  48. "sub": sub,
  49. "num_checks": Check.objects.filter(user=request.user).count(),
  50. "team_size": request.profile.member_set.count() + 1,
  51. "team_max": request.profile.team_limit + 1,
  52. "send_invoices_status": send_invoices_status,
  53. "set_plan_status": "default",
  54. "address_status": "default",
  55. "payment_method_status": "default"
  56. }
  57. if "set_plan_status" in request.session:
  58. ctx["set_plan_status"] = request.session.pop("set_plan_status")
  59. if "address_status" in request.session:
  60. ctx["address_status"] = request.session.pop("address_status")
  61. if "payment_method_status" in request.session:
  62. ctx["payment_method_status"] = \
  63. request.session.pop("payment_method_status")
  64. return render(request, "accounts/billing.html", ctx)
  65. def log_and_bail(request, result):
  66. logged_deep_error = False
  67. for error in result.errors.deep_errors:
  68. messages.error(request, error.message)
  69. logged_deep_error = True
  70. if not logged_deep_error:
  71. messages.error(request, result.message)
  72. return redirect("hc-billing")
  73. @login_required
  74. @require_POST
  75. def set_plan(request):
  76. plan_id = request.POST["plan_id"]
  77. if plan_id not in ("", "P20", "P80", "Y192", "Y768"):
  78. return HttpResponseBadRequest()
  79. sub = Subscription.objects.for_user(request.user)
  80. if sub.plan_id == plan_id:
  81. return redirect("hc-billing")
  82. # Cancel the previous plan
  83. sub.cancel()
  84. if plan_id == "":
  85. profile = request.user.profile
  86. profile.ping_log_limit = 100
  87. profile.check_limit = 20
  88. profile.team_limit = 2
  89. profile.sms_limit = 0
  90. profile.save()
  91. return redirect("hc-billing")
  92. result = sub.setup(plan_id)
  93. if not result.is_success:
  94. return log_and_bail(request, result)
  95. # Update user's profile
  96. profile = request.user.profile
  97. if plan_id in ("P20", "Y192"):
  98. profile.ping_log_limit = 1000
  99. profile.check_limit = 50
  100. profile.team_limit = 9
  101. profile.sms_limit = 50
  102. profile.sms_sent = 0
  103. profile.save()
  104. elif plan_id in ("P80", "Y768"):
  105. profile.ping_log_limit = 1000
  106. profile.check_limit = 500
  107. profile.team_limit = 500
  108. profile.sms_limit = 500
  109. profile.sms_sent = 0
  110. profile.save()
  111. request.session["set_plan_status"] = "success"
  112. return redirect("hc-billing")
  113. @login_required
  114. def address(request):
  115. sub = Subscription.objects.for_user(request.user)
  116. if request.method == "POST":
  117. error = sub.update_address(request.POST)
  118. if error:
  119. return log_and_bail(request, error)
  120. request.session["address_status"] = "success"
  121. return redirect("hc-billing")
  122. ctx = {"a": sub.address}
  123. return render(request, "payments/address.html", ctx)
  124. @login_required
  125. def payment_method(request):
  126. sub = get_object_or_404(Subscription, user=request.user)
  127. if request.method == "POST":
  128. if "payment_method_nonce" not in request.POST:
  129. return HttpResponseBadRequest()
  130. nonce = request.POST["payment_method_nonce"]
  131. error = sub.update_payment_method(nonce)
  132. if error:
  133. return log_and_bail(request, error)
  134. request.session["payment_method_status"] = "success"
  135. return redirect("hc-billing")
  136. ctx = {
  137. "sub": sub,
  138. "pm": sub.payment_method
  139. }
  140. return render(request, "payments/payment_method.html", ctx)
  141. @login_required
  142. def billing_history(request):
  143. try:
  144. sub = Subscription.objects.get(user=request.user)
  145. transactions = sub.transactions
  146. except Subscription.DoesNotExist:
  147. transactions = []
  148. ctx = {"transactions": transactions}
  149. return render(request, "payments/billing_history.html", ctx)
  150. @login_required
  151. def pdf_invoice(request, transaction_id):
  152. sub, tx = Subscription.objects.by_transaction(transaction_id)
  153. # Does this transaction belong to a customer we know about?
  154. if sub is None or tx is None:
  155. return HttpResponseForbidden()
  156. # Does the transaction's customer match the currently logged in user?
  157. if sub.user != request.user and not request.user.is_superuser:
  158. return HttpResponseForbidden()
  159. response = HttpResponse(content_type='application/pdf')
  160. filename = "MS-HC-%s.pdf" % tx.id.upper()
  161. response['Content-Disposition'] = 'attachment; filename="%s"' % filename
  162. PdfInvoice(response).render(tx, sub.flattened_address())
  163. return response
  164. @csrf_exempt
  165. @require_POST
  166. def charge_webhook(request):
  167. sub, tx = Subscription.objects.by_braintree_webhook(request)
  168. if sub.send_invoices:
  169. filename = "MS-HC-%s.pdf" % tx.id.upper()
  170. sink = six.BytesIO()
  171. PdfInvoice(sink).render(tx, sub.flattened_address())
  172. ctx = {"tx": tx}
  173. recipient = sub.invoice_email or sub.user.email
  174. emails.invoice(recipient, ctx, filename, sink.getvalue())
  175. return HttpResponse()