fangkuai.rb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +10 -0
- data/README.md +1 -0
- data/lib/square.rb +61 -0
- data/lib/square/api/apple_pay_api.rb +50 -0
- data/lib/square/api/bank_accounts_api.rb +136 -0
- data/lib/square/api/base_api.rb +43 -0
- data/lib/square/api/cash_drawers_api.rb +150 -0
- data/lib/square/api/catalog_api.rb +572 -0
- data/lib/square/api/checkout_api.rb +49 -0
- data/lib/square/api/customer_groups_api.rb +182 -0
- data/lib/square/api/customer_segments_api.rb +78 -0
- data/lib/square/api/customers_api.rb +418 -0
- data/lib/square/api/devices_api.rb +120 -0
- data/lib/square/api/disputes_api.rb +398 -0
- data/lib/square/api/employees_api.rb +87 -0
- data/lib/square/api/inventory_api.rb +296 -0
- data/lib/square/api/invoices_api.rb +358 -0
- data/lib/square/api/labor_api.rb +630 -0
- data/lib/square/api/locations_api.rb +151 -0
- data/lib/square/api/loyalty_api.rb +543 -0
- data/lib/square/api/merchants_api.rb +83 -0
- data/lib/square/api/mobile_authorization_api.rb +52 -0
- data/lib/square/api/o_auth_api.rb +163 -0
- data/lib/square/api/orders_api.rb +280 -0
- data/lib/square/api/payments_api.rb +279 -0
- data/lib/square/api/refunds_api.rb +145 -0
- data/lib/square/api/subscriptions_api.rb +251 -0
- data/lib/square/api/team_api.rb +326 -0
- data/lib/square/api/terminal_api.rb +141 -0
- data/lib/square/api/transactions_api.rb +369 -0
- data/lib/square/api/v1_employees_api.rb +723 -0
- data/lib/square/api/v1_items_api.rb +1686 -0
- data/lib/square/api/v1_locations_api.rb +65 -0
- data/lib/square/api/v1_transactions_api.rb +572 -0
- data/lib/square/api_helper.rb +276 -0
- data/lib/square/client.rb +211 -0
- data/lib/square/configuration.rb +101 -0
- data/lib/square/exceptions/api_exception.rb +15 -0
- data/lib/square/http/api_response.rb +45 -0
- data/lib/square/http/auth/o_auth2.rb +12 -0
- data/lib/square/http/faraday_client.rb +55 -0
- data/lib/square/http/http_call_back.rb +19 -0
- data/lib/square/http/http_client.rb +99 -0
- data/lib/square/http/http_method_enum.rb +8 -0
- data/lib/square/http/http_request.rb +45 -0
- data/lib/square/http/http_response.rb +24 -0
- data/lib/square/utilities/file_wrapper.rb +12 -0
- data/spec/user_journey_spec.rb +148 -0
- data/test/api/api_test_base.rb +24 -0
- data/test/api/test_catalog_api.rb +59 -0
- data/test/api/test_customers_api.rb +45 -0
- data/test/api/test_employees_api.rb +36 -0
- data/test/api/test_labor_api.rb +74 -0
- data/test/api/test_locations_api.rb +35 -0
- data/test/api/test_merchants_api.rb +40 -0
- data/test/api/test_payments_api.rb +42 -0
- data/test/api/test_refunds_api.rb +41 -0
- data/test/http_response_catcher.rb +19 -0
- data/test/test_helper.rb +94 -0
- metadata +199 -0
@@ -0,0 +1,279 @@
|
|
1
|
+
module Square
|
2
|
+
# PaymentsApi
|
3
|
+
class PaymentsApi < 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 payments taken by the account making the request.
|
9
|
+
# Max results per page: 100
|
10
|
+
# @param [String] begin_time Optional parameter: Timestamp for the beginning
|
11
|
+
# of the reporting period, in RFC 3339 format. Inclusive. Default: The
|
12
|
+
# current time minus one year.
|
13
|
+
# @param [String] end_time Optional parameter: Timestamp for the end of the
|
14
|
+
# requested reporting period, in RFC 3339 format. Default: The current
|
15
|
+
# time.
|
16
|
+
# @param [String] sort_order Optional parameter: The order in which results
|
17
|
+
# are listed. - `ASC` - oldest to newest - `DESC` - newest to oldest
|
18
|
+
# (default).
|
19
|
+
# @param [String] cursor Optional parameter: A pagination cursor returned by
|
20
|
+
# a previous call to this endpoint. Provide this to retrieve the next set of
|
21
|
+
# results for the original query. See
|
22
|
+
# [Pagination](https://developer.squareup.com/docs/basics/api101/pagination)
|
23
|
+
# for more information.
|
24
|
+
# @param [String] location_id Optional parameter: Limit results to the
|
25
|
+
# location supplied. By default, results are returned for all locations
|
26
|
+
# associated with the merchant.
|
27
|
+
# @param [Long] total Optional parameter: The exact amount in the
|
28
|
+
# total_money for a `Payment`.
|
29
|
+
# @param [String] last_4 Optional parameter: The last 4 digits of `Payment`
|
30
|
+
# card.
|
31
|
+
# @param [String] card_brand Optional parameter: The brand of `Payment`
|
32
|
+
# card. For example, `VISA`
|
33
|
+
# @return [ListPaymentsResponse Hash] response from the API call
|
34
|
+
def list_payments(begin_time: nil,
|
35
|
+
end_time: nil,
|
36
|
+
sort_order: nil,
|
37
|
+
cursor: nil,
|
38
|
+
location_id: nil,
|
39
|
+
total: nil,
|
40
|
+
last_4: nil,
|
41
|
+
card_brand: nil)
|
42
|
+
# Prepare query url.
|
43
|
+
_query_builder = config.get_base_uri
|
44
|
+
_query_builder << '/v2/payments'
|
45
|
+
_query_builder = APIHelper.append_url_with_query_parameters(
|
46
|
+
_query_builder,
|
47
|
+
'begin_time' => begin_time,
|
48
|
+
'end_time' => end_time,
|
49
|
+
'sort_order' => sort_order,
|
50
|
+
'cursor' => cursor,
|
51
|
+
'location_id' => location_id,
|
52
|
+
'total' => total,
|
53
|
+
'last_4' => last_4,
|
54
|
+
'card_brand' => card_brand
|
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(_response, data: decoded, errors: _errors)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Charges a payment source, for example, a card
|
78
|
+
# represented by customer's card on file or a card nonce. In addition
|
79
|
+
# to the payment source, the request must also include the
|
80
|
+
# amount to accept for the payment.
|
81
|
+
# There are several optional parameters that you can include in the request.
|
82
|
+
# For example, tip money, whether to autocomplete the payment, or a
|
83
|
+
# reference ID
|
84
|
+
# to correlate this payment with another system.
|
85
|
+
# For more information about these
|
86
|
+
# payment options, see [Take
|
87
|
+
# Payments](https://developer.squareup.com/docs/payments-api/take-payments).
|
88
|
+
# The `PAYMENTS_WRITE_ADDITIONAL_RECIPIENTS` OAuth permission is required
|
89
|
+
# to enable application fees.
|
90
|
+
# @param [CreatePaymentRequest] body Required parameter: An object
|
91
|
+
# containing the fields to POST for the request. See the corresponding
|
92
|
+
# object definition for field details.
|
93
|
+
# @return [CreatePaymentResponse Hash] response from the API call
|
94
|
+
def create_payment(body:)
|
95
|
+
# Prepare query url.
|
96
|
+
_query_builder = config.get_base_uri
|
97
|
+
_query_builder << '/v2/payments'
|
98
|
+
_query_url = APIHelper.clean_url _query_builder
|
99
|
+
|
100
|
+
# Prepare headers.
|
101
|
+
_headers = {
|
102
|
+
'accept' => 'application/json',
|
103
|
+
'content-type' => 'application/json; charset=utf-8'
|
104
|
+
}
|
105
|
+
|
106
|
+
# Prepare and execute HttpRequest.
|
107
|
+
_request = config.http_client.post(
|
108
|
+
_query_url,
|
109
|
+
headers: _headers,
|
110
|
+
parameters: body.to_json
|
111
|
+
)
|
112
|
+
OAuth2.apply(config, _request)
|
113
|
+
_response = execute_request(_request)
|
114
|
+
|
115
|
+
# Return appropriate response type.
|
116
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
117
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
118
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Cancels (voids) a payment identified by the idempotency key that is
|
122
|
+
# specified in the
|
123
|
+
# request.
|
124
|
+
# Use this method when status of a CreatePayment request is unknown. For
|
125
|
+
# example, after you send a
|
126
|
+
# CreatePayment request a network error occurs and you don't get a response.
|
127
|
+
# In this case, you can
|
128
|
+
# direct Square to cancel the payment using this endpoint. In the request,
|
129
|
+
# you provide the same
|
130
|
+
# idempotency key that you provided in your CreatePayment request you want
|
131
|
+
# to cancel. After
|
132
|
+
# cancelling the payment, you can submit your CreatePayment request again.
|
133
|
+
# Note that if no payment with the specified idempotency key is found, no
|
134
|
+
# action is taken, the end
|
135
|
+
# point returns successfully.
|
136
|
+
# @param [CancelPaymentByIdempotencyKeyRequest] body Required parameter: An
|
137
|
+
# object containing the fields to POST for the request. See the
|
138
|
+
# corresponding object definition for field details.
|
139
|
+
# @return [CancelPaymentByIdempotencyKeyResponse Hash] response from the API call
|
140
|
+
def cancel_payment_by_idempotency_key(body:)
|
141
|
+
# Prepare query url.
|
142
|
+
_query_builder = config.get_base_uri
|
143
|
+
_query_builder << '/v2/payments/cancel'
|
144
|
+
_query_url = APIHelper.clean_url _query_builder
|
145
|
+
|
146
|
+
# Prepare headers.
|
147
|
+
_headers = {
|
148
|
+
'accept' => 'application/json',
|
149
|
+
'content-type' => 'application/json; charset=utf-8'
|
150
|
+
}
|
151
|
+
|
152
|
+
# Prepare and execute HttpRequest.
|
153
|
+
_request = config.http_client.post(
|
154
|
+
_query_url,
|
155
|
+
headers: _headers,
|
156
|
+
parameters: body.to_json
|
157
|
+
)
|
158
|
+
OAuth2.apply(config, _request)
|
159
|
+
_response = execute_request(_request)
|
160
|
+
|
161
|
+
# Return appropriate response type.
|
162
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
163
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
164
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
165
|
+
end
|
166
|
+
|
167
|
+
# Retrieves details for a specific Payment.
|
168
|
+
# @param [String] payment_id Required parameter: Unique ID for the desired
|
169
|
+
# `Payment`.
|
170
|
+
# @return [GetPaymentResponse Hash] response from the API call
|
171
|
+
def get_payment(payment_id:)
|
172
|
+
# Prepare query url.
|
173
|
+
_query_builder = config.get_base_uri
|
174
|
+
_query_builder << '/v2/payments/{payment_id}'
|
175
|
+
_query_builder = APIHelper.append_url_with_template_parameters(
|
176
|
+
_query_builder,
|
177
|
+
'payment_id' => payment_id
|
178
|
+
)
|
179
|
+
_query_url = APIHelper.clean_url _query_builder
|
180
|
+
|
181
|
+
# Prepare headers.
|
182
|
+
_headers = {
|
183
|
+
'accept' => 'application/json'
|
184
|
+
}
|
185
|
+
|
186
|
+
# Prepare and execute HttpRequest.
|
187
|
+
_request = config.http_client.get(
|
188
|
+
_query_url,
|
189
|
+
headers: _headers
|
190
|
+
)
|
191
|
+
OAuth2.apply(config, _request)
|
192
|
+
_response = execute_request(_request)
|
193
|
+
|
194
|
+
# Return appropriate response type.
|
195
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
196
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
197
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
198
|
+
end
|
199
|
+
|
200
|
+
# Cancels (voids) a payment. If you set `autocomplete` to false when
|
201
|
+
# creating a payment,
|
202
|
+
# you can cancel the payment using this endpoint. For more information, see
|
203
|
+
# [Delayed
|
204
|
+
# Payments](https://developer.squareup.com/docs/payments-api/take-payments#d
|
205
|
+
# elayed-payments).
|
206
|
+
# @param [String] payment_id Required parameter: `payment_id` identifying
|
207
|
+
# the payment to be canceled.
|
208
|
+
# @return [CancelPaymentResponse Hash] response from the API call
|
209
|
+
def cancel_payment(payment_id:)
|
210
|
+
# Prepare query url.
|
211
|
+
_query_builder = config.get_base_uri
|
212
|
+
_query_builder << '/v2/payments/{payment_id}/cancel'
|
213
|
+
_query_builder = APIHelper.append_url_with_template_parameters(
|
214
|
+
_query_builder,
|
215
|
+
'payment_id' => payment_id
|
216
|
+
)
|
217
|
+
_query_url = APIHelper.clean_url _query_builder
|
218
|
+
|
219
|
+
# Prepare headers.
|
220
|
+
_headers = {
|
221
|
+
'accept' => 'application/json'
|
222
|
+
}
|
223
|
+
|
224
|
+
# Prepare and execute HttpRequest.
|
225
|
+
_request = config.http_client.post(
|
226
|
+
_query_url,
|
227
|
+
headers: _headers
|
228
|
+
)
|
229
|
+
OAuth2.apply(config, _request)
|
230
|
+
_response = execute_request(_request)
|
231
|
+
|
232
|
+
# Return appropriate response type.
|
233
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
234
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
235
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
236
|
+
end
|
237
|
+
|
238
|
+
# Completes (captures) a payment.
|
239
|
+
# By default, payments are set to complete immediately after they are
|
240
|
+
# created.
|
241
|
+
# If you set autocomplete to false when creating a payment, you can complete
|
242
|
+
# (capture)
|
243
|
+
# the payment using this endpoint. For more information, see
|
244
|
+
# [Delayed
|
245
|
+
# Payments](https://developer.squareup.com/docs/payments-api/take-payments#d
|
246
|
+
# elayed-payments).
|
247
|
+
# @param [String] payment_id Required parameter: Unique ID identifying the
|
248
|
+
# payment to be completed.
|
249
|
+
# @return [CompletePaymentResponse Hash] response from the API call
|
250
|
+
def complete_payment(payment_id:)
|
251
|
+
# Prepare query url.
|
252
|
+
_query_builder = config.get_base_uri
|
253
|
+
_query_builder << '/v2/payments/{payment_id}/complete'
|
254
|
+
_query_builder = APIHelper.append_url_with_template_parameters(
|
255
|
+
_query_builder,
|
256
|
+
'payment_id' => payment_id
|
257
|
+
)
|
258
|
+
_query_url = APIHelper.clean_url _query_builder
|
259
|
+
|
260
|
+
# Prepare headers.
|
261
|
+
_headers = {
|
262
|
+
'accept' => 'application/json'
|
263
|
+
}
|
264
|
+
|
265
|
+
# Prepare and execute HttpRequest.
|
266
|
+
_request = config.http_client.post(
|
267
|
+
_query_url,
|
268
|
+
headers: _headers
|
269
|
+
)
|
270
|
+
OAuth2.apply(config, _request)
|
271
|
+
_response = execute_request(_request)
|
272
|
+
|
273
|
+
# Return appropriate response type.
|
274
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
275
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
276
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
module Square
|
2
|
+
# RefundsApi
|
3
|
+
class RefundsApi < 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 refunds for the account making the request.
|
9
|
+
# Max results per page: 100
|
10
|
+
# @param [String] begin_time Optional parameter: Timestamp for the beginning
|
11
|
+
# of the requested reporting period, in RFC 3339 format. Default: The
|
12
|
+
# current time minus one year.
|
13
|
+
# @param [String] end_time Optional parameter: Timestamp for the end of the
|
14
|
+
# requested reporting period, in RFC 3339 format. Default: The current
|
15
|
+
# time.
|
16
|
+
# @param [String] sort_order Optional parameter: The order in which results
|
17
|
+
# are listed. - `ASC` - oldest to newest - `DESC` - newest to oldest
|
18
|
+
# (default).
|
19
|
+
# @param [String] cursor Optional parameter: A pagination cursor returned by
|
20
|
+
# a previous call to this endpoint. Provide this to retrieve the next set of
|
21
|
+
# results for the original query. See
|
22
|
+
# [Pagination](https://developer.squareup.com/docs/basics/api101/pagination)
|
23
|
+
# for more information.
|
24
|
+
# @param [String] location_id Optional parameter: Limit results to the
|
25
|
+
# location supplied. By default, results are returned for all locations
|
26
|
+
# associated with the merchant.
|
27
|
+
# @param [String] status Optional parameter: If provided, only refunds with
|
28
|
+
# the given status are returned. For a list of refund status values, see
|
29
|
+
# [PaymentRefund](#type-paymentrefund). Default: If omitted refunds are
|
30
|
+
# returned regardless of status.
|
31
|
+
# @param [String] source_type Optional parameter: If provided, only refunds
|
32
|
+
# with the given source type are returned. - `CARD` - List refunds only for
|
33
|
+
# payments where card was specified as payment source. Default: If omitted
|
34
|
+
# refunds are returned regardless of source type.
|
35
|
+
# @return [ListPaymentRefundsResponse Hash] response from the API call
|
36
|
+
def list_payment_refunds(begin_time: nil,
|
37
|
+
end_time: nil,
|
38
|
+
sort_order: nil,
|
39
|
+
cursor: nil,
|
40
|
+
location_id: nil,
|
41
|
+
status: nil,
|
42
|
+
source_type: nil)
|
43
|
+
# Prepare query url.
|
44
|
+
_query_builder = config.get_base_uri
|
45
|
+
_query_builder << '/v2/refunds'
|
46
|
+
_query_builder = APIHelper.append_url_with_query_parameters(
|
47
|
+
_query_builder,
|
48
|
+
'begin_time' => begin_time,
|
49
|
+
'end_time' => end_time,
|
50
|
+
'sort_order' => sort_order,
|
51
|
+
'cursor' => cursor,
|
52
|
+
'location_id' => location_id,
|
53
|
+
'status' => status,
|
54
|
+
'source_type' => source_type
|
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(_response, data: decoded, errors: _errors)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Refunds a payment. You can refund the entire payment amount or a
|
78
|
+
# portion of it. For more information, see
|
79
|
+
# [Payments and Refunds
|
80
|
+
# Overview](https://developer.squareup.com/docs/payments-api/overview).
|
81
|
+
# @param [RefundPaymentRequest] body Required parameter: An object
|
82
|
+
# containing the fields to POST for the request. See the corresponding
|
83
|
+
# object definition for field details.
|
84
|
+
# @return [RefundPaymentResponse Hash] response from the API call
|
85
|
+
def refund_payment(body:)
|
86
|
+
# Prepare query url.
|
87
|
+
_query_builder = config.get_base_uri
|
88
|
+
_query_builder << '/v2/refunds'
|
89
|
+
_query_url = APIHelper.clean_url _query_builder
|
90
|
+
|
91
|
+
# Prepare headers.
|
92
|
+
_headers = {
|
93
|
+
'accept' => 'application/json',
|
94
|
+
'content-type' => 'application/json; charset=utf-8'
|
95
|
+
}
|
96
|
+
|
97
|
+
# Prepare and execute HttpRequest.
|
98
|
+
_request = config.http_client.post(
|
99
|
+
_query_url,
|
100
|
+
headers: _headers,
|
101
|
+
parameters: body.to_json
|
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(_response, data: decoded, errors: _errors)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Retrieves a specific `Refund` using the `refund_id`.
|
113
|
+
# @param [String] refund_id Required parameter: Unique ID for the desired
|
114
|
+
# `PaymentRefund`.
|
115
|
+
# @return [GetPaymentRefundResponse Hash] response from the API call
|
116
|
+
def get_payment_refund(refund_id:)
|
117
|
+
# Prepare query url.
|
118
|
+
_query_builder = config.get_base_uri
|
119
|
+
_query_builder << '/v2/refunds/{refund_id}'
|
120
|
+
_query_builder = APIHelper.append_url_with_template_parameters(
|
121
|
+
_query_builder,
|
122
|
+
'refund_id' => refund_id
|
123
|
+
)
|
124
|
+
_query_url = APIHelper.clean_url _query_builder
|
125
|
+
|
126
|
+
# Prepare headers.
|
127
|
+
_headers = {
|
128
|
+
'accept' => 'application/json'
|
129
|
+
}
|
130
|
+
|
131
|
+
# Prepare and execute HttpRequest.
|
132
|
+
_request = config.http_client.get(
|
133
|
+
_query_url,
|
134
|
+
headers: _headers
|
135
|
+
)
|
136
|
+
OAuth2.apply(config, _request)
|
137
|
+
_response = execute_request(_request)
|
138
|
+
|
139
|
+
# Return appropriate response type.
|
140
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
141
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
142
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,251 @@
|
|
1
|
+
module Square
|
2
|
+
# SubscriptionsApi
|
3
|
+
class SubscriptionsApi < BaseApi
|
4
|
+
def initialize(config, http_call_back: nil)
|
5
|
+
super(config, http_call_back: http_call_back)
|
6
|
+
end
|
7
|
+
|
8
|
+
# Creates a subscription for a customer to a subscription plan.
|
9
|
+
# If you provide a card on file in the request, Square charges the card for
|
10
|
+
# the subscription. Otherwise, Square bills an invoice to the customer's
|
11
|
+
# email
|
12
|
+
# address. The subscription starts immediately, unless the request includes
|
13
|
+
# the optional `start_date`. Each individual subscription is associated with
|
14
|
+
# a particular location.
|
15
|
+
# @param [CreateSubscriptionRequest] body Required parameter: An object
|
16
|
+
# containing the fields to POST for the request. See the corresponding
|
17
|
+
# object definition for field details.
|
18
|
+
# @return [CreateSubscriptionResponse Hash] response from the API call
|
19
|
+
def create_subscription(body:)
|
20
|
+
# Prepare query url.
|
21
|
+
_query_builder = config.get_base_uri
|
22
|
+
_query_builder << '/v2/subscriptions'
|
23
|
+
_query_url = APIHelper.clean_url _query_builder
|
24
|
+
|
25
|
+
# Prepare headers.
|
26
|
+
_headers = {
|
27
|
+
'accept' => 'application/json',
|
28
|
+
'content-type' => 'application/json; charset=utf-8'
|
29
|
+
}
|
30
|
+
|
31
|
+
# Prepare and execute HttpRequest.
|
32
|
+
_request = config.http_client.post(
|
33
|
+
_query_url,
|
34
|
+
headers: _headers,
|
35
|
+
parameters: body.to_json
|
36
|
+
)
|
37
|
+
OAuth2.apply(config, _request)
|
38
|
+
_response = execute_request(_request)
|
39
|
+
|
40
|
+
# Return appropriate response type.
|
41
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
42
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
43
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Searches for subscriptions.
|
47
|
+
# Results are ordered chronologically by subscription creation date. If
|
48
|
+
# the request specifies more than one location ID,
|
49
|
+
# the endpoint orders the result
|
50
|
+
# by location ID, and then by creation date within each location. If no
|
51
|
+
# locations are given
|
52
|
+
# in the query, all locations are searched.
|
53
|
+
# You can also optionally specify `customer_ids` to search by customer.
|
54
|
+
# If left unset, all customers
|
55
|
+
# associated with the specified locations are returned.
|
56
|
+
# If the request specifies customer IDs, the endpoint orders results
|
57
|
+
# first by location, within location by customer ID, and within
|
58
|
+
# customer by subscription creation date.
|
59
|
+
# For more information, see
|
60
|
+
# [Retrieve
|
61
|
+
# subscriptions](https://developer.squareup.com/docs/docs/subscriptions-api/
|
62
|
+
# overview#retrieve-subscriptions).
|
63
|
+
# @param [SearchSubscriptionsRequest] body Required parameter: An object
|
64
|
+
# containing the fields to POST for the request. See the corresponding
|
65
|
+
# object definition for field details.
|
66
|
+
# @return [SearchSubscriptionsResponse Hash] response from the API call
|
67
|
+
def search_subscriptions(body:)
|
68
|
+
# Prepare query url.
|
69
|
+
_query_builder = config.get_base_uri
|
70
|
+
_query_builder << '/v2/subscriptions/search'
|
71
|
+
_query_url = APIHelper.clean_url _query_builder
|
72
|
+
|
73
|
+
# Prepare headers.
|
74
|
+
_headers = {
|
75
|
+
'accept' => 'application/json',
|
76
|
+
'content-type' => 'application/json; charset=utf-8'
|
77
|
+
}
|
78
|
+
|
79
|
+
# Prepare and execute HttpRequest.
|
80
|
+
_request = config.http_client.post(
|
81
|
+
_query_url,
|
82
|
+
headers: _headers,
|
83
|
+
parameters: body.to_json
|
84
|
+
)
|
85
|
+
OAuth2.apply(config, _request)
|
86
|
+
_response = execute_request(_request)
|
87
|
+
|
88
|
+
# Return appropriate response type.
|
89
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
90
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
91
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Retrieves a subscription.
|
95
|
+
# @param [String] subscription_id Required parameter: The ID of the
|
96
|
+
# subscription to retrieve.
|
97
|
+
# @return [RetrieveSubscriptionResponse Hash] response from the API call
|
98
|
+
def retrieve_subscription(subscription_id:)
|
99
|
+
# Prepare query url.
|
100
|
+
_query_builder = config.get_base_uri
|
101
|
+
_query_builder << '/v2/subscriptions/{subscription_id}'
|
102
|
+
_query_builder = APIHelper.append_url_with_template_parameters(
|
103
|
+
_query_builder,
|
104
|
+
'subscription_id' => subscription_id
|
105
|
+
)
|
106
|
+
_query_url = APIHelper.clean_url _query_builder
|
107
|
+
|
108
|
+
# Prepare headers.
|
109
|
+
_headers = {
|
110
|
+
'accept' => 'application/json'
|
111
|
+
}
|
112
|
+
|
113
|
+
# Prepare and execute HttpRequest.
|
114
|
+
_request = config.http_client.get(
|
115
|
+
_query_url,
|
116
|
+
headers: _headers
|
117
|
+
)
|
118
|
+
OAuth2.apply(config, _request)
|
119
|
+
_response = execute_request(_request)
|
120
|
+
|
121
|
+
# Return appropriate response type.
|
122
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
123
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
124
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Updates a subscription. You can set, modify, and clear the
|
128
|
+
# `subscription` field values.
|
129
|
+
# @param [String] subscription_id Required parameter: The ID for the
|
130
|
+
# subscription to update.
|
131
|
+
# @param [UpdateSubscriptionRequest] body Required parameter: An object
|
132
|
+
# containing the fields to POST for the request. See the corresponding
|
133
|
+
# object definition for field details.
|
134
|
+
# @return [UpdateSubscriptionResponse Hash] response from the API call
|
135
|
+
def update_subscription(subscription_id:,
|
136
|
+
body:)
|
137
|
+
# Prepare query url.
|
138
|
+
_query_builder = config.get_base_uri
|
139
|
+
_query_builder << '/v2/subscriptions/{subscription_id}'
|
140
|
+
_query_builder = APIHelper.append_url_with_template_parameters(
|
141
|
+
_query_builder,
|
142
|
+
'subscription_id' => subscription_id
|
143
|
+
)
|
144
|
+
_query_url = APIHelper.clean_url _query_builder
|
145
|
+
|
146
|
+
# Prepare headers.
|
147
|
+
_headers = {
|
148
|
+
'accept' => 'application/json',
|
149
|
+
'content-type' => 'application/json; charset=utf-8'
|
150
|
+
}
|
151
|
+
|
152
|
+
# Prepare and execute HttpRequest.
|
153
|
+
_request = config.http_client.put(
|
154
|
+
_query_url,
|
155
|
+
headers: _headers,
|
156
|
+
parameters: body.to_json
|
157
|
+
)
|
158
|
+
OAuth2.apply(config, _request)
|
159
|
+
_response = execute_request(_request)
|
160
|
+
|
161
|
+
# Return appropriate response type.
|
162
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
163
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
164
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
165
|
+
end
|
166
|
+
|
167
|
+
# Sets the `canceled_date` field to the end of the active billing period.
|
168
|
+
# After this date, the status changes from ACTIVE to CANCELED.
|
169
|
+
# @param [String] subscription_id Required parameter: The ID of the
|
170
|
+
# subscription to cancel.
|
171
|
+
# @return [CancelSubscriptionResponse Hash] response from the API call
|
172
|
+
def cancel_subscription(subscription_id:)
|
173
|
+
# Prepare query url.
|
174
|
+
_query_builder = config.get_base_uri
|
175
|
+
_query_builder << '/v2/subscriptions/{subscription_id}/cancel'
|
176
|
+
_query_builder = APIHelper.append_url_with_template_parameters(
|
177
|
+
_query_builder,
|
178
|
+
'subscription_id' => subscription_id
|
179
|
+
)
|
180
|
+
_query_url = APIHelper.clean_url _query_builder
|
181
|
+
|
182
|
+
# Prepare headers.
|
183
|
+
_headers = {
|
184
|
+
'accept' => 'application/json'
|
185
|
+
}
|
186
|
+
|
187
|
+
# Prepare and execute HttpRequest.
|
188
|
+
_request = config.http_client.post(
|
189
|
+
_query_url,
|
190
|
+
headers: _headers
|
191
|
+
)
|
192
|
+
OAuth2.apply(config, _request)
|
193
|
+
_response = execute_request(_request)
|
194
|
+
|
195
|
+
# Return appropriate response type.
|
196
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
197
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
198
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
199
|
+
end
|
200
|
+
|
201
|
+
# Lists all events for a specific subscription.
|
202
|
+
# In the current implementation, only `START_SUBSCRIPTION` and
|
203
|
+
# `STOP_SUBSCRIPTION` (when the subscription was canceled) events are
|
204
|
+
# returned.
|
205
|
+
# @param [String] subscription_id Required parameter: The ID of the
|
206
|
+
# subscription to retrieve the events for.
|
207
|
+
# @param [String] cursor Optional parameter: A pagination cursor returned by
|
208
|
+
# a previous call to this endpoint. Provide this to retrieve the next set of
|
209
|
+
# results for the original query. For more information, see
|
210
|
+
# [Pagination](https://developer.squareup.com/docs/docs/working-with-apis/pa
|
211
|
+
# gination).
|
212
|
+
# @param [Integer] limit Optional parameter: The upper limit on the number
|
213
|
+
# of subscription events to return in the response. Default: `200`
|
214
|
+
# @return [ListSubscriptionEventsResponse Hash] response from the API call
|
215
|
+
def list_subscription_events(subscription_id:,
|
216
|
+
cursor: nil,
|
217
|
+
limit: nil)
|
218
|
+
# Prepare query url.
|
219
|
+
_query_builder = config.get_base_uri
|
220
|
+
_query_builder << '/v2/subscriptions/{subscription_id}/events'
|
221
|
+
_query_builder = APIHelper.append_url_with_template_parameters(
|
222
|
+
_query_builder,
|
223
|
+
'subscription_id' => subscription_id
|
224
|
+
)
|
225
|
+
_query_builder = APIHelper.append_url_with_query_parameters(
|
226
|
+
_query_builder,
|
227
|
+
'cursor' => cursor,
|
228
|
+
'limit' => limit
|
229
|
+
)
|
230
|
+
_query_url = APIHelper.clean_url _query_builder
|
231
|
+
|
232
|
+
# Prepare headers.
|
233
|
+
_headers = {
|
234
|
+
'accept' => 'application/json'
|
235
|
+
}
|
236
|
+
|
237
|
+
# Prepare and execute HttpRequest.
|
238
|
+
_request = config.http_client.get(
|
239
|
+
_query_url,
|
240
|
+
headers: _headers
|
241
|
+
)
|
242
|
+
OAuth2.apply(config, _request)
|
243
|
+
_response = execute_request(_request)
|
244
|
+
|
245
|
+
# Return appropriate response type.
|
246
|
+
decoded = APIHelper.json_deserialize(_response.raw_body)
|
247
|
+
_errors = APIHelper.map_response(decoded, ['errors'])
|
248
|
+
ApiResponse.new(_response, data: decoded, errors: _errors)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|