diff --git a/app/controllers/admin/plans_controller.rb b/app/controllers/admin/plans_controller.rb index 882ea7c..086ac77 100644 --- a/app/controllers/admin/plans_controller.rb +++ b/app/controllers/admin/plans_controller.rb @@ -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 diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index 940251b..f7989a4 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -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 diff --git a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-show.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-show.js.es6 index 53fcb9e..0f4b396 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-plans-show.js.es6 @@ -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() diff --git a/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 b/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 index bd22b73..4c0a798 100644 --- a/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 +++ b/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 @@ -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 + // ); }); }); } diff --git a/assets/javascripts/discourse/models/admin-plan.js.es6 b/assets/javascripts/discourse/models/admin-plan.js.es6 index d0856be..e759e8a 100644 --- a/assets/javascripts/discourse/models/admin-plan.js.es6 +++ b/assets/javascripts/discourse/models/admin-plan.js.es6 @@ -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) + ); } }); diff --git a/assets/javascripts/discourse/models/admin-product.js.es6 b/assets/javascripts/discourse/models/admin-product.js.es6 index 30a4891..2a6b358 100644 --- a/assets/javascripts/discourse/models/admin-product.js.es6 +++ b/assets/javascripts/discourse/models/admin-product.js.es6 @@ -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 }; diff --git a/assets/javascripts/discourse/models/plan.js.es6 b/assets/javascripts/discourse/models/plan.js.es6 index 5ce2c94..df5138d 100644 --- a/assets/javascripts/discourse/models/plan.js.es6 +++ b/assets/javascripts/discourse/models/plan.js.es6 @@ -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)) ); } }); diff --git a/assets/javascripts/discourse/models/subscription.js.es6 b/assets/javascripts/discourse/models/subscription.js.es6 new file mode 100644 index 0000000..c57d215 --- /dev/null +++ b/assets/javascripts/discourse/models/subscription.js.es6 @@ -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; diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-show.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-show.js.es6 index 7b49b78..00ac3dd 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-show.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-plans-show.js.es6 @@ -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 }); diff --git a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 index 0a92c27..0cda325 100644 --- a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 +++ b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 @@ -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 }); } }); diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-index.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-index.hbs index 58a6dce..b89639d 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-index.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-index.hbs @@ -2,7 +2,7 @@ - + diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-show.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-show.hbs index 412f7ac..cb14999 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-show.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-plans-show.hbs @@ -3,8 +3,11 @@

- - {{input type="text" name="name" value=model.plan.name}} + + {{input type="text" name="name" value=model.plan.nickname}} +

+ {{i18n 'discourse_patrons.admin.plans.plan.nickname.description'}} +

diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs index 6c60ae6..e3e0c18 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs @@ -3,7 +3,7 @@

- + diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-show.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-show.hbs index 4fe5ded..5bcb086 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-show.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-show.hbs @@ -7,8 +7,20 @@ {{input type="text" name="name" value=model.product.name}}

- + + {{input type="text" name="statement_descriptor" value=model.product.statement_descriptor}} +

+ {{i18n 'discourse_patrons.admin.products.product.statement_descriptor.description'}} +
+

+

+ {{combo-box valueAttribute="name" content=model.groups value=model.product.metadata.group_name}} +

+ {{i18n 'discourse_patrons.admin.products.product.group.description'}} +

diff --git a/assets/javascripts/discourse/templates/patrons/subscribe.hbs b/assets/javascripts/discourse/templates/patrons/subscribe.hbs index f3ab6be..b37fe6b 100644 --- a/assets/javascripts/discourse/templates/patrons/subscribe.hbs +++ b/assets/javascripts/discourse/templates/patrons/subscribe.hbs @@ -11,7 +11,7 @@

- {{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" diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 4131379..61d29e6 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -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 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 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 diff --git a/spec/requests/admin/plans_controller_spec.rb b/spec/requests/admin/plans_controller_spec.rb index c85009e..89fe072 100644 --- a/spec/requests/admin/plans_controller_spec.rb +++ b/spec/requests/admin/plans_controller_spec.rb @@ -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
{{i18n 'discourse_patrons.admin.plans.plan.plan_id'}}{{i18n 'discourse_patrons.admin.plans.plan.nickname'}}{{i18n 'discourse_patrons.admin.plans.plan.nickname.title'}} {{i18n 'discourse_patrons.admin.plans.plan.interval'}} {{i18n 'discourse_patrons.admin.plans.plan.amount'}}
{{i18n 'discourse_patrons.admin.products.product.product_id'}} {{i18n 'discourse_patrons.admin.products.product.name'}}{{i18n 'discourse_patrons.admin.products.product.group'}}{{i18n 'discourse_patrons.admin.products.product.group.title'}} {{i18n 'discourse_patrons.admin.products.product.active'}}