The customer object is an important component of the Braintree gateway. Use customers to store and organize payment methods. A single customer can have multiple payment methods.

This guide will walk you through the basics. See Customer Request and Customer Response references for complete details.

Create

Use Braintree::Customer.create() to add new customers.

You can create just a customer without an associated payment method, and the operation will be successful if all customer validations pass:

Ruby
result = Braintree::Customer.create(
  :first_name => "Jen",
  :last_name => "Smith",
  :company => "Braintree",
  :email => "jen@example.com",
  :phone => "312.555.1234",
  :fax => "614.555.5678",
  :website => "www.example.com"
)
if result.success?
  puts result.customer.id
else
  p result.errors
end

Create with payment method

You can also create a customer with an associated payment method:

Ruby
result = Braintree::Customer.create(
  :first_name => "Charity",
  :last_name => "Smith",
  :payment_method_nonce => nonce_from_the_client
)
if result.success?
  puts result.customer.id
  puts result.customer.payment_methods[0].token
else
  p result.errors
end

Success depends on both customer validations and payment method validations, and whether the payment method is verified (if card verification is enabled).

See the reference and more examples of creating a customer.

Update

Use Braintree::Customer.update() to update an existing customer:

Ruby
result = Braintree::Customer.update(
  "a_customer_id", # id of customer to update
  :first_name => "New First Name",
  :last_name => "New Last Name"
)
if result.success?
  puts "customer successfully updated"
else
  p result.errors
end

If the customer cannot be found, it will raise a Braintree::NotFoundError.

See the reference and more examples of updating a customer.

Find

Use Braintree::Customer.find() to find a customer by ID:

Ruby
customer = Braintree::Customer.find("a_customer_id")

If the customer cannot be found, it will raise a Braintree::NotFoundError.

The return value of this call will be a Braintree::Customer response object.

Delete

Use Braintree::Customer.delete() to delete a customer and its payment methods using the customer's ID:

Ruby
result = Braintree::Customer.delete("the_customer_id")
if result.success?
  puts "customer successfully deleted"
end

If the customer cannot be found, it will raise a Braintree::NotFoundError.

Still have questions?

If you can’t find an answer, contact our Support team.