# プラン 更新

`PUT /v1/plans/{id}`

- operationId: `updatePlan`
- tags: プラン

IDで指定したプラン情報を更新します。\
プランが1つ以上のサブスクリプションで使用されているとき（`used_flag = 1`のとき）、プランは更新できません。


## コードサンプル

### cURL

```bash
curl \
    -X PUT \
    -H "Authorization:Bearer <Secret API Key>" \
    -H "Content-Type: application/json" \
    -d '{
    "plan_name": "Pro Plan",
    "description": "Pro Plan (previous: Gold Plan)"
}' \
'https://api.test.fincode.jp/v1/plans/{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 planId = "<Plan ID>";

    try {
        // リクエストの送信
        const plan = await fincode.plans.update(planId, {
            plan_name: "Pro Plan",
            description: "This is a Pro plan. (previously Gold Plan)",
            amount: "2000",
        });
    } catch (e) {
        // エラーの処理
    }
})();
```

### Go

```go
package main

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

func main() {

	apiKey := "<Secret API Key>"

	planID := "<Plan ID>"
	body := UpdatingPlanRequest{
		PlanName:    stringPointer("Pro Name"),
		Description: stringPointer("This is a plan for professionals (previously Gold Plan)"),
		Amount:      stringPointer("2000"),
	}

	marshalledBody, _ := json.Marshal(body)

	// リクエストの作成
	req, _ := http.NewRequest(
		"PUT",
		fmt.Sprintf("https://api.test.fincode.jp/v1/plans/%s", planID),
		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 UpdatingPlanRequest struct {
	PlanName        *string `json:"plan_name"`
	Description     *string `json:"description"`
	Amount          *string `json:"amount"`
	Tax             *string `json:"tax"`
	IntervalPattern *string `json:"interval_pattern"`
	IntervalCount   *string `json:"interval_count"`
}

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

### PHP

```php
<?php

$apiKey = '<Secret API Key>';

$planId = '<Plan ID>';

$baseUrl = "https://api.test.fincode.jp";
$endpoint = "/v1/plans/{$planId}";
$headers = [
    "Authorization: Bearer " . $apiKey,
    "Content-Type: application/json"
];

$data = json_encode([
    "plan_name" => "Pro Plan",
    "description" => "This is a Pro Plan (previously Gold Plan)",
]);

$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>'

plan_id = '<Plan ID>'

url = f'https://api.test.fincode.jp/v1/plans/{plan_id}'

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

data = {
    "plan_name": "Pro Plan",
    "description": "Pro Plan (previous: Gold Plan)"
}

# 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
    plan_id = '<Plan ID>'
    endpoint = "/v1/plans/#{plan_id}"
    uri = URI.parse(BASE_URL + endpoint)

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    
    data = {
        plan_name: "Pro Plan",
        description: "Pro Plan (previous: Gold Plan)"
    }

    # リクエストの作成
    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
```

## パラメータ

| 名前 | 位置 | 必須 | 型 | 説明 |
| --- | --- | --- | --- | --- |
| `id` | path | ✓ | PlanId_schema | プランID |

## リクエストボディ

Content-Type: `application/json`

| パラメータ | 型 | 必須 | 説明 |
| --- | --- | --- | --- |
| `plan_name` | plan_name |  |  |
| `description` | description |  |  |
| `amount` | x-req-properties-amount |  |  |
| `tax` | x-req-properties-tax |  |  |
| `interval_pattern` | interval_pattern |  |  |
| `interval_count` | interval_count |  |  |
| `subscription_retry_mode` | subscription_retry_mode |  |  |

## レスポンス

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

| パラメータ | 型 | 必須 | 説明 |
| --- | --- | --- | --- |
| `id` | string |  | プランID |
| `plan_name` | string |  | プラン名 |
| `description` | string |  | プランの説明 |
| `shop_id` | Shop_properties-id |  |  |
| `amount` | integer(int64) |  | 利用金額 |
| `tax` | integer(int64) |  | 税送料 |
| `total_amount` | integer(int64) |  | 合計金額 |
| `interval_pattern` | enum(month | year) |  | 課金間隔 |
| `interval_count` | enum(1 | 2 | 3 | 6) |  | 課金間隔数 |
| `used_flag` | enum(0 | 1) |  | 利用済みフラグ |
| `subscription_retry_mode` | enum(enabled | disabled) |  | リトライ対象設定 |
| `delete_flag` | delete_flag |  |  |
| `created` | created |  |  |
| `updated` | updated |  |  |

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

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

