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.
result = braintree.Customer.update("the_customer_id", {
"first_name": "New First Name",
"last_name": "New Last Name"
})
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.
result = braintree.Customer.update("the_customer_id", {
"email": "new.email@example.com",
"credit_card": {
"number": "5105105105105100",
"expiration_date": "08/2012",
"options": {
"update_existing_token": "the_token"
}
}
})
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 = braintree.Customer.find("the_customer_id")
token = customer.credit_cards[0].token
By default we will run credit card validations but not perform verification. Set the verify_card option to verify the card.
result = braintree.Customer.update("the_customer_id", {
"credit_card": {
"number": "5105105105105100",
"expiration_date": "08/2012",
"options": {
"update_existing_token": "the_token",
"verify_card": True
}
}
})
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 update_existing_token option from the examples in the previous section, a new credit card will be created and associated to the customer.
customer = braintree.Customer.find("the_customer_id")
len(customer.credit_cards)
# 1
result = braintree.Customer.update(customer.id, {
"credit_card": {
"number": "5105105105105100",
"expiration_date": "11/2011"
}
})
len(result.customer.credit_cards)
# 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 update_existing option in the billing address attributes.
result = braintree.Customer.update("the_customer_id", {
"email": "new.email@example.com",
"credit_card": {
"cardholder_name": "New Cardholder Name",
"number": "5105105105105100",
"expiration_date": "08/2012",
"options": {
"update_existing_token": "the_token",
},
"billing_address": {
"street_address": "New Street Address",
"postal_code": "60622",
"options": {
"update_existing": True
}
}
}
})
If you omit the update_existing 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.