# Update card for a customer

> For the complete machine-readable documentation index, see [llms.txt](https://apidocs.chargebee.com/llms.txt).


[Idempotency Supported](/docs/api/idempotency)

#### Deprecated[](#deprecated)

The [Payment Sources API](/docs/api/payment_sources) , with its additional options and improvements, obsoletes the [Cards APIs](/docs/api/cards) . This operation is obsoleted by the following:

-   [Create using temporary token](/docs/api/payment_sources/create-using-gateway-temporary-token)
-   [Create using permanent token](/docs/api/payment_sources/create-using-permanent-token)
-   [Create a card payment source](/docs/api/payment_sources/create-a-card-payment-source)

Adds or replaces card details of a customer. Updating card details replaces the present payment method.

Passing credit card details to this API involves PCI liability at your end as sensitive card info passes through your servers. If you wish to avoid that, you can use one of the following integration methodologies if applicable

-   If you are using Stripe gateway, you can use [Stripe.js](https://stripe.com/docs/stripe.js) with your card update form.
-   If you are using Braintree gateway, you can use [Braintree.js](https://www.braintreepayments.com/docs/javascript) with your card update form.
-   If you are using Authorize.Net gateway, you use [Accept.js](https://developer.authorize.net/api/reference/features/acceptjs.html) with your card update form.
-   In case you are using the Adyen gateway, you will have to use the Adyen's [Client Side Encryption](https://docs.adyen.com/online-payments/classic-integrations/api-integration-ecommerce/cse-integration-ecommerce) to encrypt sensitive cardholder data. Once the cardholder data is encrypted, pass the value in adyen.encrypted.data as temp token in this API.
-   You can also use our [Hosted Pages](https://www.chargebee.com/docs/billing/2.0/hosted-capabilities/hosted-capabilities) based integration. Use our [Hosted Page - Update Card](/docs/api/hosted_pages) API to generate a 'Update Card' Hosted Page link.

**Legacy behavior:**

-   **For [sites](https://www.chargebee.com/docs/sites-intro.html) created before March 1st, 2014:** On making this request, the `billing_address` and `vat_number` of the customer are **deleted** and replaced by the values passed with this request. Ensure that you pass the [billing address parameters](/docs/api/v2/pcv-1/subscriptions/create-a-subscription#card_billing_addr1) and the `vat_number` parameters each time you make this request, to avoid losing the same information at the customer-level.
-   **For [sites](https://www.chargebee.com/docs/sites-intro.html) created on or after March 1st, 2014:** This request does not alter the `billing_address` and `vat_number` of the customer.

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/customers/__test__XpbTXGTSRp3F3uCq/credit_card \
     -u {site_api_key}:\
     -d gateway_account_id="gw___test__5SK2lMgOSRp3BhV2u" \
     -d first_name="Richard" \
     -d last_name="Fox" \
     -d number="4012888888881881" \
     -d expiry_month=10 \
     -d expiry_year=2022 \
     -d cvv="999"
```

#### .NET

```dotnet
using ChargeBee.Api;
using ChargeBee.Models;

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Card.UpdateCardForCustomer("__test__XpbTXGTSRp3F3uCq")
		.GatewayAccountId("gw___test__5SK2lMgOSRp3BhV2u")
		.FirstName("Richard")
		.LastName("Fox")
		.Number("4012888888881881")
		.ExpiryMonth(10)
		.ExpiryYear(2022)
		.Cvv("999")
		.Request();

Customer customer = result.Customer;
Card card = result.Card;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    cardAction "github.com/chargebee/chargebee-go/v3/actions/card"
    "github.com/chargebee/chargebee-go/v3/models/card"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := cardAction.UpdateCardForCustomer("__test__XpbTXGTSRp3F3uCq", &card.UpdateCardForCustomerRequestParams{
        GatewayAccountId : "gw___test__5SK2lMgOSRp3BhV2u",
        FirstName : "Richard",
        LastName : "Fox",
        Number : "4012888888881881",
        ExpiryMonth : chargebee.Int32(10),
        ExpiryYear : chargebee.Int32(2022),
        Cvv : "999",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        Card := res.Card
    }
}
```

#### Go

```go
package main

import (
  "fmt"
  "github.com/chargebee/chargebee-go/v4"
)

func main() {
  config := &chargebee.ClientConfig{
    SiteName: "{site}",
    ApiKey: "{site_api_key}",
  }    
  client := chargebee.NewClient(config)
  req := &chargebee.CardUpdateCardForCustomerRequest{
    GatewayAccountId : "gw___test__5SK2lMgOSRp3BhV2u",
    FirstName : "Richard",
    LastName : "Fox",
    Number : "4012888888881881",
    ExpiryMonth : chargebee.Int32(10),
    ExpiryYear : chargebee.Int32(2022),
    Cvv : "999",
}
  res, err := client.Card.UpdateCardForCustomer("__test__XpbTXGTSRp3F3uCq", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        Card := res.Card
    }
}
```

#### Java

```java
import com.chargebee.*;
import com.chargebee.ListResult;
import com.chargebee.models.*;
import com.chargebee.models.enums.*;
import java.io.IOException;

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = Card.updateCardForCustomer("__test__XpbTXGTSRp3F3uCq")
            .gatewayAccountId("gw___test__5SK2lMgOSRp3BhV2u")
            .firstName("Richard")
            .lastName("Fox")
            .number("4012888888881881")
            .expiryMonth(10)
            .expiryYear(2022)
            .cvv("999")
            .request();

        Customer customer = result.customer();
        Card card = result.card();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.card.Card;
import com.chargebee.v4.models.card.params.UpdateCardForCustomerParams;
import com.chargebee.v4.models.card.responses.UpdateCardForCustomerResponse;
import com.chargebee.v4.models.customer.Customer;

public class UpdateCardForCustomer {

    public static void main(String[] args) {
        ChargebeeClient client = ChargebeeClient.builder()
            .apiKey("{site_api_key}")
            .siteName("{site}")
            .build();

        UpdateCardForCustomerParams params = UpdateCardForCustomerParams.builder()
            .gatewayAccountId("gw___test__5SK2lMgOSRp3BhV2u")
            .firstName("Richard")
            .lastName("Fox")
            .number("4012888888881881")
            .expiryMonth(10)
            .expiryYear(2022)
            .cvv("999")
            .build();

        UpdateCardForCustomerResponse response = client
            .cards()
            .updateCardForCustomer("__test__XpbTXGTSRp3F3uCq", params);

        Customer customer = response.getCustomer();
        Card card = response.getCard();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

const chargebee = new Chargebee({
    site: "{site}",
    apiKey: "{site_api_key}",
});

try {
    const result = await chargebee.card.updateCardForCustomer("__test__XpbTXGTSRp3F3uCq", {
        gateway_account_id: "gw___test__5SK2lMgOSRp3BhV2u",
        first_name: "Richard",
        last_name: "Fox",
        number: "4012888888881881",
        expiry_month: 10,
        expiry_year: 2022,
        cvv: "999"
    });

    console.log(result);
    const customer = result.customer;
    const card = result.card;
} catch (err) {
    console.log(err);
}
```

#### PHP

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Chargebee\ChargebeeClient;

$chargebee = new ChargebeeClient(options: [
    "site" => "{site}",
    "apiKey" => "{site_api_key}",
]);
$result = $chargebee->card()->updateCardForCustomer("__test__XpbTXGTSRp3F3uCq", [
    "gateway_account_id" => "gw___test__5SK2lMgOSRp3BhV2u",
    "first_name" => "Richard",
    "last_name" => "Fox",
    "number" => "4012888888881881",
    "expiry_month" => 10,
    "expiry_year" => 2022,
    "cvv" => "999"
]);
$customer = $result->customer;
$card = $result->card;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Card.update_card_for_customer("__test__XpbTXGTSRp3F3uCq",
    cb_client.Card.UpdateCardForCustomerParams(
        gateway_account_id="gw___test__5SK2lMgOSRp3BhV2u",
        first_name="Richard",
        last_name="Fox",
        number="4012888888881881",
        expiry_month=10,
        expiry_year=2022,
        cvv="999"
    )
)
customer = response.customer
card = response.card
```

#### Ruby

```ruby
require 'chargebee'

ChargeBee.configure(:site => "{site}",
  :api_key => "{site_api_key}")

result = ChargeBee::Card.update_card_for_customer("__test__XpbTXGTSRp3F3uCq",{
  :gateway_account_id => "gw___test__5SK2lMgOSRp3BhV2u",
  :first_name => "Richard",
  :last_name => "Fox",
  :number => "4012888888881881",
  :expiry_month => 10,
  :expiry_year => 2022,
  :cvv => "999"
})

customer = result.customer
card = result.card
```

## Sample Response

```json
{
  "card": {
    "card_type": "visa",
    "created_at": 1517486950,
    "customer_id": "__test__XpbTXGTSRp3F3uCq",
    "expiry_month": 10,
    "expiry_year": 2022,
    "first_name": "Richard",
    "funding_type": "credit",
    "gateway": "stripe",
    "gateway_account_id": "gw___test__5SK2lMgOSRp3BhV2u",
    "iin": "401288",
    "issuing_country": "CA",
    "last4": "1881",
    "last_name": "Fox",
    "masked_number": "************1881",
    "object": "card",
    "payment_source_id": "pm___test__XpbTXGTSRp3FRuCt",
    "resource_version": 1517486950330,
    "status": "valid",
    "updated_at": 1517486950
  },
  "customer": {
    "allow_direct_debit": false,
    "auto_collection": "on",
    "card_status": "valid",
    "created_at": 1517486948,
    "deleted": false,
    "excess_payments": 0,
    "first_name": "Richard",
    "id": "__test__XpbTXGTSRp3F3uCq",
    "last_name": "Fox",
    "net_term_days": 0,
    "object": "customer",
    "payment_method": {
      "gateway": "stripe",
      "gateway_account_id": "gw___test__5SK2lMgOSRp3BhV2u",
      "object": "payment_method",
      "reference_id": "cus_J7rQYxGS0QeW4M/card_1IVbhxJv9j0DyntJN9j65gTP",
      "status": "valid",
      "type": "card"
    },
    "pii_cleared": "active",
    "preferred_currency_code": "USD",
    "primary_payment_source_id": "pm___test__XpbTXGTSRp3FRuCt",
    "promotional_credits": 0,
    "refundable_credits": 0,
    "resource_version": 1517486950332,
    "taxability": "taxable",
    "unbilled_charges": 0,
    "updated_at": 1517486950
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/customers/{customer-id}/credit_card

## Input Parameters

- `gateway_account_id` (optional, string, max chars=50)
  The gateway account in which this payment source is stored.

- `tmp_token` (optional, string, max chars=300)
  The single-use card token returned by vaults like Stripe/Braintree which act as a substitute for your card details. Before calling this API, you should have submitted your card details to the gateway and gotten this token in return. **Note:** Supported only for Stripe, Braintree and Authorize.Net. If this value is specified, there is no need to specify other card details (like number, cvv, etc).

- `first_name` (optional, string, max chars=50)
  Cardholder's first name.

- `last_name` (optional, string, max chars=50)
  Cardholder's last name.

- `number` (required, string, max chars=1500)
  The credit card number without any format. If you are using [Braintree.js](https://developer.paypal.com/braintree/docs/guides/client-sdk/setup/javascript/v2#getting-braintree.js) , you can specify the Braintree encrypted card number here.

- `expiry_month` (required, integer, min=1, max=12)
  Card expiry month.

- `expiry_year` (required, integer)
  Card expiry year.

- `cvv` (optional, string, max chars=520)
  The card verification value (CVV). If you are using [Braintree.js](https://developer.paypal.com/braintree/docs/guides/client-sdk/setup/javascript/v2#getting-braintree.js) , you can specify the Braintree encrypted CVV here.

- `preferred_scheme` (optional, enumerated string)
  The customer's preferred card scheme for co-branded cards.
  
  **Note**: Currently, this parameter is only supported for Stripe.
  Possible enum values:
    - `cartes_bancaires`
      A Cartes Bancaires card scheme.
    - `mastercard`
      A MasterCard scheme.
    - `visa`
      A Visa card scheme.

- `billing_addr1` (optional, string, max chars=150)
  Address line 1, as available in card billing address.

- `billing_addr2` (optional, string, max chars=150)
  Address line 2, as available in card billing address.

- `billing_city` (optional, string, max chars=50)
  City, as available in card billing address.

- `billing_state_code` (optional, string, max chars=50)
  The [ISO 3166-2 state/province code](https://www.iso.org/obp/ui/#search/code) without the country prefix. Currently supported for USA, Canada, India and UAE. For instance, for Arizona (USA), set `billing_state_code` as `AZ` (not `US-AZ` ). For Tamil Nadu (India), set as `TN` (not `IN-TN` ). For British Columbia (Canada), set as `BC` (not `CA-BC` ). For Dubai (UAE), set as `DU` (not `AE-DU` ).

- `billing_state` (optional, string, max chars=50)
  The state/province name. Is set by Chargebee automatically for US, Canada, India and UAE, if `billing_state_code` is provided.

- `billing_zip` (optional, string, max chars=20)
  Postal or Zip code, as available in card billing address.

- `billing_country` (optional, string, max chars=50)
  The billing address country of the customer. Must be one of [ISO 3166 alpha-2 country code](https://www.iso.org/iso-3166-country-codes.html) .
  
  **Note**: If you enter an invalid country code, the system will return an error.
  
  **Brexit**
  
  If you have enabled [EU VAT](https://www.chargebee.com/docs/eu-vat.html) in 2021 or later, or have [manually enable](https://www.chargebee.com/docs/brexit.html#what-needs-to-be-done-in-chargebee) the Brexit configuration, then `XI` (the code for **United Kingdom - Northern Ireland**) is available as an option.
  
  .

## Returns

- `customer` (Customer object)
  Resource object representing customer

- `card` (Card object)
  Resource object representing card
