Class: PaypalClient::Client

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

Constant Summary collapse

LIVE_URL =
'https://api.paypal.com'.freeze
SANDBOX_URL =
'https://api.sandbox.paypal.com'.freeze
VERSION =
'v1'.freeze
TOKEN_CACHE_KEY =
'paypal_oauth_token'.freeze
TOKEN_EXPIRY_MARGIN =

We don’t want to use a token that expired 1 second ago so we have them expiry 3600 seconds before they actually expire

60 * 60

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, cache:, sandbox: true, version:, logger: nil) ⇒ Client

Creates a new instance of PaypalClient::Client

Parameters:

  • client_id: (String)

    Paypal Client ID

  • client_secret: (String)

    Paypal Client Secret

  • cache: (ActiveSupport::Cache::Store)

    ActiveSupport::Cache::Store compaitable store to store auth token

  • sandbox: (Boolean) (defaults to: true)

    true <description>

  • version: (String)

    <description>

  • logger: (Logger) (defaults to: nil)

    nil <description>



49
50
51
52
53
54
55
56
# File 'lib/paypal_client.rb', line 49

def initialize(client_id:, client_secret:, cache:, sandbox: true, version:, logger: nil)
  @client_id = client_id
  @client_secret = client_secret
  @cache = cache
  @sandbox = sandbox
  @version = version
  @logger = logger
end

Class Method Details

.buildPaypalClient::Client

Build a new instance of PaypalClient based on defaults and environment variables

Returns:



30
31
32
33
34
35
36
37
38
# File 'lib/paypal_client.rb', line 30

def self.build
  @client ||= Client.new(
    client_id: ENV.fetch('PAYPAL_CLIENT_ID'),
    client_secret: ENV.fetch('PAYPAL_CLIENT_SECRET'),
    sandbox: ENV.fetch('PAYPAL_SANDBOX', true),
    cache: ActiveSupport::Cache::MemoryStore.new,
    version: VERSION
  )
end

Instance Method Details

#auth_token(force: false) ⇒ <String>

Request auth token from Paypal using the client_id and client_secret

Parameters:

  • force: (<Boolean>) (defaults to: false)

    false Forces a refresh of the token even if cached

Returns:

  • (<String>)

    Valid auth token from Paypal



124
125
126
127
128
129
130
131
# File 'lib/paypal_client.rb', line 124

def auth_token(force: false)
  return @cache.read(TOKEN_CACHE_KEY) if @cache.exist?(TOKEN_CACHE_KEY) && force == false

  auth_response = authenticate
  @cache.fetch(TOKEN_CACHE_KEY, expires_in: auth_response[:expires_in] - TOKEN_EXPIRY_MARGIN) do
    auth_response[:access_token]
  end
end

#connectionObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/paypal_client.rb', line 133

def connection
  @conn ||= Faraday.new(url: base_url) do |faraday|
    faraday.use PaypalClient::Errors::Middleware

    faraday.headers = default_headers
    faraday.response @logger if @logger
    faraday.request  :json
    faraday.response :json,
                     content_type: /\bjson$/,
                     parser_options: { symbolize_names: true }

    faraday.adapter *adapter
  end
end

#delete(path, data = {}, headers = {}) ⇒ Faraday::Response

Send a DELETE request to the Paypal API

Parameters:

  • path (String)

    Path to call on Paypal API (eg. /payments/payment)

  • data (Hash) (defaults to: {})

    Hash to send as POST data. Will be turned into JSON.

  • headers (Hash) (defaults to: {})

    Hash of custom request headers

Returns:

  • (Faraday::Response)

    ] Faraday response. Call the ‘.body` method on it to access the response data.



114
115
116
# File 'lib/paypal_client.rb', line 114

def delete(path, data = {}, headers = {})
  connection.delete(merged_path(path), data, merged_headers(headers))
end

#get(path, data = {}, headers = {}) ⇒ Faraday::Response

Send a GET request to the Paypal API

Parameters:

  • path (String)

    Path to call on Paypal API (eg. /payments/payment)

  • data (Hash) (defaults to: {})

    Hash to send as query parameters

  • headers (Hash) (defaults to: {})

    Hash of custom request headers

Returns:

  • (Faraday::Response)

    ] Faraday response. Call the ‘.body` method on it to access the response data.



66
67
68
# File 'lib/paypal_client.rb', line 66

def get(path, data = {}, headers = {})
  connection.get(merged_path(path), data, merged_headers(headers))
end

#patch(path, data = {}, headers = {}) ⇒ Faraday::Response

Send a PATCH request to the Paypal API

Parameters:

  • path (String)

    Path to call on Paypal API (eg. /payments/payment)

  • data (Hash) (defaults to: {})

    Hash to send as POST data. Will be turned into JSON.

  • headers (Hash) (defaults to: {})

    Hash of custom request headers

Returns:

  • (Faraday::Response)

    ] Faraday response. Call the ‘.body` method on it to access the response data.



102
103
104
# File 'lib/paypal_client.rb', line 102

def patch(path, data = {}, headers = {})
  connection.patch(merged_path(path), data, merged_headers(headers))
end

#post(path, data = {}, headers = {}) ⇒ Faraday::Response

Send a POST request to the Paypal API

Parameters:

  • path (String)

    Path to call on Paypal API (eg. /payments/payment)

  • data (Hash) (defaults to: {})

    Hash to send as POST data. Will be turned into JSON.

  • headers (Hash) (defaults to: {})

    Hash of custom request headers

Returns:

  • (Faraday::Response)

    ] Faraday response. Call the ‘.body` method on it to access the response data.



78
79
80
# File 'lib/paypal_client.rb', line 78

def post(path, data = {}, headers = {})
  connection.post(merged_path(path), data, merged_headers(headers))
end

#put(path, data = {}, headers = {}) ⇒ Faraday::Response

Send a PUT request to the Paypal API

Parameters:

  • path (String)

    Path to call on Paypal API (eg. /payments/payment)

  • data (Hash) (defaults to: {})

    Hash to send as POST data. Will be turned into JSON.

  • headers (Hash) (defaults to: {})

    Hash of custom request headers

Returns:

  • (Faraday::Response)

    ] Faraday response. Call the ‘.body` method on it to access the response data.



90
91
92
# File 'lib/paypal_client.rb', line 90

def put(path, data = {}, headers = {})
  connection.put(merged_path(path), data, merged_headers(headers))
end