2019-12-11 20:59:26 -05:00
|
|
|
import Customer from "discourse/plugins/discourse-subscriptions/discourse/models/customer";
|
2019-12-12 18:41:14 -05:00
|
|
|
import Payment from "discourse/plugins/discourse-subscriptions/discourse/models/payment";
|
2019-12-11 20:59:26 -05:00
|
|
|
import Subscription from "discourse/plugins/discourse-subscriptions/discourse/models/subscription";
|
2019-12-12 18:41:14 -05:00
|
|
|
import computed from "discourse-common/utils/decorators";
|
2019-10-10 21:26:01 -04:00
|
|
|
|
2019-10-09 22:52:55 -04:00
|
|
|
export default Ember.Controller.extend({
|
2019-12-12 18:41:14 -05:00
|
|
|
planTypeIsSelected: true,
|
|
|
|
|
|
|
|
@computed("planTypeIsSelected")
|
|
|
|
type(planTypeIsSelected) {
|
|
|
|
return planTypeIsSelected ? "plans" : "payment";
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("type")
|
|
|
|
buttonText(type) {
|
2019-12-12 20:29:34 -05:00
|
|
|
return I18n.t(`discourse_subscriptions.${type}.payment_button`);
|
2019-12-12 18:41:14 -05:00
|
|
|
},
|
|
|
|
|
2019-10-10 21:26:01 -04:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2019-12-17 05:14:13 -05:00
|
|
|
this.set(
|
|
|
|
"paymentsAllowed",
|
|
|
|
Discourse.SiteSettings.discourse_subscriptions_allow_payments
|
|
|
|
);
|
2019-10-10 21:26:01 -04:00
|
|
|
this.set(
|
|
|
|
"stripe",
|
2019-12-03 19:53:05 -05:00
|
|
|
Stripe(Discourse.SiteSettings.discourse_subscriptions_public_key)
|
2019-10-10 21:26:01 -04:00
|
|
|
);
|
|
|
|
const elements = this.get("stripe").elements();
|
2019-11-28 23:08:51 -05:00
|
|
|
|
2019-12-09 18:15:30 -05:00
|
|
|
this.set("cardElement", elements.create("card", { hidePostalCode: true }));
|
2019-10-10 21:26:01 -04:00
|
|
|
},
|
|
|
|
|
2019-12-12 18:41:14 -05:00
|
|
|
alert(path) {
|
|
|
|
bootbox.alert(I18n.t(`discourse_subscriptions.${path}`));
|
|
|
|
},
|
|
|
|
|
|
|
|
createPayment(plan) {
|
|
|
|
return this.stripe
|
|
|
|
.createPaymentMethod("card", this.get("cardElement"))
|
|
|
|
.then(result => {
|
|
|
|
const payment = Payment.create({
|
|
|
|
payment_method: result.paymentMethod.id,
|
|
|
|
amount: plan.get("amount"),
|
|
|
|
currency: plan.get("currency")
|
|
|
|
});
|
|
|
|
|
|
|
|
return payment.save();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-01-14 02:46:48 -05:00
|
|
|
createSubscription(plan) {
|
2019-12-12 18:41:14 -05:00
|
|
|
return this.stripe.createToken(this.get("cardElement")).then(result => {
|
|
|
|
if (result.error) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
const customer = Customer.create({ source: result.token.id });
|
|
|
|
|
|
|
|
return customer.save().then(c => {
|
|
|
|
const subscription = Subscription.create({
|
|
|
|
customer: c.id,
|
|
|
|
plan: plan.get("id")
|
|
|
|
});
|
|
|
|
|
|
|
|
return subscription.save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-10-09 22:52:55 -04:00
|
|
|
actions: {
|
2019-10-10 21:26:01 -04:00
|
|
|
stripePaymentHandler() {
|
2019-11-13 21:43:18 -05:00
|
|
|
this.set("loading", true);
|
2019-12-12 18:41:14 -05:00
|
|
|
const type = this.get("type");
|
2019-12-09 18:55:24 -05:00
|
|
|
const plan = this.get("model.plans")
|
|
|
|
.filterBy("selected")
|
|
|
|
.get("firstObject");
|
|
|
|
|
|
|
|
if (!plan) {
|
2019-12-12 18:41:14 -05:00
|
|
|
this.alert(`${type}.validate.payment_options.required`);
|
2019-12-09 18:55:24 -05:00
|
|
|
this.set("loading", false);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-13 21:43:18 -05:00
|
|
|
|
2019-12-12 18:41:14 -05:00
|
|
|
let transaction;
|
|
|
|
|
|
|
|
if (this.planTypeIsSelected) {
|
2020-01-14 02:46:48 -05:00
|
|
|
transaction = this.createSubscription(plan);
|
2019-12-12 18:41:14 -05:00
|
|
|
} else {
|
|
|
|
transaction = this.createPayment(plan);
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction
|
|
|
|
.then(result => {
|
|
|
|
if (result.error) {
|
|
|
|
bootbox.alert(result.error.message || result.error);
|
|
|
|
} else {
|
2020-01-14 04:15:54 -05:00
|
|
|
if (result.status === "incomplete") {
|
2020-01-14 02:46:48 -05:00
|
|
|
this.alert(`${type}.incomplete`);
|
2020-01-14 04:15:54 -05:00
|
|
|
} else {
|
2020-01-14 02:46:48 -05:00
|
|
|
this.alert(`${type}.success`);
|
|
|
|
}
|
2019-12-12 21:44:25 -05:00
|
|
|
|
|
|
|
const success_route = this.planTypeIsSelected
|
2019-12-15 18:47:59 -05:00
|
|
|
? "user.billing.subscriptions"
|
|
|
|
: "user.billing.payments";
|
2019-12-12 21:44:25 -05:00
|
|
|
|
2019-12-12 18:41:14 -05:00
|
|
|
this.transitionToRoute(
|
2019-12-12 21:44:25 -05:00
|
|
|
success_route,
|
2019-12-12 18:41:14 -05:00
|
|
|
Discourse.User.current().username.toLowerCase()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(result => {
|
|
|
|
bootbox.alert(result.errorThrown);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-11-13 21:43:18 -05:00
|
|
|
this.set("loading", false);
|
2019-12-12 18:41:14 -05:00
|
|
|
});
|
2019-10-09 22:52:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|