discourse-subscriptions/assets/javascripts/discourse/controllers/subscribe-show.js

134 lines
3.7 KiB
JavaScript
Raw Normal View History

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