49 lines
1.4 KiB
Plaintext
Raw Normal View History

import { ajax } from "discourse/lib/ajax";
2019-10-10 13:52:55 +11:00
export default Ember.Controller.extend({
init() {
this._super(...arguments);
this.set(
"stripe",
Stripe(Discourse.SiteSettings.discourse_patrons_public_key)
);
const elements = this.get("stripe").elements();
this.set("cardElement", elements.create("card", { hidePostalCode: true }));
},
2019-10-10 13:52:55 +11:00
actions: {
stripePaymentHandler() {
this.stripe.createToken(this.get("cardElement")).then(result => {
if (result.error) {
bootbox.alert(result.error.message);
} else {
2019-10-14 11:47:49 +11:00
const customerData = {
source: result.token.id
};
2019-10-10 13:52:55 +11:00
2019-10-15 21:50:30 +11:00
return ajax("/patrons/customers", {
method: "post",
data: customerData
}).then(customer => {
2019-10-17 20:34:26 +11:00
const subscription = this.get("model.subscription");
2019-10-25 14:00:59 +11:00
subscription.set("customer", customer.id);
2019-10-14 11:47:49 +11:00
2019-10-17 20:34:26 +11:00
if (subscription.get("plan") === undefined) {
subscription.set("plan", this.get("model.plans.firstObject.id"));
}
2019-10-14 11:47:49 +11:00
2019-10-17 20:34:26 +11:00
subscription.save().then(() => {
bootbox.alert("ok payment good... some kind of message");
2019-10-25 14:00:59 +11:00
this.transitionToRoute(
"user.billing",
Discourse.User.current().username.toLowerCase()
);
2019-10-15 21:50:30 +11:00
});
});
}
});
2019-10-10 13:52:55 +11:00
}
}
});