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.

150 lines
4.0 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. import braintree
  2. from django.contrib.auth.decorators import login_required
  3. from django.contrib import messages
  4. from django.http import HttpResponseForbidden, JsonResponse
  5. from django.shortcuts import redirect, render
  6. from django.views.decorators.http import require_POST
  7. from hc.accounts.models import Profile
  8. from .models import Subscription
  9. @login_required
  10. def get_client_token(request):
  11. sub = Subscription.objects.get(user=request.user)
  12. client_token = braintree.ClientToken.generate({
  13. "customer_id": sub.customer_id
  14. })
  15. return JsonResponse({"client_token": client_token})
  16. def pricing(request):
  17. sub = None
  18. if request.user.is_authenticated():
  19. try:
  20. sub = Subscription.objects.get(user=request.user)
  21. except Subscription.DoesNotExist:
  22. sub = Subscription(user=request.user)
  23. sub.save()
  24. first_charge = False
  25. if "first_charge" in request.session:
  26. first_charge = True
  27. del request.session["first_charge"]
  28. ctx = {
  29. "page": "pricing",
  30. "sub": sub,
  31. "first_charge": first_charge
  32. }
  33. return render(request, "payments/pricing.html", ctx)
  34. def log_and_bail(request, result):
  35. for error in result.errors.deep_errors:
  36. messages.error(request, error.message)
  37. else:
  38. messages.error(request, result.message)
  39. return redirect("hc-pricing")
  40. @login_required
  41. @require_POST
  42. def create_plan(request):
  43. plan_id = request.POST["plan_id"]
  44. assert plan_id in ("P5", "P20")
  45. sub = Subscription.objects.get(user=request.user)
  46. # Cancel the previous plan
  47. if sub.subscription_id:
  48. braintree.Subscription.cancel(sub.subscription_id)
  49. sub.subscription_id = ""
  50. sub.plan_id = ""
  51. sub.save()
  52. # Create Braintree customer record
  53. if not sub.customer_id:
  54. result = braintree.Customer.create({
  55. "email": request.user.email
  56. })
  57. if not result.is_success:
  58. return log_and_bail(request, result)
  59. sub.customer_id = result.customer.id
  60. sub.save()
  61. # Create Braintree payment method
  62. if "payment_method_nonce" in request.POST:
  63. result = braintree.PaymentMethod.create({
  64. "customer_id": sub.customer_id,
  65. "payment_method_nonce": request.POST["payment_method_nonce"]
  66. })
  67. if not result.is_success:
  68. return log_and_bail(request, result)
  69. sub.payment_method_token = result.payment_method.token
  70. sub.save()
  71. # Create Braintree subscription
  72. result = braintree.Subscription.create({
  73. "payment_method_token": sub.payment_method_token,
  74. "plan_id": plan_id,
  75. })
  76. if not result.is_success:
  77. return log_and_bail(request, result)
  78. sub.subscription_id = result.subscription.id
  79. sub.plan_id = plan_id
  80. sub.save()
  81. # Update user's profile
  82. profile = Profile.objects.for_user(request.user)
  83. if plan_id == "P5":
  84. profile.ping_log_limit = 1000
  85. profile.save()
  86. elif plan_id == "P20":
  87. profile.ping_log_limit = 10000
  88. profile.save()
  89. request.session["first_charge"] = True
  90. return redirect("hc-pricing")
  91. @login_required
  92. @require_POST
  93. def cancel_plan(request):
  94. sub = Subscription.objects.get(user=request.user)
  95. braintree.Subscription.cancel(sub.subscription_id)
  96. sub.subscription_id = ""
  97. sub.plan_id = ""
  98. sub.save()
  99. return redirect("hc-pricing")
  100. @login_required
  101. def billing(request):
  102. sub = Subscription.objects.get(user=request.user)
  103. transactions = braintree.Transaction.search(braintree.TransactionSearch.customer_id == sub.customer_id)
  104. ctx = {"transactions": transactions}
  105. return render(request, "payments/billing.html", ctx)
  106. @login_required
  107. def invoice(request, transaction_id):
  108. sub = Subscription.objects.get(user=request.user)
  109. transaction = braintree.Transaction.find(transaction_id)
  110. if transaction.customer_details.id != sub.customer_id:
  111. return HttpResponseForbidden()
  112. ctx = {"tx": transaction}
  113. return render(request, "payments/invoice.html", ctx)