Creating Customers

You can create a customer by itself, with a credit card, or with a credit card with a billing address.

This page describes creating customers using the server-to-server API. Customers can also be created using Transparent Redirect. The server-to-server API is easier to understand, so even if you’re planning to use TR, you may want to read this page first.

Just a Customer

Here is an example of creating a customer without credit card information. If all customer validations pass, success will be true.

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

Specify Your Own Customer ID

If you do not specify the customer ID, as in the example above, the gateway will generate one. You can also choose what you would like the ID to be.

result = Braintree::Customer.create(
  :id => "customer_123",
  :first_name => "Katrina"
)

Blank Customer

None of the parameters are required, so if you’re only interested in storing credit card details without any customer information, you can create a blank customer.

result = Braintree::Customer.create
if result.success?
  puts result.customer.id
else
  raise "this should never happen"
end

Customer with Credit Card

You can also create a credit card along with a customer. If all customer validations and credit card validations pass, and the card is verified (if requested), success will return true.

result = Braintree::Customer.create(
  :first_name => "Charity",
  :last_name => "Smith",
  :credit_card => {
    :number => "5105105105105100",
    :expiration_date => "05/2012"
  }
)
if result.success?
  puts result.customer.id
  puts result.customer.credit_cards[0].token
else
  p result.errors
end

If you do not specify a token for the credit card, as in the example above, the gateway will generate one. You can also choose what you want the token to be.

result = Braintree::Customer.create(
  :credit_card => {
    :token => "credit_card_123",
    :number => "5105105105105100",
    :expiration_date => "05/2012",
  }
)

By default, the credit card number is not verified to be a valid, open account. If you want to check for that, use the card verification API.

Customer with Credit Card with Billing Address

You can also create a credit card with billing address along with a customer.

result = Braintree::Customer.create(
  :credit_card => {
    :number => "5105105105105100",
    :expiration_date => "05/2012",
    :billing_address => {
      :street_address => "1 E Main St",
      :extended_address => "Suite 3",
      :locality => "Chicago",
      :region => "Illinois",
      :postal_code => "60622",
      :country_code_alpha2 => "US"
    }
  }
)
if result.success?
  puts result.customer.id
  p result.customer.credit_cards[0].billing_address
  p result.customer.addresses
else
  p result.errors
end

See Also