Class: Square::Subscriptions::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/square/subscriptions/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Square::Subscriptions::Client



7
8
9
# File 'lib/square/subscriptions/client.rb', line 7

def initialize(client:)
  @client = client
end

Instance Method Details

#bulk_swap_plan(request_options: {}, **params) ⇒ Square::Types::BulkSwapPlanResponse

Schedules a plan variation change for all active subscriptions under a given plan variation. For more information, see [Swap Subscription Plan Variations](developer.squareup.com/docs/subscriptions-api/swap-plan-variations).



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/square/subscriptions/client.rb', line 46

def bulk_swap_plan(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/subscriptions/bulk-swap-plan",
    body: params
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::BulkSwapPlanResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#cancel(request_options: {}, **params) ⇒ Square::Types::CancelSubscriptionResponse

Schedules a ‘CANCEL` action to cancel an active subscription. This sets the `canceled_date` field to the end of the active billing period. After this date, the subscription status changes from ACTIVE to CANCELED.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/square/subscriptions/client.rb', line 217

def cancel(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/cancel"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::CancelSubscriptionResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#change_billing_anchor_date(request_options: {}, **params) ⇒ Square::Types::ChangeBillingAnchorDateResponse

Changes the [billing anchor date](developer.squareup.com/docs/subscriptions-api/subscription-billing#billing-dates) for a subscription.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/square/subscriptions/client.rb', line 189

def change_billing_anchor_date(request_options: {}, **params)
  _path_param_names = ["subscription_id"]

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/billing-anchor",
    body: params.except(*_path_param_names)
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::ChangeBillingAnchorDateResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#create(request_options: {}, **params) ⇒ Square::Types::CreateSubscriptionResponse

Enrolls a customer in a subscription.

If you provide a card on file in the request, Square charges the card for the subscription. Otherwise, Square sends an invoice to the customer’s email address. The subscription starts immediately, unless the request includes the optional ‘start_date`. Each individual subscription is associated with a particular location.

For more information, see [Create a subscription](developer.squareup.com/docs/subscriptions-api/manage-subscriptions#create-a-subscription).



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/square/subscriptions/client.rb', line 21

def create(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/subscriptions",
    body: params
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::CreateSubscriptionResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#delete_action(request_options: {}, **params) ⇒ Square::Types::DeleteSubscriptionActionResponse

Deletes a scheduled action for a subscription.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/square/subscriptions/client.rb', line 165

def delete_action(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "DELETE",
    path: "v2/subscriptions/#{params[:subscription_id]}/actions/#{params[:action_id]}"
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::DeleteSubscriptionActionResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#get(request_options: {}, **params) ⇒ Square::Types::GetSubscriptionResponse

Retrieves a specific subscription.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/square/subscriptions/client.rb', line 107

def get(request_options: {}, **params)
  _query_param_names = [
    ["include"],
    %i[include]
  ].flatten
  _query = params.slice(*_query_param_names)
  params = params.except(*_query_param_names)

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "GET",
    path: "v2/subscriptions/#{params[:subscription_id]}",
    query: _query
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::GetSubscriptionResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#list_events(request_options: {}, **params) ⇒ Square::Types::ListSubscriptionEventsResponse

Lists all [events](developer.squareup.com/docs/subscriptions-api/actions-events) for a specific subscription.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/square/subscriptions/client.rb', line 240

def list_events(request_options: {}, **params)
  _query_param_names = [
    %w[cursor limit],
    %i[cursor limit]
  ].flatten
  _query = params.slice(*_query_param_names)
  params = params.except(*_query_param_names)

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "GET",
    path: "v2/subscriptions/#{params[:subscription_id]}/events",
    query: _query
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::ListSubscriptionEventsResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#pause(request_options: {}, **params) ⇒ Square::Types::PauseSubscriptionResponse

Schedules a ‘PAUSE` action to pause an active subscription.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/square/subscriptions/client.rb', line 271

def pause(request_options: {}, **params)
  _path_param_names = ["subscription_id"]

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/pause",
    body: params.except(*_path_param_names)
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::PauseSubscriptionResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#resume(request_options: {}, **params) ⇒ Square::Types::ResumeSubscriptionResponse

Schedules a ‘RESUME` action to resume a paused or a deactivated subscription.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/square/subscriptions/client.rb', line 297

def resume(request_options: {}, **params)
  _path_param_names = ["subscription_id"]

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/resume",
    body: params.except(*_path_param_names)
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::ResumeSubscriptionResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#search(request_options: {}, **params) ⇒ Square::Types::SearchSubscriptionsResponse

Searches for subscriptions.

Results are ordered chronologically by subscription creation date. If the request specifies more than one location ID, the endpoint orders the result by location ID, and then by creation date within each location. If no locations are given in the query, all locations are searched.

You can also optionally specify ‘customer_ids` to search by customer. If left unset, all customers associated with the specified locations are returned. If the request specifies customer IDs, the endpoint orders results first by location, within location by customer ID, and within customer by subscription creation date.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/square/subscriptions/client.rb', line 83

def search(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/subscriptions/search",
    body: params
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::SearchSubscriptionsResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#swap_plan(request_options: {}, **params) ⇒ Square::Types::SwapPlanResponse

Schedules a ‘SWAP_PLAN` action to swap a subscription plan variation in an existing subscription. For more information, see [Swap Subscription Plan Variations](developer.squareup.com/docs/subscriptions-api/swap-plan-variations).



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/square/subscriptions/client.rb', line 324

def swap_plan(request_options: {}, **params)
  _path_param_names = ["subscription_id"]

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/swap-plan",
    body: params.except(*_path_param_names)
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::SwapPlanResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end

#update(request_options: {}, **params) ⇒ Square::Types::UpdateSubscriptionResponse

Updates a subscription by modifying or clearing ‘subscription` field values. To clear a field, set its value to `null`.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/square/subscriptions/client.rb', line 139

def update(request_options: {}, **params)
  _path_param_names = ["subscription_id"]

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::PRODUCTION,
    method: "PUT",
    path: "v2/subscriptions/#{params[:subscription_id]}",
    body: params.except(*_path_param_names)
  )
  begin
    _response = @client.send(_request)
  rescue Net::HTTPRequestTimeout
    raise Square::Errors::TimeoutError
  end
  code = _response.code.to_i
  if code.between?(200, 299)
    Square::Types::UpdateSubscriptionResponse.load(_response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(_response.body, code: code)
  end
end