fix up forms and requests to plan api

This commit is contained in:
Rimian Perkins 2019-10-17 20:34:26 +11:00
parent 95a23eafd1
commit b05b03e25b
17 changed files with 120 additions and 62 deletions

View File

@ -21,9 +21,10 @@ module DiscoursePatrons
def create
begin
plan = ::Stripe::Plan.create(
nickname: params[:nickname],
amount: params[:amount],
interval: params[:interval],
product: product,
product: params[:product_id],
currency: SiteSetting.discourse_patrons_currency,
)
@ -34,6 +35,17 @@ module DiscoursePatrons
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
begin
plan = ::Stripe::Plan.delete(params[:id])
@ -44,12 +56,6 @@ module DiscoursePatrons
return render_json_error e.message
end
end
private
def product
params[:product].slice(:id, :name).permit!.to_h.symbolize_keys if params[:product]
end
end
end
end

View File

@ -31,7 +31,8 @@ module DiscoursePatrons
private
def subscription_ok(subscription)
['active', 'trialing'].include?(subscription[:status])
# ['active', 'trialing'].include?(subscription[:status])
false
end
end
end

View File

@ -3,18 +3,10 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
export default Ember.Controller.extend({
actions: {
createPlan() {
let product;
if(this.get("model.plan.product_id")) {
product = this.get("model.products")
.filterBy('id', this.get("model.plan.product_id"))
.get("firstObject");
if(this.get("model.plan.product_id") === undefined) {
const productID = this.get("model.products.firstObject.id");
this.set("model.plan.product_id", productID);
}
else {
product = this.get("model.products").get("firstObject");
}
this.set("model.plan.product", product);
this.get("model.plan")
.save()

View File

@ -1,5 +1,6 @@
import DiscourseURL from "discourse/lib/url";
import { ajax } from "discourse/lib/ajax";
import Subscription from "discourse/plugins/discourse-patrons/discourse/models/subscription";
export default Ember.Controller.extend({
init() {
@ -30,27 +31,20 @@ export default Ember.Controller.extend({
method: "post",
data: customerData
}).then(customer => {
// TODO move default plan into settings
if (this.get("model.selectedPlan") === undefined) {
this.set(
"model.selectedPlan",
this.get("model.plans.firstObject")
);
const subscription = this.get("model.subscription");
subscription.set('customer', customer.id);
if (subscription.get("plan") === undefined) {
subscription.set("plan", this.get("model.plans.firstObject.id"));
}
const subscriptionData = {
customer: customer.id,
plan: this.get("model.selectedPlan")
};
return ajax("/patrons/subscriptions", {
method: "post",
data: subscriptionData
}).then(() => {
return DiscourseURL.redirectTo(
Discourse.SiteSettings
.discourse_patrons_subscription_group_landing_page
);
subscription.save().then(() => {
console.log('ok');
// return DiscourseURL.redirectTo(
// Discourse.SiteSettings
// .discourse_patrons_subscription_group_landing_page
// );
});
});
}

View File

@ -12,17 +12,12 @@ const AdminPlan = Discourse.Model.extend({
save() {
const data = {
nickname: this.nickname,
interval: this.interval,
amount: this.amount,
name: this.name,
product: {
id: this.product.id,
// name: this.product.name
}
product_id: this.product_id
};
console.log(12, data);
return ajax("/patrons/admin/plans", { method: "post", data });
}
});
@ -32,6 +27,12 @@ AdminPlan.reopenClass({
return ajax("/patrons/admin/plans", { method: "get" }).then(result =>
result.map(plan => AdminPlan.create(plan))
);
},
find(id) {
return ajax(`/patrons/admin/plans/${id}`, { method: "get" }).then(plan =>
AdminPlan.create(plan)
);
}
});

View File

@ -11,6 +11,7 @@ const AdminProduct = Discourse.Model.extend({
save() {
const data = {
name: this.name,
statement_descriptor: this.statement_descriptor,
metadata: this.metadata,
active: this.active
};
@ -21,6 +22,7 @@ const AdminProduct = Discourse.Model.extend({
update() {
const data = {
name: this.name,
statement_descriptor: this.statement_descriptor,
metadata: this.metadata,
active: this.active
};

View File

@ -5,7 +5,7 @@ const Plan = Discourse.Model.extend({});
Plan.reopenClass({
findAll() {
return ajax("/patrons/plans", { method: "get" }).then(result =>
result.plans.map(plan => Plan.create(plan))
result.map(plan => Plan.create(plan))
);
}
});

View File

@ -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;

View File

@ -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";
export default Discourse.Route.extend({
model() {
const plan = AdminPlan.create();
model(params) {
const id = params['plan-id'];
let plan;
if(id === 'new') {
plan = AdminPlan.create();
}
else {
plan = AdminPlan.find(id);
}
const products = AdminProduct.findAll();
return Ember.RSVP.hash({ plan, products });

View File

@ -1,11 +1,13 @@
import Group from "discourse/plugins/discourse-patrons/discourse/models/group";
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({
model() {
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 });
}
});

View File

@ -2,7 +2,7 @@
<table class="table discourse-patrons-admin">
<thead>
<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.amount'}}</th>
<th></th>

View File

@ -3,8 +3,11 @@
<form class="form-horizontal">
<p>
<label for="name">{{i18n 'discourse_patrons.admin.plans.plan.name'}}</label>
{{input type="text" name="name" value=model.plan.name}}
<label for="name">{{i18n 'discourse_patrons.admin.plans.plan.nickname.title'}}</label>
{{input type="text" name="name" value=model.plan.nickname}}
<div class="control-instructions">
{{i18n 'discourse_patrons.admin.plans.plan.nickname.description'}}
</div>
</p>
<p>
<label for="amount">{{i18n 'discourse_patrons.admin.plans.plan.amount'}}</label>

View File

@ -3,7 +3,7 @@
<thead>
<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.group'}}</th>
<th>{{i18n 'discourse_patrons.admin.products.product.group.title'}}</th>
<th>{{i18n 'discourse_patrons.admin.products.product.active'}}</th>
<th></th>
</thead>

View File

@ -7,8 +7,20 @@
{{input type="text" name="name" value=model.product.name}}
</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}}
<div class="control-instructions">
{{i18n 'discourse_patrons.admin.products.product.group.description'}}
</div>
</p>
<p>
<label for="interval">{{i18n 'discourse_patrons.admin.products.product.active'}}</label>

View File

@ -11,7 +11,7 @@
</p>
</div>
<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
action="stripePaymentHandler"

View File

@ -70,7 +70,12 @@ en:
product:
product_id: Product ID
name: Product Name
group: User Group
statement_descriptor:
title: Statement Descriptor
description: Extra information about a product which will appear on your customers 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
plans:
title: Plans
@ -80,9 +85,10 @@ en:
destroy:
confirm: Are you sure you want to destroy this 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
name: Plan Name
nickname: Nickname
product: Product
interval: Interval
amount: Amount

View File

@ -34,6 +34,18 @@ module DiscoursePatrons
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
it "does not delete a plan" do
::Stripe::Plan.expects(:delete).never
@ -60,6 +72,11 @@ module DiscoursePatrons
end
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
SiteSetting.stubs(:discourse_patrons_currency).returns('aud')
::Stripe::Plan.expects(:create).with(has_entry(:currency, 'aud'))
@ -77,9 +94,8 @@ module DiscoursePatrons
end
it "creates a plan with a product" do
product = { id: 'prod_ww', name: 'Walter White' }
::Stripe::Plan.expects(:create).with(has_entry(product: product))
post "/patrons/admin/plans.json", params: { product: product }
::Stripe::Plan.expects(:create).with(has_entry(product: 'prod_walterwhite'))
post "/patrons/admin/plans.json", params: { product_id: 'prod_walterwhite' }
end
end