account-overview.page.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. parasails.registerPage('account-overview', {
  2. // ╦╔╗╔╦╔╦╗╦╔═╗╦ ╔═╗╔╦╗╔═╗╔╦╗╔═╗
  3. // ║║║║║ ║ ║╠═╣║ ╚═╗ ║ ╠═╣ ║ ║╣
  4. // ╩╝╚╝╩ ╩ ╩╩ ╩╩═╝ ╚═╝ ╩ ╩ ╩ ╩ ╚═╝
  5. data: {
  6. me: { /* ... */ },
  7. isBillingEnabled: false,
  8. hasBillingCard: false,
  9. // Syncing/loading states for this page.
  10. syncingUpdateCard: false,
  11. syncingRemoveCard: false,
  12. // Form data
  13. formData: { /* … */ },
  14. // Server error state for the form
  15. cloudError: '',
  16. // For the Stripe checkout window
  17. checkoutHandler: undefined,
  18. // For the confirmation modal:
  19. removeCardModalVisible: false,
  20. },
  21. // ╦ ╦╔═╗╔═╗╔═╗╦ ╦╔═╗╦ ╔═╗
  22. // ║ ║╠╣ ║╣ ║ ╚╦╝║ ║ ║╣
  23. // ╩═╝╩╚ ╚═╝╚═╝ ╩ ╚═╝╩═╝╚═╝
  24. beforeMount: function (){
  25. _.extend(this, window.SAILS_LOCALS);
  26. this.isBillingEnabled = !!this.stripePublishableKey;
  27. // Determine whether there is billing info for this user.
  28. this.hasBillingCard = (
  29. this.me.billingCardBrand &&
  30. this.me.billingCardLast4 &&
  31. this.me.billingCardExpMonth &&
  32. this.me.billingCardExpYear
  33. );
  34. },
  35. // ╦╔╗╔╔╦╗╔═╗╦═╗╔═╗╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
  36. // ║║║║ ║ ║╣ ╠╦╝╠═╣║ ║ ║║ ║║║║╚═╗
  37. // ╩╝╚╝ ╩ ╚═╝╩╚═╩ ╩╚═╝ ╩ ╩╚═╝╝╚╝╚═╝
  38. methods: {
  39. clickStripeCheckoutButton: async function() {
  40. // Import utilities.
  41. var openStripeCheckout = parasails.require('openStripeCheckout');
  42. // Prevent double-posting if it's still loading.
  43. if(this.syncingUpdateCard) { return; }
  44. // Clear out error states.
  45. this.cloudError = false;
  46. // Open Stripe Checkout.
  47. var billingCardInfo = await openStripeCheckout(this.stripePublishableKey, this.me.emailAddress);
  48. if (!billingCardInfo) {
  49. // (if the user canceled the dialog, avast)
  50. return;
  51. }
  52. // Now that payment info has been successfully added, update the billing
  53. // info for this user in our backend.
  54. this.syncingUpdateCard = true;
  55. await Cloud.updateBillingCard.with(billingCardInfo)
  56. .tolerate(()=>{
  57. this.cloudError = true;
  58. });
  59. this.syncingUpdateCard = false;
  60. // Upon success, update billing info in the UI.
  61. if (!this.cloudError) {
  62. Object.assign(this.me, _.pick(billingCardInfo, ['billingCardLast4', 'billingCardBrand', 'billingCardExpMonth', 'billingCardExpYear']));
  63. this.hasBillingCard = true;
  64. }
  65. },
  66. clickRemoveCardButton: function() {
  67. this.removeCardModalVisible = true;
  68. },
  69. closeRemoveCardModal: function() {
  70. this.removeCardModalVisible = false;
  71. this.cloudError = false;
  72. },
  73. submittedRemoveCardForm: function() {
  74. // Update billing info on success.
  75. this.me.billingCardLast4 = undefined;
  76. this.me.billingCardBrand = undefined;
  77. this.me.billingCardExpMonth = undefined;
  78. this.me.billingCardExpYear = undefined;
  79. this.hasBillingCard = false;
  80. // Close the modal and clear it out.
  81. this.closeRemoveCardModal();
  82. },
  83. handleParsingRemoveCardForm: function() {
  84. return {
  85. // Set to empty string to indicate the default payment source
  86. // for this customer is being completely removed.
  87. stripeToken: ''
  88. };
  89. },
  90. }
  91. });