2020-05-28 10:32:57 -05:00
|
|
|
import Controller from "@ember/controller";
|
2019-12-12 12:59:26 +11:00
|
|
|
import Subscription from "discourse/plugins/discourse-subscriptions/discourse/models/subscription";
|
2020-07-24 15:07:18 -05:00
|
|
|
import Transaction from "discourse/plugins/discourse-subscriptions/discourse/models/transaction";
|
2020-05-22 11:20:05 -05:00
|
|
|
import I18n from "I18n";
|
2020-11-30 10:35:21 -06:00
|
|
|
import { not } from "@ember/object/computed";
|
2019-10-11 12:26:01 +11:00
|
|
|
|
2020-05-28 10:32:57 -05:00
|
|
|
export default Controller.extend({
|
2020-07-22 11:06:34 -05:00
|
|
|
selectedPlan: null,
|
2020-11-30 10:35:21 -06:00
|
|
|
isAnonymous: not("currentUser"),
|
2019-12-13 10:41:14 +11:00
|
|
|
|
2019-10-11 12:26:01 +11:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set(
|
|
|
|
"stripe",
|
2019-12-04 11:53:05 +11:00
|
|
|
Stripe(Discourse.SiteSettings.discourse_subscriptions_public_key)
|
2019-10-11 12:26:01 +11:00
|
|
|
);
|
|
|
|
const elements = this.get("stripe").elements();
|
2019-11-29 15:08:51 +11:00
|
|
|
|
2019-12-10 10:15:30 +11:00
|
|
|
this.set("cardElement", elements.create("card", { hidePostalCode: true }));
|
2019-10-11 12:26:01 +11:00
|
|
|
},
|
|
|
|
|
2019-12-13 10:41:14 +11:00
|
|
|
alert(path) {
|
|
|
|
bootbox.alert(I18n.t(`discourse_subscriptions.${path}`));
|
|
|
|
},
|
|
|
|
|
2020-01-14 18:46:48 +11:00
|
|
|
createSubscription(plan) {
|
2020-09-16 09:53:50 -05:00
|
|
|
return this.stripe.createToken(this.get("cardElement")).then((result) => {
|
2019-12-13 10:41:14 +11:00
|
|
|
if (result.error) {
|
2020-10-19 20:54:30 -05:00
|
|
|
this.set("loading", false);
|
2019-12-13 10:41:14 +11:00
|
|
|
return result;
|
|
|
|
} else {
|
2020-10-21 13:36:31 -05:00
|
|
|
const subscription = Subscription.create({
|
|
|
|
source: result.token.id,
|
|
|
|
plan: plan.get("id"),
|
2019-12-13 10:41:14 +11:00
|
|
|
});
|
2020-10-21 13:36:31 -05:00
|
|
|
|
|
|
|
return subscription.save();
|
2019-12-13 10:41:14 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-07-24 15:07:18 -05:00
|
|
|
handleAuthentication(plan, transaction) {
|
|
|
|
return this.stripe
|
|
|
|
.confirmCardPayment(transaction.payment_intent.client_secret)
|
2020-09-16 09:53:50 -05:00
|
|
|
.then((result) => {
|
2020-07-24 15:07:18 -05:00
|
|
|
if (
|
|
|
|
result.paymentIntent &&
|
|
|
|
result.paymentIntent.status === "succeeded"
|
|
|
|
) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
this.set("loading", false);
|
|
|
|
bootbox.alert(result.error.message || result.error);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_advanceSuccessfulTransaction(plan) {
|
|
|
|
this.alert("plans.success");
|
|
|
|
this.set("loading", false);
|
|
|
|
|
|
|
|
this.transitionToRoute(
|
|
|
|
plan.type === "recurring"
|
|
|
|
? "user.billing.subscriptions"
|
|
|
|
: "user.billing.payments",
|
|
|
|
Discourse.User.current().username.toLowerCase()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2019-10-10 13:52:55 +11:00
|
|
|
actions: {
|
2019-10-11 12:26:01 +11:00
|
|
|
stripePaymentHandler() {
|
2019-11-14 13:43:18 +11:00
|
|
|
this.set("loading", true);
|
2019-12-10 10:55:24 +11:00
|
|
|
const plan = this.get("model.plans")
|
2020-07-22 11:06:34 -05:00
|
|
|
.filterBy("id", this.selectedPlan)
|
2019-12-10 10:55:24 +11:00
|
|
|
.get("firstObject");
|
|
|
|
|
|
|
|
if (!plan) {
|
2020-07-22 11:06:34 -05:00
|
|
|
this.alert("plans.validate.payment_options.required");
|
2019-12-10 10:55:24 +11:00
|
|
|
this.set("loading", false);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-14 13:43:18 +11:00
|
|
|
|
2020-07-22 11:06:34 -05:00
|
|
|
let transaction = this.createSubscription(plan);
|
2019-12-13 10:41:14 +11:00
|
|
|
|
|
|
|
transaction
|
2020-09-16 09:53:50 -05:00
|
|
|
.then((result) => {
|
2019-12-13 10:41:14 +11:00
|
|
|
if (result.error) {
|
|
|
|
bootbox.alert(result.error.message || result.error);
|
2020-07-24 15:07:18 -05:00
|
|
|
} else if (
|
|
|
|
result.status === "incomplete" ||
|
|
|
|
result.status === "open"
|
|
|
|
) {
|
|
|
|
const transactionId = result.id;
|
|
|
|
const planId = this.selectedPlan;
|
|
|
|
this.handleAuthentication(plan, result).then(
|
2020-09-16 09:53:50 -05:00
|
|
|
(authenticationResult) => {
|
2020-07-24 15:07:18 -05:00
|
|
|
if (authenticationResult && !authenticationResult.error) {
|
|
|
|
return Transaction.finalize(transactionId, planId).then(
|
|
|
|
() => {
|
|
|
|
this._advanceSuccessfulTransaction(plan);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 10:41:14 +11:00
|
|
|
);
|
2020-07-24 15:07:18 -05:00
|
|
|
} else {
|
|
|
|
this._advanceSuccessfulTransaction(plan);
|
2019-12-13 10:41:14 +11:00
|
|
|
}
|
|
|
|
})
|
2020-09-16 09:53:50 -05:00
|
|
|
.catch((result) => {
|
2019-12-13 10:41:14 +11:00
|
|
|
bootbox.alert(result.errorThrown);
|
2019-11-14 13:43:18 +11:00
|
|
|
this.set("loading", false);
|
2019-12-13 10:41:14 +11:00
|
|
|
});
|
2020-09-16 09:53:50 -05:00
|
|
|
},
|
|
|
|
},
|
2019-10-10 13:52:55 +11:00
|
|
|
});
|