REFACTOR: Use the Prices API in place of Plans (#17)

Stripe has a newer API called Prices where you can create a price for any product and it can either be recurring or one-time. The easy part is existing Plans work with the Prices API by passing a Plan ID, but objects are returned in the slightly-different Prices API object format.

This commit is a refactor to the new API to handle the data in its new form, and lays the foundation for a one time payment plan to be added to any subscriptions product.
This commit is contained in:
Justin DiRose
2020-07-15 08:44:40 -05:00
committed by GitHub
parent 8bcb7aa93c
commit c9ff55b46a
22 changed files with 128 additions and 136 deletions
@@ -6,28 +6,24 @@ const AdminPlan = Plan.extend({
isNew: false,
name: "",
interval: "month",
amount: 0,
unit_amount: 0,
intervals: ["day", "week", "month", "year"],
metadata: {},
@discourseComputed("trial_period_days")
parseTrialPeriodDays(trial_period_days) {
if (trial_period_days) {
return parseInt(0 + trial_period_days, 10);
parseTrialPeriodDays(trialDays) {
if (trialDays) {
return parseInt(0 + trialDays, 10);
} else {
return 0;
}
},
destroy() {
return ajax(`/s/admin/plans/${this.id}`, { method: "delete" });
},
save() {
const data = {
nickname: this.nickname,
interval: this.interval,
amount: this.amount,
amount: this.unit_amount,
currency: this.currency,
trial_period_days: this.parseTrialPeriodDays,
product: this.product,
@@ -16,9 +16,7 @@ const AdminSubscription = EmberObject.extend({
@discourseComputed("metadata")
subscriptionUserPath(metadata) {
return getURL(
`/admin/users/${metadata.user_id}/${metadata.username}`
);
return getURL(`/admin/users/${metadata.user_id}/${metadata.username}`);
},
destroy() {
@@ -3,18 +3,22 @@ import discourseComputed from "discourse-common/utils/decorators";
import { ajax } from "discourse/lib/ajax";
const Plan = EmberObject.extend({
amountDollars: Ember.computed("amount", {
amountDollars: Ember.computed("unit_amount", {
get() {
return parseFloat(this.get("amount") / 100).toFixed(2);
return parseFloat(this.get("unit_amount") / 100).toFixed(2);
},
set(key, value) {
const decimal = parseFloat(value) * 100;
this.set("amount", decimal);
this.set("unit_amount", decimal);
return value;
}
}),
@discourseComputed("recurring.interval")
billingInterval(interval) {
return interval || "one-time";
},
@discourseComputed("amountDollars", "currency", "interval")
@discourseComputed("amountDollars", "currency", "billingInterval")
subscriptionRate(amountDollars, currency, interval) {
return `${amountDollars} ${currency.toUpperCase()} / ${interval}`;
}