square.rb 18.1.0.20220316 → 20.1.0.20220616

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,173 @@
1
+ module Square
2
+ # PayoutsApi
3
+ class PayoutsApi < BaseApi
4
+ def initialize(config, http_call_back: nil)
5
+ super(config, http_call_back: http_call_back)
6
+ end
7
+
8
+ # Retrieves a list of all payouts for the default location.
9
+ # You can filter payouts by location ID, status, time range, and order them
10
+ # in ascending or descending order.
11
+ # To call this endpoint, set `PAYOUTS_READ` for the OAuth scope.
12
+ # @param [String] location_id Optional parameter: The ID of the location for
13
+ # which to list the payouts. By default, payouts are returned for the
14
+ # default (main) location associated with the seller.
15
+ # @param [PayoutStatus] status Optional parameter: If provided, only payouts
16
+ # with the given status are returned.
17
+ # @param [String] begin_time Optional parameter: The timestamp for the
18
+ # beginning of the payout creation time, in RFC 3339 format. Inclusive.
19
+ # Default: The current time minus one year.
20
+ # @param [String] end_time Optional parameter: The timestamp for the end of
21
+ # the payout creation time, in RFC 3339 format. Default: The current time.
22
+ # @param [SortOrder] sort_order Optional parameter: The order in which
23
+ # payouts are listed.
24
+ # @param [String] cursor Optional parameter: A pagination cursor returned by
25
+ # a previous call to this endpoint. Provide this cursor to retrieve the next
26
+ # set of results for the original query. For more information, see
27
+ # [Pagination](https://developer.squareup.com/docs/basics/api101/pagination)
28
+ # . If request parameters change between requests, subsequent results may
29
+ # contain duplicates or missing records.
30
+ # @param [Integer] limit Optional parameter: The maximum number of results
31
+ # to be returned in a single page. It is possible to receive fewer results
32
+ # than the specified limit on a given page. The default value of 100 is also
33
+ # the maximum allowed value. If the provided value is greater than 100, it
34
+ # is ignored and the default value is used instead. Default: `100`
35
+ # @return [ListPayoutsResponse Hash] response from the API call
36
+ def list_payouts(location_id: nil,
37
+ status: nil,
38
+ begin_time: nil,
39
+ end_time: nil,
40
+ sort_order: nil,
41
+ cursor: nil,
42
+ limit: nil)
43
+ # Prepare query url.
44
+ _query_builder = config.get_base_uri
45
+ _query_builder << '/v2/payouts'
46
+ _query_builder = APIHelper.append_url_with_query_parameters(
47
+ _query_builder,
48
+ 'location_id' => location_id,
49
+ 'status' => status,
50
+ 'begin_time' => begin_time,
51
+ 'end_time' => end_time,
52
+ 'sort_order' => sort_order,
53
+ 'cursor' => cursor,
54
+ 'limit' => limit
55
+ )
56
+ _query_url = APIHelper.clean_url _query_builder
57
+
58
+ # Prepare headers.
59
+ _headers = {
60
+ 'accept' => 'application/json'
61
+ }
62
+
63
+ # Prepare and execute HttpRequest.
64
+ _request = config.http_client.get(
65
+ _query_url,
66
+ headers: _headers
67
+ )
68
+ OAuth2.apply(config, _request)
69
+ _response = execute_request(_request)
70
+
71
+ # Return appropriate response type.
72
+ decoded = APIHelper.json_deserialize(_response.raw_body)
73
+ _errors = APIHelper.map_response(decoded, ['errors'])
74
+ ApiResponse.new(
75
+ _response, data: decoded, errors: _errors
76
+ )
77
+ end
78
+
79
+ # Retrieves details of a specific payout identified by a payout ID.
80
+ # To call this endpoint, set `PAYOUTS_READ` for the OAuth scope.
81
+ # @param [String] payout_id Required parameter: The ID of the payout to
82
+ # retrieve the information for.
83
+ # @return [GetPayoutResponse Hash] response from the API call
84
+ def get_payout(payout_id:)
85
+ # Prepare query url.
86
+ _query_builder = config.get_base_uri
87
+ _query_builder << '/v2/payouts/{payout_id}'
88
+ _query_builder = APIHelper.append_url_with_template_parameters(
89
+ _query_builder,
90
+ 'payout_id' => { 'value' => payout_id, 'encode' => true }
91
+ )
92
+ _query_url = APIHelper.clean_url _query_builder
93
+
94
+ # Prepare headers.
95
+ _headers = {
96
+ 'accept' => 'application/json'
97
+ }
98
+
99
+ # Prepare and execute HttpRequest.
100
+ _request = config.http_client.get(
101
+ _query_url,
102
+ headers: _headers
103
+ )
104
+ OAuth2.apply(config, _request)
105
+ _response = execute_request(_request)
106
+
107
+ # Return appropriate response type.
108
+ decoded = APIHelper.json_deserialize(_response.raw_body)
109
+ _errors = APIHelper.map_response(decoded, ['errors'])
110
+ ApiResponse.new(
111
+ _response, data: decoded, errors: _errors
112
+ )
113
+ end
114
+
115
+ # Retrieves a list of all payout entries for a specific payout.
116
+ # To call this endpoint, set `PAYOUTS_READ` for the OAuth scope.
117
+ # @param [String] payout_id Required parameter: The ID of the payout to
118
+ # retrieve the information for.
119
+ # @param [SortOrder] sort_order Optional parameter: The order in which
120
+ # payout entries are listed.
121
+ # @param [String] cursor Optional parameter: A pagination cursor returned by
122
+ # a previous call to this endpoint. Provide this cursor to retrieve the next
123
+ # set of results for the original query. For more information, see
124
+ # [Pagination](https://developer.squareup.com/docs/basics/api101/pagination)
125
+ # . If request parameters change between requests, subsequent results may
126
+ # contain duplicates or missing records.
127
+ # @param [Integer] limit Optional parameter: The maximum number of results
128
+ # to be returned in a single page. It is possible to receive fewer results
129
+ # than the specified limit on a given page. The default value of 100 is also
130
+ # the maximum allowed value. If the provided value is greater than 100, it
131
+ # is ignored and the default value is used instead. Default: `100`
132
+ # @return [ListPayoutEntriesResponse Hash] response from the API call
133
+ def list_payout_entries(payout_id:,
134
+ sort_order: nil,
135
+ cursor: nil,
136
+ limit: nil)
137
+ # Prepare query url.
138
+ _query_builder = config.get_base_uri
139
+ _query_builder << '/v2/payouts/{payout_id}/payout-entries'
140
+ _query_builder = APIHelper.append_url_with_template_parameters(
141
+ _query_builder,
142
+ 'payout_id' => { 'value' => payout_id, 'encode' => true }
143
+ )
144
+ _query_builder = APIHelper.append_url_with_query_parameters(
145
+ _query_builder,
146
+ 'sort_order' => sort_order,
147
+ 'cursor' => cursor,
148
+ 'limit' => limit
149
+ )
150
+ _query_url = APIHelper.clean_url _query_builder
151
+
152
+ # Prepare headers.
153
+ _headers = {
154
+ 'accept' => 'application/json'
155
+ }
156
+
157
+ # Prepare and execute HttpRequest.
158
+ _request = config.http_client.get(
159
+ _query_url,
160
+ headers: _headers
161
+ )
162
+ OAuth2.apply(config, _request)
163
+ _response = execute_request(_request)
164
+
165
+ # Return appropriate response type.
166
+ decoded = APIHelper.json_deserialize(_response.raw_body)
167
+ _errors = APIHelper.map_response(decoded, ['errors'])
168
+ ApiResponse.new(
169
+ _response, data: decoded, errors: _errors
170
+ )
171
+ end
172
+ end
173
+ end
@@ -260,9 +260,6 @@ module Square
260
260
  end
