2019-10-13 22:54:46 -04:00
|
|
|
import DiscourseURL from "discourse/lib/url";
|
2019-10-10 21:26:01 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
|
2019-10-09 22:52:55 -04:00
|
|
|
export default Ember.Controller.extend({
|
2019-10-10 21:26:01 -04:00
|
|
|
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-09 22:52:55 -04:00
|
|
|
actions: {
|
2019-10-10 21:26:01 -04:00
|
|
|
stripePaymentHandler() {
|
|
|
|
// https://stripe.com/docs/billing/subscriptions/payment#signup-flow
|
|
|
|
|
|
|
|
this.stripe.createToken(this.get("cardElement")).then(result => {
|
|
|
|
if (result.error) {
|
|
|
|
// Inform the customer that there was an error.
|
|
|
|
// var errorElement = document.getElementById('card-errors');
|
|
|
|
// errorElement.textContent = result.error.message;
|
|
|
|
} else {
|
2019-10-13 20:47:49 -04:00
|
|
|
const customerData = {
|
2019-10-10 21:26:01 -04:00
|
|
|
source: result.token.id
|
|
|
|
};
|
2019-10-09 22:52:55 -04:00
|
|
|
|
2019-10-15 06:50:30 -04:00
|
|
|
return ajax("/patrons/customers", {
|
|
|
|
method: "post",
|
|
|
|
data: customerData
|
|
|
|
}).then(customer => {
|
|
|
|
// TODO move default plan into settings
|
|
|
|
if (this.get("model.selectedPlan") === undefined) {
|
|
|
|
this.set(
|
|
|
|
"model.selectedPlan",
|
|
|
|
this.get("model.plans.firstObject")
|
|
|
|
);
|
|
|
|
}
|
2019-10-13 20:47:49 -04:00
|
|
|
|
2019-10-15 06:50:30 -04:00
|
|
|
const subscriptionData = {
|
|
|
|
customer: customer.id,
|
|
|
|
plan: this.get("model.selectedPlan")
|
|
|
|
};
|
2019-10-13 20:47:49 -04:00
|
|
|
|
2019-10-15 06:50:30 -04:00
|
|
|
return ajax("/patrons/subscriptions", {
|
|
|
|
method: "post",
|
|
|
|
data: subscriptionData
|
|
|
|
}).then(() => {
|
|
|
|
return DiscourseURL.redirectTo(
|
|
|
|
Discourse.SiteSettings
|
|
|
|
.discourse_patrons_subscription_group_landing_page
|
2019-10-13 20:47:49 -04:00
|
|
|
);
|
2019-10-15 06:50:30 -04:00
|
|
|
});
|
|
|
|
});
|
2019-10-10 21:26:01 -04:00
|
|
|
}
|
|
|
|
});
|
2019-10-09 22:52:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|