Class: PayPal::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/pay_pal/authentication.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, sandbox:, adapter: Faraday.default_adapter) ⇒ Authentication

Returns a new instance of Authentication.



6
7
8
9
10
11
# File 'lib/pay_pal/authentication.rb', line 6

def initialize(client_id:, client_secret:, sandbox:, adapter: Faraday.default_adapter)
  @client_id = client_id
  @client_secret = client_secret
  @sandbox = sandbox
  @adapter = adapter
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



4
5
6
# File 'lib/pay_pal/authentication.rb', line 4

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



4
5
6
# File 'lib/pay_pal/authentication.rb', line 4

def client_secret
  @client_secret
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



4
5
6
# File 'lib/pay_pal/authentication.rb', line 4

def sandbox
  @sandbox
end

Instance Method Details

#get_tokenObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pay_pal/authentication.rb', line 13

def get_token
  if @sandbox
    url = "https://api-m.sandbox.paypal.com"
  else
    url = "https://api-m.paypal.com"
  end

  request_helper = Faraday.new(url: url) do |conn|
    conn.request :authorization, :basic, client_id, client_secret
  end

  response = request_helper.post "/v1/oauth2/token?grant_type=client_credentials"

  body = JSON.parse(response.body)
  AccessToken.new(body)
end