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 @@
{{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'}} | 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 @@ |
---|