class Simpal::Client

Provides an API client for performing requests under an account within a particular environment.

PayPal API credentials can be obtained from the following URL:

=> https://developer.paypal.com/developer/applications

Attributes

client_id[R]

@return [String] The 'Client ID' from your PayPal account dashboard.

client_secret[R]

@return [String] The 'Client Secret' from your PayPal account dashboard.

sandbox[R]

@return [Boolean] `true` when using the sandbox API, `false` when using the production API.

Public Class Methods

new(client_id:, client_secret:, sandbox: false) click to toggle source

Create a new API client.

@parameter client_id [String] The 'Client ID' from your PayPal account dashboard. @parameter client_secret [String] The 'Client Secret' from your PayPal account dashboard. @parameter sandbox [Boolean] `true` when using the sandbox API, `false` when using the live API.

# File lib/simpal/client.rb, line 28
def initialize(client_id:, client_secret:, sandbox: false)
  @client_id = client_id
  @client_secret = client_secret
  @sandbox = sandbox
end

Public Instance Methods

connection() click to toggle source

@return [Faraday::Connection] The connection to use when executing an API request.

# File lib/simpal/client.rb, line 52
def connection
  @connection ||= Faraday.new(service_url, headers: headers) do |connection|
    connection.use Faraday::Response::RaiseError
    connection.use Simpal::Middleware::Headers
    connection.use FaradayMiddleware::EncodeJson
    connection.use FaradayMiddleware::ParseJson
    connection.use Simpal::Middleware::Authorization, self
    connection.adapter Faraday.default_adapter
  end
end
headers() click to toggle source

@return [Hash] The default headers, which should be merged into every API request.

# File lib/simpal/client.rb, line 46
def headers
  { 'User-Agent' => "Simpal/#{Simpal::VERSION}" }
end
service_url() click to toggle source

@return [String] The URL for the PayPal API service.

# File lib/simpal/client.rb, line 36
def service_url
  if sandbox
    'https://api-m.sandbox.paypal.com'
  else
    'https://api-m.paypal.com'
  end
end