# カード 更新

`PUT /v1/customers/{customer_id}/cards/{id}`

- operationId: `updateCustomerCard`
- tags: カード

`customer_id`で指定した顧客に対し紐づくカードのうち`id`で指定したものを更新します。

<span class="smallText">※ カード情報の更新は決済手段API（<code>PUT /v1/customers/{customer_id}/payment_methods/{id}</code>, <code>pay_type = "Card"</code>）でも行えます。新規に実装する場合は決済手段APIの利用を推奨します。</span>


## コードサンプル

### cURL

```bash
curl \
    -X PUT \
    -H "Authorization:Bearer <Secret API Key>" \
    -H "Content-Type: application/json" \
    -d '{
    "default_flag": "1",
}' \
'https://api.test.fincode.jp/v1/customers/{customer_id}/cards/{id}'
```

### Node.js

```javascript
import { createFincode } from "@fincode/node";

const API_KEY = "<Secret API Key>";

(async () => {
    const fincode = createFincode({ apiKey: API_KEY, isLiveMode: false });

    const customerId = "<Customer ID>";
    const cardId = "<Card ID>";

    try {
        // リクエストの送信
        const card = await fincode.cards.update(customerId, cardId, {
            default_flag: "0",
        });
    } catch (e) {
        // エラーの処理
    }
})();
```

### Go

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

func main() {

	apiKey := "<Secret API Key>"

	body := UpdatingCustomerCardRequest{
		DefaultFlag: stringPointer("1"),
	}

	marshalledBody, _ := json.Marshal(body)

	// リクエストの作成
	req, _ := http.NewRequest("PUT", "https://api.test.fincode.jp/v1/customers/{customer_id}/cards/{id}", bytes.NewBuffer(marshalledBody))
	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
	req.Header.Set("Content-Type", "application/json")

	// リクエストの送信
	client := &http.Client{}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

}

type UpdatingCustomerCardRequest struct {
	DefaultFlag *string `json:"default_flag,omitempty"`
	Token       *string `json:"token,omitempty"`
	HolderName  *string `json:"holder_name,omitempty"`
	Expire      *string `json:"expire,omitempty"`
}

func stringPointer(s string) *string {
	return &s
}
```

### PHP

```php
<?php

$apiKey = '<Secret API Key>';

$customerId = '<Customer ID>';
$cardId = '<Card ID>';

$baseUrl = "https://api.test.fincode.jp";
$endpoint = "/v1/customers/{$customerId}/cards/{$cardId}";
$headers = [
    "Authorization: Bearer " . $apiKey,
];

$data = json_encode([
    "default_flag" => "0"
]);

$session = curl_init();
curl_setopt($session, CURLOPT_URL, $baseUrl . $endpoint);
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session, CURLOPT_POSTFIELDS, $data);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($session, CURLOPT_SSL_VERIFYPEER, );
// curl_setopt($session, CURLOPT_SSL_VERIFYHOST, );

$response = curl_exec($session);

if ($response === false) {
    # エラー処理
    echo "cURL Error: " . curl_error($session);
} else {
    # APIからのデータを処理
    var_dump($response);
}

curl_close($session);
```

### Python 3

```python
import requests

api_key = '<Secret API Key>'

customer_id = '<Customer ID>'
card_id = '<Card ID>'

url = f'https://api.test.fincode.jp/v1/customers/{customer_id}/cards/{card_id}/'

# ヘッダーを設定
headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json'
}

data = {
    "default_flag": "0",
}

# HTTP POSTリクエストの送信
try:
    response = requests.put(url, headers=headers, json=data)

    # レスポンスの処理
    if response.status_code == 200:
        # 成功した場合の処理
        print(f"Success: {response.json()}")
    else:
        # エラーの処理
        print(f"Error: {response.json()}")
except requests.RequestException as e:
    # 通信エラーの処理
    print(f"Request error: {e}")
```

### Ruby

```ruby
require 'net/http'
require 'uri'
require 'json'


API_KEY = '<Secret API Key>'
BASE_URL = 'https://api.test.fincode.jp'

def main
    customer_id = '<Customer ID>'
    card_id = '<Card ID>'

    endpoint = "/v1/customers/#{customer_id}/cards/#{card_id}"
    uri = URI.parse(BASE_URL + endpoint)

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    data = {
        default_flag: '0'
    }

    # リクエストの作成
    request = Net::HTTP::Put.new(uri.request_uri)
    request['Authorization'] = "Bearer #{API_KEY}"
    request['Content-Type'] = 'application/json'

    request.body = data.to_json

    # リクエストの送信
    response = http.request(request)

    case response
    when Net::HTTPSuccess
        puts 'SUCCESS'
    else
        puts 'ERROR'
    end

    # レスポンスの表示
    puts response.body
end

main
```

## パラメータ

| 名前 | 位置 | 必須 | 型 | 説明 |
| --- | --- | --- | --- | --- |
| `customer_id` | path | ✓ | CustomerId_schema | このカードが紐づく顧客のID |
| `id` | path | ✓ | CardId_schema | 更新するカードのID |
| `Tenant-Shop-Id` | header |  | schema | <span class="smallText color--red-400">※ 顧客情報を共有しないプラットフォームのメインショップのみ指定可</span> |

## リクエストボディ

Content-Type: `application/json`

| パラメータ | 型 | 必須 | 説明 |
| --- | --- | --- | --- |
| `token` | x-req-properties-token |  |  |
| `default_flag` | default_flag |  | デフォルトフラグ。 |
| `holder_name` | holder_name |  | ※ `token`に入力がある場合は無視 |
| `expire` | expire |  | ※ `token`に入力がある場合は無視 |
| `card_updater_mode` | card_updater_mode |  | カード更新機能（洗替）利用設定 |

## レスポンス

### 200 リクエストに成功

| パラメータ | 型 | 必須 | 説明 |
| --- | --- | --- | --- |
| `id` | string |  | カードID |
| `customer_id` | string |  | 顧客ID |
| `default_flag` | enum(0 | 1) |  | デフォルトフラグ。 |
| `card_no` | card_no |  |  |
| `expire` | expire |  |  |
| `holder_name` | holder_name |  |  |
| `type` | CardType |  |  |
| `brand` | CardBrand |  |  |
| `card_no_hash` | card_no_hash |  |  |
| `created` | created |  |  |
| `updated` | updated |  |  |
| `card_updater_mode` | enum(enabled | disabled | inherit) |  | カード更新対象カードステータス |
| `card_updater_last_success_date` | process_date |  | カード更新成功日時 |
| `card_updater_last_attempt_date` | process_date |  | カード更新実施日時 |

### 400 不正なリクエスト

| パラメータ | 型 | 必須 | 説明 |
| --- | --- | --- | --- |
| `errors` | array<FincodeAPIError> |  |  |

