paypal-rest-api 0.1.1 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9974530916382123b35a29697760ddeed6a7c67e53cf9b3c03edabe72d1e878e
4
- data.tar.gz: b6ab203108f40419f0ca4b94208e716d26a59c8a94487f632a40b37110d40bf4
3
+ metadata.gz: d478a936d84b05a9f4a6eb98491649648250cc49dcb3f524381404d9ded0084f
4
+ data.tar.gz: 5ec4bc0bbf68f3be84ce5cd1cd31e85f6fec5c9bc6ce795fd3b64eea902d033f
5
5
  SHA512:
6
- metadata.gz: af49071e00c4bf5bc36b5819fc4867164546133f345784a9fd29e146d97b4bddf0fbd5169e75528ad63bc7aff8a7239df2e298cde9a4a9ec4ef60ca78aefcfa9
7
- data.tar.gz: 138fb221137c2d726e7b432da2269e17e2ba42f359dae415052d8c47f8f06d729663deb3ae26214fafd96374c6f785760699db83f415aa979e41cebb3ff733da
6
+ metadata.gz: 15d2abe1866d6724c00520b43dfec0ac4b9e8383039e3580b78c182bbd184fd2499c0beaa1328500453e85868622fc0e1e49a4dbdc4f58e6401258021f04dd69
7
+ data.tar.gz: c51693b8445126a269f7d356baa4cba3ca40ffc0a19e80bc1278b69061146e723589237ac85a5940381309868f2af2e619cbdc4bc76295f0420d7606ab010d45
data/README.md CHANGED
@@ -35,7 +35,11 @@ PaypalAPI.client = PaypalAPI::Client.new(
35
35
  )
36
36
 
37
37
  # in your business logic
38
- response = PaypalAPI::Orders.show(order_id)
38
+ PaypalAPI.live? # => false
39
+ PaypalAPI.api_url # => "https://api-m.sandbox.paypal.com"
40
+ PaypalAPI.web_url # => "https://sandbox.paypal.com"
41
+
42
+ response = between redeploys::Orders.show(order_id)
39
43
  response = PaypalAPI::Orders.create(body: body)
40
44
  ```
41
45
 
@@ -49,6 +53,10 @@ client = PaypalAPI::Client.new(
49
53
  live: false
50
54
  )
51
55
 
56
+ client.live? # => false
57
+ client.api_url # => "https://api-m.sandbox.paypal.com"
58
+ client.web_url # => "https://sandbox.paypal.com"
59
+
52
60
  response = client.orders.show(order_id)
53
61
  response = client.orders.create(body: body)
54
62
  ```
@@ -91,7 +99,12 @@ response.request # Request that generates this response
91
99
 
92
100
  ## Configuration options
93
101
 
94
- PaypalAPI client accepts this additional options: `:live`, `:retries`, `:http_opts`
102
+ PaypalAPI client accepts this additional options:
103
+
104
+ - `:live`
105
+ - `:retries`,
106
+ - `:http_opts`
107
+ - `:cache`
95
108
 
96
109
  ### Option `:live`
97
110
 
