Sha256: 531ef8fa61f88ee31287261f20852b05b090c043ec80e1ae7337d3d53579d434

Contents?: true

Size: 1.92 KB

Versions: 81

Compression:

Stored size: 1.92 KB

Contents

module Effective
  module Providers
    module StripeConnect
      extend ActiveSupport::Concern

      included do
        prepend_before_filter :set_stripe_connect_state_params, :only => [:stripe_connect_redirect_uri]
      end

      # So this is the postback after Stripe does its oAuth authentication
      def stripe_connect_redirect_uri
        if params[:code].present?
          token_params = request_access_token(params[:code]) # We got a code, so now we make a curl request for the access_token
          customer = Effective::Customer.for_user(current_user)

          if token_params['access_token'].present? && customer.present?
            if customer.update_attributes(:stripe_connect_access_token => token_params['access_token'])
              flash[:success] = 'Successfully Connected with Stripe Connect'
            else
              flash[:danger] = "Unable to update customer: #{customer.errors[:base].first}"
            end
          else
            flash[:danger] = "Error when connecting to Stripe /oauth/token: #{token_params[:error]}.  Please try again."
          end
        else
          flash[:danger] = "Error when connecting to Stripe /oauth/authorize: #{params[:error]}.  Please try again."
        end

        redirect_to URI.parse(@stripe_state_params['redirect_to']).path rescue effective_orders.orders_path
      end

      private

      def request_access_token(code)
        stripe_response = `curl -F client_secret='#{EffectiveOrders.stripe[:secret_key]}' -F code='#{code}' -F grant_type='authorization_code' #{EffectiveStripeHelper::STRIPE_CONNECT_TOKEN_URL}`
        JSON.parse(stripe_response) rescue {}
      end

      def set_stripe_connect_state_params
        @stripe_state_params = (JSON.parse(params[:state]) rescue {})
        @stripe_state_params = {} unless @stripe_state_params.kind_of?(Hash)

        params[:authenticity_token] = @stripe_state_params['form_authenticity_token']
      end
    end
  end
end

Version data entries

81 entries across 81 versions & 1 rubygems

Version Path
effective_orders-1.4.8 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.4.7 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.4.6 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.4.5 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.4.4 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.4.3 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.4.2 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.4.1 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.4.0 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.13 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.12 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.11 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.10 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.9 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.8 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.7 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.6 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.5 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.4 app/controllers/effective/providers/stripe_connect.rb
effective_orders-1.3.3 app/controllers/effective/providers/stripe_connect.rb