Class: Braintree::OAuthGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/braintree/oauth_gateway.rb

Instance Method Summary collapse

Constructor Details

#initialize(gateway) ⇒ OAuthGateway

Returns a new instance of OAuthGateway.


3
4
5
6
7
# File 'lib/braintree/oauth_gateway.rb', line 3

def initialize(gateway)
  @gateway = gateway
  @config = gateway.config
  @config.assert_has_client_credentials
end

Instance Method Details

#_create_token(params) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/braintree/oauth_gateway.rb', line 19

def _create_token(params)
  response = @config.http.post("/oauth/access_tokens", {
    :credentials => params,
  })
  if response[:credentials]
    Braintree::SuccessfulResult.new(
      :credentials => OAuthCredentials._new(response[:credentials]),
    )
  elsif response[:api_error_response]
    ErrorResult.new(@gateway, response[:api_error_response])
  else
    raise "expected :credentials or :api_error_response"
  end
end

#_sub_array_query(params, root) ⇒ Object


66
67
68
69
70
71
# File 'lib/braintree/oauth_gateway.rb', line 66

def _sub_array_query(params, root)
  sub_params = params.delete(root) || []
  sub_params.map do |value|
    ["#{root}[]", value]
  end
end

#_sub_query(params, root) ⇒ Object


59
60
61
62
63
64
# File 'lib/braintree/oauth_gateway.rb', line 59

def _sub_query(params, root)
  sub_params = params.delete(root) || {}
  sub_params.map do |key, value|
    ["#{root}[#{key}]", value]
  end
end

#connect_url(raw_params) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/braintree/oauth_gateway.rb', line 45

def connect_url(raw_params)
  params = raw_params.merge({:client_id => @config.client_id})
  user_params = _sub_query(params, :user)
  business_params = _sub_query(params, :business)
  payment_methods = _sub_array_query(params, :payment_methods)
  query = params.to_a
    .concat(user_params)
    .concat(business_params)
    .concat(payment_methods)

  query_string = query.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
  "#{@config.base_url}/oauth/connect?#{query_string}"
end

#create_token_from_code(params) ⇒ Object


9
10
11
12
# File 'lib/braintree/oauth_gateway.rb', line 9

def create_token_from_code(params)
  params[:grant_type] = "authorization_code"
  _create_token(params)
end

#create_token_from_refresh_token(params) ⇒ Object


14
15
16
17
# File 'lib/braintree/oauth_gateway.rb', line 14

def create_token_from_refresh_token(params)
  params[:grant_type] = "refresh_token"
  _create_token(params)
end

#revoke_access_token(access_token) ⇒ Object


34
35
36
37
38
39
40
41
42
43
# File 'lib/braintree/oauth_gateway.rb', line 34

def revoke_access_token(access_token)
  response = @config.http.post("/oauth/revoke_access_token", {
    :token => access_token
  })
  if response[:result][:success] == true
    Braintree::SuccessfulResult.new
  else
    ErrorResult.new(@gateway, "could not revoke access token")
  end
end