@@ -108,8 +121,8 @@ client = PaypalAPI::Client.new(
108
121
  ### Option `:retries`
109
122
 
110
123
  This is a Hash with retries configuration.
111
- By default retries are enabled, 3 retries with 0.25, 0.75, 1.5 seconds delay.
112
- Default config: `{enabled: true, count: 3, sleep: [0.25, 0.75, 1.5]}`.
124
+ By default retries are enabled, 4 retries with 0, 0.25, 0.75, 1.5 seconds delay.
125
+ Default config: `{enabled: true, count: 4, sleep: [0, 0.25, 0.75, 1.5]}`.
113
126
  New options are merged with defaults.
114
127
  Please keep `sleep` array same size as `count`.
115
128
 
@@ -139,6 +152,22 @@ client = PaypalAPI::Client.new(
139
152
  )
140
153
  ```
141
154
 
155
+ ### Option `:cache`
156
+
157
+ This option can be added to save certificates to between redeploys to validate
158
+ webhooks offline. By default this gem has only in-memory caching.
159
+ Cache object must response to standard caching `#fetch(key, &block)` method.
160
+
161
+ By default it is `nil`, so downloaded certificates will be downloaded again after
162
+ redeploys.
163
+
164
+ ```ruby
165
+ client = PaypalAPI::Client.new(
166
+ cache: Rails.cache
167
+ # ...
168
+ )
169
+ ```
170
+
142
171
  ## Webhoooks verification
143
172
 
144
173
  Webhooks can be verified [offline](https://developer.paypal.com/api/rest/webhooks/rest/#link-selfverificationmethod)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.1
@@ -43,7 +43,7 @@ module PaypalAPI
43
43
 
44
44
  default_headers = {
45
45
  "content-type" => "application/x-www-form-urlencoded",
46
- "authorization" => "Basic #{["#{client.config.client_id}:#{client.config.client_secret}"].pack("m0")}"
46
+ "authorization" => "Basic #{["#{client.env.client_id}:#{client.env.client_secret}"].pack("m0")}"
47
47
  }
48
48
 
49
49
  client.post(PATH, query: query, body: body, headers: merge_headers!(default_headers, headers))
@@ -5,7 +5,18 @@ module PaypalAPI
5
5
  # PaypalAPI Client
6
6
  #
7
7
  class Client
8
- attr_reader :config, :callbacks
8
+ # Paypal environment
9
+ attr_reader :env
10
+
11
+ # Gem Configuration
12
+ #
13
+ # @return [Config] Gem Configuration
14
+ attr_reader :config
15
+
16
+ # Registered callbacks
17
+ #
18
+ # @return [Hash] Registered callbacks
19
+ attr_reader :callbacks
9
20
 
10
21
  # Initializes Client
11
22
  # @api public
@@ -18,15 +29,9 @@ module PaypalAPI
18
29
  #
19
30
  # @return [Client] Initialized client
20
31
  #
21
- def initialize(client_id:, client_secret:, live: nil, http_opts: nil, retries: nil, cache: nil)
22
- @config = PaypalAPI::Config.new(
23
- client_id: client_id,
24
- client_secret: client_secret,
25
- live: live,
26
- http_opts: http_opts,
27
- retries: retries,
28
- cache: cache
29
- )
32
+ def initialize(client_id:, client_secret:, live: false, http_opts: nil, retries: nil, cache: nil)
33
+ @env = PaypalAPI::Environment.new(client_id: client_id, client_secret: client_secret, live: live)
34
+ @config = PaypalAPI::Config.new(http_opts: http_opts, retries: retries, cache: cache)
30
35
 
31
36
  @callbacks = {
32
37
  before: [],
@@ -38,10 +43,34 @@ module PaypalAPI
38
43
  @access_token = nil
39
44
  end
40
45
 
46
+ # Checks if PayPal LIVE environment enabled
47
+ # @return [Boolean] Checks if PayPal LIVE environment enabled
48
+ def live?
49
+ env.live?
50
+ end
51
+
52
+ # Checks if PayPal SANDBOX environment enabled
53
+ # @return [Boolean] Checks if PayPal SANDBOX environment enabled
54
+ def sandbox?
55
+ env.sandbox?
56
+ end
57
+
58
+ # Base API URL
59
+ # @return [String] Base API URL
60
+ def api_url
61
+ env.api_url
62
+ end
63
+
64
+ # Base WEB URL
65
+ # @return [String] Base WEB URL
66
+ def web_url
67
+ env.web_url
68
+ end
69
+
41
70
  # Registers callback
42
71
  #
43
72
  # @param callback_name [Symbol] Callback name.
44
- # Allowed values: :before, :after_success, :after_faile, :after_network_error
73
+ # Allowed values: :before, :after_success, :after_fail, :after_network_error
45
74
  #
46
75
  # @param block [Proc] Block that must be call
47
76
  # For `:before` callback proc should accept 2 params -
@@ -89,6 +118,7 @@ module PaypalAPI
89
118
 
90
119
  #
91
120
  # Verifies Webhook
121
+ #
92
122
  # It requires one-time request to download and cache certificate.
93
123
  # If local verification returns false it tries to verify webhook online.
94
124
  #
@@ -98,7 +128,7 @@ module PaypalAPI
98
128
  # class Webhooks::PaypalController < ApplicationController
99
129
  # def create
100
130
  # webhook_id = ENV['PAYPAL_WEBHOOK_ID'] # PayPal registered webhook ID for current URL
101
- # headers = request.headers # must be a Hash
131
+ # headers = request.headers # must respond to #[] to get headers
102
132
  # body = request.raw_post # must be a raw String body
103
133
  #
104
134
  # webhook_is_valid = PaypalAPI.verify_webhook(webhook_id: webhook_id, headers: headers, body: body)
@@ -2,71 +2,32 @@
2
2
 
3
3
  module PaypalAPI
4
4
  #
5
- # Stores configuration for PaypalAPI Client
5
+ # Stores client requests configuration
6
6
  #
7
7
  class Config
8
- # Live PayPal URL
9
- LIVE_URL = "https://api-m.paypal.com"
10
-
11
- # Sandbox PayPal URL
12
- SANDBOX_URL = "https://api-m.sandbox.paypal.com"
13
-
14
8
  # Default config options
15
9
  DEFAULTS = {
16
- live: false,
17
10
  http_opts: {}.freeze,
18
- retries: {enabled: true, count: 3, sleep: [0.25, 0.75, 1.5].freeze}.freeze
11
+ retries: {enabled: true, count: 4, sleep: [0, 0.25, 0.75, 1.5].freeze}.freeze
19
12
  }.freeze
20
13
 
21
- attr_reader :client_id, :client_secret, :live, :http_opts, :retries, :certs_cache
14
+ attr_reader :http_opts, :retries, :certs_cache
22
15
 
23
16
  # Initializes Config
24
17
  #
25
- # @param client_id [String] PayPal client id
26
- # @param client_secret [String] PayPal client secret
27
- # @param live [Boolean] PayPal live/sandbox mode
28
18
  # @param http_opts [Hash] Net::Http opts for all requests
29
19
  # @param retries [Hash] Retries configuration
30
20
  # @param cache [#read, nil] Application cache to store certificates to validate webhook events locally.
31
- # Must respond to #read(key) and #write(key, expires_in: Integer)
21
+ # Must respond to #fetch(key, &block)
32
22
  #
33
23
  # @return [Client] Initialized config object
34
24
  #
35
- def initialize(client_id:, client_secret:, live: nil, http_opts: nil, retries: nil, cache: nil)
36
- @client_id = client_id
37
- @client_secret = client_secret
38
- @live = with_default(:live, live)
39
- @http_opts = with_default(:http_opts, http_opts)
40
- @retries = with_default(:retries, retries)
25
+ def initialize(http_opts: nil, retries: {}, cache: nil)
26
+ @http_opts = http_opts || DEFAULTS[:http_opts]
27
+ @retries = DEFAULTS[:retries].merge(retries || {})
41
28
  @certs_cache = WebhookVerifier::CertsCache.new(cache)
42
29
 
43
30
  freeze
44
31
  end
45
-
46
- # @return [String] PayPal live or sandbox URL
47
- def url
48
- live ? LIVE_URL : SANDBOX_URL
49
- end
50
-
51
- #
52
- # Instance representation string. Default was overwritten to hide secrets
53
- #
54
- def inspect
55
- "#<#{self.class.name} live: #{live}>"
56
- end
57
-
58
- alias_method :to_s, :inspect
59
-
60
- private
61
-
62
- def with_default(option_name, value)
63
- default = DEFAULTS.fetch(option_name)
64
-
65
- case value
66
- when NilClass then default
67
- when Hash then default.merge(value)
68
- else value
69
- end
70
- end
71
32
  end
72
33
  end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PaypalAPI
4
+ # Sandbox API PayPal URL
5
+ SANDBOX_API_URL = "https://api-m.sandbox.paypal.com"
6
+
7
+ # Live API PayPal URL
8
+ LIVE_API_URL = "https://api-m.paypal.com"
9
+
10
+ # Sandbox PayPal Web URL
11
+ SANDBOX_WEB_URL = "https://sandbox.paypal.com"
12
+
13
+ # Live PayPal Web URL
14
+ LIVE_WEB_URL = "https://paypal.com"
15
+
16
+ #
17
+ # PayPal environment info
18
+ #
19
+ class Environment
20
+ # PayPal client_id
21
+ attr_accessor :client_id
22
+
23
+ # PayPal client_secret
24
+ attr_reader :client_secret
25
+
26
+ # PayPal API base URL
27
+ attr_reader :api_url
28
+
29
+ # PayPal web URL
30
+ attr_reader :web_url
31
+
32
+ # Initializes Environment
33
+ #
34
+ # @param client_id [String] PayPal client id
35
+ # @param client_secret [String] PayPal client secret
36
+ # @param live [Boolean] PayPal live/sandbox mode
37
+ #
38
+ # @return [Client] Initialized config object
39
+ #
40
+ def initialize(client_id:, client_secret:, live: false)
41
+ @live = live || false
42
+ @api_url = live ? LIVE_API_URL : SANDBOX_API_URL
43
+ @web_url = live ? LIVE_WEB_URL : SANDBOX_WEB_URL
44
+ @client_id = client_id
45
+ @client_secret = client_secret
46
+ freeze
47
+ end
48
+
49
+ #
50
+ # Instance representation string. Default was overwritten to hide secrets
51
+ # @return [String]
52
+ #
53
+ def inspect
54
+ "#<#{self.class.name} live: #{@live}>"
55
+ end
56
+
57
+ alias_method :to_s, :inspect
58
+
59
+ #
60
+ # Checks if live environment enabled
61
+ # @return [Boolean]
62
+ #
63
+ def live?
64
+ @live
65
+ end
66
+
67
+ #
68
+ # Checks if sandbox environment enabled
69
+ # @return [Boolean]
70
+ #
71
+ def sandbox?
72
+ !live?
73
+ end
74
+ end
75
+ end
@@ -107,7 +107,7 @@ module PaypalAPI
107
107
  end
108
108
 
109
109
  def build_http_uri(path, query)
110
- uri = URI.join(client.config.url, path)
110
+ uri = URI.join(client.env.api_url, path)
111
111
  uri.query = URI.encode_www_form(query) if query && !query.empty?
112
112
  uri
113
113
  end
data/lib/paypal-api.rb CHANGED
@@ -18,6 +18,30 @@ module PaypalAPI
18
18
  # @return [Client] PaypalAPI client
19
19
  attr_writer :client
20
20
 
21
+ # Checks if PayPal LIVE environment enabled
22
+ # @return [Boolean] Checks if PayPal LIVE environment enabled
23
+ def live?
24
+ client.live?
25
+ end
26
+
27
+ # Checks if PayPal SANDBOX environment enabled
28
+ # @return [Boolean] Checks if PayPal SANDBOX environment enabled
29
+ def sandbox?
30
+ client.sandbox?
31
+ end
32
+
33
+ # Base API URL
34
+ # @return [String] Base API URL
35
+ def api_url
36
+ client.api_url
37
+ end
38
+
39
+ # Base WEB URL
40
+ # @return [String] Base WEB URL
41
+ def web_url
42
+ client.web_url
43
+ end
44
+
21
45
  # @!macro [new] request
22
46
  #
23
47
  # @api public
@@ -210,6 +234,7 @@ end
210
234
 
211
235
  require_relative "paypal-api/access_token"
212
236
  require_relative "paypal-api/api_collection"
237
+ require_relative "paypal-api/environment"
213
238
  require_relative "paypal-api/client"
214
239
  require_relative "paypal-api/config"
215
240
  require_relative "paypal-api/error"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paypal-rest-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Glushkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-01 00:00:00.000000000 Z
11
+ date: 2024-09-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: PayPal REST API with no dependencies.
14
14
  email:
@@ -45,6 +45,7 @@ files:
45
45
  - lib/paypal-api/api_collections/webhooks.rb
46
46
  - lib/paypal-api/client.rb
47
47
  - lib/paypal-api/config.rb
48
+ - lib/paypal-api/environment.rb
48
49
  - lib/paypal-api/error.rb
49
50
  - lib/paypal-api/failed_request_error_builder.rb
50
51
  - lib/paypal-api/network_error_builder.rb