Class: Square::Invoices::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void

Parameters:



9
10
11
# File 'lib/square/invoices/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

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

Cancels an invoice. The seller cannot collect payments for the canceled invoice.

You cannot cancel an invoice in the ‘DRAFT` state or in a terminal state: `PAID`, `REFUNDED`, `CANCELED`, or `FAILED`.

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :invoice_id (String)

Returns:



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/square/invoices/client.rb', line 370

def cancel(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  request_data = Square::Invoices::Types::CancelInvoiceRequest.new(params).to_h
  non_body_param_names = ["invoice_id"]
  body = request_data.except(*non_body_param_names)

  request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "v2/invoices/#{params[:invoice_id]}/cancel",
    body: body,
    request_options: request_options
  )
  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::CancelInvoiceResponse.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::CreateInvoiceResponse

Creates a draft [invoice](entity:Invoice) for an order created using the Orders API.

A draft invoice remains in your account and no action is taken. You must publish the invoice before Square can process it (send it to the customer’s email address or charge the customer’s card on file).

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:



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

def create(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "v2/invoices",
    body: Square::Invoices::Types::CreateInvoiceRequest.new(params).to_h,
    request_options: request_options
  )
  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::CreateInvoiceResponse.load(response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#create_invoice_attachment(request_options: {}, **params) ⇒ Square::Types::CreateInvoiceAttachmentResponse

Uploads a file and attaches it to an invoice. This endpoint accepts HTTP multipart/form-data file uploads with a JSON ‘request` part and a `file` part. The `file` part must be a `readable stream` that contains a file in a supported format: GIF, JPEG, PNG, TIFF, BMP, or PDF.

Invoices can have up to 10 attachments with a total file size of 25 MB. Attachments can be added only to invoices in the ‘DRAFT`, `SCHEDULED`, `UNPAID`, or `PARTIALLY_PAID` state.

NOTE: When testing in the Sandbox environment, the total file size is limited to 1 KB.

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (void)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :invoice_id (String)

Returns:



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/square/invoices/client.rb', line 284

def create_invoice_attachment(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  body = Internal::Multipart::FormData.new

  if params[:request]
    body.add(
      name: "request",
      value: params[:request],
      content_type: "application/json; charset=utf-8"
    )
  end
  body.add_part(params[:image_file].to_form_data_part(name: "image_file")) if params[:image_file]

  request = Square::Internal::Multipart::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "v2/invoices/#{params[:invoice_id]}/attachments",
    body: body,
    request_options: request_options
  )
  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::CreateInvoiceAttachmentResponse.load(response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#delete(request_options: {}, **params) ⇒ Square::Types::DeleteInvoiceResponse

Deletes the specified invoice. When an invoice is deleted, the associated order status changes to CANCELED. You can only delete a draft invoice (you cannot delete a published invoice, including one that is scheduled for processing).

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :invoice_id (String)
  • :version (Integer, nil)

Returns:



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/square/invoices/client.rb', line 236

def delete(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  query_param_names = %i[version]
  query_params = {}
  query_params["version"] = params[:version] if params.key?(:version)
  params = params.except(*query_param_names)

  request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "DELETE",
    path: "v2/invoices/#{params[:invoice_id]}",
    query: query_params,
    request_options: request_options
  )
  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::DeleteInvoiceResponse.load(response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#delete_invoice_attachment(request_options: {}, **params) ⇒ Square::Types::DeleteInvoiceAttachmentResponse

Removes an attachment from an invoice and permanently deletes the file. Attachments can be removed only from invoices in the ‘DRAFT`, `SCHEDULED`, `UNPAID`, or `PARTIALLY_PAID` state.

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :invoice_id (String)
  • :attachment_id (String)

Returns:



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/square/invoices/client.rb', line 332

def delete_invoice_attachment(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "DELETE",
    path: "v2/invoices/#{params[:invoice_id]}/attachments/#{params[:attachment_id]}",
    request_options: request_options
  )
  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::DeleteInvoiceAttachmentResponse.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::GetInvoiceResponse

