Created
March 14, 2016 13:32
-
-
Save cw6365/84d094c6345ed3e4478b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
manageCards() { | |
// quantumui scope limits workaround | |
this.$rootScope._eotc = this.$rootScope._eotc || {}; | |
let cardScope = this.$rootScope._eotc.cardsManagerScope = { | |
creditCard: { | |
cardNumber: this.user.subscription.cardNumber | |
}, | |
errors: '', | |
close: () => { | |
}, | |
add: (card) => { | |
// start a spinner | |
let stripe = { | |
number: card.number, | |
exp_month: card.expiry_month, | |
exp_year: card.expiry_year, | |
cvc: card.cvc | |
}; | |
this.SpinControl.start('add-card-spinner'); | |
Stripe.card.createToken(stripe, (status, response) => { | |
if (status === 200 && !response.error) { | |
// add token to profile | |
this.User.updateSubscription(this.user._id, response, this.auth) | |
.then((user) => { | |
this.$rootScope._eotc.cardsManagerScope.creditCard = { | |
cardNumber: user.subscription.cardNumber | |
}; | |
this.user = user; | |
}, () => { | |
this.$rootScope._eotc.cardsManagerScope.errors = 'There\'s been an error on our side while storing your details. Please try again.'; | |
}); | |
} else { | |
// deal with stripe errors | |
this.$rootScope.$apply(() => { | |
this.$rootScope._eotc.cardsManagerScope.errors = 'There\'s been an error adding your card. Please try again soon.'; | |
}); | |
} | |
this.SpinControl.stop('add-card-spinner'); | |
}); | |
}, | |
del: () => { | |
this.SpinControl.start('add-card-spinner'); | |
let details = { | |
id: '', | |
card: { | |
exp_month: '', | |
exp_year: '' | |
} | |
}; | |
this.User.updateSubscription(this.user._id, details, this.auth) | |
.then((user) => { | |
this.$rootScope._eotc.cardsManagerScope.creditCard = { | |
cardNumber: null | |
}; | |
}, () => { | |
this.$rootScope._eotc.cardsManagerScope.errors = 'There\'s been an error on our side while deleting your details. Please try again.'; | |
}); | |
this.SpinControl.stop('add-card-spinner'); | |
} | |
}; | |
let self = this; | |
let boxOptions = { | |
boxType: 'confirm', | |
confirmTemplate: cardsManagerTemplate, | |
afterConfirm: () => { | |
// for security, we don't leak finance info on the user object | |
() => delete this.$rootScope._eotc.cardsManagerScope; | |
// let user = angular.copy(self.user); // a working copy | |
// user.creditCards = this.finance.creditCards = cardScope.creditCards; | |
// self.User.update(user._id, user, this.auth) | |
// .catch((error) => { | |
// self.errorMessage = self.errorMessage || error; | |
// }) | |
// .finally( | |
// () => delete this.$rootScope._eotc.cardsManagerScope | |
// ); | |
} | |
}; | |
this.$modalBox(angular.extend({}, this.defaultBoxOptions, boxOptions)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment