Customers

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

var request = new CustomerRequest
{
    FirstName = "Mark",
    LastName = "Jones",
    Company = "Jones Co.",
    Email = "mark.jones@example.com",
    Fax = "419-555-1234",
    Phone = "614-555-1234",
    Website = "http://example.com"
};
Result<Customer> result = gateway.Customer.Create(request);

bool success = result.IsSuccess();
// true

string customerId = result.Target.Id;
// e.g. 594019
C♯

Create with payment method

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

var request = new CustomerRequest
{
    FirstName = "Fred",
    LastName = "Jones",
    PaymentMethodNonce = nonceFromTheClient
};
Result<Customer> result = gateway.Customer.Create(request);

bool success = result.IsSuccess();
// true

Customer customer = result.Target;
string customerId = customer.Id;
// e.g. 160923

string cardToken = customer.PaymentMethods[0].Token;
// e.g. f28w
C♯

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

See the reference and more examples of creating a customer.

Update

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

var request = new CustomerRequest
{
    FirstName = "New First Name",
    LastName = "New Last Name"
};

Result<Customer> updateResult = gateway.Customer.Update("the_customer_id", request);
C♯

See the reference and more examples of updating a customer.

Custom Fields

Use custom fields to store additional data about your customers. You'll need to configure your custom fields in the Control Panel to use them via the API.

For example, to set custom fields custom_field_one and custom_field_two:

var result = gateway.Customer.Create(
    new CustomerRequest
    {
        FirstName = "Bob",
        LastName = "Smith",
        CustomFields = new Dictionary<string, string>
        {
            { "custom_field_one", "value one" },
            { "custom_field_two", "value two" }
        }
    });

if (result.IsSuccess())
{
  Dictionary<string, string> customFields = result.Target.CustomFields;
}
C♯

See more on the custom fields parameter in the reference.

Find

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

Customer customer = gateway.Customer.Find("the_customer_id");
C♯

If the customer cannot be found, it will throw a NotFoundException.

Delete

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

gateway.Customer.Delete("the_customer_id");
C♯