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.

164 lines
5.3 KiB

  1. $(function () {
  2. var preloadedToken = null;
  3. function getToken(callback) {
  4. if (preloadedToken) {
  5. callback(preloadedToken);
  6. } else {
  7. $.getJSON("/pricing/token/", function(response) {
  8. preloadedToken = response.client_token;
  9. callback(response.client_token);
  10. });
  11. }
  12. }
  13. // Preload client token:
  14. if ($("#billing-address").length) {
  15. getToken(function(token){});
  16. }
  17. function getAmount(planId) {
  18. return planId.substr(1);
  19. }
  20. function showPaymentMethodForm(planId) {
  21. $("#plan-id").val(planId);
  22. $("#nonce").val("");
  23. if (planId == "") {
  24. // Don't need a payment method when switching to the free plan
  25. // -- can submit the form right away:
  26. $("#update-subscription-form").submit();
  27. return;
  28. }
  29. $("#payment-form-submit").prop("disabled", true);
  30. $("#payment-method-modal").modal("show");
  31. getToken(function(token) {
  32. braintree.dropin.create({
  33. authorization: token,
  34. container: "#dropin",
  35. threeDSecure: {
  36. amount: getAmount(planId),
  37. },
  38. paypal: { flow: 'vault' },
  39. preselectVaultedPaymentMethod: false
  40. }, function(createErr, instance) {
  41. $("#payment-form-submit").off().click(function() {
  42. instance.requestPaymentMethod(function (err, payload) {
  43. $("#payment-method-modal").modal("hide");
  44. $("#please-wait-modal").modal("show");
  45. $("#nonce").val(payload.nonce);
  46. $("#update-subscription-form").submit();
  47. });
  48. });
  49. $("#payment-method-modal").off("hidden.bs.modal").on("hidden.bs.modal", function() {
  50. instance.teardown();
  51. });
  52. instance.on("paymentMethodRequestable", function() {
  53. $("#payment-form-submit").prop("disabled", false);
  54. });
  55. instance.on("noPaymentMethodRequestable", function() {
  56. $("#payment-form-submit").prop("disabled", true);
  57. });
  58. });
  59. });
  60. }
  61. $("#change-plan-btn").click(function() {
  62. $("#change-billing-plan-modal").modal("hide");
  63. showPaymentMethodForm(this.dataset.planId);
  64. });
  65. $("#update-payment-method").click(function() {
  66. showPaymentMethodForm($("#old-plan-id").val());
  67. });
  68. $("#billing-history").load("/accounts/profile/billing/history/");
  69. $("#billing-address").load("/accounts/profile/billing/address/", function() {
  70. $("#billing-address input").each(function(idx, obj) {
  71. $("#" + obj.name).val(obj.value);
  72. });
  73. });
  74. $("#payment-method").load("/accounts/profile/billing/payment_method/", function() {
  75. $("#next-billing-date").text($("#nbd").val());
  76. });
  77. $("#billing-periods input").click(updateChangePlanForm);
  78. $("#plan-hobbyist").click(function() {
  79. $(".plan").removeClass("selected");
  80. $("#plan-hobbyist").addClass("selected");
  81. updateChangePlanForm();
  82. });
  83. $("#plan-business").click(function() {
  84. $(".plan").removeClass("selected");
  85. $("#plan-business").addClass("selected");
  86. updateChangePlanForm();
  87. });
  88. $("#plan-business-plus").click(function() {
  89. $(".plan").removeClass("selected");
  90. $("#plan-business-plus").addClass("selected");
  91. updateChangePlanForm();
  92. });
  93. function updateChangePlanForm() {
  94. var planId = $("#old-plan-id").val();
  95. // "Monthly" is selected: dispplay monthly prices
  96. if ($("#billing-monthly").is(":checked")) {
  97. var period = "monthly";
  98. $("#business-price").text("$20");
  99. $("#business-plus-price").text("$80");
  100. }
  101. // "Annual" is selected: dispplay annual prices
  102. if ($("#billing-annual").is(":checked")) {
  103. var period = "annual";
  104. $("#business-price").text("$16");
  105. $("#business-plus-price").text("$64");
  106. }
  107. // "Hobbyist" is selected, set planId
  108. if ($("#plan-hobbyist").hasClass("selected")) {
  109. planId = "";
  110. }
  111. // "Business" is selected, set planId
  112. if ($("#plan-business").hasClass("selected")) {
  113. planId = period == "monthly" ? "P20" : "Y192";
  114. }
  115. // "Business Plus" is selected, set planId
  116. if ($("#plan-business-plus").hasClass("selected")) {
  117. planId = period == "monthly" ? "P80" : "Y768";
  118. }
  119. if (planId == $("#old-plan-id").val()) {
  120. $("#change-plan-btn")
  121. .attr("disabled", "disabled")
  122. .text("Change Billing Plan");
  123. } else {
  124. var caption = "Change Billing Plan";
  125. if (planId) {
  126. var amount = planId.substr(1);
  127. caption += " And Pay $" + amount + " Now";
  128. }
  129. $("#change-plan-btn")
  130. .removeAttr("disabled")
  131. .text(caption)
  132. .attr("data-plan-id", planId);
  133. }
  134. }
  135. updateChangePlanForm();
  136. });