diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index 6a63ff0..1bdf1d3 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -38,7 +38,9 @@ module DiscoursePatrons group.add(current_user) end - DiscoursePatrons::Customer.create(user_id: current_user.id, customer_id: params[:customer]) + unless DiscoursePatrons::Customer.exists?(user_id: current_user.id) + DiscoursePatrons::Customer.create(user_id: current_user.id, customer_id: params[:customer]) + end render_json_dump @subscription diff --git a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 index 08d06eb..ad50074 100644 --- a/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 +++ b/assets/javascripts/discourse/routes/patrons-subscribe.js.es6 @@ -13,8 +13,9 @@ export default Discourse.Route.extend({ }; const plans = Plan.findAll().then(results => - results.map(p => planSelectText(p)) + results.map(p => ({ id: p.id, name: planSelectText(p) })) ); + const subscription = Subscription.create(); return Ember.RSVP.hash({ plans, subscription }); diff --git a/config/routes.rb b/config/routes.rb index 5ba8fa3..af17e39 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,4 +20,5 @@ DiscoursePatrons::Engine.routes.draw do resources :subscriptions, only: [:index, :create] get '/' => 'patrons#index' + get '/subscribe' => 'patrons#index' end diff --git a/spec/requests/subscriptions_controller_spec.rb b/spec/requests/subscriptions_controller_spec.rb index de6c82a..6fa5e80 100644 --- a/spec/requests/subscriptions_controller_spec.rb +++ b/spec/requests/subscriptions_controller_spec.rb @@ -51,6 +51,15 @@ module DiscoursePatrons post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' } }.to change { DiscoursePatrons::Customer.count } end + + it "does not create a customer id one existeth" do + ::Stripe::Plan.expects(:retrieve).returns(metadata: {}) + ::Stripe::Subscription.expects(:create).returns(status: 'active') + DiscoursePatrons::Customer.create(user_id: user.id, customer_id: 'cus_1234') + + DiscoursePatrons::Customer.expects(:create).never + post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' } + end end describe "user groups" do