discourse-subscriptions/assets/javascripts/discourse/components/stripe-card.js.es6

75 lines
1.7 KiB
Plaintext
Raw Normal View History

2019-09-11 06:19:50 -04:00
export default Ember.Component.extend({
init() {
this._super(...arguments);
const settings = Discourse.SiteSettings;
this.setProperties({
cardError: false,
color: jQuery("body").css("color"),
backgroundColor: jQuery("body").css("background-color"),
2019-09-11 17:14:16 -04:00
stripe: Stripe(settings.discourse_patrons_public_key)
2019-09-11 06:19:50 -04:00
});
},
didInsertElement() {
this._super(...arguments);
2019-09-11 17:14:16 -04:00
const color = this.get("color");
2019-09-11 06:19:50 -04:00
const style = {
base: {
color,
iconColor: color,
"::placeholder": { color }
}
};
const elements = this.stripe.elements();
const card = elements.create("card", { style, hidePostalCode: true });
2019-09-11 17:14:16 -04:00
card.mount("#card-element");
2019-09-11 06:19:50 -04:00
this.set("card", card);
2019-09-11 17:14:16 -04:00
card.on("change", result => {
this.set("cardError", false);
2019-09-11 06:19:50 -04:00
2019-09-11 17:14:16 -04:00
if (result.error) {
this.set("cardError", result.error.message);
2019-09-11 06:19:50 -04:00
}
});
},
2019-09-12 03:47:08 -04:00
validateBilling() {
2019-09-12 03:47:44 -04:00
const billing = this.get("billing");
const deleteEmpty = key => {
if (Ember.isEmpty(billing.get(key))) {
2019-09-12 03:47:08 -04:00
billing.set(key, undefined);
}
2019-09-12 03:47:44 -04:00
};
["name", "phone", "email"].forEach(key => deleteEmpty(key));
2019-09-12 03:47:08 -04:00
},
2019-09-11 06:19:50 -04:00
actions: {
submitStripeCard() {
2019-09-12 03:47:08 -04:00
this.validateBilling();
2019-09-12 03:47:44 -04:00
const paymentOptions = { billing_details: this.get("billing") };
2019-09-12 01:13:08 -04:00
this.stripe.createPaymentMethod("card", this.card, paymentOptions).then(
2019-09-11 17:14:16 -04:00
result => {
if (result.error) {
this.set("cardError", result.error.message);
} else {
this.handleConfirmStripeCard(result.paymentMethod);
}
},
() => {
this.set("cardError", "Unknown error.");
2019-09-11 06:19:50 -04:00
}
2019-09-11 17:14:16 -04:00
);
}
}
2019-09-11 06:19:50 -04:00
});