The code in the plugin needed a dramatic cleanup. This refactor collapses the Plan/Product/Subscription controllers on the backend into one new controller: `SubscribeController`. This reduces N+1 calls to the back end during the subscription process and simplifies use of the code. I've also removed a bunch of dead code and refactored some logic into methods for easier readability. No feature/functionality changes in this commit; only refactoring. However, refactoring will allow for implementation of better anonymous user handling, so this is largely a foundation to enable making that change.
27 lines
761 B
JavaScript
27 lines
761 B
JavaScript
import EmberObject from "@ember/object";
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
|
|
const Plan = EmberObject.extend({
|
|
amountDollars: Ember.computed("unit_amount", {
|
|
get() {
|
|
return parseFloat(this.get("unit_amount") / 100).toFixed(2);
|
|
},
|
|
set(key, value) {
|
|
const decimal = parseFloat(value) * 100;
|
|
this.set("unit_amount", decimal);
|
|
return value;
|
|
},
|
|
}),
|
|
@discourseComputed("recurring.interval")
|
|
billingInterval(interval) {
|
|
return interval || "one-time";
|
|
},
|
|
|
|
@discourseComputed("amountDollars", "currency", "billingInterval")
|
|
subscriptionRate(amountDollars, currency, interval) {
|
|
return `${amountDollars} ${currency.toUpperCase()} / ${interval}`;
|
|
},
|
|
});
|
|
|
|
export default Plan;
|