2020-05-28 11:32:57 -04:00
|
|
|
import Controller from "@ember/controller";
|
2019-12-11 20:59:26 -05:00
|
|
|
import Subscription from "discourse/plugins/discourse-subscriptions/discourse/models/subscription";
|
2020-07-24 16:07:18 -04:00
|
|
|
import Transaction from "discourse/plugins/discourse-subscriptions/discourse/models/transaction";
|
2020-05-22 12:20:05 -04:00
|
|
|
import I18n from "I18n";
|
2020-11-30 11:35:21 -05:00
|
|
|
import { not } from "@ember/object/computed";
|
2021-01-31 15:17:44 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2022-10-17 14:42:40 -04:00
|
|
|
import { inject as service } from "@ember/service";
|
2019-10-10 21:26:01 -04:00
|
|
|
|
2020-05-28 11:32:57 -04:00
|
|
|
export default Controller.extend({
|
2022-10-17 14:42:40 -04:00
|
|
|
dialog: service(),
|
2020-07-22 12:06:34 -04:00
|
|
|
selectedPlan: null,
|
2021-01-07 16:25:44 -05:00
|
|
|
promoCode: null,
|
2020-11-30 11:35:21 -05:00
|
|
|
isAnonymous: not("currentUser"),
|
2019-12-12 18:41:14 -05:00
|
|
|
|
2019-10-10 21:26:01 -04:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set(
|
|
|
|
"stripe",
|
2021-06-02 14:15:03 -04:00
|
|
|
Stripe(this.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) {
|
2022-10-17 14:42:40 -04:00
|
|
|
this.dialog.alert(I18n.t(`discourse_subscriptions.${path}`));
|
2019-12-12 18:41:14 -05:00
|
|
|
},
|
|
|
|
|
2021-01-31 15:17:44 -05:00
|
|
|
@discourseComputed("model.product.repurchaseable", "model.product.subscribed")
|
|
|
|
canPurchase(repurchaseable, subscribed) {
|
|
|
|
if (!repurchaseable && subscribed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2020-01-14 02:46:48 -05:00
|
|
|
createSubscription(plan) {
|
2020-09-16 10:53:50 -04:00
|
|
|
return this.stripe.createToken(this.get("cardElement")).then((result) => {
|
2019-12-12 18:41:14 -05:00
|
|
|
if (result.error) {
|
2020-10-19 21:54:30 -04:00
|
|
|
this.set("loading", false);
|
2019-12-12 18:41:14 -05:00
|
|
|
return result;
|
|
|
|
} else {
|
2020-10-21 14:36:31 -04:00
|
|
|
const subscription = Subscription.create({
|
|
|
|
source: result.token.id,
|
|
|
|
plan: plan.get("id"),
|
2021-01-07 16:25:44 -05:00
|
|
|
promo: this.promoCode,
|
2019-12-12 18:41:14 -05:00
|
|
|
});
|
2020-10-21 14:36:31 -04:00
|
|
|
|
|
|
|
return subscription.save();
|
2019-12-12 18:41:14 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-07-24 16:07:18 -04:00
|
|
|
handleAuthentication(plan, transaction) {
|
|
|
|
return this.stripe
|
|
|
|
.confirmCardPayment(transaction.payment_intent.client_secret)
|
2020-09-16 10:53:50 -04:00
|
|
|
.then((result) => {
|
2020-07-24 16:07:18 -04:00
|
|
|
if (
|
|
|
|
result.paymentIntent &&
|
|
|
|
result.paymentIntent.status === "succeeded"
|
|
|
|
) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
this.set("loading", false);
|
2022-10-17 14:42:40 -04:00
|
|
|
this.dialog.alert(result.error.message || result.error);
|
2020-07-24 16:07:18 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_advanceSuccessfulTransaction(plan) {
|
|
|
|
this.alert("plans.success");
|
|
|
|
this.set("loading", false);
|
|
|
|
|
|
|
|
this.transitionToRoute(
|
|
|
|
plan.type === "recurring"
|
|
|
|
? "user.billing.subscriptions"
|
|
|
|
: "user.billing.payments",
|
2021-06-02 14:15:03 -04:00
|
|
|
this.currentUser.username.toLowerCase()
|
2020-07-24 16:07:18 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
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-09 18:55:24 -05:00
|
|
|
const plan = this.get("model.plans")
|
2020-07-22 12:06:34 -04:00
|
|
|
.filterBy("id", this.selectedPlan)
|
2019-12-09 18:55:24 -05:00
|
|
|
.get("firstObject");
|
|
|
|
|
|
|
|
if (!plan) {
|
2020-07-22 12:06:34 -04:00
|
|
|
this.alert("plans.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
|
|
|
|
2020-07-22 12:06:34 -04:00
|
|
|
let transaction = this.createSubscription(plan);
|
2019-12-12 18:41:14 -05:00
|
|
|
|
|
|
|
transaction
|
2020-09-16 10:53:50 -04:00
|
|
|
.then((result) => {
|
2019-12-12 18:41:14 -05:00
|
|
|
if (result.error) {
|
2022-10-17 14:42:40 -04:00
|
|
|
this.dialog.alert(result.error.message || result.error);
|
2020-07-24 16:07:18 -04: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 10:53:50 -04:00
|
|
|
(authenticationResult) => {
|
2020-07-24 16:07:18 -04:00
|
|
|
if (authenticationResult && !authenticationResult.error) {
|
|
|
|
return Transaction.finalize(transactionId, planId).then(
|
|
|
|
() => {
|
|
|
|
this._advanceSuccessfulTransaction(plan);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 18:41:14 -05:00
|
|
|
);
|
2020-07-24 16:07:18 -04:00
|
|
|
} else {
|
|
|
|
this._advanceSuccessfulTransaction(plan);
|
2019-12-12 18:41:14 -05:00
|
|
|
}
|
|
|
|
})
|
2020-09-16 10:53:50 -04:00
|
|
|
.catch((result) => {
|
2022-10-17 14:42:40 -04:00
|
|
|
this.dialog.alert(
|
2021-06-16 13:54:54 -04:00
|
|
|
result.jqXHR.responseJSON.errors[0] || result.errorThrown
|
|
|
|
);
|
2019-11-13 21:43:18 -05:00
|
|
|
this.set("loading", false);
|
2019-12-12 18:41:14 -05:00
|
|
|
});
|
2020-09-16 10:53:50 -04:00
|
|
|
},
|
|
|
|
},
|
2019-10-09 22:52:55 -04:00
|
|
|
});
|