261
261
 
262
262
  # Lists all events for a specific subscription.
263
- # In the current implementation, only `START_SUBSCRIPTION` and
264
- # `STOP_SUBSCRIPTION` (when the subscription was canceled) events are
265
- # returned.
266
263
  # @param [String] subscription_id Required parameter: The ID of the
267
264
  # subscription to retrieve the events for.
268
265
  # @param [String] cursor Optional parameter: When the total number of
@@ -5,6 +5,147 @@ module Square
5
5
  super(config, http_call_back: http_call_back)
6
6
  end
7
7
 
8
+ # Creates a Terminal action request and sends it to the specified device.
9
+ # @param [CreateTerminalActionRequest] body Required parameter: An object
10
+ # containing the fields to POST for the request. See the corresponding
11
+ # object definition for field details.
12
+ # @return [CreateTerminalActionResponse Hash] response from the API call
13
+ def create_terminal_action(body:)
14
+ # Prepare query url.
15
+ _query_builder = config.get_base_uri
16
+ _query_builder << '/v2/terminals/actions'
17
+ _query_url = APIHelper.clean_url _query_builder
18
+
19
+ # Prepare headers.
20
+ _headers = {
21
+ 'accept' => 'application/json',
22
+ 'Content-Type' => 'application/json'
23
+ }
24
+
25
+ # Prepare and execute HttpRequest.
26
+ _request = config.http_client.post(
27
+ _query_url,
28
+ headers: _headers,
29
+ parameters: body.to_json
30
+ )
31
+ OAuth2.apply(config, _request)
32
+ _response = execute_request(_request)
33
+
34
+ # Return appropriate response type.
35
+ decoded = APIHelper.json_deserialize(_response.raw_body)
36
+ _errors = APIHelper.map_response(decoded, ['errors'])
37
+ ApiResponse.new(
38
+ _response, data: decoded, errors: _errors
39
+ )
40
+ end
41
+
42
+ # Retrieves a filtered list of Terminal action requests created by the
43
+ # account making the request. Terminal action requests are available for 30
44
+ # days.
45
+ # @param [SearchTerminalActionsRequest] body Required parameter: An object
46
+ # containing the fields to POST for the request. See the corresponding
47
+ # object definition for field details.
48
+ # @return [SearchTerminalActionsResponse Hash] response from the API call
49
+ def search_terminal_actions(body:)
50
+ # Prepare query url.
51
+ _query_builder = config.get_base_uri
52
+ _query_builder << '/v2/terminals/actions/search'
53
+ _query_url = APIHelper.clean_url _query_builder
54
+
55
+ # Prepare headers.
56
+ _headers = {
57
+ 'accept' => 'application/json',
58
+ 'Content-Type' => 'application/json'
59
+ }
60
+
61
+ # Prepare and execute HttpRequest.
62
+ _request = config.http_client.post(
63
+ _query_url,
64
+ headers: _headers,
65
+ parameters: body.to_json
66
+ )
67
+ OAuth2.apply(config, _request)
68
+ _response = execute_request(_request)
69
+
70
+ # Return appropriate response type.
71
+ decoded = APIHelper.json_deserialize(_response.raw_body)
72
+ _errors = APIHelper.map_response(decoded, ['errors'])
73
+ ApiResponse.new(
74
+ _response, data: decoded, errors: _errors
75
+ )
76
+ end
77
+
78
+ # Retrieves a Terminal action request by `action_id`. Terminal action
79
+ # requests are available for 30 days.
80
+ # @param [String] action_id Required parameter: Unique ID for the desired
81
+ # `TerminalAction`
82
+ # @return [GetTerminalActionResponse Hash] response from the API call
83
+ def get_terminal_action(action_id:)
84
+ # Prepare query url.
85
+ _query_builder = config.get_base_uri
86
+ _query_builder << '/v2/terminals/actions/{action_id}'
87
+ _query_builder = APIHelper.append_url_with_template_parameters(
88
+ _query_builder,
89
+ 'action_id' => { 'value' => action_id, 'encode' => true }
90
+ )
91
+ _query_url = APIHelper.clean_url _query_builder
92
+
93
+ # Prepare headers.
94
+ _headers = {
95
+ 'accept' => 'application/json'
96
+ }
97
+
98
+ # Prepare and execute HttpRequest.
99
+ _request = config.http_client.get(
100
+ _query_url,
101
+ headers: _headers
102
+ )
103
+ OAuth2.apply(config, _request)
104
+ _response = execute_request(_request)
105
+
106
+ # Return appropriate response type.
107
+ decoded = APIHelper.json_deserialize(_response.raw_body)
108
+ _errors = APIHelper.map_response(decoded, ['errors'])
109
+ ApiResponse.new(
110
+ _response, data: decoded, errors: _errors
111
+ )
112
+ end
113
+
114
+ # Cancels a Terminal action request if the status of the request permits it.
115
+ # @param [String] action_id Required parameter: Unique ID for the desired
116
+ # `TerminalAction`
117
+ # @return [CancelTerminalActionResponse Hash] response from the API call
118
+ def cancel_terminal_action(action_id:)
119
+ # Prepare query url.
120
+ _query_builder = config.get_base_uri
121
+ _query_builder << '/v2/terminals/actions/{action_id}/cancel'
122
+ _query_builder = APIHelper.append_url_with_template_parameters(
123
+ _query_builder,
124
+ 'action_id' => { 'value' => action_id, 'encode' => true }
125
+ )
126
+ _query_url = APIHelper.clean_url _query_builder
127
+
128
+ # Prepare headers.
129
+ _headers = {
130
+ 'accept' => 'application/json'
131
+ }
132
+
133
+ # Prepare and execute HttpRequest.
134
+ _request = config.http_client.post(
135
+ _query_url,
136
+ headers: _headers
137
+ )
138
+ OAuth2.apply(config, _request)
139
+ _response = execute_request(_request)
140
+
141
+ # Return appropriate response type.
142
+ decoded = APIHelper.json_deserialize(_response.raw_body)
143
+ _errors = APIHelper.map_response(decoded, ['errors'])
144
+ ApiResponse.new(
145
+ _response, data: decoded, errors: _errors
146
+ )
147
+ end
148
+
8
149
  # Creates a Terminal checkout request and sends it to the specified device
