fix up forms and requests to plan api
This commit is contained in:
parent
95a23eafd1
commit
b05b03e25b
|
@ -21,9 +21,10 @@ module DiscoursePatrons
|
||||||
def create
|
def create
|
||||||
begin
|
begin
|
||||||
plan = ::Stripe::Plan.create(
|
plan = ::Stripe::Plan.create(
|
||||||
|
nickname: params[:nickname],
|
||||||
amount: params[:amount],
|
amount: params[:amount],
|
||||||
interval: params[:interval],
|
interval: params[:interval],
|
||||||
product: product,
|
product: params[:product_id],
|
||||||
currency: SiteSetting.discourse_patrons_currency,
|
currency: SiteSetting.discourse_patrons_currency,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,6 +35,17 @@ module DiscoursePatrons
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
begin
|
||||||
|
plan = ::Stripe::Plan.retrieve(params[:id])
|
||||||
|
|
||||||
|
render_json_dump plan
|
||||||
|
|
||||||
|
rescue ::Stripe::InvalidRequestError => e
|
||||||
|
return render_json_error e.message
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
begin
|
begin
|
||||||
plan = ::Stripe::Plan.delete(params[:id])
|
plan = ::Stripe::Plan.delete(params[:id])
|
||||||
|
@ -44,12 +56,6 @@ module DiscoursePatrons
|
||||||
return render_json_error e.message
|
return render_json_error e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def product
|
|
||||||
params[:product].slice(:id, :name).permit!.to_h.symbolize_keys if params[:product]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,7 +31,8 @@ module DiscoursePatrons
|
||||||
private
|
private
|
||||||
|
|
||||||
def subscription_ok(subscription)
|
def subscription_ok(subscription)
|
||||||
['active', 'trialing'].include?(subscription[:status])
|
# ['active', 'trialing'].include?(subscription[:status])
|
||||||
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,18 +3,10 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
|
||||||
export default Ember.Controller.extend({
|
export default Ember.Controller.extend({
|
||||||
actions: {
|
actions: {
|
||||||
createPlan() {
|
createPlan() {
|
||||||
let product;
|
if(this.get("model.plan.product_id") === undefined) {
|
||||||
|
const productID = this.get("model.products.firstObject.id");
|
||||||
if(this.get("model.plan.product_id")) {
|
this.set("model.plan.product_id", productID);
|
||||||
product = this.get("model.products")
|
|
||||||
.filterBy('id', this.get("model.plan.product_id"))
|
|
||||||
.get("firstObject");
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
product = this.get("model.products").get("firstObject");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.set("model.plan.product", product);
|
|
||||||
|
|
||||||
this.get("model.plan")
|
this.get("model.plan")
|
||||||
.save()
|
.save()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import DiscourseURL from "discourse/lib/url";
|
import DiscourseURL from "discourse/lib/url";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
|
import Subscription from "discourse/plugins/discourse-patrons/discourse/models/subscription";
|
||||||
|
|
||||||
export default Ember.Controller.extend({
|
export default Ember.Controller.extend({
|
||||||
init() {
|
init() {
|
||||||
|
@ -30,27 +31,20 @@ export default Ember.Controller.extend({
|
||||||
method: "post",
|
method: "post",
|
||||||
data: customerData
|
data: customerData
|
||||||
}).then(customer => {
|
}).then(customer => {
|
||||||
// TODO move default plan into settings
|
const subscription = this.get("model.subscription");
|
||||||
if (this.get("model.selectedPlan") === undefined) {
|
|
||||||
this.set(
|
subscription.set('customer', customer.id);
|
||||||
"model.selectedPlan",
|
|
||||||
this.get("model.plans.firstObject")
|
if (subscription.get("plan") === undefined) {
|
||||||
);
|
subscription.set("plan", this.get("model.plans.firstObject.id"));
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscriptionData = {
|
subscription.save().then(() => {
|
||||||
customer: customer.id,
|
console.log('ok');
|
||||||
plan: this.get("model.selectedPlan")
|
// return DiscourseURL.redirectTo(
|
||||||
};
|
// Discourse.SiteSettings
|
||||||
|
// .discourse_patrons_subscription_group_landing_page
|
||||||
return ajax("/patrons/subscriptions", {
|
// );
|
||||||
method: "post",
|
|
||||||
data: subscriptionData
|
|
||||||
}).then(() => {
|
|
||||||
return DiscourseURL.redirectTo(
|
|
||||||
Discourse.SiteSettings
|
|
||||||
.discourse_patrons_subscription_group_landing_page
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,17 +12,12 @@ const AdminPlan = Discourse.Model.extend({
|
||||||
|
|
||||||
save() {
|
save() {
|
||||||
const data = {
|
const data = {
|
||||||
|
nickname: this.nickname,
|
||||||
interval: this.interval,
|
interval: this.interval,
|
||||||
amount: this.amount,
|
amount: this.amount,
|
||||||
name: this.name,
|
product_id: this.product_id
|
||||||
product: {
|
|
||||||
id: this.product.id,
|
|
||||||
// name: this.product.name
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(12, data);
|
|
||||||
|
|
||||||
return ajax("/patrons/admin/plans", { method: "post", data });
|
return ajax("/patrons/admin/plans", { method: "post", data });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -32,6 +27,12 @@ AdminPlan.reopenClass({
|
||||||
return ajax("/patrons/admin/plans", { method: "get" }).then(result =>
|
return ajax("/patrons/admin/plans", { method: "get" }).then(result =>
|
||||||
result.map(plan => AdminPlan.create(plan))
|
result.map(plan => AdminPlan.create(plan))
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
find(id) {
|
||||||
|
return ajax(`/patrons/admin/plans/${id}`, { method: "get" }).then(plan =>
|
||||||
|
AdminPlan.create(plan)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ const AdminProduct = Discourse.Model.extend({
|
||||||
save() {
|
save() {
|
||||||
const data = {
|
const data = {
|
||||||
name: this.name,
|
name: this.name,
|
||||||
|
statement_descriptor: this.statement_descriptor,
|
||||||
metadata: this.metadata,
|
metadata: this.metadata,
|
||||||
active: this.active
|
active: this.active
|
||||||
};
|
};
|
||||||
|
@ -21,6 +22,7 @@ const AdminProduct = Discourse.Model.extend({
|
||||||
update() {
|
update() {
|
||||||
const data = {
|
const data = {
|
||||||
name: this.name,
|
name: this.name,
|
||||||
|
statement_descriptor: this.statement_descriptor,
|
||||||
metadata: this.metadata,
|
metadata: this.metadata,
|
||||||
active: this.active
|
active: this.active
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@ const Plan = Discourse.Model.extend({});
|
||||||
Plan.reopenClass({
|
Plan.reopenClass({
|
||||||
findAll() {
|
findAll() {
|
||||||
return ajax("/patrons/plans", { method: "get" }).then(result =>
|
return ajax("/patrons/plans", { method: "get" }).then(result =>
|
||||||
result.plans.map(plan => Plan.create(plan))
|
result.map(plan => Plan.create(plan))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { ajax } from "discourse/lib/ajax";
|
||||||
|
|
||||||
|
const Subscription = Discourse.Model.extend({
|
||||||
|
save() {
|
||||||
|
const data = {
|
||||||
|
customer: this.customer,
|
||||||
|
plan: this.plan
|
||||||
|
};
|
||||||
|
|
||||||
|
return ajax("/patrons/subscriptions", { method: "post", data });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default Subscription;
|
|
@ -2,8 +2,17 @@ import AdminPlan from "discourse/plugins/discourse-patrons/discourse/models/admi
|
||||||
import AdminProduct from "discourse/plugins/discourse-patrons/discourse/models/admin-product";
|
import AdminProduct from "discourse/plugins/discourse-patrons/discourse/models/admin-product";
|
||||||
|
|
||||||
export default Discourse.Route.extend({
|
export default Discourse.Route.extend({
|
||||||
model() {
|
model(params) {
|
||||||
const plan = AdminPlan.create();
|
const id = params['plan-id'];
|
||||||
|
let plan;
|
||||||
|
|
||||||
|
if(id === 'new') {
|
||||||
|
plan = AdminPlan.create();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
plan = AdminPlan.find(id);
|
||||||
|
}
|
||||||
|
|
||||||
const products = AdminProduct.findAll();
|
const products = AdminProduct.findAll();
|
||||||
|
|
||||||
return Ember.RSVP.hash({ plan, products });
|
return Ember.RSVP.hash({ plan, products });
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
import Group from "discourse/plugins/discourse-patrons/discourse/models/group";
|
import Group from "discourse/plugins/discourse-patrons/discourse/models/group";
|
||||||
import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan";
|
import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan";
|
||||||
|
import Subscription from "discourse/plugins/discourse-patrons/discourse/models/subscription";
|
||||||
|
|
||||||
export default Discourse.Route.extend({
|
export default Discourse.Route.extend({
|
||||||
model() {
|
model() {
|
||||||
const group = Group.find();
|
const group = Group.find();
|
||||||
const plans = Plan.findAll().then(results => results.map(p => p.id));
|
const plans = Plan.findAll().then(results => results.map(p => ({ id: p.id, name: p.nickname })));
|
||||||
|
const subscription = Subscription.create();
|
||||||
|
|
||||||
return Ember.RSVP.hash({ group, plans });
|
return Ember.RSVP.hash({ group, plans, subscription });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<table class="table discourse-patrons-admin">
|
<table class="table discourse-patrons-admin">
|
||||||
<thead>
|
<thead>
|
||||||
<th>{{i18n 'discourse_patrons.admin.plans.plan.plan_id'}}</th>
|
<th>{{i18n 'discourse_patrons.admin.plans.plan.plan_id'}}</th>
|
||||||
<th>{{i18n 'discourse_patrons.admin.plans.plan.nickname'}}</th>
|
<th>{{i18n 'discourse_patrons.admin.plans.plan.nickname.title'}}</th>
|
||||||
<th>{{i18n 'discourse_patrons.admin.plans.plan.interval'}}</th>
|
<th>{{i18n 'discourse_patrons.admin.plans.plan.interval'}}</th>
|
||||||
<th>{{i18n 'discourse_patrons.admin.plans.plan.amount'}}</th>
|
<th>{{i18n 'discourse_patrons.admin.plans.plan.amount'}}</th>
|
||||||
<th></th>
|
<th></th>
|
||||||
|
|
|
@ -3,8 +3,11 @@
|
||||||
|
|
||||||
<form class="form-horizontal">
|
<form class="form-horizontal">
|
||||||
<p>
|
<p>
|
||||||
<label for="name">{{i18n 'discourse_patrons.admin.plans.plan.name'}}</label>
|
<label for="name">{{i18n 'discourse_patrons.admin.plans.plan.nickname.title'}}</label>
|
||||||
{{input type="text" name="name" value=model.plan.name}}
|
{{input type="text" name="name" value=model.plan.nickname}}
|
||||||
|
<div class="control-instructions">
|
||||||
|
{{i18n 'discourse_patrons.admin.plans.plan.nickname.description'}}
|
||||||
|
</div>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label for="amount">{{i18n 'discourse_patrons.admin.plans.plan.amount'}}</label>
|
<label for="amount">{{i18n 'discourse_patrons.admin.plans.plan.amount'}}</label>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<thead>
|
<thead>
|
||||||
<th>{{i18n 'discourse_patrons.admin.products.product.product_id'}}</th>
|
<th>{{i18n 'discourse_patrons.admin.products.product.product_id'}}</th>
|
||||||
<th>{{i18n 'discourse_patrons.admin.products.product.name'}}</th>
|
<th>{{i18n 'discourse_patrons.admin.products.product.name'}}</th>
|
||||||
<th>{{i18n 'discourse_patrons.admin.products.product.group'}}</th>
|
<th>{{i18n 'discourse_patrons.admin.products.product.group.title'}}</th>
|
||||||
<th>{{i18n 'discourse_patrons.admin.products.product.active'}}</th>
|
<th>{{i18n 'discourse_patrons.admin.products.product.active'}}</th>
|
||||||
<th></th>
|
<th></th>
|
||||||
</thead>
|
</thead>
|
||||||
|
|
|
@ -7,8 +7,20 @@
|
||||||
{{input type="text" name="name" value=model.product.name}}
|
{{input type="text" name="name" value=model.product.name}}
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label for="interval">{{i18n 'discourse_patrons.admin.products.product.group'}}</label>
|
<label for="statement_descriptor">
|
||||||
|
{{i18n 'discourse_patrons.admin.products.product.statement_descriptor.title'}}
|
||||||
|
</label>
|
||||||
|
{{input type="text" name="statement_descriptor" value=model.product.statement_descriptor}}
|
||||||
|
<div class="control-instructions">
|
||||||
|
{{i18n 'discourse_patrons.admin.products.product.statement_descriptor.description'}}
|
||||||
|
</div>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<label for="interval">{{i18n 'discourse_patrons.admin.products.product.group.title'}}</label>
|
||||||
{{combo-box valueAttribute="name" content=model.groups value=model.product.metadata.group_name}}
|
{{combo-box valueAttribute="name" content=model.groups value=model.product.metadata.group_name}}
|
||||||
|
<div class="control-instructions">
|
||||||
|
{{i18n 'discourse_patrons.admin.products.product.group.description'}}
|
||||||
|
</div>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label for="interval">{{i18n 'discourse_patrons.admin.products.product.active'}}</label>
|
<label for="interval">{{i18n 'discourse_patrons.admin.products.product.active'}}</label>
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section-column">
|
<div class="section-column">
|
||||||
{{combo-box valueAttribute="id" content=model.plans value=model.selectedPlan}}
|
{{combo-box valueAttribute="id" content=model.plans value=model.subscription.plan}}
|
||||||
|
|
||||||
{{#d-button
|
{{#d-button
|
||||||
action="stripePaymentHandler"
|
action="stripePaymentHandler"
|
||||||
|
|
|
@ -70,7 +70,12 @@ en:
|
||||||
product:
|
product:
|
||||||
product_id: Product ID
|
product_id: Product ID
|
||||||
name: Product Name
|
name: Product Name
|
||||||
group: User Group
|
statement_descriptor:
|
||||||
|
title: Statement Descriptor
|
||||||
|
description: Extra information about a product which will appear on your customer’s credit card statement.
|
||||||
|
group:
|
||||||
|
title: User Group
|
||||||
|
description: This is the discourse user group the customer gets added to when the subscription is created.
|
||||||
active: Active
|
active: Active
|
||||||
plans:
|
plans:
|
||||||
title: Plans
|
title: Plans
|
||||||
|
@ -80,9 +85,10 @@ en:
|
||||||
destroy:
|
destroy:
|
||||||
confirm: Are you sure you want to destroy this plan?
|
confirm: Are you sure you want to destroy this plan?
|
||||||
plan:
|
plan:
|
||||||
|
nickname:
|
||||||
|
title: Plan Nickname
|
||||||
|
description: This won't be visible to customers, but will help you find this plan later.
|
||||||
plan_id: Plan ID
|
plan_id: Plan ID
|
||||||
name: Plan Name
|
|
||||||
nickname: Nickname
|
|
||||||
product: Product
|
product: Product
|
||||||
interval: Interval
|
interval: Interval
|
||||||
amount: Amount
|
amount: Amount
|
||||||
|
|
|
@ -34,6 +34,18 @@ module DiscoursePatrons
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "show" do
|
||||||
|
it "does not show the plan" do
|
||||||
|
::Stripe::Plan.expects(:retrieve).never
|
||||||
|
get "/patrons/admin/plans/plan_12345.json"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is not ok" do
|
||||||
|
get "/patrons/admin/plans/plan_12345.json"
|
||||||
|
expect(response.status).to eq 403
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "delete" do
|
describe "delete" do
|
||||||
it "does not delete a plan" do
|
it "does not delete a plan" do
|
||||||
::Stripe::Plan.expects(:delete).never
|
::Stripe::Plan.expects(:delete).never
|
||||||
|
@ -60,6 +72,11 @@ module DiscoursePatrons
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "create" do
|
describe "create" do
|
||||||
|
it "creates a plan with a nickname" do
|
||||||
|
::Stripe::Plan.expects(:create).with(has_entry(:nickname, 'Veg'))
|
||||||
|
post "/patrons/admin/plans.json", params: { nickname: 'Veg' }
|
||||||
|
end
|
||||||
|
|
||||||
it "creates a plan with a currency" do
|
it "creates a plan with a currency" do
|
||||||
SiteSetting.stubs(:discourse_patrons_currency).returns('aud')
|
SiteSetting.stubs(:discourse_patrons_currency).returns('aud')
|
||||||
::Stripe::Plan.expects(:create).with(has_entry(:currency, 'aud'))
|
::Stripe::Plan.expects(:create).with(has_entry(:currency, 'aud'))
|
||||||
|
@ -77,9 +94,8 @@ module DiscoursePatrons
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a plan with a product" do
|
it "creates a plan with a product" do
|
||||||
product = { id: 'prod_ww', name: 'Walter White' }
|
::Stripe::Plan.expects(:create).with(has_entry(product: 'prod_walterwhite'))
|
||||||
::Stripe::Plan.expects(:create).with(has_entry(product: product))
|
post "/patrons/admin/plans.json", params: { product_id: 'prod_walterwhite' }
|
||||||
post "/patrons/admin/plans.json", params: { product: product }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue