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:
-4
@@ -47,10 +47,6 @@ export default Controller.extend({
|
||||
},
|
||||
|
||||
actions: {
|
||||
cancelPlan(product_id) {
|
||||
this.redirect(product_id);
|
||||
},
|
||||
|
||||
createPlan() {
|
||||
// TODO: set default group name beforehand
|
||||
if (this.get("model.plan.metadata.group_name") === undefined) {
|
||||
|
||||
@@ -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}`;
|
||||
}
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
<td>{{plan.id}}</td>
|
||||
<td>{{plan.nickname}}</td>
|
||||
<td>{{plan.interval}}</td>
|
||||
<td>{{plan.amount}}</td>
|
||||
<td>{{plan.unit_amount}}</td>
|
||||
<td class="td-right">
|
||||
{{d-button
|
||||
action=(action "editPlan" plan.id)
|
||||
|
||||
-1
@@ -80,7 +80,6 @@
|
||||
</p>
|
||||
|
||||
<div class="pull-right">
|
||||
{{d-button label="cancel" action=(action "cancelPlan" model.plan.product) icon="times"}}
|
||||
|
||||
{{#if model.plan.isNew}}
|
||||
{{d-button label="discourse_subscriptions.admin.plans.operations.create" action="createPlan" icon="plus" class="btn btn-primary"}}
|
||||
|
||||
-5
@@ -62,11 +62,6 @@
|
||||
{{#link-to "adminPlugins.discourse-subscriptions.products.show.plans.show" model.product.id plan.id class="btn no-text btn-icon"}}
|
||||
{{d-icon "far-edit"}}
|
||||
{{/link-to}}
|
||||
{{d-button
|
||||
action=(route-action "destroyPlan")
|
||||
actionParam=plan
|
||||
icon="trash-alt"
|
||||
class="btn-danger btn no-text btn-icon"}}
|
||||
</td>
|
||||
</tr>
|
||||
{{/each}}
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
}}
|
||||
<div class="interval">
|
||||
{{#if planTypeIsSelected}}
|
||||
{{i18n (concat "discourse_subscriptions.plans.interval.adverb." plan.interval)}}
|
||||
{{i18n (concat "discourse_subscriptions.plans.interval.adverb." plan.recurring.interval)}}
|
||||
{{else}}
|
||||
{{i18n "discourse_subscriptions.payment.interval"}}
|
||||
{{/if}}
|
||||
|
||||
Reference in New Issue
Block a user