paypal-rest-api 0.0.1 → 0.0.2
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 +16 -3
- data/VERSION +1 -1
- data/lib/paypal-api/access_token.rb +16 -0
- data/lib/paypal-api/client.rb +76 -2
- data/lib/paypal-api/collection.rb +8 -0
- data/lib/paypal-api/collections/authentication.rb +1 -1
- data/lib/paypal-api/collections/orders.rb +3 -0
- data/lib/paypal-api/collections/payments.rb +3 -0
- data/lib/paypal-api/collections/webhooks.rb +9 -6
- data/lib/paypal-api/config.rb +27 -9
- data/lib/paypal-api/failed_request_error_builder.rb +8 -0
- data/lib/paypal-api/network_error_builder.rb +8 -0
- data/lib/paypal-api/request_executor.rb +8 -0
- data/lib/paypal-api/response.rb +20 -0
- data/lib/paypal-api.rb +83 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01d19ac0652fbd1ee8cf49d9e5d91d2fd46071b38dfdd5524bf926d2ad9cf6b1
|
4
|
+
data.tar.gz: 68eb65741710644c17b329fa4dff432671badafa4d1f325e9ba6003be934fb58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1728d99460680ef7b8ce8f2cb98447e00a82d7186e0fe3d84c6883e20e089fe1fa52c7041bcce96e5ee415bde6f09959b2cb08cdac55a2e4c1eb1efce7cb36cd
|
7
|
+
data.tar.gz: 3bcfd687d6e0300dfb0d2463954e7706dd18cfaf176fce356050f4ae5b5ffd62f9f877299c17671ecedd5147947bc570e9d72af75901db87a7e9fd55fd6ebda7
|
data/README.md
CHANGED
@@ -62,7 +62,11 @@ Also PaypalAPI client can be added globally and class methods can be used instea
|
|
62
62
|
|
63
63
|
```ruby
|
64
64
|
# in config/initializers/paypal_api.rb
|
65
|
-
PaypalAPI.client = PaypalAPI::Client.new(
|
65
|
+
PaypalAPI.client = PaypalAPI::Client.new(
|
66
|
+
client_id: ENV['PAYPAL_CLIENT_ID'],
|
67
|
+
client_secret: ENV['PAYPAL_CLIENT_SECRET'],
|
68
|
+
live: false
|
69
|
+
)
|
66
70
|
|
67
71
|
# in your business logic
|
68
72
|
response = PaypalAPI.orders.create(body: body)
|
@@ -91,7 +95,7 @@ When `live` is `false` all requests will be send to the sandbox endpoints.
|
|
91
95
|
|
92
96
|
```ruby
|
93
97
|
client = PaypalAPI::Client.new(
|
94
|
-
live: true
|
98
|
+
live: true
|
95
99
|
# ...
|
96
100
|
)
|
97
101
|
```
|
@@ -167,7 +171,16 @@ All errors have additional methods:
|
|
167
171
|
begin
|
168
172
|
response = PaypalAPI.payments.capture(authorization_id, body: body)
|
169
173
|
rescue PaypalAPI::Error => error
|
170
|
-
YourLogger.error(
|
174
|
+
YourLogger.error(
|
175
|
+
error,
|
176
|
+
context: {
|
177
|
+
error_name: error.error_name,
|
178
|
+
error_message: error.error_message,
|
179
|
+
error_debug_id: error.error_debug_id,
|
180
|
+
error_details: error.error_details
|
181
|
+
}
|
182
|
+
)
|
183
|
+
# `error.request` and `error.response` methods can be used also
|
171
184
|
end
|
172
185
|
|
173
186
|
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
@@ -7,6 +7,16 @@ module PaypalAPI
|
|
7
7
|
class AccessToken
|
8
8
|
attr_reader :requested_at, :expires_at, :authorization_string
|
9
9
|
|
10
|
+
#
|
11
|
+
# Initializes AccessToken object
|
12
|
+
#
|
13
|
+
# @param requested_at [Time] Time when token was requested
|
14
|
+
# @param expires_in [Integer] Count of seconds until token expires
|
15
|
+
# @param access_token [String] Aceess token string generated by PayPal
|
16
|
+
# @param token_type [String] Aceess token type, which is constantly `Bearer`
|
17
|
+
#
|
18
|
+
# @return [AccessToken] Generated AccessToken object
|
19
|
+
#
|
10
20
|
def initialize(requested_at:, expires_in:, access_token:, token_type:)
|
11
21
|
@requested_at = requested_at
|
12
22
|
@expires_at = requested_at + expires_in
|
@@ -14,10 +24,16 @@ module PaypalAPI
|
|
14
24
|
freeze
|
15
25
|
end
|
16
26
|
|
27
|
+
#
|
28
|
+
# Shows if current AccessToken was expired
|
29
|
+
#
|
17
30
|
def expired?
|
18
31
|
Time.now >= expires_at
|
19
32
|
end
|
20
33
|
|
34
|
+
#
|
35
|
+
# Instance representation string. Default was overwritten to hide original access_token
|
36
|
+
#
|
21
37
|
def inspect
|
22
38
|
"#<#{self.class.name} methods: (requested_at, expires_at, expired?, authorization_string)>"
|
23
39
|
end
|
data/lib/paypal-api/client.rb
CHANGED
@@ -7,6 +7,16 @@ module PaypalAPI
|
|
7
7
|
class Client
|
8
8
|
attr_reader :config
|
9
9
|
|
10
|
+
# Initializes Client
|
11
|
+
#
|
12
|
+
# @param client_id [String] PayPal client id
|
13
|
+
# @param client_secret [String] PayPal client secret
|
14
|
+
# @param live [Boolean] PayPal live/sandbox mode
|
15
|
+
# @param http_opts [Hash] Net::Http opts for all requests
|
16
|
+
# @param retries [Hash] Retries configuration
|
17
|
+
#
|
18
|
+
# @return [Client] Initialized client
|
19
|
+
#
|
10
20
|
def initialize(client_id:, client_secret:, live: nil, http_opts: nil, retries: nil)
|
11
21
|
@config = PaypalAPI::Config.new(
|
12
22
|
client_id: client_id,
|
@@ -19,12 +29,22 @@ module PaypalAPI
|
|
19
29
|
@access_token = nil
|
20
30
|
end
|
21
31
|
|
32
|
+
#
|
33
|
+
# Checks cached access token is expired and returns it or generates new one
|
34
|
+
#
|
35
|
+
# @return [AccessToken] AccessToken object
|
36
|
+
#
|
22
37
|
def access_token
|
23
38
|
(@access_token.nil? || @access_token.expired?) ? refresh_access_token : @access_token
|
24
39
|
end
|
25
40
|
|
41
|
+
#
|
42
|
+
# Generates and caches new AccessToken
|
43
|
+
#
|
44
|
+
# @return [AccessToken] new AccessToken object
|
45
|
+
#
|
26
46
|
def refresh_access_token
|
27
|
-
response =
|
47
|
+
response = authentication.generate_access_token
|
28
48
|
|
29
49
|
@access_token = AccessToken.new(
|
30
50
|
requested_at: response.requested_at,
|
@@ -34,38 +54,92 @@ module PaypalAPI
|
|
34
54
|
)
|
35
55
|
end
|
36
56
|
|
57
|
+
#
|
58
|
+
# Executes POST http request
|
59
|
+
#
|
60
|
+
# @param path [String] Request path
|
61
|
+
# @param query [Hash, nil] Request query parameters
|
62
|
+
# @param body [Hash, nil] Request body parameters
|
63
|
+
# @param headers [Hash, nil] Request headers
|
64
|
+
#
|
65
|
+
# @return [Response] Response object
|
66
|
+
#
|
37
67
|
def post(path, query: nil, body: nil, headers: nil)
|
38
68
|
execute_request(Net::HTTP::Post, path, query: query, body: body, headers: headers)
|
39
69
|
end
|
40
70
|
|
71
|
+
#
|
72
|
+
# Executes GET http request
|
73
|
+
#
|
74
|
+
# @param path [String] Request path
|
75
|
+
# @param query [Hash, nil] Request query parameters
|
76
|
+
# @param body [Hash, nil] Request body parameters
|
77
|
+
# @param headers [Hash, nil] Request headers
|
78
|
+
#
|
79
|
+
# @return [Response] Response object
|
80
|
+
#
|
41
81
|
def get(path, query: nil, body: nil, headers: nil)
|
42
82
|
execute_request(Net::HTTP::Get, path, query: query, body: body, headers: headers)
|
43
83
|
end
|
44
84
|
|
85
|
+
#
|
86
|
+
# Executes PATCH http request
|
87
|
+
#
|
88
|
+
# @param path [String] Request path
|
89
|
+
# @param query [Hash, nil] Request query parameters
|
90
|
+
# @param body [Hash, nil] Request body parameters
|
91
|
+
# @param headers [Hash, nil] Request headers
|
92
|
+
#
|
93
|
+
# @return [Response] Response object
|
94
|
+
#
|
45
95
|
def patch(path, query: nil, body: nil, headers: nil)
|
46
96
|
execute_request(Net::HTTP::Patch, path, query: query, body: body, headers: headers)
|
47
97
|
end
|
48
98
|
|
99
|
+
#
|
100
|
+
# Executes PUT http request
|
101
|
+
#
|
102
|
+
# @param path [String] Request path
|
103
|
+
# @param query [Hash, nil] Request query parameters
|
104
|
+
# @param body [Hash, nil] Request body parameters
|
105
|
+
# @param headers [Hash, nil] Request headers
|
106
|
+
#
|
107
|
+
# @return [Response] Response object
|
108
|
+
#
|
49
109
|
def put(path, query: nil, body: nil, headers: nil)
|
50
110
|
execute_request(Net::HTTP::Put, path, query: query, body: body, headers: headers)
|
51
111
|
end
|
52
112
|
|
113
|
+
#
|
114
|
+
# Executes DELETE http request
|
115
|
+
#
|
116
|
+
# @param path [String] Request path
|
117
|
+
# @param query [Hash, nil] Request query parameters
|
118
|
+
# @param body [Hash, nil] Request body parameters
|
119
|
+
# @param headers [Hash, nil] Request headers
|
120
|
+
#
|
121
|
+
# @return [Response] Response object
|
122
|
+
#
|
53
123
|
def delete(path, query: nil, body: nil, headers: nil)
|
54
124
|
execute_request(Net::HTTP::Delete, path, query: query, body: body, headers: headers)
|
55
125
|
end
|
56
126
|
|
57
|
-
|
127
|
+
# @return [Authentication] Authentication APIs collection
|
128
|
+
def authentication
|
58
129
|
Authentication.new(self)
|
59
130
|
end
|
60
131
|
|
132
|
+
# @return [Orders] Orders APIs collection
|
61
133
|
def orders
|
62
134
|
Orders.new(self)
|
63
135
|
end
|
64
136
|
|
137
|
+
# @return [Payments] Payments APIs collection
|
65
138
|
def payments
|
66
139
|
Payments.new(self)
|
67
140
|
end
|
68
141
|
|
142
|
+
# @return [Webhooks] Webhooks APIs collection
|
69
143
|
def webhooks
|
70
144
|
Webhooks.new(self)
|
71
145
|
end
|
@@ -5,12 +5,20 @@ module PaypalAPI
|
|
5
5
|
# Base class for all PayPal API collections classes
|
6
6
|
#
|
7
7
|
class Collection
|
8
|
+
# @return current client
|
8
9
|
attr_reader :client
|
9
10
|
|
11
|
+
# Initializes APIs collection
|
12
|
+
#
|
13
|
+
# @param client [Client] current client
|
14
|
+
#
|
15
|
+
# @return [Collection] APIs collection
|
16
|
+
#
|
10
17
|
def initialize(client)
|
11
18
|
@client = client
|
12
19
|
end
|
13
20
|
|
21
|
+
# @return global client
|
14
22
|
def self.client
|
15
23
|
PaypalAPI.client
|
16
24
|
end
|
@@ -33,7 +33,7 @@ module PaypalAPI
|
|
33
33
|
#
|
34
34
|
# @param query [Hash, nil] Request query string parameters
|
35
35
|
# @param body [Hash, nil] Request body parameters
|
36
|
-
# @param
|
36
|
+
# @param headers [Hash, nil] Request headers
|
37
37
|
#
|
38
38
|
# @raise [Error] on network error or non 2** status code returned from PayPal
|
39
39
|
# @return [Response] detailed http request-response representation
|
@@ -5,44 +5,47 @@ module PaypalAPI
|
|
5
5
|
# https://developer.paypal.com/docs/api/webhooks/v1/
|
6
6
|
#
|
7
7
|
class Webhooks < Collection
|
8
|
+
#
|
9
|
+
# Common class and instance methods
|
10
|
+
#
|
8
11
|
module APIs
|
9
12
|
#
|
10
|
-
# https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_post
|
13
|
+
# @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_post
|
11
14
|
#
|
12
15
|
def create(query: nil, body: nil, headers: nil)
|
13
16
|
client.post("/v1/notifications/webhooks", query: query, body: body, headers: headers)
|
14
17
|
end
|
15
18
|
|
16
19
|
#
|
17
|
-
# https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_delete
|
20
|
+
# @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_delete
|
18
21
|
#
|
19
22
|
def delete(webhook_id, query: nil, body: nil, headers: nil)
|
20
23
|
client.delete("/v1/notifications/webhooks/#{webhook_id}", query: query, body: body, headers: headers)
|
21
24
|
end
|
22
25
|
|
23
26
|
#
|
24
|
-
# https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_list
|
27
|
+
# @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_list
|
25
28
|
#
|
26
29
|
def list(query: nil, body: nil, headers: nil)
|
27
30
|
client.get("/v1/notifications/webhooks", query: query, body: body, headers: headers)
|
28
31
|
end
|
29
32
|
|
30
33
|
#
|
31
|
-
# https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_get
|
34
|
+
# @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_get
|
32
35
|
#
|
33
36
|
def show(webhook_id, query: nil, body: nil, headers: nil)
|
34
37
|
client.get("/v1/notifications/webhooks/#{webhook_id}", query: query, body: body, headers: headers)
|
35
38
|
end
|
36
39
|
|
37
40
|
#
|
38
|
-
# https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_update
|
41
|
+
# @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_update
|
39
42
|
#
|
40
43
|
def update(webhook_id, query: nil, body: nil, headers: nil)
|
41
44
|
client.patch("/v1/notifications/webhooks/#{webhook_id}", query: query, body: body, headers: headers)
|
42
45
|
end
|
43
46
|
|
44
47
|
#
|
45
|
-
# https://developer.paypal.com/docs/api/webhooks/v1/#verify-webhook-signature_post
|
48
|
+
# @see https://developer.paypal.com/docs/api/webhooks/v1/#verify-webhook-signature_post
|
46
49
|
#
|
47
50
|
def verify(query: nil, body: nil, headers: nil)
|
48
51
|
client.post("/v1/notifications/verify-webhook-signature", query: query, body: body, headers: headers)
|
data/lib/paypal-api/config.rb
CHANGED
@@ -1,21 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module PaypalAPI
|
4
|
-
LIVE_URL = "https://api-m.paypal.com"
|
5
|
-
SANDBOX_URL = "https://api-m.sandbox.paypal.com"
|
6
|
-
|
7
|
-
DEFAULTS = {
|
8
|
-
live: false,
|
9
|
-
http_opts: {}.freeze,
|
10
|
-
retries: {enabled: true, count: 3, sleep: [0.25, 0.75, 1.5].freeze}.freeze
|
11
|
-
}.freeze
|
12
|
-
|
13
4
|
#
|
14
5
|
# Stores configuration for PaypalAPI Client
|
15
6
|
#
|
16
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
|
+
# Default config options
|
15
|
+
DEFAULTS = {
|
16
|
+
live: false,
|
17
|
+
http_opts: {}.freeze,
|
18
|
+
retries: {enabled: true, count: 3, sleep: [0.25, 0.75, 1.5].freeze}.freeze
|
19
|
+
}.freeze
|
20
|
+
|
17
21
|
attr_reader :client_id, :client_secret, :live, :http_opts, :retries
|
18
22
|
|
23
|
+
# Initializes Config
|
24
|
+
#
|
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
|
+
# @param http_opts [Hash] Net::Http opts for all requests
|
29
|
+
# @param retries [Hash] Retries configuration
|
30
|
+
#
|
31
|
+
# @return [Client] Initialized config object
|
32
|
+
#
|
19
33
|
def initialize(client_id:, client_secret:, live: nil, http_opts: nil, retries: nil)
|
20
34
|
@client_id = client_id
|
21
35
|
@client_secret = client_secret
|
@@ -25,10 +39,14 @@ module PaypalAPI
|
|
25
39
|
freeze
|
26
40
|
end
|
27
41
|
|
42
|
+
# @return [String] PayPal live or sandbox URL
|
28
43
|
def url
|
29
44
|
live ? LIVE_URL : SANDBOX_URL
|
30
45
|
end
|
31
46
|
|
47
|
+
#
|
48
|
+
# Instance representation string. Default was overwritten to hide secrets
|
49
|
+
#
|
32
50
|
def inspect
|
33
51
|
"#<#{self.class.name} live: #{live}>"
|
34
52
|
end
|
@@ -7,6 +7,7 @@ module PaypalAPI
|
|
7
7
|
# Builds PaypalAPI::FailedRequest error
|
8
8
|
#
|
9
9
|
class FailedRequestErrorBuilder
|
10
|
+
# Matchings for Net::HTTP response class to PaypalAPI::Error class
|
10
11
|
RESPONSE_ERROR_MAP = {
|
11
12
|
Net::HTTPBadRequest => BadRequestError, # 400
|
12
13
|
Net::HTTPUnauthorized => UnauthorizedError, # 401
|
@@ -23,6 +24,13 @@ module PaypalAPI
|
|
23
24
|
}.freeze
|
24
25
|
|
25
26
|
class << self
|
27
|
+
# Builds FailedRequestError instance
|
28
|
+
#
|
29
|
+
# @param request [Request] Original request
|
30
|
+
# @param response [Response] Original response
|
31
|
+
#
|
32
|
+
# @return [FailedRequestError] Built FailedRequestError
|
33
|
+
#
|
26
34
|
def call(request:, response:)
|
27
35
|
http_response = response.http_response
|
28
36
|
error_message = "#{http_response.code} #{http_response.message}"
|
@@ -5,6 +5,7 @@ module PaypalAPI
|
|
5
5
|
# Builds PaypalAPI::NetowrkError error
|
6
6
|
#
|
7
7
|
class NetworkErrorBuilder
|
8
|
+
# List of possible Network errors
|
8
9
|
ERRORS = [
|
9
10
|
EOFError,
|
10
11
|
Errno::ECONNABORTED,
|
@@ -20,6 +21,13 @@ module PaypalAPI
|
|
20
21
|
].freeze
|
21
22
|
|
22
23
|
class << self
|
24
|
+
# Builds NetworkError instance
|
25
|
+
#
|
26
|
+
# @param request [Request] Original request
|
27
|
+
# @param error [StandardError] Original error
|
28
|
+
#
|
29
|
+
# @return [NetworkError] Built NetworkError
|
30
|
+
#
|
23
31
|
def call(request:, error:)
|
24
32
|
NetworkError.new(error.message, request: request, error: error)
|
25
33
|
end
|
@@ -5,6 +5,7 @@ module PaypalAPI
|
|
5
5
|
# Executes PaypalAPI::Request and returns PaypalAPI::Response or raises PaypalAPI::Error
|
6
6
|
#
|
7
7
|
class RequestExecutor
|
8
|
+
# List of Net::HTTP responses that must be retried
|
8
9
|
RETRYABLE_RESPONSES = [
|
9
10
|
Net::HTTPServerError, # 5xx
|
10
11
|
Net::HTTPConflict, # 409
|
@@ -12,6 +13,13 @@ module PaypalAPI
|
|
12
13
|
].freeze
|
13
14
|
|
14
15
|
class << self
|
16
|
+
#
|
17
|
+
# Executes prepared Request, handles retries and preparation of errors
|
18
|
+
#
|
19
|
+
# @param [Request] request
|
20
|
+
#
|
21
|
+
# @return [Response] Response
|
22
|
+
#
|
15
23
|
def call(request)
|
16
24
|
http_response = execute(request)
|
17
25
|
response = Response.new(http_response, requested_at: request.requested_at)
|
data/lib/paypal-api/response.rb
CHANGED
@@ -9,6 +9,14 @@ module PaypalAPI
|
|
9
9
|
class Response
|
10
10
|
attr_reader :http_response, :requested_at
|
11
11
|
|
12
|
+
#
|
13
|
+
# Initializes Response object
|
14
|
+
#
|
15
|
+
# @param http_response [Net::HTTP::Response] original response
|
16
|
+
# @param requested_at [Time] Time when original response was requested
|
17
|
+
#
|
18
|
+
# @return [Response] Initialized Response object
|
19
|
+
#
|
12
20
|
def initialize(http_response, requested_at:)
|
13
21
|
@requested_at = requested_at
|
14
22
|
@http_response = http_response
|
@@ -18,31 +26,43 @@ module PaypalAPI
|
|
18
26
|
@body = nil
|
19
27
|
end
|
20
28
|
|
29
|
+
# Parses JSON body if response body contains JSON or returns original
|
30
|
+
# http body string
|
31
|
+
#
|
32
|
+
# @return [Hash, String] Parsed response body (with symbolized keys)
|
21
33
|
def body
|
22
34
|
@body ||= json_response? ? parse_json(http_body) : http_body
|
23
35
|
end
|
24
36
|
|
37
|
+
# @return [Integer] HTTP status as Integer
|
25
38
|
def http_status
|
26
39
|
@http_status ||= http_response.code.to_i
|
27
40
|
end
|
28
41
|
|
42
|
+
# @return [Hash] HTTP headers as Hash
|
29
43
|
def http_headers
|
30
44
|
@http_headers ||= http_response.each_header.to_h
|
31
45
|
end
|
32
46
|
|
47
|
+
# @return [String] Original http body
|
33
48
|
def http_body
|
34
49
|
@http_body ||= http_response.body
|
35
50
|
end
|
36
51
|
|
52
|
+
# Takes specific key from body, returns nil if key not present in parsed body
|
37
53
|
def [](key)
|
38
54
|
body[key.to_sym] if body.is_a?(Hash)
|
39
55
|
end
|
40
56
|
|
57
|
+
# Fetches specific key from body, raises error if key not exists
|
41
58
|
def fetch(key)
|
42
59
|
data = body.is_a?(Hash) ? body : {}
|
43
60
|
data.fetch(key.to_sym)
|
44
61
|
end
|
45
62
|
|
63
|
+
#
|
64
|
+
# Instance representation string. Default was overwritten to hide secrets
|
65
|
+
#
|
46
66
|
def inspect
|
47
67
|
"#<#{self.class.name} (#{http_response.code})>"
|
48
68
|
end
|
data/lib/paypal-api.rb
CHANGED
@@ -2,27 +2,104 @@
|
|
2
2
|
|
3
3
|
#
|
4
4
|
# PaypalAPI is a main gem module.
|
5
|
+
#
|
5
6
|
# It can store global PaypalAPI::Client for easier access to APIs.
|
6
7
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
8
|
+
# @example Initializing new global client
|
9
|
+
# PaypalAPI.client = PaypalAPI::Client.new(
|
10
|
+
# client_id: ENV.fetch('PAYPAL_CLIENT_ID'),
|
11
|
+
# client_secret: ENV.fetch('PAYPAL_CLIENT_SECRET'),
|
12
|
+
# live: false
|
13
|
+
# )
|
10
14
|
#
|
11
|
-
# # And then
|
15
|
+
# # And then call any APIs without mentioning the client
|
12
16
|
# PaypalAPI::Webhooks.list # or PaypalAPI.webhooks.list
|
13
17
|
#
|
14
18
|
module PaypalAPI
|
15
19
|
class << self
|
20
|
+
# Sets client
|
16
21
|
attr_writer :client
|
17
22
|
|
23
|
+
# @!method post
|
24
|
+
#
|
25
|
+
# Executes POST http request
|
26
|
+
#
|
27
|
+
# @param path [String] Request path
|
28
|
+
# @param query [Hash, nil] Request query parameters
|
29
|
+
# @param body [Hash, nil] Request body parameters
|
30
|
+
# @param headers [Hash, nil] Request headers
|
31
|
+
#
|
32
|
+
# @return [Response] Response object
|
33
|
+
#
|
34
|
+
#
|
35
|
+
# @!method get
|
36
|
+
#
|
37
|
+
# Executes GET http request
|
38
|
+
#
|
39
|
+
# @param path [String] Request path
|
40
|
+
# @param query [Hash, nil] Request query parameters
|
41
|
+
# @param body [Hash, nil] Request body parameters
|
42
|
+
# @param headers [Hash, nil] Request headers
|
43
|
+
#
|
44
|
+
# @return [Response] Response object
|
45
|
+
#
|
46
|
+
#
|
47
|
+
# @!method patch
|
48
|
+
#
|
49
|
+
# Executes PATCH http request
|
50
|
+
#
|
51
|
+
# @param path [String] Request path
|
52
|
+
# @param query [Hash, nil] Request query parameters
|
53
|
+
# @param body [Hash, nil] Request body parameters
|
54
|
+
# @param headers [Hash, nil] Request headers
|
55
|
+
#
|
56
|
+
# @return [Response] Response object
|
57
|
+
#
|
58
|
+
#
|
59
|
+
# @!method put
|
60
|
+
#
|
61
|
+
# Executes PUT http request
|
62
|
+
#
|
63
|
+
# @param path [String] Request path
|
64
|
+
# @param query [Hash, nil] Request query parameters
|
65
|
+
# @param body [Hash, nil] Request body parameters
|
66
|
+
# @param headers [Hash, nil] Request headers
|
67
|
+
#
|
68
|
+
# @return [Response] Response object
|
69
|
+
#
|
70
|
+
|
71
|
+
# @!method delete
|
72
|
+
#
|
73
|
+
# Executes DELETE http request
|
74
|
+
#
|
75
|
+
# @param path [String] Request path
|
76
|
+
# @param query [Hash, nil] Request query parameters
|
77
|
+
# @param body [Hash, nil] Request body parameters
|
78
|
+
# @param headers [Hash, nil] Request headers
|
79
|
+
#
|
80
|
+
# @return [Response] Response object
|
81
|
+
#
|
18
82
|
[:post, :get, :patch, :put, :delete].each do |method_name|
|
19
83
|
define_method(method_name) do |path, query: nil, body: nil, headers: nil|
|
20
84
|
client.public_send(method_name, path, query: query, body: body, headers: headers)
|
21
85
|
end
|
22
86
|
end
|
23
87
|
|
88
|
+
#
|
89
|
+
# @!method authentication
|
90
|
+
# @return [Authentication]
|
91
|
+
#
|
92
|
+
# @!method orders
|
93
|
+
# @return [Orders]
|
94
|
+
#
|
95
|
+
# @!method payments
|
96
|
+
# @return [Payments]
|
97
|
+
#
|
98
|
+
# @!method webhooks
|
99
|
+
# @return [Webhooks]
|
100
|
+
#
|
24
101
|
%i[
|
25
|
-
|
102
|
+
authentication
|
26
103
|
orders
|
27
104
|
payments
|
28
105
|
webhooks
|
@@ -32,6 +109,7 @@ module PaypalAPI
|
|
32
109
|
end
|
33
110
|
end
|
34
111
|
|
112
|
+
# Globally set Client object
|
35
113
|
def client
|
36
114
|
raise "#{name}.client must be set" unless @client
|
37
115
|
|