9
150
  # to take a payment
10
151
  # for the requested amount.
@@ -41,8 +182,10 @@ module Square
41
182
  )
42
183
  end
43
184
 
44
- # Retrieves a filtered list of Terminal checkout requests created by the
45
- # account making the request.
185
+ # Returns a filtered list of Terminal checkout requests created by the
186
+ # application making the request. Only Terminal checkout requests created
187
+ # for the merchant scoped to the OAuth token are returned. Terminal checkout
188
+ # requests are available for 30 days.
46
189
  # @param [SearchTerminalCheckoutsRequest] body Required parameter: An object
47
190
  # containing the fields to POST for the request. See the corresponding
48
191
  # object definition for field details.
@@ -76,7 +219,8 @@ module Square
76
219
  )
77
220
  end
78
221
 
79
- # Retrieves a Terminal checkout request by `checkout_id`.
222
+ # Retrieves a Terminal checkout request by `checkout_id`. Terminal checkout
223
+ # requests are available for 30 days.
80
224
  # @param [String] checkout_id Required parameter: The unique ID for the
81
225
  # desired `TerminalCheckout`.
82
226
  # @return [GetTerminalCheckoutResponse Hash] response from the API call
@@ -148,7 +292,10 @@ module Square
148
292
  end
149
293
 
