diff --git a/app/helpers/admin/stripe_helper.rb b/app/helpers/admin/stripe_helper.rb index babcaec14f..2e54158b83 100644 --- a/app/helpers/admin/stripe_helper.rb +++ b/app/helpers/admin/stripe_helper.rb @@ -1,13 +1,19 @@ +require File.join(Rails.root, '/lib/oauth2/strategy/deauthorize') +require File.join(Rails.root, '/lib/oauth2/client') +require 'oauth2' module Admin module StripeHelper class << self attr_accessor :client, :options end + @options = { :site => 'https://connect.stripe.com', :authorize_url => '/oauth/authorize', + :deauthorize_url => '/oauth/deauthorize', :token_url => '/oauth/token' } + @client = OAuth2::Client.new( ENV['STRIPE_CLIENT_ID'], ENV['STRIPE_INSTANCE_SECRET_KEY'], @@ -23,5 +29,12 @@ module Admin # State param will be passed back after auth StripeHelper.client.auth_code.authorize_url(state: options) end + + def deauthorize_stripe(account_id) + stripe_account = StripeAccount.find(account_id) + if stripe_account + response = StripeHelper.client.deauthorize(stripe_account.stripe_user_id).deauthorize_request + end + end end end diff --git a/lib/oauth2/client.rb b/lib/oauth2/client.rb new file mode 100644 index 0000000000..6790a1f425 --- /dev/null +++ b/lib/oauth2/client.rb @@ -0,0 +1,21 @@ +require 'oauth2' +OAuth2::Client.class_eval do + def deauthorize_url(params = nil) + connection.build_url(options[:deauthorize_url]).to_s + end + + def deauthorize(account) + client_object = self.dup + client_object.options[:stripe_user_id] = account + @deauthorize ||= OAuth2::Strategy::Deauthorize.new(client_object) + end + + def deauthorize_request(params) + headers = params.delete(:headers) + opts = {} + opts[:body] = params + opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'} + opts[:headers].merge!(headers) if headers + request(:post, deauthorize_url, opts) + end +end diff --git a/lib/oauth2/strategy/deauthorize.rb b/lib/oauth2/strategy/deauthorize.rb new file mode 100644 index 0000000000..5d4970e4e2 --- /dev/null +++ b/lib/oauth2/strategy/deauthorize.rb @@ -0,0 +1,24 @@ +module OAuth2 + module Strategy + # Deauthorization Strategy -- for Stripe + class Deauthorize < Base + # The required query parameters for the authorize URL + # + def deauthorize_params(params = {}) + params.merge({ 'client_id' => @client.id, + 'stripe_user_id' => @client.options[:stripe_user_id] + }) + end + + def deauthorize_url(params = {}) + @client.deauthorize_url(deauthorize_params.merge(params)) + end + + def deauthorize_request(params = {}) + params = params.merge(deauthorize_params).merge(client_params) + @client.deauthorize_request(params) + end + + end + end +end