diff --git a/app/controllers/admin/plans_controller.rb b/app/controllers/admin/plans_controller.rb index 89e1ec0..882ea7c 100644 --- a/app/controllers/admin/plans_controller.rb +++ b/app/controllers/admin/plans_controller.rb @@ -20,13 +20,11 @@ module DiscoursePatrons def create begin - plan = ::Stripe::Plan.create( amount: params[:amount], interval: params[:interval], - product: { name: params[:name] }, + product: product, currency: SiteSetting.discourse_patrons_currency, - id: plan_id, ) render_json_dump plan @@ -49,8 +47,8 @@ module DiscoursePatrons private - def plan_id - params[:name].parameterize.dasherize if params[:name] + def product + params[:product].slice(:id, :name).permit!.to_h.symbolize_keys if params[:product] end end end diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index cb351b5..50772c7 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -47,11 +47,10 @@ module DiscoursePatrons def update begin product = ::Stripe::Product.update( - params[:id], { - name: params[:name], - active: params[:active], - metadata: metadata - } + params[:id], + name: params[:name], + active: params[:active], + metadata: metadata ) render_json_dump product diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index bb3bc7a..e0253be 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -7,8 +7,14 @@ module DiscoursePatrons before_action :set_api_key def index - plans = ::Stripe::Plan.list - render json: plans.data + begin + plans = ::Stripe::Plan.list + + render_json_dump plans.data + + rescue ::Stripe::InvalidRequestError => e + return render_json_error e.message + end end end end diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index 521f73c..940251b 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -7,14 +7,31 @@ module DiscoursePatrons before_action :set_api_key def create - subscription = ::Stripe::Subscription.create( - customer: params[:customer], - items: [ - { plan: params[:plan] }, - ] - ) + begin + subscription = ::Stripe::Subscription.create( + customer: params[:customer], + items: [ + { plan: params[:plan] }, + ] + ) - render_json_dump subscription + if subscription_ok(subscription) + # TODO: check group credentials + group = Group.find_by_name('group-123') + group.add(current_user) + end + + render_json_dump subscription + + rescue ::Stripe::InvalidRequestError => e + return render_json_error e.message + end + end + + private + + def subscription_ok(subscription) + ['active', 'trialing'].include?(subscription[:status]) 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 ce50880..53fcb9e 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,6 +3,19 @@ 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"); + } + else { + product = this.get("model.products").get("firstObject"); + } + + this.set("model.plan.product", product); + this.get("model.plan") .save() .then(() => { diff --git a/assets/javascripts/discourse/models/admin-plan.js.es6 b/assets/javascripts/discourse/models/admin-plan.js.es6 index a11c989..d0856be 100644 --- a/assets/javascripts/discourse/models/admin-plan.js.es6 +++ b/assets/javascripts/discourse/models/admin-plan.js.es6 @@ -14,9 +14,15 @@ const AdminPlan = Discourse.Model.extend({ const data = { interval: this.interval, amount: this.amount, - name: this.name + name: this.name, + product: { + id: this.product.id, + // name: this.product.name + } }; + console.log(12, data); + return ajax("/patrons/admin/plans", { method: "post", data }); } }); diff --git a/assets/javascripts/discourse/models/admin-product.js.es6 b/assets/javascripts/discourse/models/admin-product.js.es6 index 2b8fd52..30a4891 100644 --- a/assets/javascripts/discourse/models/admin-product.js.es6 +++ b/assets/javascripts/discourse/models/admin-product.js.es6 @@ -2,6 +2,7 @@ import { ajax } from "discourse/lib/ajax"; const AdminProduct = Discourse.Model.extend({ isNew: false, + metadata: {}, destroy() { return ajax(`/patrons/admin/products/${this.id}`, { method: "delete" }); diff --git a/assets/javascripts/discourse/models/plan.js.es6 b/assets/javascripts/discourse/models/plan.js.es6 index a9fe0fa..5ce2c94 100644 --- a/assets/javascripts/discourse/models/plan.js.es6 +++ b/assets/javascripts/discourse/models/plan.js.es6 @@ -3,7 +3,7 @@ import { ajax } from "discourse/lib/ajax"; const Plan = Discourse.Model.extend({}); Plan.reopenClass({ - find() { + findAll() { return ajax("/patrons/plans", { method: "get" }).then(result => result.plans.map(plan => Plan.create(plan)) ); diff --git a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 index 1c0d1a6..0a92c27 100644 --- a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 +++ b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 @@ -4,7 +4,8 @@ import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan"; export default Discourse.Route.extend({ model() { const group = Group.find(); - const plans = Plan.find().then(results => results.map(p => p.id)); + const plans = Plan.findAll().then(results => results.map(p => p.id)); + return Ember.RSVP.hash({ group, plans }); } }); 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 afe355a..412f7ac 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 @@ -12,7 +12,7 @@

