paypal-rest-api 0.3.1 → 0.5.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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +97 -16
  3. data/VERSION +1 -1
  4. data/lib/paypal-api/api_collections/authentication.rb +1 -1
  5. data/lib/paypal-api/api_collections/authorized_payments.rb +1 -1
  6. data/lib/paypal-api/api_collections/captured_payments.rb +1 -1
  7. data/lib/paypal-api/api_collections/catalog_products.rb +1 -1
  8. data/lib/paypal-api/api_collections/disputes.rb +1 -1
  9. data/lib/paypal-api/api_collections/invoice_templates.rb +1 -1
  10. data/lib/paypal-api/api_collections/invoices.rb +1 -1
  11. data/lib/paypal-api/api_collections/orders.rb +1 -1
  12. data/lib/paypal-api/api_collections/partner_referrals.rb +48 -0
  13. data/lib/paypal-api/api_collections/payment_experience_web_profiles.rb +94 -0
  14. data/lib/paypal-api/api_collections/payment_tokens.rb +70 -0
  15. data/lib/paypal-api/api_collections/payout_items.rb +1 -1
  16. data/lib/paypal-api/api_collections/payouts.rb +1 -1
  17. data/lib/paypal-api/api_collections/referenced_payout_items.rb +2 -2
  18. data/lib/paypal-api/api_collections/referenced_payouts.rb +1 -1
  19. data/lib/paypal-api/api_collections/refunds.rb +1 -1
  20. data/lib/paypal-api/api_collections/setup_tokens.rb +47 -0
  21. data/lib/paypal-api/api_collections/shipment_tracking.rb +1 -1
  22. data/lib/paypal-api/api_collections/subscription_plans.rb +1 -1
  23. data/lib/paypal-api/api_collections/subscriptions.rb +1 -1
  24. data/lib/paypal-api/api_collections/transaction_search.rb +46 -0
  25. data/lib/paypal-api/api_collections/user_info.rb +1 -1
  26. data/lib/paypal-api/api_collections/users.rb +2 -2
  27. data/lib/paypal-api/api_collections/webhook_events.rb +1 -1
  28. data/lib/paypal-api/api_collections/webhook_lookups.rb +1 -1
  29. data/lib/paypal-api/api_collections/webhooks.rb +1 -1
  30. data/lib/paypal-api/client/access_token_methods.rb +36 -0
  31. data/lib/paypal-api/client/api_methods.rb +140 -0
  32. data/lib/paypal-api/client/environment_methods.rb +34 -0
  33. data/lib/paypal-api/client/http_methods.rb +70 -0
  34. data/lib/paypal-api/client.rb +5 -215
  35. data/lib/paypal-api/error.rb +3 -1
  36. data/lib/paypal-api/request.rb +47 -22
  37. data/lib/paypal-api/request_executor.rb +32 -15
  38. data/lib/paypal-api/response.rb +68 -0
  39. data/lib/paypal-api.rb +27 -3
  40. metadata +12 -3
@@ -30,31 +30,40 @@ module PaypalAPI
30
30
 
31
31
  private
32
32
 
33
- def execute_request(retry_number: 0)
33
+ def start_execution(retry_number)
34
34
  callbacks_context[:retry_number] = retry_number
35
-
36
35
  run_callbacks(:before)
37
- response = execute_net_http_request
38
- rescue => error
39
- raise error if NetworkErrorBuilder::ERRORS.none? { |network_error_class| error.is_a?(network_error_class) }
36
+ execute_net_http_request
37
+ end
40
38
 
39
+ def handle_network_error(error, retry_number)
41
40
  will_retry = retries[:enabled] && !retries_limit_reached?(retry_number)
42
41
  callbacks_context[:will_retry] = will_retry
43
42
  run_callbacks(:after_network_error, error)
44
43
  raise NetworkErrorBuilder.call(request: request, error: error) unless will_retry
45
44
 
46
45
  retry_request(retry_number)
46
+ end
47
+
48
+ def handle_success_response(response)
49
+ callbacks_context.delete(:will_retry)
50
+ run_callbacks(:after_success, response)
51
+ response
52
+ end
53
+
54
+ def handle_failed_response(response, retry_number)
55
+ will_retry = retries[:enabled] && retryable?(response, retry_number)
56
+ callbacks_context[:will_retry] = will_retry
57
+ run_callbacks(:after_fail, response)
58
+ will_retry ? retry_request(retry_number) : response
59
+ end
60
+
61
+ def execute_request(retry_number: 0)
62
+ response = start_execution(retry_number)
63
+ rescue => error
64
+ unknown_network_error?(error) ? handle_unknown_error(error) : handle_network_error(error, retry_number)
47
65
  else
48
- if response.success?
49
- callbacks_context.delete(:will_retry)
50
- run_callbacks(:after_success, response)
51
- response
52
- else
53
- will_retry = retries[:enabled] && retryable?(response, retry_number)
54
- callbacks_context[:will_retry] = will_retry
55
- run_callbacks(:after_fail, response)
56
- will_retry ? retry_request(retry_number) : response
57
- end
66
+ response.success? ? handle_success_response(response) : handle_failed_response(response, retry_number)
58
67
  end