150
294
  # Creates a request to refund an Interac payment completed on a Square
151
- # Terminal.
295
+ # Terminal. Refunds for Interac payments on a Square Terminal are supported
296
+ # only for Interac debit cards in Canada. Other refunds for Terminal
297
+ # payments should use the Refunds API. For more information, see [Refunds
298
+ # API]($e/Refunds).
152
299
  # @param [CreateTerminalRefundRequest] body Required parameter: An object
153
300
  # containing the fields to POST for the request. See the corresponding
154
301
  # object definition for field details.
@@ -183,7 +330,8 @@ module Square
183
330
  end
184
331
 
185
332
  # Retrieves a filtered list of Interac Terminal refund requests created by
186
- # the seller making the request.
333
+ # the seller making the request. Terminal refund requests are available for
334
+ # 30 days.
187
335
  # @param [SearchTerminalRefundsRequest] body Required parameter: An object
188
336
  # containing the fields to POST for the request. See the corresponding
189
337
  # object definition for field details.
@@ -217,7 +365,8 @@ module Square
217
365
  )
218
366
  end
219
367
 
220
- # Retrieves an Interac Terminal refund object by ID.
368
+ # Retrieves an Interac Terminal refund object by ID. Terminal refund objects
369
+ # are available for 30 days.
221
370
  # @param [String] terminal_refund_id Required parameter: The unique ID for
