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.

182 lines
5.8 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. if ($("#no-billing-address-modal").length) {
  67. $("#no-billing-address-modal").modal("show");
  68. return;
  69. }
  70. showPaymentMethodForm($("#old-plan-id").val());
  71. });
  72. $("#billing-history").load("/accounts/profile/billing/history/");
  73. $("#billing-address").load("/accounts/profile/billing/address/", function() {
  74. $("#billing-address input").each(function(idx, obj) {
  75. $("#" + obj.name).val(obj.value);
  76. });
  77. });
  78. $("#payment-method").load("/accounts/profile/billing/payment_method/", function() {
  79. $("#next-billing-date").text($("#nbd").val());
  80. });
  81. $("#billing-periods input").click(updateChangePlanForm);
  82. $("#plan-hobbyist").click(function() {
  83. $(".plan").removeClass("selected");
  84. $("#plan-hobbyist").addClass("selected");
  85. updateChangePlanForm();
  86. });
  87. $("#plan-supporter").click(function() {
  88. $(".plan").removeClass("selected");
  89. $("#plan-supporter").addClass("selected");
  90. updateChangePlanForm();
  91. });
  92. $("#plan-business").click(function() {
  93. $(".plan").removeClass("selected");
  94. $("#plan-business").addClass("selected");
  95. updateChangePlanForm();
  96. });
  97. $("#plan-business-plus").click(function() {
  98. $(".plan").removeClass("selected");
  99. $("#plan-business-plus").addClass("selected");
  100. updateChangePlanForm();
  101. });
  102. function updateChangePlanForm() {
  103. var planId = $("#old-plan-id").val();
  104. // "Monthly" is selected: dispplay monthly prices
  105. if ($("#billing-monthly").is(":checked")) {
  106. var period = "monthly";
  107. $("#supporter-price").text("$5");
  108. $("#business-price").text("$20");
  109. $("#business-plus-price").text("$80");
  110. }
  111. // "Annual" is selected: dispplay annual prices
  112. if ($("#billing-annual").is(":checked")) {
  113. var period = "annual";
  114. $("#supporter-price").text("$4");
  115. $("#business-price").text("$16");
  116. $("#business-plus-price").text("$64");
  117. }
  118. // "Hobbyist" is selected, set planId
  119. if ($("#plan-hobbyist").hasClass("selected")) {
  120. planId = "";
  121. }
  122. // "Supporter" is selected, set planId
  123. if ($("#plan-supporter").hasClass("selected")) {
  124. planId = period == "monthly" ? "S5" : "S48";
  125. }
  126. // "Business" is selected, set planId
  127. if ($("#plan-business").hasClass("selected")) {
  128. planId = period == "monthly" ? "P20" : "Y192";
  129. }
  130. // "Business Plus" is selected, set planId
  131. if ($("#plan-business-plus").hasClass("selected")) {
  132. planId = period == "monthly" ? "P80" : "Y768";
  133. }
  134. if (planId == $("#old-plan-id").val()) {
  135. $("#change-plan-btn")
  136. .attr("disabled", "disabled")
  137. .text("Change Billing Plan");
  138. } else {
  139. var caption = "Change Billing Plan";
  140. if (planId) {
  141. caption += " And Pay $" + getAmount(planId) + " Now";
  142. }
  143. $("#change-plan-btn")
  144. .removeAttr("disabled")
  145. .text(caption)
  146. .attr("data-plan-id", planId);
  147. }
  148. }
  149. updateChangePlanForm();
  150. });