59
68
 
60
69
  def execute_net_http_request
@@ -105,6 +114,14 @@ module PaypalAPI
105
114
  true
106
115
  end
107
116
 
117
+ def unknown_network_error?(error)
118
+ NetworkErrorBuilder::ERRORS.none? { |network_error_class| error.is_a?(network_error_class) }
119
+ end
120
+
121
+ def handle_unknown_error(error)
122
+ raise error
123
+ end
124
+
108
125
  def run_callbacks(callback_name, resp = nil)
109
126
  callbacks[callback_name].each { |callback| callback.call(request, callbacks_context, resp) }
110
127
  end
@@ -108,6 +108,72 @@ module PaypalAPI
108
108
  "#<#{self.class.name} (#{http_response.code})>"
109
109
  end
110
110
 
111
+ #
112
+ # Follow up HATEOAS link
113
+ #
114
+ # @see https://developer.paypal.com/api/rest/responses/#link-hateoaslinks
115
+ #
116
+ # @param rel [String] Target link "rel" attribute
117
+ # @param query [Hash] Request additional query parameters
118
+ # @param body [Hash] Request body parameters
119
+ # @param headers [Hash] Request headers
120
+ #
121
+ # @return [Response, nil] Follow-up link response, if link with provided "rel" exists
122
+ #
123
+ def follow_up_link(rel, query: nil, body: nil, headers: nil)
124
+ links = self[:links]
125
+ return unless links
126
+
127
+ link = links.find { |curr_link| curr_link[:rel] == rel.to_s }
128
+ return unless link
129
+
130
+ http_method = link[:method]&.downcase || :get
131
+ request.client.public_send(http_method, link[:href], query: query, body: body, headers: headers)
132
+ end
133
+
134
+ #
135
+ # Pagination methods
136
+ #
137
+ module PaginationHelpers
138
+ #
139
+ # Iterates through all response pages by requesting HATEOAS "next" links,
140
+ # making additional fetches to the API as necessary.
141
+ #
142
+ # @see #follow_up_link
143
+ #
144
+ # @yield [Response] Next page response
145
+ #
146
+ # @return [void]
147
+ #
148
+ def each_page(&block)
149
+ return enum_for(:each_page) unless block
150
+
151
+ page = self
152
+ yield(page)
153
+ yield(page) while (page = page.follow_up_link("next"))
154
+ end
155
+
156
+ #
157
+ # Iterates through all items in response +items_field_name+ field,
158
+ # making additional pages requests as necessary.
159
+ #
160
+ # @see #each_page
161
+ #
162
+ # @param items_field_name [Symbol] Name of field containing items
163
+ #
164
+ # @yield [Hash] Item
165
+ #
166
+ # @return [void]
167
+ #
168
+ def each_page_item(items_field_name, &block)
169
+ return enum_for(:each_page_item, items_field_name) unless block
170
+
171
+ each_page do |page|
172
+ page.fetch(items_field_name).each(&block)
173
+ end
174
+ end
175
+ end
176
+
111
177
  private
112
178
 
113
179
  def json_response?
@@ -120,5 +186,7 @@ module PaypalAPI
120
186
  rescue JSON::ParserError, TypeError
121
187
  json
122
188
  end
189
+
190
+ include PaginationHelpers
123
191
  end
124
192
  end
data/lib/paypal-api.rb CHANGED
@@ -100,10 +100,8 @@ module PaypalAPI
100
100
 
101
101
  #
102
102
  # @!macro [new] api_collection
103
- # $0 APIs collection
103
+ # $3 APIs collection
104
104
  # @api public
105
- # @example
106
- # PaypalAPI.$0
107
105
  #
108
106
 
109
107
  #
@@ -139,6 +137,14 @@ module PaypalAPI
139
137
  # @macro api_collection
140
138
  # @return [Orders]
141
139
  #
140
+ # @!method partner_referrals
141
+ # @macro api_collection
142
+ # @return [PartnerReferrals]
143
+ #
144
+ # @!method payment_experience_web_profiles
145
+ # @macro api_collection
146
+ # @return [PaymentExperienceWebProfile]
147
+ #
142
148
  # @!method payout_items
143
149
  # @macro api_collection
144
150
  # @return [PayoutItems]
@@ -171,6 +177,10 @@ module PaypalAPI
171
177
  # @macro api_collection
172
178
  # @return [SubscriptionPlans]
173
179
  #
180
+ # @!method transaction_search
181
+ # @macro api_collection
182
+ # @return [TransactionSearch]
183
+ #
174
184
  # @!method user_info
175
185
  # @macro api_collection
176
186
  # @return [UserInfo]
@@ -200,14 +210,19 @@ module PaypalAPI
200
210
  invoice_templates
201
211
  invoices
202
212
  orders