222
371
  # the desired `TerminalRefund`.
223
372
  # @return [GetTerminalRefundResponse Hash] response from the API call
data/lib/square/client.rb CHANGED
@@ -4,7 +4,7 @@ module Square
4
4
  attr_reader :config
5
5
 
6
6
  def sdk_version
7
- '18.1.0.20220316'
7
+ '20.1.0.20220616'
8
8
  end
9
9
 
10
10
  def square_version
@@ -75,6 +75,12 @@ module Square
75
75
  @customers ||= CustomersApi.new config
76
76
  end
77
77
 
78
+ # Access to customer_custom_attributes controller.
79
+ # @return [CustomerCustomAttributesApi] Returns the controller instance.
80
+ def customer_custom_attributes
81
+ @customer_custom_attributes ||= CustomerCustomAttributesApi.new config
82
+ end
83
+
78
84
  # Access to customer_groups controller.
79
85
  # @return [CustomerGroupsApi] Returns the controller instance.
80
86
  def customer_groups
@@ -177,6 +183,12 @@ module Square
177
183
  @payments ||= PaymentsApi.new config
178
184
  end
179
185
 
186
+ # Access to payouts controller.
187
+ # @return [PayoutsApi] Returns the controller instance.
188
+ def payouts
189
+ @payouts ||= PayoutsApi.new config
190
+ end
191
+
180
192
  # Access to refunds controller.
181
193
  # @return [RefundsApi] Returns the controller instance.
182
194
  def refunds
@@ -219,16 +231,17 @@ module Square
219
231
  @vendors ||= VendorsApi.new config
220
232
  end
221
233
 
