mirror of
https://github.com/discourse/discourse-subscriptions.git
synced 2025-02-07 20:28:09 +00:00
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.
85 lines
2.1 KiB
JavaScript
85 lines
2.1 KiB
JavaScript
import discourseComputed from "discourse-common/utils/decorators";
|
|
import DiscourseURL from "discourse/lib/url";
|
|
import Controller from "@ember/controller";
|
|
|
|
const RECURRING = "recurring";
|
|
const ONE_TIME = "one_time";
|
|
|
|
export default Controller.extend({
|
|
// Also defined in settings.
|
|
selectedCurrency: Ember.computed.alias("model.plan.currency"),
|
|
selectedInterval: Ember.computed.alias("model.plan.interval"),
|
|
|
|
@discourseComputed
|
|
currencies() {
|
|
return [
|
|
{ id: "AUD", name: "AUD" },
|
|
{ id: "CAD", name: "CAD" },
|
|
{ id: "EUR", name: "EUR" },
|
|
{ id: "GBP", name: "GBP" },
|
|
{ id: "USD", name: "USD" },
|
|
{ id: "INR", name: "INR" },
|
|
{ id: "BRL", name: "BRL" }
|
|
];
|
|
},
|
|
|
|
@discourseComputed
|
|
availableIntervals() {
|
|
return [
|
|
{ id: "day", name: "day" },
|
|
{ id: "week", name: "week" },
|
|
{ id: "month", name: "month" },
|
|
{ id: "year", name: "year" }
|
|
];
|
|
},
|
|
|
|
@discourseComputed("model.plan.isNew")
|
|
planFieldDisabled(isNew) {
|
|
return !isNew;
|
|
},
|
|
|
|
@discourseComputed("model.product.id")
|
|
productId(id) {
|
|
return id;
|
|
},
|
|
|
|
redirect(product_id) {
|
|
DiscourseURL.redirectTo(
|
|
`/admin/plugins/discourse-subscriptions/products/${product_id}`
|
|
);
|
|
},
|
|
|
|
actions: {
|
|
changeRecurring() {
|
|
const recurring = this.get("model.plan.isRecurring");
|
|
this.set("model.plan.type", recurring ? ONE_TIME : RECURRING);
|
|
this.set("model.plan.isRecurring", !recurring);
|
|
},
|
|
|
|
createPlan() {
|
|
// TODO: set default group name beforehand
|
|
if (this.get("model.plan.metadata.group_name") === undefined) {
|
|
this.set("model.plan.metadata", {
|
|
group_name: this.get("model.groups.firstObject.name")
|
|
});
|
|
}
|
|
|
|
this.get("model.plan")
|
|
.save()
|
|
.then(() => this.redirect(this.productId))
|
|
.catch(data =>
|
|
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))
|
|
);
|
|
},
|
|
|
|
updatePlan() {
|
|
this.get("model.plan")
|
|
.update()
|
|
.then(() => this.redirect(this.productId))
|
|
.catch(data =>
|
|
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))
|
|
);
|
|
}
|
|
}
|
|
});
|