Stripe auth and callback working. Still need to process callback.

This commit is contained in:
Steve Pettitt
2016-09-17 23:19:28 +01:00
committed by Rob Harrington
parent 3623325cab
commit 06279848c6
7 changed files with 57 additions and 35 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,3 +1,5 @@
= render 'admin/enterprises/form/stripe_connect'
- if @payment_methods.count > 0
%table
%thead

View File

@@ -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