213
+ partner_referrals
214
+ payment_experience_web_profiles
215
+ payment_tokens
203
216
  payout_items
204
217
  payouts
205
218
  refunds
206
219
  referenced_payout_items
207
220
  referenced_payouts
221
+ setup_tokens
208
222
  shipment_tracking
209
223
  subscriptions
210
224
  subscription_plans
225
+ transaction_search
211
226
  user_info
212
227
  users
213
228
  webhooks
@@ -235,6 +250,10 @@ end
235
250
  require_relative "paypal-api/access_token"
236
251
  require_relative "paypal-api/api_collection"
237
252
  require_relative "paypal-api/environment"
253
+ require_relative "paypal-api/client/access_token_methods"
254
+ require_relative "paypal-api/client/api_methods"
255
+ require_relative "paypal-api/client/environment_methods"
256
+ require_relative "paypal-api/client/http_methods"
238
257
  require_relative "paypal-api/client"
239
258
  require_relative "paypal-api/config"
240
259
  require_relative "paypal-api/error"
@@ -253,14 +272,19 @@ require_relative "paypal-api/api_collections/disputes"
253
272
  require_relative "paypal-api/api_collections/invoice_templates"
254
273
  require_relative "paypal-api/api_collections/invoices"
255
274
  require_relative "paypal-api/api_collections/orders"
275
+ require_relative "paypal-api/api_collections/partner_referrals"
276
+ require_relative "paypal-api/api_collections/payment_experience_web_profiles"
277
+ require_relative "paypal-api/api_collections/payment_tokens"
256
278
  require_relative "paypal-api/api_collections/payout_items"
257
279
  require_relative "paypal-api/api_collections/payouts"
258
280
  require_relative "paypal-api/api_collections/refunds"
259
281
  require_relative "paypal-api/api_collections/referenced_payout_items"
260
282
  require_relative "paypal-api/api_collections/referenced_payouts"
283
+ require_relative "paypal-api/api_collections/setup_tokens"
261
284
  require_relative "paypal-api/api_collections/shipment_tracking"
262
285
  require_relative "paypal-api/api_collections/subscriptions"
263
286
  require_relative "paypal-api/api_collections/subscription_plans"
287
+ require_relative "paypal-api/api_collections/transaction_search"
264
288
  require_relative "paypal-api/api_collections/user_info"
265
289
  require_relative "paypal-api/api_collections/users"
266
290
  require_relative "paypal-api/api_collections/webhooks"
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.3.1
4
+ version: 0.5.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-02 00:00:00.000000000 Z
11
+ date: 2024-09-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: PayPal REST API with no dependencies.
14
14
  email:
@@ -30,20 +30,29 @@ files:
30
30
  - lib/paypal-api/api_collections/invoice_templates.rb
31
31
  - lib/paypal-api/api_collections/invoices.rb
32
32
  - lib/paypal-api/api_collections/orders.rb
33
+ - lib/paypal-api/api_collections/partner_referrals.rb
34
+ - lib/paypal-api/api_collections/payment_experience_web_profiles.rb
35
+ - lib/paypal-api/api_collections/payment_tokens.rb
33
36
  - lib/paypal-api/api_collections/payout_items.rb
34
37
  - lib/paypal-api/api_collections/payouts.rb
35
38
  - lib/paypal-api/api_collections/referenced_payout_items.rb
36
39
  - lib/paypal-api/api_collections/referenced_payouts.rb
37
40
  - lib/paypal-api/api_collections/refunds.rb
41
+ - lib/paypal-api/api_collections/setup_tokens.rb
38
42
  - lib/paypal-api/api_collections/shipment_tracking.rb
39
43
  - lib/paypal-api/api_collections/subscription_plans.rb
40
44
  - lib/paypal-api/api_collections/subscriptions.rb
45
+ - lib/paypal-api/api_collections/transaction_search.rb
41
46
  - lib/paypal-api/api_collections/user_info.rb
42
47
  - lib/paypal-api/api_collections/users.rb
43
48
  - lib/paypal-api/api_collections/webhook_events.rb
44
49
  - lib/paypal-api/api_collections/webhook_lookups.rb
45
50
  - lib/paypal-api/api_collections/webhooks.rb
46
51
  - lib/paypal-api/client.rb
52
+ - lib/paypal-api/client/access_token_methods.rb
53
+ - lib/paypal-api/client/api_methods.rb
54
+ - lib/paypal-api/client/environment_methods.rb
55
+ - lib/paypal-api/client/http_methods.rb
47
56
  - lib/paypal-api/config.rb
48
57
  - lib/paypal-api/environment.rb
49
58
  - lib/paypal-api/error.rb
@@ -78,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
87
  - !ruby/object:Gem::Version
79
88
  version: '0'
80
89
  requirements: []
81
- rubygems_version: 3.5.18
90
+ rubygems_version: 3.5.20
82
91
  signing_key:
83
92
  specification_version: 4
84
93
  summary: PayPal REST API