paypal-rest-api 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +28 -4
- data/VERSION +1 -1
- data/lib/paypal-api/api_collections/authentication.rb +1 -1
- data/lib/paypal-api/client.rb +42 -12
- data/lib/paypal-api/config.rb +7 -46
- data/lib/paypal-api/environment.rb +75 -0
- data/lib/paypal-api/request.rb +1 -1
- data/lib/paypal-api.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e1ec764b9c93fe1fb6fe6f49e2fca6d5e0280a1182b0c4c4f54bee84c43b98d
|
4
|
+
data.tar.gz: c03728c11831a20270e38f111165747634298a3ca4d8bf97a26de9da9baa53b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26ca09bb472c049aa9a5cfa81485e11180dc5d1d19776e960beebb6f9db485b9d58ddda18f232943affb1b3d4c27e55ec3c82b4a55be92f16be6ea5c2f836b7e
|
7
|
+
data.tar.gz: f082f9263cffbc5a7a0561557284685eed6c2c2c67204d8c89271e82dedc007b63e3e616e49fc5a86925986df7c52b6afd5e053061785b2909d703c0017db4e7
|
data/README.md
CHANGED
@@ -35,7 +35,11 @@ PaypalAPI.client = PaypalAPI::Client.new(
|
|
35
35
|
)
|
36
36
|
|
37
37
|
# in your business logic
|
38
|
-
|
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,7 @@ 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: `:live`, `:retries`, `:http_opts`, `:cache`
|
95
103
|
|
96
104
|
### Option `:live`
|
97
105
|
|
@@ -108,8 +116,8 @@ client = PaypalAPI::Client.new(
|
|
108
116
|
### Option `:retries`
|
109
117
|
|
110
118
|
This is a Hash with retries configuration.
|
111
|
-
By default retries are enabled,
|
112
|
-
Default config: `{enabled: true, count:
|
119
|
+
By default retries are enabled, 4 retries with 0, 0.25, 0.75, 1.5 seconds delay.
|
120
|
+
Default config: `{enabled: true, count: 4, sleep: [0, 0.25, 0.75, 1.5]}`.
|
113
121
|
New options are merged with defaults.
|
114
122
|
Please keep `sleep` array same size as `count`.
|
115
123
|
|
@@ -139,6 +147,22 @@ client = PaypalAPI::Client.new(
|
|
139
147
|
)
|
140
148
|
```
|
141
149
|
|
150
|
+
### Option `:cache`
|
151
|
+
|
152
|
+
This option can be added to save certificates to between redeploys to validate
|
153
|
+
webhooks offline. By default this gem has only in-memory caching.
|
154
|
+
Cache object must response to standard caching `#fetch(key, &block)` method.
|
155
|
+
|
156
|
+
By default it is `nil`, so downloaded certificates will be downloaded again after
|
157
|
+
redeploys.
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
client = PaypalAPI::Client.new(
|
161
|
+
cache: Rails.cache
|
162
|
+
# ...
|
163
|
+
)
|
164
|
+
```
|
165
|
+
|
142
166
|
## Webhoooks verification
|
143
167
|
|
144
168
|
Webhooks can be verified [offline](https://developer.paypal.com/api/rest/webhooks/rest/#link-selfverificationmethod)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -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.
|
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))
|
data/lib/paypal-api/client.rb
CHANGED
@@ -5,7 +5,18 @@ module PaypalAPI
|
|
5
5
|
# PaypalAPI Client
|
6
6
|
#
|
7
7
|
class Client
|
8
|
-
|
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:
|
22
|
-
@
|
23
|
-
|
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, :
|
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
|
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)
|
data/lib/paypal-api/config.rb
CHANGED
@@ -2,71 +2,32 @@
|
|
2
2
|
|
3
3
|
module PaypalAPI
|
4
4
|
#
|
5
|
-
# Stores
|
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:
|
11
|
+
retries: {enabled: true, count: 4, sleep: [0, 0.25, 0.75, 1.5].freeze}.freeze
|
19
12
|
}.freeze
|
20
13
|
|
21
|
-
attr_reader :
|
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 #
|
21
|
+
# Must respond to #fetch(key, &block)
|
32
22
|
#
|
33
23
|
# @return [Client] Initialized config object
|
34
24
|
#
|
35
|
-
def initialize(
|
36
|
-
@
|
37
|
-
@
|
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
|
data/lib/paypal-api/request.rb
CHANGED
data/lib/paypal-api.rb
CHANGED
@@ -210,6 +210,7 @@ end
|
|
210
210
|
|
211
211
|
require_relative "paypal-api/access_token"
|
212
212
|
require_relative "paypal-api/api_collection"
|
213
|
+
require_relative "paypal-api/environment"
|
213
214
|
require_relative "paypal-api/client"
|
214
215
|
require_relative "paypal-api/config"
|
215
216
|
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.
|
4
|
+
version: 0.2.0
|
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-
|
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
|