Retrieves an invoice by invoice ID.

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :invoice_id (String)

Returns:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/square/invoices/client.rb', line 157

def get(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "v2/invoices/#{params[:invoice_id]}",
    request_options: request_options
  )
  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::GetInvoiceResponse.load(response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#list(request_options: {}, **params) ⇒ Square::Types::ListInvoicesResponse

Returns a list of invoices for a given location. The response is paginated. If truncated, the response includes a ‘cursor` that you use in a subsequent request to retrieve the next set of invoices.

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :location_id (String)
  • :cursor (String, nil)
  • :limit (Integer, nil)

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/square/invoices/client.rb', line 29

def list(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  query_param_names = %i[location_id cursor limit]
  query_params = {}
  query_params["location_id"] = params[:location_id] if params.key?(:location_id)
  query_params["cursor"] = params[:cursor] if params.key?(:cursor)
  query_params["limit"] = params[:limit] if params.key?(:limit)
  params.except(*query_param_names)

  Square::Internal::CursorItemIterator.new(
    cursor_field: :cursor,
    item_field: :invoices,
    initial_cursor: query_params[:cursor]
  ) do |next_cursor|
    query_params[:cursor] = next_cursor
    request = Square::Internal::JSON::Request.new(
      base_url: request_options[:base_url],
      method: "GET",
      path: "v2/invoices",
      query: query_params,
      request_options: request_options
    )
    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::ListInvoicesResponse.load(response.body)
    else
      error_class = Square::Errors::ResponseError.subclass_for_code(code)
      raise error_class.new(response.body, code: code)
    end
  end
end

#publish(request_options: {}, **params) ⇒ Square::Types::PublishInvoiceResponse

Publishes the specified draft invoice.

After an invoice is published, Square follows up based on the invoice configuration. For example, Square sends the invoice to the customer’s email address, charges the customer’s card on file, or does nothing. Square also makes the invoice available on a Square-hosted invoice page.

The invoice ‘status` also changes from `DRAFT` to a status based on the invoice configuration. For example, the status changes to `UNPAID` if Square emails the invoice or `PARTIALLY_PAID` if Square charges a card on file for a portion of the invoice amount.

In addition to the required ‘ORDERS_WRITE` and `INVOICES_WRITE` permissions, `CUSTOMERS_READ` and `PAYMENTS_WRITE` are required when publishing invoices configured for card-on-file payments.

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :invoice_id (String)

Returns:



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/square/invoices/client.rb', line 422

def publish(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  request_data = Square::Invoices::Types::PublishInvoiceRequest.new(params).to_h
  non_body_param_names = ["invoice_id"]
  body = request_data.except(*non_body_param_names)

  request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "v2/invoices/#{params[:invoice_id]}/publish",
    body: body,
    request_options: request_options
  )
  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::PublishInvoiceResponse.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::SearchInvoicesResponse

Searches for invoices from a location specified in the filter. You can optionally specify customers in the filter for whom to retrieve invoices. In the current implementation, you can only specify one location and optionally one customer.

The response is paginated. If truncated, the response includes a ‘cursor` that you use in a subsequent request to retrieve the next set of invoices.

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/square/invoices/client.rb', line 122

def search(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "v2/invoices/search",
    body: Square::Invoices::Types::SearchInvoicesRequest.new(params).to_h,
    request_options: request_options
  )
  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::SearchInvoicesResponse.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::UpdateInvoiceResponse

Updates an invoice. This endpoint supports sparse updates, so you only need to specify the fields you want to change along with the required ‘version` field. Some restrictions apply to updating invoices. For example, you cannot change the `order_id` or `location_id` field.

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :invoice_id (String)

Returns:



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/square/invoices/client.rb', line 194

def update(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  request_data = Square::Invoices::Types::UpdateInvoiceRequest.new(params).to_h
  non_body_param_names = ["invoice_id"]
  body = request_data.except(*non_body_param_names)

  request = Square::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "PUT",
    path: "v2/invoices/#{params[:invoice_id]}",
    body: body,
    request_options: request_options
  )
  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::UpdateInvoiceResponse.load(response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end