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.

222 lines
6.7 KiB

9 years ago
7 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.conf import settings
  3. from django.contrib import messages
  4. from django.contrib.auth.decorators import login_required
  5. from django.http import (
  6. HttpResponseBadRequest,
  7. HttpResponseForbidden,
  8. JsonResponse,
  9. HttpResponse,
  10. )
  11. from django.shortcuts import get_object_or_404, redirect, render
  12. from django.views.decorators.csrf import csrf_exempt
  13. from django.views.decorators.http import require_POST
  14. from hc.api.models import Check
  15. from hc.lib import emails
  16. from hc.payments.forms import InvoiceEmailingForm
  17. from hc.payments.invoices import PdfInvoice
  18. from hc.payments.models import Subscription
  19. @login_required
  20. def token(request):
  21. sub = Subscription.objects.for_user(request.user)
  22. return JsonResponse({"client_token": sub.get_client_token()})
  23. def pricing(request):
  24. if request.user.is_authenticated and request.user != request.project.owner:
  25. ctx = {"page": "pricing"}
  26. return render(request, "payments/pricing_not_owner.html", ctx)
  27. # Don't use Subscription.objects.for_user method here, so a
  28. # subscription object is not created just by viewing a page.
  29. sub = Subscription.objects.filter(user_id=request.user.id).first()
  30. ctx = {
  31. "page": "pricing",
  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. if plan_id not in ("", "P20", "P80", "Y192", "Y768"):
  81. return HttpResponseBadRequest()
  82. sub = Subscription.objects.for_user(request.user)
  83. # If plan_id has not changed then just update the payment method:
  84. if plan_id == sub.plan_id:
  85. error = sub.update_payment_method(nonce)
  86. if error:
  87. return log_and_bail(request, error)
  88. request.session["payment_method_status"] = "success"
  89. return redirect("hc-billing")
  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)
  147. @login_required
  148. def pdf_invoice(request, transaction_id):
  149. sub, tx = Subscription.objects.by_transaction(transaction_id)
  150. # Does this transaction belong to a customer we know about?
  151. if sub is None or tx is None:
  152. return HttpResponseForbidden()
  153. # Does the transaction's customer match the currently logged in user?
  154. if sub.user != request.user and not request.user.is_superuser:
  155. return HttpResponseForbidden()
  156. response = HttpResponse(content_type="application/pdf")
  157. filename = "MS-HC-%s.pdf" % tx.id.upper()
  158. response["Content-Disposition"] = 'attachment; filename="%s"' % filename
  159. PdfInvoice(response).render(tx, sub.flattened_address())
  160. return response
  161. @csrf_exempt
  162. @require_POST
  163. def charge_webhook(request):
  164. sub, tx = Subscription.objects.by_braintree_webhook(request)
  165. if sub.send_invoices:
  166. filename = "MS-HC-%s.pdf" % tx.id.upper()
  167. sink = BytesIO()
  168. PdfInvoice(sink).render(tx, sub.flattened_address())
  169. ctx = {"tx": tx}
  170. recipient = sub.invoice_email or sub.user.email
  171. emails.invoice(recipient, ctx, filename, sink.getvalue())
  172. return HttpResponse()