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.

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