Class: PaypalClient::Client
- Inherits:
-
Object
- Object
- PaypalClient::Client
- 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
-
.build ⇒ PaypalClient::Client
Build a new instance of PaypalClient based on defaults and environment variables.
Instance Method Summary collapse
-
#auth_token(force: false) ⇒ <String>
Request auth token from Paypal using the client_id and client_secret.
- #connection ⇒ Object
-
#delete(path, data = {}, headers = {}) ⇒ Faraday::Response
Send a DELETE request to the Paypal API.
-
#get(path, data = {}, headers = {}) ⇒ Faraday::Response
Send a GET request to the Paypal API.
-
#initialize(client_id:, client_secret:, cache:, sandbox: true, version:, logger: nil) ⇒ Client
constructor
Creates a new instance of PaypalClient::Client.
-
#patch(path, data = {}, headers = {}) ⇒ Faraday::Response
Send a PATCH request to the Paypal API.
-
#post(path, data = {}, headers = {}) ⇒ Faraday::Response
Send a POST request to the Paypal API.
-
#put(path, data = {}, headers = {}) ⇒ Faraday::Response
Send a PUT request to the Paypal API.
Constructor Details
#initialize(client_id:, client_secret:, cache:, sandbox: true, version:, logger: nil) ⇒ Client
Creates a new instance of PaypalClient::Client
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
.build ⇒ PaypalClient::Client
Build a new instance of PaypalClient based on defaults and environment variables
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
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 |
#connection ⇒ Object
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
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
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
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
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
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 |