Building off the foundation of using the Prices API, this PR adds the ability to create a one-time purchase plan for any product, which then can add a user to the specified plan group.
Some things to be aware of:
One-time purchases cannot have trials.
One-time purchases use the Invoice API instead of Subscriptions. Invoices are created then charged immediately.
Users should receive emails for these invoices directly from Stripe just like subscriptions.
65 lines
1.5 KiB
JavaScript
65 lines
1.5 KiB
JavaScript
import Plan from "discourse/plugins/discourse-subscriptions/discourse/models/plan";
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
const AdminPlan = Plan.extend({
|
|
isNew: false,
|
|
name: "",
|
|
interval: "month",
|
|
unit_amount: 0,
|
|
intervals: ["day", "week", "month", "year"],
|
|
metadata: {},
|
|
|
|
@discourseComputed("trial_period_days")
|
|
parseTrialPeriodDays(trialDays) {
|
|
if (trialDays) {
|
|
return parseInt(0 + trialDays, 10);
|
|
} else {
|
|
return 0;
|
|
}
|
|
},
|
|
|
|
save() {
|
|
const data = {
|
|
nickname: this.nickname,
|
|
interval: this.interval,
|
|
amount: this.unit_amount,
|
|
currency: this.currency,
|
|
trial_period_days: this.parseTrialPeriodDays,
|
|
type: this.type,
|
|
product: this.product,
|
|
metadata: this.metadata,
|
|
active: this.active
|
|
};
|
|
|
|
return ajax("/s/admin/plans", { method: "post", data });
|
|
},
|
|
|
|
update() {
|
|
const data = {
|
|
nickname: this.nickname,
|
|
trial_period_days: this.parseTrialPeriodDays,
|
|
metadata: this.metadata,
|
|
active: this.active
|
|
};
|
|
|
|
return ajax(`/s/admin/plans/${this.id}`, { method: "patch", data });
|
|
}
|
|
});
|
|
|
|
AdminPlan.reopenClass({
|
|
findAll(data) {
|
|
return ajax("/s/admin/plans", { method: "get", data }).then(result =>
|
|
result.map(plan => AdminPlan.create(plan))
|
|
);
|
|
},
|
|
|
|
find(id) {
|
|
return ajax(`/s/admin/plans/${id}`, { method: "get" }).then(plan =>
|
|
AdminPlan.create(plan)
|
|
);
|
|
}
|
|
});
|
|
|
|
export default AdminPlan;
|