diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 2680bab..f7a8150 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -8,7 +8,11 @@ module DiscoursePatrons def index begin - plans = ::Stripe::Plan.list(active: true) + if params[:product_id].present? + plans = ::Stripe::Plan.list(active: true, product: params[:product_id]) + else + plans = ::Stripe::Plan.list(active: true) + end serialized = plans[:data].map do |plan| plan.to_h.slice(:id, :amount, :currency, :interval) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 74ca99d..a943996 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -8,14 +8,38 @@ module DiscoursePatrons def index begin - products = ::Stripe::Product.list(active: true) + response = ::Stripe::Product.list(active: true) - # TODO: Serialize. Remove some attributes like metadata - render_json_dump products.data + products = response[:data].map do |p| + serialize(p) + end + + render_json_dump products rescue ::Stripe::InvalidRequestError => e return render_json_error e.message end end + + def show + begin + product = ::Stripe::Product.retrieve(params[:id]) + + render_json_dump serialize(product) + + rescue ::Stripe::InvalidRequestError => e + return render_json_error e.message + end + end + + private + + def serialize(product) + { + id: product[:id], + name: product[:name], + description: product[:metadata][:description] + } + end end end diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index cbe50e2..b544763 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -8,14 +8,14 @@ module DiscoursePatrons def index begin - customers = ::Stripe::Customer.list( - email: current_user.email, - expand: ['data.subscriptions'] - ) + products = ::Stripe::Product.list(active: true) - subscriptions = customers[:data].map do |customer| - customer[:subscriptions][:data] - end.flatten(1) + subscriptions = products[:data].map do |p| + { + id: p[:id], + description: p.dig(:metadata, :description) + } + end render_json_dump subscriptions @@ -50,17 +50,6 @@ module DiscoursePatrons end end - def destroy - begin - subscription = ::Stripe::Subscription.delete(params[:id]) - - render_json_dump subscription - - rescue ::Stripe::InvalidRequestError => e - return render_json_error e.message - end - end - private def plan_group(plan) diff --git a/app/controllers/user/subscriptions_controller.rb b/app/controllers/user/subscriptions_controller.rb index c9a7595..7128f37 100644 --- a/app/controllers/user/subscriptions_controller.rb +++ b/app/controllers/user/subscriptions_controller.rb @@ -25,6 +25,17 @@ module DiscoursePatrons return render_json_error e.message end end + + def destroy + begin + subscription = ::Stripe::Subscription.delete(params[:id]) + + render_json_dump subscription + + rescue ::Stripe::InvalidRequestError => e + return render_json_error e.message + end + end end end end diff --git a/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 b/assets/javascripts/discourse/controllers/patrons-subscribe-show.js.es6 similarity index 91% rename from assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 rename to assets/javascripts/discourse/controllers/patrons-subscribe-show.js.es6 index 74ec8e2..a0ce61b 100644 --- a/assets/javascripts/discourse/controllers/patrons-subscribe.js.es6 +++ b/assets/javascripts/discourse/controllers/patrons-subscribe-show.js.es6 @@ -34,9 +34,9 @@ export default Ember.Controller.extend({ } subscription.save().then(() => { - bootbox.alert("ok payment good... some kind of message"); + bootbox.alert(I18n.t("discourse_patrons.transactions.payment.success")); this.transitionToRoute( - "user.billing", + "user.subscriptions", Discourse.User.current().username.toLowerCase() ); }); diff --git a/assets/javascripts/discourse/models/plan.js.es6 b/assets/javascripts/discourse/models/plan.js.es6 index f0d2956..dac6f16 100644 --- a/assets/javascripts/discourse/models/plan.js.es6 +++ b/assets/javascripts/discourse/models/plan.js.es6 @@ -20,8 +20,8 @@ const Plan = Discourse.Model.extend({ }); Plan.reopenClass({ - findAll() { - return ajax("/patrons/plans", { method: "get" }).then(result => + findAll(data) { + return ajax("/patrons/plans", { method: "get", data }).then(result => result.map(plan => Plan.create(plan)) ); } diff --git a/assets/javascripts/discourse/models/product.js.es6 b/assets/javascripts/discourse/models/product.js.es6 index 71dd5a2..1f9e490 100644 --- a/assets/javascripts/discourse/models/product.js.es6 +++ b/assets/javascripts/discourse/models/product.js.es6 @@ -7,6 +7,12 @@ Product.reopenClass({ return ajax("/patrons/products", { method: "get" }).then(result => result.map(product => Product.create(product)) ); + }, + + find(id) { + return ajax(`/patrons/products/${id}`, { method: "get" }).then( + product => Product.create(product) + ); } }); diff --git a/assets/javascripts/discourse/models/subscription.js.es6 b/assets/javascripts/discourse/models/subscription.js.es6 index 2231b25..fc4c355 100644 --- a/assets/javascripts/discourse/models/subscription.js.es6 +++ b/assets/javascripts/discourse/models/subscription.js.es6 @@ -15,22 +15,13 @@ const Subscription = Discourse.Model.extend({ }; return ajax("/patrons/subscriptions", { method: "post", data }); - }, - - destroy() { - return ajax(`/patrons/subscriptions/${this.id}`, { method: "delete" }).then( - result => Subscription.create(result) - ); } }); Subscription.reopenClass({ findAll() { return ajax("/patrons/subscriptions", { method: "get" }).then(result => - result.map(subscription => { - subscription.plan = Plan.create(subscription.plan); - return Subscription.create(subscription); - }) + result.map(subscription => Subscription.create(subscription)) ); } }); diff --git a/assets/javascripts/discourse/models/user-subscription.js.es6 b/assets/javascripts/discourse/models/user-subscription.js.es6 index 31e66d7..8782a6a 100644 --- a/assets/javascripts/discourse/models/user-subscription.js.es6 +++ b/assets/javascripts/discourse/models/user-subscription.js.es6 @@ -6,6 +6,12 @@ const UserSubscription = Discourse.Model.extend({ @computed("status") canceled(status) { return status === "canceled"; + }, + + destroy() { + return ajax(`/patrons/user/subscriptions/${this.id}`, { method: "delete" }).then( + result => UserSubscription.create(result) + ); } }); diff --git a/assets/javascripts/discourse/patrons-route-map.js.es6 b/assets/javascripts/discourse/patrons-route-map.js.es6 index 2969cda..566c954 100644 --- a/assets/javascripts/discourse/patrons-route-map.js.es6 +++ b/assets/javascripts/discourse/patrons-route-map.js.es6 @@ -1,5 +1,7 @@ export default function() { this.route("patrons", function() { - this.route("subscribe"); + this.route("subscribe", function() { + this.route("show", { path: "/:subscription-id" }); + }); }); } diff --git a/assets/javascripts/discourse/routes/patrons-subscribe-show.js.es6 b/assets/javascripts/discourse/routes/patrons-subscribe-show.js.es6 new file mode 100644 index 0000000..41f9c14 --- /dev/null +++ b/assets/javascripts/discourse/routes/patrons-subscribe-show.js.es6 @@ -0,0 +1,16 @@ +import Product from "discourse/plugins/discourse-patrons/discourse/models/product"; +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(params) { + const product_id = params["subscription-id"]; + const product = Product.find(product_id); + const subscription = Subscription.create(); + const plans = Plan.findAll({ product_id: product_id }).then(results => + results.map(p => ({ id: p.id, name: p.subscriptionRate })) + ); + + return Ember.RSVP.hash({ plans, product, subscription }); + }, +}); diff --git a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 index 336bf20..dbc5431 100644 --- a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 +++ b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 @@ -1,14 +1,7 @@ -import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan"; -import Subscription from "discourse/plugins/discourse-patrons/discourse/models/subscription"; +import Product from "discourse/plugins/discourse-patrons/discourse/models/product"; export default Discourse.Route.extend({ model() { - const plans = Plan.findAll().then(results => - results.map(p => ({ id: p.id, name: p.subscriptionRate })) - ); - - const subscription = Subscription.create(); - - return Ember.RSVP.hash({ plans, subscription }); - } + return Product.findAll(); + }, }); diff --git a/assets/javascripts/discourse/templates/patrons/subscribe.hbs b/assets/javascripts/discourse/templates/patrons/subscribe.hbs index b37fe6b..42e43d5 100644 --- a/assets/javascripts/discourse/templates/patrons/subscribe.hbs +++ b/assets/javascripts/discourse/templates/patrons/subscribe.hbs @@ -1,34 +1,12 @@ -
- {{{model.group.bio_cooked}}} -
++ {{product.description}} +
+ ++ {{model.product.description}} +
+