- {{combo-box valueAttribute="value" content=model.products value=defaultProduct}} + {{combo-box valueAttribute="id" content=model.products value=model.plan.product_id}}

diff --git a/spec/requests/admin/plans_controller_spec.rb b/spec/requests/admin/plans_controller_spec.rb index 2d59109..c85009e 100644 --- a/spec/requests/admin/plans_controller_spec.rb +++ b/spec/requests/admin/plans_controller_spec.rb @@ -76,14 +76,10 @@ module DiscoursePatrons post "/patrons/admin/plans.json", params: { amount: '102' } end - it "creates a plan with a title" do - ::Stripe::Plan.expects(:create).with(has_entry(:product, name: 'Rick Astley')) - post "/patrons/admin/plans.json", params: { name: 'Rick Astley' } - end - - it "creates a plan with an id" do - ::Stripe::Plan.expects(:create).with(has_entry(id: 'rick-astley')) - post "/patrons/admin/plans.json", params: { name: 'Rick Astley' } + 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 } end end diff --git a/spec/requests/admin/products_controller_spec.rb b/spec/requests/admin/products_controller_spec.rb index ba7651c..6e95f15 100644 --- a/spec/requests/admin/products_controller_spec.rb +++ b/spec/requests/admin/products_controller_spec.rb @@ -71,7 +71,7 @@ module DiscoursePatrons it 'has a metadata' do ::Stripe::Product.expects(:create).with(has_entry(metadata: { group_name: 'discourse-user-group-name' })) - post "/patrons/admin/products.json", params: { metadata: { group_name: 'discourse-user-group-name' }} + post "/patrons/admin/products.json", params: { metadata: { group_name: 'discourse-user-group-name' } } end end @@ -85,7 +85,7 @@ module DiscoursePatrons describe 'update' do it 'updates the product' do ::Stripe::Product.expects(:update) - patch "/patrons/admin/products/prod_walterwhite.json", params: { metadata: { group_name: '' }} + patch "/patrons/admin/products/prod_walterwhite.json", params: { metadata: { group_name: '' } } end end diff --git a/spec/requests/subscriptions_controller_spec.rb b/spec/requests/subscriptions_controller_spec.rb index 4b1c859..508bc07 100644 --- a/spec/requests/subscriptions_controller_spec.rb +++ b/spec/requests/subscriptions_controller_spec.rb @@ -4,21 +4,51 @@ require 'rails_helper' module DiscoursePatrons RSpec.describe SubscriptionsController do - describe "create" do + context "authenticated" do let(:user) { Fabricate(:user, email: 'hello.2@example.com') } before do sign_in(user) end - it "creates a subscription with a customer" do - ::Stripe::Subscription.expects(:create).with(has_entry(customer: 'cus_1234')) - post "/patrons/subscriptions.json", params: { customer: 'cus_1234' } + describe "create" do + it "creates a subscription with a customer" do + ::Stripe::Subscription.expects(:create).with(has_entry(customer: 'cus_1234')) + post "/patrons/subscriptions.json", params: { customer: 'cus_1234' } + end + + it "creates a subscription with a plan" do + ::Stripe::Subscription.expects(:create).with(has_entry(items: [ plan: 'plan_1234' ])) + post "/patrons/subscriptions.json", params: { plan: 'plan_1234' } + end end - it "creates a subscription with a plan" do - ::Stripe::Subscription.expects(:create).with(has_entry(items: [ plan: 'plan_1234' ])) - post "/patrons/subscriptions.json", params: { plan: 'plan_1234' } + describe "user groups" do + let(:group) { Fabricate(:group, name: 'group-123') } + + it "does not add the user to the group" do + ::Stripe::Subscription.expects(:create).returns(status: 'failed') + + expect { + post "/patrons/subscriptions.json", params: { plan: 'plan_1234' } + }.not_to change { group.users.count } + end + + it "adds the user to the group when the subscription is active" do + ::Stripe::Subscription.expects(:create).returns(status: 'active') + + expect { + post "/patrons/subscriptions.json", params: { plan: 'plan_1234' } + }.to change { group.users.count } + end + + it "adds the user to the group when the subscription is trialing" do + ::Stripe::Subscription.expects(:create).returns(status: 'trialing') + + expect { + post "/patrons/subscriptions.json", params: { plan: 'plan_1234' } + }.to change { group.users.count } + end end end end