Updating Customers

You can update a customer by itself, a customer along with a credit card, or a customer along with a credit card and billing address. You can also use the customer API to update customer details while adding a new credit card to the customer. Examples on this page use the server-to-server API, but customers can also be updated using transparent redirect.

Only the Customer

To update a customer use its ID along with new attributes. The same validations apply as when creating a customer. Any attribute not passed will remain unchanged.

CustomerRequest request = new CustomerRequest().
    firstName("New First Name").
    lastName("New Last Name");

Result<Customer> updateResult = gateway.customer().update("the_customer_id", request);

Update Customer and Update Existing Credit Card

To update a credit card when updating a customer you need to pass the token of the credit card to update under the credit card options.

CustomerRequest request = new CustomerRequest().
    email("new.email@example.com").
    creditCard().
        expirationDate("10/2012").
        options().
            updateExistingToken("the_token").
            done().
        done();

Result<Customer> updateResult = gateway.customer().update("the_customer_id", request);

If you are only storing the customer ID in your system and using a 1:1 model of customer to credit card, then you can get the token for the credit card from the customer details.

Customer customer = gateway.customer().find("the_customer_id");
String token = customer.getCreditCards().get(0).getToken();

By default we will run credit card validations but not perform verification. Set the verifyCard option to verify the card.

CustomerRequest request = new CustomerRequest().
  email("new.email@example.com").
  creditCard().
      expirationDate("10/2012").
      options().
          updateExistingToken("the_token").
          verifyCard(true).
          done().
      done();

Result<Customer> updateResult = gateway.customer().update("the_customer_id", request);

See the card verification API for more information on how verification works.

Update Customer and Create New Credit Card

You can add a new credit card to an existing customer using the customer update API. If you omit the updateExistingToken option from the examples in the previous section, a new credit card will be created and associated to the customer.

Customer customer = gateway.customer().find("the_customer_id");
customer.getCreditCards().size();
// 1

CustomerRequest request = new CustomerRequest().
    creditCard().
        number("4111111111111111").
        expirationDate("10/2012").
        done();

Customer updatedCustomer = gateway.customer().update(customer.getId(), request).getTarget();
updatedCustomer.getCreditCards().size();
// 2

Update Customer, Credit Card, and Billing Address

The billing address can also be updated by adding in the billing address details and setting the updateExisting option in the billing address attributes.

CustomerRequest request = new CustomerRequest().
    email("new.email@example.com").
    creditCard().
        expirationDate("10/2012").
        options().
            updateExistingToken("the_token").
            verifyCard(true).
            done().
        billingAddress().
            streetAddress("New Street Address").
            postalCode("60622").
            options().
                updateExisting(true).
                done().
            done().
        done();

Result<Customer> updateResult = gateway.customer().update("the_customer_id", request);

If you omit the updateExisting option under the billing address, we will create a new address for the customer and associate it to the credit card. The old address will remain associated to the customer but no longer associated as the billing address of the credit card.

Transparent Redirect

All of these update combinations can also be done when updating a customer through transparent redirect.

See Also