diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 1e8a4f0968..86be4b58f0 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -23,6 +23,7 @@ module Admin helper 'spree/products' include ActionView::Helpers::TextHelper include OrderCyclesHelper + include Admin::StripeHelper def index respond_to do |format| @@ -113,6 +114,25 @@ module Admin end end + def stripe_connect + redirect_to authorize_stripe(params[:enterprise_id]) # csrf: form_authenticity_token) + end + + def stripe_connect_callback + if params["code"] + # Get the deets from Stripe + + stripe_account = StripeAccount.new(stripe_user_id: params["stripe_user_id"], stripe_publishable_key: params["stripe_publishable_key"], enterprise: enterprise) + if stripe_account.save + render json: stripe_account + else + render text: "Failed to save Stripe token", status: 500 + end + else + render text: params["error_description"], status: 500 + end + end + protected def build_resource_with_address diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index d913cb4c57..3bbfa6cce2 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -11,13 +11,13 @@ class EnterprisesController < BaseController before_filter :clean_permalink, only: :check_permalink before_filter :enable_embedded_shopfront + before_filter :set_enterprise, only: :relatives respond_to :js, only: :permalink_checker def relatives respond_to do |format| format.json do - enterprise = Enterprise.find(params[:id]) enterprises = enterprise.andand.relatives.andand.activated render(json: enterprises, each_serializer: Api::EnterpriseSerializer, @@ -39,23 +39,13 @@ class EnterprisesController < BaseController end - def stripe_connect - if params["code"] - # Get the deets from Stripe - - stripe_account = StripeAccount.new(stripe_user_id: params["stripe_user_id"], stripe_publishable_key: params["stripe_publishable_key"], enterprise: self) - if stripe_account.save - render_json stripe_account - else - render text "Failed to save Stripe token", status: 500 - end - else - render text params["error_description"], status: 500 - end - end private + def set_enterprise + enterprise = Enterprise.find(params[:id]) + end + def clean_permalink params[:permalink] = params[:permalink].parameterize end diff --git a/app/helpers/admin/stripe_helper.rb b/app/helpers/admin/stripe_helper.rb index 1ec28e7bf0..0109b7ba2b 100644 --- a/app/helpers/admin/stripe_helper.rb +++ b/app/helpers/admin/stripe_helper.rb @@ -1,19 +1,27 @@ -module StripeHelper - class << self - attr_accessor :client, :options - end - @options = { - :site => 'https://connect.stripe.com', - :authorize_url => '/oauth/authorize', - :token_url => '/oauth/token' - } - @client = OAuth2::Client.new( - ENV['STRIPE_CLIENT_ID'], - ENV['STRIPE_INSTANCE_SECRET_KEY'], - options - ) +module Admin + module StripeHelper + class << self + attr_accessor :client, :options + end + @options = { + :site => 'https://connect.stripe.com', + :authorize_url => '/oauth/authorize', + :token_url => '/oauth/token' + } + @client = OAuth2::Client.new( + ENV['STRIPE_CLIENT_ID'], + ENV['STRIPE_INSTANCE_SECRET_KEY'], + options + ) - def get_token(code, options={params: {scope: 'read_write'}}) - @client.get_token(code, options) + def get_stripe_token(code, options={params: {scope: 'read_write'}}) + StripeHelper.client.get_token(code, options) + end + + def authorize_stripe(enterprise_id, options={}) + options = options.merge({enterprise_id: enterprise_id}) + # State param will be passed back after auth + StripeHelper.client.auth_code.authorize_url(state: options) + end end end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9856c75b93..edf1fe9a7d 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -42,7 +42,7 @@ class Enterprise < ActiveRecord::Base has_many :billable_periods has_many :inventory_items has_many :tag_rules - has_many :stripe_accounts + has_one :stripe_account, dependent: :destroy delegate :latitude, :longitude, :city, :state_name, :to => :address diff --git a/app/models/stripe_account.rb b/app/models/stripe_account.rb index b97e8f632f..d84f875df0 100644 --- a/app/models/stripe_account.rb +++ b/app/models/stripe_account.rb @@ -1,5 +1,5 @@ class StripeAccount < ActiveRecord::Base belongs_to :enterprise - valdates_presence_of :stripe_user_id, :stripe_publishable_key - validates_uniqueness_of :stripe_user_id + validates_presence_of :stripe_user_id, :stripe_publishable_key + validates_uniqueness_of :stripe_user_id, :enterprise_id end diff --git a/app/views/admin/enterprises/form/_payment_methods.html.haml b/app/views/admin/enterprises/form/_payment_methods.html.haml index efcde16dc0..261fa33bcc 100644 --- a/app/views/admin/enterprises/form/_payment_methods.html.haml +++ b/app/views/admin/enterprises/form/_payment_methods.html.haml @@ -1,3 +1,5 @@ += render 'admin/enterprises/form/stripe_connect' + - if @payment_methods.count > 0 %table %thead diff --git a/config/routes.rb b/config/routes.rb index 32500d9a1b..404214f2f2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -65,7 +65,7 @@ Openfoodnetwork::Application.routes.draw do post 'embedded_shopfront/enable', to: 'application#enable_embedded_styles' post 'embedded_shopfront/disable', to: 'application#disable_embedded_styles' - get '/stripe/callback', :to => 'enterprise#stripe_connect' + get '/stripe/callback', :to => 'admin/enterprises#stripe_connect_callback' resources :enterprises do collection do @@ -100,6 +100,8 @@ Openfoodnetwork::Application.routes.draw do post :bulk_update, as: :bulk_update end + get "/stripe_connect", to: "enterprises#stripe_connect" + member do get :welcome put :register