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.
var 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.
var request = new CustomerRequest
{
Email = "new.email@example.com",
CreditCard = new CreditCardRequest
{
ExpirationDate = "10/2012",
Options = new CreditCardOptionsRequest
{
UpdateExistingToken = "the_token"
}
}
};
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.CreditCards[0].Token;
By default we will run credit card validations but not perform verification. Set the VerifyCard option to verify the card.
var request = new CustomerRequest
{
Email = "new.email@example.com",
CreditCard = new CreditCardRequest
{
ExpirationDate = "10/2012",
Options = new CreditCardOptionsRequest
{
UpdateExistingToken = "the_token",
VerifyCard = true
}
}
};
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");
int count = customer.CreditCards.Length;
// 1
var request = new CustomerRequest
{
Email = "new.email@example.com",
CreditCard = new CreditCardRequest
{
Number = "4111111111111111",
ExpirationDate = "10/2012"
}
};
Customer updatedCustomer = gateway.Customer.Update(customer.Id, request).Target;
count = updatedCustomer.CreditCards.Length;
// 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.
var request = new CustomerRequest
{
Email = "new.email@example.com",
CreditCard = new CreditCardRequest
{
ExpirationDate = "10/2012",
Options = new CreditCardOptionsRequest
{
UpdateExistingToken = "the_token",
VerifyCard = true
},
BillingAddress = new CreditCardAddressRequest
{
StreetAddress = "New Street Address",
PostalCode = "60622",
Options = new CreditCardAddressOptionsRequest
{
UpdateExisting = true
}
}
}
};
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.