222
- def initialize(connection: nil, timeout: 60, max_retries: 0,
223
- retry_interval: 1, backoff_factor: 2,
234
+ def initialize(connection: nil, adapter: Faraday.default_adapter,
235
+ timeout: 60, max_retries: 0, retry_interval: 1,
236
+ backoff_factor: 2,
224
237
  retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
225
238
  retry_methods: %i[get put], environment: 'production',
226
239
  custom_url: 'https://connect.squareup.com',
227
- square_version: '2022-03-16', access_token: '',
240
+ square_version: '2022-06-16', access_token: '',
228
241
  user_agent_detail: '', additional_headers: {}, config: nil)
229
242
  @config = if config.nil?
230
- Configuration.new(connection: connection, timeout: timeout,
231
- max_retries: max_retries,
243
+ Configuration.new(connection: connection, adapter: adapter,
244
+ timeout: timeout, max_retries: max_retries,
232
245
  retry_interval: retry_interval,
233
246
  backoff_factor: backoff_factor,
234
247
  retry_statuses: retry_statuses,
@@ -3,9 +3,9 @@ module Square
3
3
  # are configured in this class.
4
4
  class Configuration
5
5
  # The attribute readers for properties.
6
- attr_reader :http_client, :connection, :timeout, :max_retries, :retry_interval, :backoff_factor,
7
- :retry_statuses, :retry_methods, :environment, :custom_url, :square_version,
8
- :access_token, :user_agent_detail
6
+ attr_reader :http_client, :connection, :adapter, :timeout, :max_retries, :retry_interval,
7
+ :backoff_factor, :retry_statuses, :retry_methods, :environment, :custom_url,
8
+ :square_version, :access_token, :user_agent_detail
9
9
 
10
10
  def additional_headers
11
11
  @additional_headers.clone
@@ -15,16 +15,20 @@ module Square
15
15
  attr_reader :environments
16
16
  end
17
17
 
18
- def initialize(connection: nil, timeout: 60, max_retries: 0,
19
- retry_interval: 1, backoff_factor: 2,
18
+ def initialize(connection: nil, adapter: Faraday.default_adapter,
19
+ timeout: 60, max_retries: 0, retry_interval: 1,
20
+ backoff_factor: 2,
20
21
  retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
21
22
  retry_methods: %i[get put], environment: 'production',
22
23
  custom_url: 'https://connect.squareup.com',
23
- square_version: '2022-03-16', access_token: '',
24
+ square_version: '2022-06-16', access_token: '',
24
25
  user_agent_detail: '', additional_headers: {})
25
26
  # The Faraday connection object passed by the SDK user for making requests
26
27
  @connection = connection
27
28
 
29
+ # The Faraday adapter object passed by the SDK user for performing http requests
30
+ @adapter = adapter
31
+
28
32
  # The value to use for connection timeout
29
33
  @timeout = timeout
30
34
 
@@ -66,12 +70,13 @@ module Square
66
70
  @user_agent_detail = get_user_agent(user_agent_detail)
67
71
  end
68
72
 
69
- def clone_with(connection: nil, timeout: nil, max_retries: nil,
70
- retry_interval: nil, backoff_factor: nil,
73
+ def clone_with(connection: nil, adapter: nil, timeout: nil,
74
+ max_retries: nil, retry_interval: nil, backoff_factor: nil,
71
75
  retry_statuses: nil, retry_methods: nil, environment: nil,
72
76
  custom_url: nil, square_version: nil, access_token: nil,
73
77
  user_agent_detail: nil, additional_headers: nil)
74
78
  connection ||= self.connection
79
+ adapter ||= self.adapter
75
80
  timeout ||= self.timeout
76
81
  max_retries ||= self.max_retries
77
82
  retry_interval ||= self.retry_interval
@@ -85,8 +90,8 @@ module Square
85
90
  user_agent_detail ||= self.user_agent_detail
86
91
  additional_headers ||= self.additional_headers
87
92
 
88
- Configuration.new(connection: connection, timeout: timeout,
89
- max_retries: max_retries,
93
+ Configuration.new(connection: connection, adapter: adapter,
94
+ timeout: timeout, max_retries: max_retries,
90
95
  retry_interval: retry_interval,
91
96
  backoff_factor: backoff_factor,
92
97
  retry_statuses: retry_statuses,
@@ -102,7 +107,8 @@ module Square
102
107
  retry_interval: retry_interval,
103
108
  backoff_factor: backoff_factor,
104
109
  retry_statuses: retry_statuses,
105
- retry_methods: retry_methods, connection: connection)
110
+ retry_methods: retry_methods, connection: connection,
111
+ adapter: adapter)
106
112
  end
107
113
 
108
114
  def get_user_agent(user_agent_detail)
@@ -18,16 +18,14 @@ module Square
18
18
  @request = http_response.request
19
19
  @errors = errors
20
20
 
21
- if data.is_a? Hash
22
- if data.keys.any?
23
- @body = Struct.new(*data.keys) do
24
- define_method(:to_s) { http_response.raw_body }
25
- end.new(*data.values)
21
+ if (data.is_a? Hash) && data.keys.any?
22
+ @body = Struct.new(*data.keys) do
23
+ define_method(:to_s) { http_response.raw_body }
24
+ end.new(*data.values)
26
25
 
27
- @cursor = data.fetch(:cursor, nil)
28
- data.reject! { |k| %i[cursor errors].include?(k) }
29
- @data = Struct.new(*data.keys).new(*data.values) if data.keys.any?
30
- end
26
+ @cursor = data.fetch(:cursor, nil)
27
+ data.reject! { |k| %i[cursor errors].include?(k) }
28
+ @data = Struct.new(*data.keys).new(*data.values) if data.keys.any?
31
29
  else
32
30
  @data = data
33
31
  @body = data
@@ -1,5 +1,8 @@
1
1
  require 'faraday/http_cache'
2
- require 'faraday_middleware'
2
+ require 'faraday/retry'
3
+ require 'faraday/multipart'
4
+ require 'faraday/follow_redirects'
5
+ require 'faraday/gzip'
3
6
 
4
7
  module Square
5
8
  # An implementation of HttpClient.
@@ -10,12 +13,12 @@ module Square
10
13
  # The constructor.
11
14
  def initialize(timeout:, max_retries:, retry_interval:,
12
15
  backoff_factor:, retry_statuses:, retry_methods:,
13
- connection: nil, cache: false, verify: true)
16
+ connection:, adapter:, cache: false, verify: true)
14
17
  @connection = if connection.nil?
15
18
  create_connection(timeout: timeout, max_retries: max_retries,
16
19
  retry_interval: retry_interval, backoff_factor: backoff_factor,
17
20
  retry_statuses: retry_statuses, retry_methods: retry_methods,
18
- cache: cache, verify: verify)
21
+ adapter: adapter, cache: cache, verify: verify)
19
22
  else
20
23
  connection
21
24
  end
@@ -24,11 +27,11 @@ module Square
24
27
  # Method to initialize connection.
25
28
  def create_connection(timeout:, max_retries:, retry_interval:,
26
29
  backoff_factor:, retry_statuses:, retry_methods:,
27
- cache: false, verify: true)
30
+ adapter:, cache: false, verify: true)
28
31
  Faraday.new do |faraday|
29
32
  faraday.use Faraday::HttpCache, serializer: Marshal if cache
30
- faraday.use FaradayMiddleware::FollowRedirects
31
- faraday.use :gzip
33
+ faraday.use Faraday::FollowRedirects::Middleware
34
+ faraday.request :gzip
32
35
  faraday.request :multipart
33
36
  faraday.request :url_encoded
34
37
  faraday.ssl[:ca_file] = Certifi.where
@@ -36,8 +39,11 @@ module Square
36
39
  faraday.request :retry, max: max_retries, interval: retry_interval,
37
40
  backoff_factor: backoff_factor,
38
41
  retry_statuses: retry_statuses,
39
- methods: retry_methods
40
- faraday.adapter Faraday.default_adapter
42
+ methods: retry_methods,
43
+ retry_if: proc { |env, _exc|
44
+ env.request.context['forced_retry'] ||= false
45
+ }
46
+ faraday.adapter adapter
41
47
  faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
42
48
  faraday.options[:timeout] = timeout if timeout.positive?
43
49
  end
@@ -49,7 +55,9 @@ module Square
49
55
  http_request.http_method.downcase,
50
56
  http_request.query_url
51
57
  ) do |request|
52
- request.headers = http_request.headers
58
+ request.headers = http_request.headers.map { |k, v| [k.to_s, v.to_s] }
59
+ request.options.context ||= {}
60
+ request.options.context.merge!(http_request.context)
53
61
  unless http_request.http_method == HttpMethodEnum::GET &&
54
62
  http_request.parameters.empty?
55
63
  request.body = http_request.parameters
@@ -65,6 +73,8 @@ module Square
65
73
  http_request.query_url
66
74
  ) do |request|
67
75
  request.headers = http_request.headers
76
+ request.options.context ||= {}
77
+ request.options.context.merge!(http_request.context)
68
78
  unless http_request.http_method == HttpMethodEnum::GET &&
69
79
  http_request.parameters.empty?
70
80
  request.body = http_request.parameters