75 lines
1.7 KiB
Plaintext
Raw Normal View History

2019-09-11 20:19:50 +10: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-12 07:14:16 +10:00
stripe: Stripe(settings.discourse_patrons_public_key)
2019-09-11 20:19:50 +10:00
});
},
didInsertElement() {
this._super(...arguments);
2019-09-12 07:14:16 +10:00
const color = this.get("color");
2019-09-11 20:19:50 +10:00
const style = {
base: {
color,
iconColor: color,
"::placeholder": { color }
}
};
const elements = this.stripe.elements();
const card = elements.create("card", { style, hidePostalCode: true });
2019-09-12 07:14:16 +10:00
card.mount("#card-element");
2019-09-11 20:19:50 +10:00
this.set("card", card);
2019-09-12 07:14:16 +10:00
card.on("change", result => {
this.set("cardError", false);
2019-09-11 20:19:50 +10:00
2019-09-12 07:14:16 +10:00
if (result.error) {
this.set("cardError", result.error.message);
2019-09-11 20:19:50 +10:00
}
});
},
2019-09-12 17:47:08 +10:00
validateBilling() {
2019-09-12 17:47:44 +10:00
const billing = this.get("billing");
const deleteEmpty = key => {
if (Ember.isEmpty(billing.get(key))) {
2019-09-12 17:47:08 +10:00
billing.set(key, undefined);
}
2019-09-12 17:47:44 +10:00
};
2019-09-13 12:35:38 +10:00
["name", "phone"].forEach(key => deleteEmpty(key));
2019-09-12 17:47:08 +10:00
},
2019-09-11 20:19:50 +10:00
actions: {
submitStripeCard() {
2019-09-12 17:47:08 +10:00
this.validateBilling();
2019-09-12 17:47:44 +10:00
const paymentOptions = { billing_details: this.get("billing") };
2019-09-12 15:13:08 +10:00
this.stripe.createPaymentMethod("card", this.card, paymentOptions).then(
2019-09-12 07:14:16 +10:00
result => {
if (result.error) {
this.set("cardError", result.error.message);
} else {
this.handleConfirmStripeCard(result.paymentMethod);
}
},
() => {
this.set("cardError", "Unknown error.");
2019-09-11 20:19:50 +10:00
}
2019-09-12 07:14:16 +10:00
);
}
}
2019-09-11 20:19:50 +10:00
});