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.7 KiB

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
  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 (
  5. HttpResponseBadRequest,
  6. HttpResponseForbidden,
  7. JsonResponse,
  8. HttpResponse,
  9. )
  10. from django.shortcuts import get_object_or_404, redirect, render
  11. from django.views.decorators.csrf import csrf_exempt
  12. from django.views.decorators.http import require_POST
  13. from hc.api.models import Check
  14. from hc.lib import emails
  15. from hc.payments.forms import InvoiceEmailingForm
  16. from hc.payments.invoices import PdfInvoice
  17. from hc.payments.models import Subscription
  18. @login_required
  19. def get_client_token(request):
  20. sub = Subscription.objects.for_user(request.user)
  21. return JsonResponse({"client_token": sub.get_client_token()})
  22. def pricing(request):
  23. if request.user.is_authenticated and request.user != request.project.owner:
  24. ctx = {"page": "pricing"}
  25. return render(request, "payments/pricing_not_owner.html", ctx)
  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 = {"page": "pricing", "sub": sub}
  30. return render(request, "payments/pricing.html", ctx)
  31. @login_required
  32. def billing(request):
  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(project__owner=request.user).count(),
  50. "send_invoices_status": send_invoices_status,
  51. "set_plan_status": "default",
  52. "address_status": "default",
  53. "payment_method_status": "default",
  54. }
  55. if "set_plan_status" in request.session:
  56. ctx["set_plan_status"] = request.session.pop("set_plan_status")
  57. if "address_status" in request.session:
  58. ctx["address_status"] = request.session.pop("address_status")
  59. if "payment_method_status" in request.session:
  60. ctx["payment_method_status"] = request.session.pop("payment_method_status")
  61. return render(request, "accounts/billing.html", ctx)
  62. def log_and_bail(request, result):
  63. logged_deep_error = False
  64. for error in result.errors.deep_errors:
  65. messages.error(request, error.message)
  66. logged_deep_error = True
  67. if not logged_deep_error:
  68. messages.error(request, result.message)
  69. return redirect("hc-billing")
  70. @login_required
  71. @require_POST
  72. def set_plan(request):
  73. plan_id = request.POST["plan_id"]
  74. if plan_id not in ("", "P20", "P80", "Y192", "Y768"):
  75. return HttpResponseBadRequest()
  76. sub = Subscription.objects.for_user(request.user)
  77. if sub.plan_id == plan_id:
  78. return redirect("hc-billing")
  79. # Cancel the previous plan
  80. sub.cancel()
  81. if plan_id == "":
  82. profile = request.user.profile
  83. profile.ping_log_limit = 100
  84. profile.check_limit = 20
  85. profile.team_limit = 2
  86. profile.sms_limit = 0
  87. profile.save()
  88. return redirect("hc-billing")
  89. result = sub.setup(plan_id)
  90. if not result.is_success:
  91. return log_and_bail(request, result)
  92. # Update user's profile
  93. profile = request.user.profile
  94. if plan_id in ("P20", "Y192"):
  95. profile.ping_log_limit = 1000
  96. profile.check_limit = 100
  97. profile.team_limit = 9
  98. profile.sms_limit = 50
  99. profile.sms_sent = 0
  100. profile.save()
  101. elif plan_id in ("P80", "Y768"):
  102. profile.ping_log_limit = 1000
  103. profile.check_limit = 1000
  104. profile.team_limit = 500
  105. profile.sms_limit = 500
  106. profile.sms_sent = 0
  107. profile.save()
  108. request.session["set_plan_status"] = "success"
  109. return redirect("hc-billing")
  110. @login_required
  111. def address(request):
  112. sub = Subscription.objects.for_user(request.user)
  113. if request.method == "POST":
  114. error = sub.update_address(request.POST)
  115. if error:
  116. return log_and_bail(request, error)
  117. request.session["address_status"] = "success"
  118. return redirect("hc-billing")
  119. ctx = {"a": sub.address, "email": request.user.email}
  120. return render(request, "payments/address.html", ctx)
  121. @login_required
  122. def payment_method(request):
  123. sub = get_object_or_404(Subscription, user=request.user)
  124. if request.method == "POST":
  125. if "payment_method_nonce" not in request.POST:
  126. return HttpResponseBadRequest()
  127. nonce = request.POST["payment_method_nonce"]
  128. error = sub.update_payment_method(nonce)
  129. if error:
  130. return log_and_bail(request, error)
  131. request.session["payment_method_status"] = "success"
  132. return redirect("hc-billing")
  133. ctx = {"sub": sub, "pm": sub.payment_method}
  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()