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).



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/square/subscriptions/client.rb', line 40

def bulk_swap_plan(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::SANDBOX,
    method: "POST",
    path: "v2/subscriptions/bulk-swap-plan",
    body: params
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::BulkSwapPlanResponse.load(_response.body)
  end

  raise _response.body
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.



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/square/subscriptions/client.rb', line 172

def cancel(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::SANDBOX,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/cancel"
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::CancelSubscriptionResponse.load(_response.body)
  end

  raise _response.body
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.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/square/subscriptions/client.rb', line 150

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::SANDBOX,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/billing-anchor",
    body: params.except(*_path_param_names)
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::ChangeBillingAnchorDateResponse.load(_response.body)
  end

  raise _response.body
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
# 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::SANDBOX,
    method: "POST",
    path: "v2/subscriptions",
    body: params
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::CreateSubscriptionResponse.load(_response.body)
  end

  raise _response.body
end

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

Deletes a scheduled action for a subscription.



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/square/subscriptions/client.rb', line 132

def delete_action(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::SANDBOX,
    method: "DELETE",
    path: "v2/subscriptions/#{params[:subscription_id]}/actions/#{params[:action_id]}"
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::DeleteSubscriptionActionResponse.load(_response.body)
  end

  raise _response.body
end

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

Retrieves a specific subscription.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/square/subscriptions/client.rb', line 89

def get(request_options: {}, **params)
  _query_param_names = ["include"]
  _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::SANDBOX,
    method: "GET",
    path: "v2/subscriptions/#{params[:subscription_id]}",
    query: _query
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::GetSubscriptionResponse.load(_response.body)
  end

  raise _response.body
end

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

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



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

def list_events(request_options: {}, **params)
  _query_param_names = %w[cursor limit]
  _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::SANDBOX,
    method: "GET",
    path: "v2/subscriptions/#{params[:subscription_id]}/events",
    query: _query
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::ListSubscriptionEventsResponse.load(_response.body)
  end

  raise _response.body
end

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

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/square/subscriptions/client.rb', line 211

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

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::SANDBOX,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/pause",
    body: params.except(*_path_param_names)
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::PauseSubscriptionResponse.load(_response.body)
  end

  raise _response.body
end

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

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



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/square/subscriptions/client.rb', line 231

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

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::SANDBOX,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/resume",
    body: params.except(*_path_param_names)
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::ResumeSubscriptionResponse.load(_response.body)
  end

  raise _response.body
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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/square/subscriptions/client.rb', line 71

def search(request_options: {}, **params)
  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::SANDBOX,
    method: "POST",
    path: "v2/subscriptions/search",
    body: params
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::SearchSubscriptionsResponse.load(_response.body)
  end

  raise _response.body
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).



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/square/subscriptions/client.rb', line 252

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::SANDBOX,
    method: "POST",
    path: "v2/subscriptions/#{params[:subscription_id]}/swap-plan",
    body: params.except(*_path_param_names)
  )
  _response = @client.send(_request)
  return Square::Types::SwapPlanResponse.load(_response.body) if _response.code >= "200" && _response.code < "300"

  raise _response.body
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`.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/square/subscriptions/client.rb', line 112

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

  _request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url] || Square::Environment::SANDBOX,
    method: "PUT",
    path: "v2/subscriptions/#{params[:subscription_id]}",
    body: params.except(*_path_param_names)
  )
  _response = @client.send(_request)
  if _response.code >= "200" && _response.code < "300"
    return Square::Types::UpdateSubscriptionResponse.load(_response.body)
  end

  raise _response.body
end