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

196 lines
5.6 KiB
JavaScript
Raw Normal View History

2024-01-16 17:51:44 +01:00
/* global Stripe */
import Controller from "@ember/controller";
2024-11-29 15:42:50 +00:00
import { action } from "@ember/object";
2024-01-16 17:51:44 +01:00
import { not } from "@ember/object/computed";
2024-11-19 10:31:14 +00:00
import { service } from "@ember/service";
2024-01-16 17:51:44 +01:00
import discourseComputed from "discourse-common/utils/decorators";
import I18n from "I18n";
2019-12-12 12:59:26 +11:00
import Subscription from "discourse/plugins/discourse-subscriptions/discourse/models/subscription";
import Transaction from "discourse/plugins/discourse-subscriptions/discourse/models/transaction";
2019-10-10 13:52:55 +11:00
2024-11-29 15:42:50 +00:00
export default class SubscribeShowController extends Controller {
@service dialog;
@service router;
selectedPlan = null;
promoCode = null;
cardholderName = null;
cardholderAddress = {
line1: null,
city: null,
state: null,
country: null,
postalCode: null,
2024-11-29 15:42:50 +00:00
};
@not("currentUser") isAnonymous;
isCountryUS = false;
isCountryCA = false;
init() {
2024-11-29 15:42:50 +00:00
super.init(...arguments);
this.set(
"stripe",
2021-06-02 13:15:03 -05:00
Stripe(this.siteSettings.discourse_subscriptions_public_key)
);
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 }));
this.set("isCountryUS", this.cardholderAddress.country === "US");
this.set("isCountryCA", this.cardholderAddress.country === "CA");
2024-11-29 15:42:50 +00:00
}
alert(path) {
2022-10-17 14:42:40 -04:00
this.dialog.alert(I18n.t(`discourse_subscriptions.${path}`));
2024-11-29 15:42:50 +00:00
}
@discourseComputed("model.product.repurchaseable", "model.product.subscribed")
canPurchase(repurchaseable, subscribed) {
if (!repurchaseable && subscribed) {
return false;
}
return true;
2024-11-29 15:42:50 +00:00
}
2020-01-14 18:46:48 +11:00
createSubscription(plan) {
return this.stripe
.createToken(this.get("cardElement"), {
name: this.cardholderName, // Recommended by Stripe
address_line1: this.cardholderAddress.line1 ?? "",
address_city: this.cardholderAddress.city ?? "",
address_state: this.cardholderAddress.state ?? "",
address_zip: this.cardholderAddress.postalCode ?? "",
address_country: this.cardholderAddress.country, // Recommended by Stripe
})
.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,
cardholderName: this.cardholderName,
cardholderAddress: this.cardholderAddress,
});
2020-10-21 13:36:31 -05:00
return subscription.save();
}
});
2024-11-29 15:42:50 +00:00
}
handleAuthentication(plan, transaction) {
return this.stripe
.confirmCardPayment(transaction.payment_intent.client_secret)
2020-09-16 09:53:50 -05: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;
}
});
2024-11-29 15:42:50 +00:00
}
_advanceSuccessfulTransaction(plan) {
this.alert("plans.success");
this.set("loading", false);
this.router.transitionTo(
plan.type === "recurring"
? "user.billing.subscriptions"
: "user.billing.payments",
2021-06-02 13:15:03 -05:00
this.currentUser.username.toLowerCase()
);
2024-11-29 15:42:50 +00:00
}
2024-11-29 15:42:50 +00:00
@action
changeCountry(country) {
this.set("cardholderAddress.country", country);
this.set("isCountryUS", country === "US");
this.set("isCountryCA", country === "CA");
}
2024-11-29 15:42:50 +00:00
@action
changeState(stateOrProvince) {
this.set("cardholderAddress.state", stateOrProvince);
}
2024-11-29 15:42:50 +00:00
@action
stripePaymentHandler() {
this.set("loading", true);
const plan = this.get("model.plans")
.filterBy("id", this.selectedPlan)
.get("firstObject");
const cardholderAddress = this.cardholderAddress;
const cardholderName = this.cardholderName;
2019-12-10 10:55:24 +11:00
2024-11-29 15:42:50 +00:00
if (!plan) {
this.alert("plans.validate.payment_options.required");
this.set("loading", false);
return;
}
2019-11-14 13:43:18 +11:00
2024-11-29 15:42:50 +00:00
if (!cardholderName) {
this.alert("subscribe.invalid_cardholder_name");
this.set("loading", false);
return;
}
2024-11-29 15:42:50 +00:00
if (!cardholderAddress.country) {
this.alert("subscribe.invalid_cardholder_country");
this.set("loading", false);
return;
}
2024-11-29 15:42:50 +00:00
if (cardholderAddress.country === "US" && !cardholderAddress.state) {
this.alert("subscribe.invalid_cardholder_state");
this.set("loading", false);
return;
}
2024-11-29 15:42:50 +00:00
if (cardholderAddress.country === "CA" && !cardholderAddress.state) {
this.alert("subscribe.invalid_cardholder_province");
this.set("loading", false);
return;
}
2024-11-29 15:42:50 +00:00
let transaction = this.createSubscription(plan);
2024-11-29 15:42:50 +00:00
transaction
.then((result) => {
if (result.error) {
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(
(authenticationResult) => {
if (authenticationResult && !authenticationResult.error) {
return Transaction.finalize(transactionId, planId).then(() => {
this._advanceSuccessfulTransaction(plan);
});
}
2024-11-29 15:42:50 +00:00
}
);
2024-11-29 15:42:50 +00:00
} else {
this._advanceSuccessfulTransaction(plan);
}
})
.catch((result) => {
this.dialog.alert(
result.jqXHR.responseJSON.errors[0] || result.errorThrown
);
this.set("loading", false);
});
}
}