123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- parasails.registerPage('account-overview', {
- // ╦╔╗╔╦╔╦╗╦╔═╗╦ ╔═╗╔╦╗╔═╗╔╦╗╔═╗
- // ║║║║║ ║ ║╠═╣║ ╚═╗ ║ ╠═╣ ║ ║╣
- // ╩╝╚╝╩ ╩ ╩╩ ╩╩═╝ ╚═╝ ╩ ╩ ╩ ╩ ╚═╝
- data: {
- me: { /* ... */ },
- isBillingEnabled: false,
- hasBillingCard: false,
- // Syncing/loading states for this page.
- syncingUpdateCard: false,
- syncingRemoveCard: false,
- // Form data
- formData: { /* … */ },
- // Server error state for the form
- cloudError: '',
- // For the Stripe checkout window
- checkoutHandler: undefined,
- // For the confirmation modal:
- removeCardModalVisible: false,
- },
- // ╦ ╦╔═╗╔═╗╔═╗╦ ╦╔═╗╦ ╔═╗
- // ║ ║╠╣ ║╣ ║ ╚╦╝║ ║ ║╣
- // ╩═╝╩╚ ╚═╝╚═╝ ╩ ╚═╝╩═╝╚═╝
- beforeMount: function (){
- _.extend(this, window.SAILS_LOCALS);
- this.isBillingEnabled = !!this.stripePublishableKey;
- // Determine whether there is billing info for this user.
- this.hasBillingCard = (
- this.me.billingCardBrand &&
- this.me.billingCardLast4 &&
- this.me.billingCardExpMonth &&
- this.me.billingCardExpYear
- );
- },
- // ╦╔╗╔╔╦╗╔═╗╦═╗╔═╗╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
- // ║║║║ ║ ║╣ ╠╦╝╠═╣║ ║ ║║ ║║║║╚═╗
- // ╩╝╚╝ ╩ ╚═╝╩╚═╩ ╩╚═╝ ╩ ╩╚═╝╝╚╝╚═╝
- methods: {
- clickStripeCheckoutButton: async function() {
- // Import utilities.
- var openStripeCheckout = parasails.require('openStripeCheckout');
- // Prevent double-posting if it's still loading.
- if(this.syncingUpdateCard) { return; }
- // Clear out error states.
- this.cloudError = false;
- // Open Stripe Checkout.
- var billingCardInfo = await openStripeCheckout(this.stripePublishableKey, this.me.emailAddress);
- if (!billingCardInfo) {
- // (if the user canceled the dialog, avast)
- return;
- }
- // Now that payment info has been successfully added, update the billing
- // info for this user in our backend.
- this.syncingUpdateCard = true;
- await Cloud.updateBillingCard.with(billingCardInfo)
- .tolerate(()=>{
- this.cloudError = true;
- });
- this.syncingUpdateCard = false;
- // Upon success, update billing info in the UI.
- if (!this.cloudError) {
- Object.assign(this.me, _.pick(billingCardInfo, ['billingCardLast4', 'billingCardBrand', 'billingCardExpMonth', 'billingCardExpYear']));
- this.hasBillingCard = true;
- }
- },
- clickRemoveCardButton: function() {
- this.removeCardModalVisible = true;
- },
- closeRemoveCardModal: function() {
- this.removeCardModalVisible = false;
- this.cloudError = false;
- },
- submittedRemoveCardForm: function() {
- // Update billing info on success.
- this.me.billingCardLast4 = undefined;
- this.me.billingCardBrand = undefined;
- this.me.billingCardExpMonth = undefined;
- this.me.billingCardExpYear = undefined;
- this.hasBillingCard = false;
- // Close the modal and clear it out.
- this.closeRemoveCardModal();
- },
- handleParsingRemoveCardForm: function() {
- return {
- // Set to empty string to indicate the default payment source
- // for this customer is being completely removed.
- stripeToken: ''
- };
- },
- }
- });
|