Class: Square::Customers::Cards::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void

Parameters:



10
11
12
# File 'lib/square/customers/cards/client.rb', line 10

def initialize(client:)
  @client = client
end

Instance Method Details

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

Adds a card on file to an existing customer.

As with charges, calls to ‘CreateCustomerCard` are idempotent. Multiple calls with the same card nonce return the same card record that was created with the provided nonce during the first call.

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

  • :customer_id (String)

Returns:



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
# File 'lib/square/customers/cards/client.rb', line 30

def create(request_options: {}, **params)
  params = Square::Internal::Types::Utils.normalize_keys(params)
  request_data = Square::Customers::Cards::Types::CreateCustomerCardRequest.new(params).to_h
  non_body_param_names = ["customer_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/customers/#{params[:customer_id]}/cards",
    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::CreateCustomerCardResponse.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::DeleteCustomerCardResponse

Removes a card on file from a customer.

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

  • :customer_id (String)
  • :card_id (String)

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/square/customers/cards/client.rb', line 70

def delete(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/customers/#{params[:customer_id]}/cards/#{params[:card_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::DeleteCustomerCardResponse.load(response.body)
  else
    error_class = Square::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end