# OutboundPayment オブジェクトを使用した資金移動

金融口座から第三者に資金を移動させるためのアウトバウンド決済の作成方法をご紹介します。

> #### 従来の実装
> 
> Treasury for platforms の v1 は従来の実装であり、[Treasury for platforms v2](https://docs.stripe.com/treasury/connect.md) で導入された機能の多くをサポートしていません。新しい v1 の実装を構築しないでください。

`OutboundPayment` オブジェクトは、金融口座からのプッシュベースの送金を表します。これには、ACH または電信送金経由でサードパーティーの外部口座に送金するものや、`stripe` ネットワークを使用して同じプラットフォームに関連付けられている別の金融口座に即時送金するものがあります。たとえば、金融口座から、ベンダーの外部のアメリカ銀行口座に送金するには、`OutboundPayment` を作成して資金を移動します。`OutboundPayment` の受け取り側の口座は、外部銀行口座または別の金融口座です。

アウトバウンド決済の一般的な送金時間は、数分 (Stripe ネットワーク利用時) から当日、1 ～ 2 営業日 (ACH ネットワーク利用時) まで幅があります。詳しくは、[資金移動のタイムライン](https://docs.stripe.com/treasury/connect/legacy/v1/money-movement/timelines.md#outboundpayment-and-outboundtransfer-transactions)ガイドをご覧ください。

## OutboundPayment を作成する

`POST /v1/treasury/outbound_payments` を使用して `OutboundPayment` を作成します。リクエストに使用可能なパラメーターのうち、以下は必須です。

- `amount`: セント単位での支払い金額。
- `currency`: 3 文字の ISO 通貨コード (サポートされるのは `usd` のみ)。
- `financial_account`: 資金が送金される支払い元の金融口座。
- `destination_payment_method` または `destination_payment_method_data`: 支払いの送金先に関する情報。
  - `destination_payment_method` では、まず [SetupIntent (支払いインテント)](https://docs.stripe.com/api/setup_intents.md) を使用して、アウトバウンドフローの `PaymentMethod` を設定する必要があります。また、`PaymentMethod` が関連付けられる `Customer` オブジェクトと一致する顧客 ID を指定する必要もあります。別の方法として、`Customer` に関連付けられた既存のレガシーの [BankAccount](https://docs.stripe.com/payments/ach-direct-debit/migrating-from-charges.md) を `PaymentMethod`の代わりに使用することもできます。
  - `destination_payment_method_data` を使用すると、インラインで支払い方法の詳細を指定できます。このパラメーターは、銀行口座の詳細の指定や、Stripe ネットワーク経由の[別の金融口座への送金](https://docs.stripe.com/treasury/connect/legacy/v1/moving-money/out-of/outbound-payments.md#create-obp-for-fa)に使用できます。

## 外部銀行口座への OutboundPayment を作成する

`POST /v1/treasury/outbound_payments` を使用して、本文の `financial_account` パラメーター値の ID で特定された金融口座から `OutboundPayment` を作成します。以下のリクエストは、`statement_descriptor` と `destination_payment_method_data` の情報を追加します。

```curl
curl https://api.stripe.com/v1/treasury/outbound_payments \
  -u "<<YOUR_SECRET_KEY>>:" \
  -H "Stripe-Account: {{CONNECTEDACCOUNT_ID}}" \
  -d "financial_account={{TREASURYFINANCIALACCOUNT_ID}}" \
  -d amount=2000 \
  -d currency=usd \
  -d statement_descriptor=payment_1 \
  -d "destination_payment_method_data[type]=us_bank_account" \
  -d "destination_payment_method_data[us_bank_account][account_holder_type]=individual" \
  -d "destination_payment_method_data[us_bank_account][routing_number]=110000000" \
  -d "destination_payment_method_data[us_bank_account][account_number]=1234567890" \
  --data-urlencode "destination_payment_method_data[billing_details][email]=jenny@example.com" \
  -d "destination_payment_method_data[billing_details][phone]=7135551212" \
  -d "destination_payment_method_data[billing_details][address][city]=Alvin" \
  -d "destination_payment_method_data[billing_details][address][state]=TX" \
  -d "destination_payment_method_data[billing_details][address][postal_code]=77511" \
  -d "destination_payment_method_data[billing_details][address][line1]=123 Main St." \
  -d "destination_payment_method_data[billing_details][name]=Jenny Rosen"
```

成功すると、新しく作成された `OutboundPayment` がレスポンスで返されます。

#### JSON (コメント付き)

```json
{
  "id": "{{OUTBOUND_PAYMENT_ID}}",
  "object": "outbound_payment",
  // The source FinancialAccount. Funds are pulled from this account.
  "financial_account": "{{FINANCIAL_ACCOUNT_ID}}",
  // The amount to send. 10.00 USD in this case.
  "amount": 1000,
  "cancelable": true | false,
  "currency": "usd",
  // The destination payment method. Either this or `destination_payment_method_data`
  // must be specified. Use this parameter if you wish to use a reusable
  // Customer-attached PaymentMethod or a legacy BankAccount for the OutboundPayment.
  "destination_payment_method": null | "{{PAYMENTMETHOD_ID}}" | "{{BANK_ACCOUNT_ID}}",
  // The destination payment method. Either this or `destination_payment_method`
  // must be specified. Use this parameter if you do not need a reusable
  // PaymentMethod for the OutboundPayment.
  "destination_payment_method_data": null | {
    "type": "us_bank_account",
    "us_bank_account": {
      "routing_number": "12341234",
      "account_number": "0123456789",
      "account_holder_type": "individual" | "company"
    },
    "billing_details": {
      // `name` must be specified for `us_bank_account` type
      "name": "Jenny Rosen",
      "phone": null | "{{String}}",
      "email": null | "{{String}}",
      "address": null | {
        "line1": null | "{{String}}",
        "line2": null | "{{String}}",
        "city": null | "{{String}}",
        "state": null | "{{String}}",
        "postal_code": null | "{{String}}",
        "country": null | "{{String}}"
      }
    }
  },
  // Optional. To explicitly specify a network, override the `network` value of
  // `destination_payment_method_options`
  "destination_payment_method_options": {
    "us_bank_account": {
      "network": "ach" | "us_domestic_wire"
    }
  },
  // Must be specified if passing in `destination_payment_method` and must match
  // the Customer to which the PaymentMethod is attached. Can also be optionally
  // passed in with `destination_payment_method_data`. The Customer represents
  // the recipient of the OutboundPayment.
  "customer": null | "{{CUSTOMER_ID}}",
  // An internal description for the OutboundPayment.
  "description": null | "Testing",
  // A descriptor for the OutboundPayment to send
  // to the network. For `ach` and `us_domestic_wire` networks,
  // this is the statement descriptor on the bank statement.
  // - `ach`: maximum 10 characters
  // - `us_domestic_wire`: maximum 140 characters
  "statement_descriptor": "payment_1",
  "end_user_details": {
    // When making requests on behalf of a user, set `end_user_details.present=true`
    // and pass the user's IP address.
    // If the request is on behalf of yourself (initiating transfers out of your
    // FinancialAccounts), `end_user_details.present` should be set to `false`
    "present": true | false,
    // You are required to collect the IP address of the creator of this transfer for
    // risk and compliance reasons. This will be used to help determine if this transfer
    // is authorized or should be blocked.
    "ip_address": "127.0.0.1"
  },
  // We will not support updating OutboundPayments after creation. As such, the
  // metadata can only be set at creation time.
  "metadata": null | {{Hash}},
}
```

### 同日の ACH

Same-day ACH により、送金の決済を迅速化し、資金が同営業日中に到着します。

> Same-day ACH は、限定提供のプレビュー段階です。アクセスをリクエストするには、[treasury-support@stripe.com](mailto:treasury-support@stripe.com) までメールでお問い合わせください。アクセスが付与されるまで、Same-day ACH パラメーターを含む API コールはエラーを返します。

同日 ACH を使用すると、`OutboundPayment` の呼び出しが[締切時間](https://docs.stripe.com/treasury/connect/legacy/v1/money-movement/timelines.md#bank-partner-timelines--outbound)前に正常に完了した場合、同日中に到着する資金を送信できます。同日 ACH を使用するには、`destination_payment_method_options.us_bank_account.network` パラメーターを `ach` に設定し、`destination_payment_method_options.us_bank_account.ach.submission` パラメーターを `same_day` に設定します。

### 電信送金: 金融番号

一部の銀行では、ACH とは異なる別の電信送金の金融番号を使用することがあります。このため、支払い方法の金融番号が電信送金をサポートしていない場合、電信送金の作成時にエラーが発生することがあります。このエラーを受信した場合には、銀行の電信送金用の金融番号を指定して新しい支払い方法を追加する必要があります。

### 電信送金: 受取人の住所

電信送金には ACH メタデータのほかに、受取人名と請求先住所が必要です。住所は銀行の住所ではなく、電信送金を受け取るアカウント所有者の住所です。

支払い方法の `billing_details.address` を入力する際は、すべての住所フィールドに入力する必要があります。 `billing_details.address` に不完全なフィールドがある状態で電信送金しようとすると、エラーが発生します。

> `OutboundTransfer` を使用して電信送金を行う際、住所フィールドに入力しない場合は、デフォルトで Stripe アカウント所有者を代表する法人に設定されます。

### NACHA 給与コンプライアンス

NACHA では、発信者が給与計算を目的とした ACH 決済を明示的に識別する必要があります。OutboundPayment を送信する際は、`purpose` パラメータを `payroll` に設定して、この要件のコンプライアンスを確保します。

## 金融口座への OutboundPayment を作成する

金融口座間で資金を移動するには、`POST /v1/treasury/outbound_payments` を起点口座で呼び出し、`destination_payment_method_data` パラメーターで移動先口座を指定します。両方の金融口座は同じプラットフォームに関連付けられていることが前提になりますが、同じ連結アカウントに関連付けることはできません。同じ連結アカウントに関連付けられている金融口座間で送金を行うには、[OutboundTransfer](https://docs.stripe.com/treasury/connect/legacy/v1/moving-money/out-of/outbound-transfers.md) を使用します。

```curl
curl https://api.stripe.com/v1/treasury/outbound_payments \
  -u "<<YOUR_SECRET_KEY>>:" \
  -H "Stripe-Account: {{CONNECTEDACCOUNT_ID}}" \
  -d financial_account={{SOURCE_FINANCIAL_ACCOUNT_ID}} \
  -d amount=2000 \
  -d currency=usd \
  -d "statement_descriptor=Test outbound payment to FA" \
  -d "destination_payment_method_data[type]=financial_account" \
  -d "destination_payment_method_data[financial_account]={{DESTINATION_FINANCIAL_ACCOUNT_ID}}"
```

リクエストの本文は、`x-www-form-urlencoded` でなければなりませんが、以下の JSON は、送信できるデータを定義します。

#### JSON (コメント付き)

```json
{
  // The source FinancialAccount. Funds are pulled from this account.
  "financial_account": "{{SOURCE_FINANCIAL_ACCOUNT_ID}}",
  // The amount to send.
  "amount": 1000,
  "currency": "usd",
  // The destination payment method. This parameter is the only way to
  // send an OutboundPayment through the `stripe` network.
  "destination_payment_method_data": {
    "type": "financial_account",
    "financial_account": "{{DESTINATION_FINANCIAL_ACCOUNT_ID}}"
  },
  // Optional. The Customer represents the recipient of the OutboundPayment.
  "customer": null | "{{CUSTOMER_ID}}",
  // An internal description for the OutboundPayment.
  "description": null | "Testing",
  // A descriptor for the OutboundPayment to send to the `stripe` network.
  // Maximum 500 characters.
  "statement_descriptor": "Test outbound payment to FA",
  "end_user_details": {
    // When making requests on behalf of a user, set `end_user_details.present=true`
    // and pass the user's IP address.
    // If the request is on behalf of yourself (initiating transfers out of your
    // FinancialAccounts), `end_user_details.present` should be set to `false`
    "present": true | false,
    // You are required to collect the IP address of the creator of this transfer for
    // risk and compliance reasons. This will be used to help determine if this transfer
    // is authorized or should be blocked.
    "ip_address": "127.0.0.1"
  },
  // We will not support updating OutboundPayments after creation. As such, the
  // metadata can only be set at creation time.
  "metadata": null | {{Hash}},
}
```

## OutboundPayment を取得する

`GET /v1/treasury/outbound_payments/{{OUTBOUND_PAYMENT_ID}}` を使用し、関連付けられた ID の `OutboundPayment` の詳細を取得します。

```curl
curl https://api.stripe.com/v1/treasury/outbound_payments/{{OUTBOUND_PAYMENT_ID}} \
  -u "<<YOUR_SECRET_KEY>>:" \
  -H "Stripe-Account: {{CONNECTEDACCOUNT_ID}}"
```

成功すると、関連付けられた ID の `OutboundPayment` オブジェクトがレスポンスで返されます。 レスポンスの一部のパラメーターには、`expand[]` パラメーターに値として追加した場合にのみ返される追加情報があります。次のレスポンス例で、拡張できるフィールドには、「Expandable」コメントが付いています。オブジェクトレスポンスの拡張の詳細については、[レスポンスを拡張する](https://docs.stripe.com/api/expanding_objects.md)をご覧ください。

#### JSON (コメント付き)

```json
{
  "id": "{{OUTBOUND_PAYMENT_ID}}",
  "object": "outbound_payment",
  "livemode": true | false,
  "created": "{{Timestamp}}",
  "financial_account": "{{FINANCIAL_ACCOUNT_ID}}", // Expandable
  "amount": 1000,
  "currency": "usd",
  // Will only be set if `destination_payment_method` was used during the creation of
  // the OutboundPayment
  "destination_payment_method": "{{PAYMENT_METHOD_ID}}",
  "destination_payment_method_details": null | {
    "type": "us_bank_acount" | "financial_account",
    "billing_details": {
      "name": null | "{{String}}",
      "phone": null | "{{String}}",
      "email": null | "{{String}}",
      "address": null | {
        "line1": null | "{{String}}",
        "line2": null | "{{String}}",
        "city": null | "{{String}}",
        "state": null | "{{String}}",
        "postal_code": null | "{{String}}",
        "country": null | "{{String}}"
      }
    },
    // Will only be set if type is `us_bank_account`
    "us_bank_account": {
      "network": "ach" | "us_domestic_wire",
      "routing_number": "12341234",
      "last4": "6789",
      "account_holder_type": "company",
      "bank_name": "Bank A",
      "fingerprint": "abc123"
    },
    // Will only be set if type is `financial_account`
    "financial_account": {
      "id": "{{DESTINATION_FINANCIAL_ACCOUNT_ID}}",
      "network": "stripe"
    }
  },
  // If the OutboundPayment hasn't yet been sent, this field is `true`, indicating
  // that the user may still cancel through the /cancel endpoint
  // (POST /v1/treasury/outbound_payments/obp_123/cancel)
  "cancelable": true | false,
  "description": "Testing",
  "statement_descriptor": "payment_1",
  // A unique, Stripe-hosted direct link to the regulatory receipt for the OutboundPayment
  "hosted_regulatory_receipt_url": "{{Url}}",
  // See the "OutboundPayment states" section below
  "status": "processing" | "canceled" | "failed" | "posted" | "returned",
  "status_transitions": {
    "processing_at": null | "{{Timestamp}}",
    "canceled_at": null | "{{Timestamp}}",
    "failed_at": null | "{{Timestamp}}",
    "posted_at": null | "{{Timestamp}}",
    "returned_at": null |"{{Timestamp}}"
  },
  // The local date when funds are expected to arrive in the
  // destination account.
  // Set when the status is processing
  // Can change after being set (for example, due to a partner delay) - Stripe will fire a
  // `treasury.outbound_payment.expected_arrival_date_updated` webhook when it does
  "expected_arrival_date": null | "{{Timestamp}}",
  // If the OutboundPayment has been returned, this field will be included with more
  // information about the return, including the Transaction that returns the funds.
  // See the "Handling Returned Funds" section below for more details on Returns.
  "returned_details": {
    "code": "account_closed" |
            "account_frozen" |
            "bank_account_restricted" |
            "bank_ownership_changed" |
            "could_not_process" |
            "invalid_account_number" |
            "incorrect_account_holder_name" |
            "invalid_currency" |
            "no_account" |
            "declined", // Generic fallback code
    // Human readable reason for the return. This message is geared towards the
    // end user, to help them determine next steps.
    "message": "The destination has been closed." |
               "The destination has been frozen." |
               "The destination bank account has restrictions on either the type or number of transfers allowed. This normally indicates that the bank account is a savings or other non-checking account." |
               "The destination bank account is no longer valid because its branch has changed ownership." |
               "The destination could not process this OutboundPayment." |
               "The destination bank account details on file are probably incorrect. The routing number seems correct, but the account number is invalid." |
               "The destination bank account details on file may be incorrect." |
               "The destination was unable to process this OutboundPayment because of its currency." |
               "The details of the destination may be incorrect." |
               "The destination has declined this OutboundPayment.",
    "transaction": "trxn_456" // Expandable
  },
  // If available, this field shows network-specific tracking information.
  // Tracking details can appear anytime after the object is no longer cancelable.
  // Stripe sends the `treasury.outbound_payment.tracking_details_updated` event
  // when this field is updated.
  "tracking_details": null | {
    "type": "ach" | "us_domestic_wire",
    // Only set for ACH transfers
    "ach": null | {
      "trace_id": "12345678901234"
    },
    // Only set for wire transfers
    "us_domestic_wire": null | {
      "imad": "20230101MMQFMPD1001234",
      "omad": "20230101MMQFMPD1002345"
    }
  }
  // Transaction representing balance impact of the OutboundPayment, created
  // synchronously with the OutboundPayment.
  // OutboundPayments always have a Transaction from creation (the funds are
  // held immediately).
  // If the OutboundPayment fails, the Transaction will be voided.
  // If the OutboundPayment is returned, its Transaction remains posted. Funds are
  // returned to the balance with returned_details.transaction
  "transaction": "{{TRANSACTION_ID}}", // Expandable
  "end_user_details": {
    "present": true,
    "ip_address": "127.0.0.1"
  },
  "metadata": {}
}
```

## OutboundPayment をキャンセルする

`POST /v1/treasury/outbound_payments/{{OUTBOUND_PAYMENT_ID}}/cancel` を使用し、関連付けられた ID の `OutboundPayment` をキャンセルします。`OutboundPayment` オブジェクトには、送金をキャンセルできるかどうかを示すブール値を含む `cancelable` パラメーターが含まれます。`OutboundPayment` がネットワークに送信されると、`cancelable` 値が `false` になり、このエンドポイントからの送金に対するエラーが受信されます。

```curl
curl -X POST https://api.stripe.com/v1/treasury/outbound_payments/{{OUTBOUND_PAYMENT_ID}}/cancel \
  -u "<<YOUR_SECRET_KEY>>:" \
  -H "Stripe-Account: {{CONNECTEDACCOUNT_ID}}"
```

成功すると、レスポンスで、`status` 値が `canceled` に設定された `OutboundPayment` オブジェクトが返されます。

```json
{
  "id": "{{OUTBOUND_PAYMENT_ID}}",
  "object": "outbound_payment",
  "livemode": false,
  "created": 123456,
  "financial_account": "{{FINANCIAL_ACCOUNT_ID}}",
  "amount": 1000,
  "currency": "usd",
  ...
  "status": "canceled",
  "status_transitions": {
    "processing_at": null,
    "canceled_at": 123456,
    "failed_at": null,
    "posted_at": null,
    "returned_at": null
  },
  ...
}
```

## OutboundPayment を一覧表示する

`GET /v1/treasury/outbound_payments` を使用し、関連付けられた ID の金融口座からの `OutboundPayments` をリストします。標準のリストパラメーターや、`status` または `customer` でリストをフィルタリングできます。

```
{
  // Standard list parameters
  "limit", "starting_after", "ending_before",
  // Filter by status
  "status": "processing" | "canceled" | "failed" | "posted" | "returned",
  // Filter by FinancialAccount (Required)
  "financial_account": "{{FINANCIAL_ACCOUNT_ID}}",
  // Filter by Customer
  "customer": "{{CUSTOMER_ID}}",
}
```

以下のリクエストは、プラットフォームに関連付けられた金融口座において、特定された `Customer` に支払われた最新 5 件の [OutboundPayment オブジェクト](https://docs.stripe.com/api/treasury/outbound_payments/object.md) を取得します。

```curl
curl -G https://api.stripe.com/v1/treasury/outbound_payments \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d "financial_account={{TREASURYFINANCIALACCOUNT_ID}}" \
  -d limit=5 \
  -d "customer={{CUSTOMER_ID}}"
```

## OutboundPayment の状態

以下の表は、各ステータスと可能な移行状態について説明したものです。

| ステータス | 説明 | 可能な状態の移行 |
| --- | --- | --- |
| `processing` | `OutboundPayment` の開始状態。資金は、保留中の取引に割り当てられます (ただし、引き続き現在の残高に含まれています)。`cancelable` パラメーターの値が `true` の間は 、ユーザーは `OutboundPayment` をキャンセルできます。 | `posted`、`canceled`、`failed` |
| `failed` (最終状態) | `OutboundPayment` の確認に失敗しました。Stripe は保留中の取引を無効にし、資金をユーザーに返金します。 | 該当なし |
| `canceled` (最終状態) | ユーザーが、転記前に `OutboundPayment` をキャンセルしました。Stripe は、保留中の取引を無効にし、資金をユーザーに返金します。 | 該当なし |
| `posted` | `OutboundPayment` が転記され、資金がアカウントから送金されました。基となる取引が転記されます。 | `returned` |
| `returned` (最終状態) | `OutboundPayment` は、送金先に正常に入金されませんでした。資金は取引 (`returned_details[transaction]`) とともにユーザーに返されます。 | 該当なし |

## OutboundPayment をテストする

導入をエンドツーエンドでテストするには、テスト [SetupIntent リクエスト](https://docs.stripe.com/treasury/connect/legacy/v1/moving-money/working-with-bankaccount-objects.md#setupintents)を使用して `PaymentMethod` を作成し、その `PaymentMethod`　を `OutboundPayment` 作成リクエストに渡し、`destination_payment_method` パラメーターを使用することをお勧めします。

Stripe では、以下のようなテスト用の `PaymentMethod` トークンおよび番号を使用して、特定の機能をトリガーすることもできます。

- テスト用の `PaymentMethod` トークンを `destination_payment_method` に渡す (`ach` および `us_domestic_wire` のネットワークの場合)
  - テスト用の `PaymentMethod` トークンを `destination_payment_method` に直接渡す場合でも、`customer` パラメーターに顧客 ID を渡す必要があります。便宜上、Stripe では既存のテスト用顧客を渡すことができます。これは、既存の `PaymentMethod` を `Customer` に関連付け、同じ顧客 ID を `customer` パラメーターに渡す必要がある本番環境とは異なります。
- テスト用の金融番号と口座番号を `destination_payment_method_data[us_bank_account]` に渡す (`ach` および `us_domestic_wire` のネットワークの場合)。
- プラットフォーム内アカウントが所有する既存のテスト用金融口座の ID を `destination_payment_method_data[financial_account]` (Stripe ネットワーク用) に渡す。

いずれの場合も、`OutboundPayment` のレスポンスは処理中のステータスを返します。Stripe は、該当する状態の移行に対して [Webhook](https://docs.stripe.com/webhooks.md) をトリガーします。`OutboundPayment` を作成後に取得すると、想定された状態が返されます。

| 作成 | DESTINATION_PAYMENT_METHOD (既存のテスト用顧客) | DESTINATION_PAYMENT_METHOD_DATA[US_BANK_ACCOUNT] |
| --- | --- | --- |
| 初期の処理状態の `OutboundPayment` | `pm_usBankAccount_processing` | - `routing_number`: 110000000
- `account_number`: 000000000009 |
| (`processing` から) `posted` に移行する `OutboundPayment` | `pm_usBankAccount` | - `routing_number`: 110000000
- `account_number`: 000123456789 |
| (`processing` から) `posted` に移行する `OutboundPayment`。さらに、元の `expected_arrival_date` に 1 日追加します | `pm_usBankAccount_expectedArrivalDateUpdated` | - `routing_number`: 110000000
- `account_number`: 000123457890 |
| (`processing` から) `canceled` に移行する `OutboundPayment` | `pm_usBankAccount_canceledByUser` | - `routing_number`: 110000000
- `account_number`: 000000000123 |
| (`processing` から) `failed` に移行する `OutboundPayment` | `pm_usBankAccount_internalFailure` | - `routing_number`: 110000000
- `account_number`: 000000000234 |
| アカウント閉鎖のために (転記後に processing から) `returned` に移行する `OutboundPayment` | `pm_usBankAccount_accountClosed` | - `routing_number`: 110000000
- `account_number`: 000111111113 |
| 口座がないために (転記後に processing から) returned に移行する `OutboundPayment` | `pm_usBankAccount_noAccount` | - `routing_number`: 110000000
- `account_number`: 000111111116 |
| 口座番号が無効であるために (転記後に processing から) `returned` に移行する `OutboundPayment` | `pm_usBankAccount_invalidAccountNumber` | - `routing_number`: 110000000
- `account_number`: 000111111119 |

### OutboundPayment のテスト用ヘルパーエンドポイント

Stripe は、さまざまな状態の `OutboundPayments` をテストできるエンドポイントを提供しています。テストエンドポイントを使用して、作成した `OutboundPayment` を `posted`、`failed`、または `returned` などの新しい状態に直接移行します。

- [テスト用の post エンドポイント](https://docs.stripe.com/api/treasury/outbound_payments/test_mode_post.md)を使用して、特定された `OutboundPayment` を `processing` から `posted` に移行します。

  `POST /v1/test_helpers/treasury/outbound_payments/{{OUTBOUND_PAYMENT_ID}}/post`

- [テスト用の fail エンドポイント](https://docs.stripe.com/api/treasury/outbound_payments/test_mode_fail.md)を使用して、特定された `OutboundPayment` を `processing` から `failed` に移行します。

  `POST /v1/test_helpers/treasury/outbound_payments/{{OUTBOUND_PAYMENT_ID}}/fail`

- [テスト用の return エンドポイント](https://docs.stripe.com/api/treasury/outbound_payments/test_mode_return.md)を使用して、特定された `OutboundPayment` を `processing` から `returned` に移行します。

  `POST /v1/test_helpers/treasury/outbound_payments/{{OUTBOUND_PAYMENT_ID}}/return`

これらのエンドポイントは、返品などのエラーシナリオをテストするために特に便利です。こういったエンドポイントがなければ、外部でのアクションが必要になります。

`return` エンドポイントでは、オプションの `returned_details.code` パラメーターを本文に含め、返金理由を示します。この情報が提供されない場合、送金にはデフォルトの `declined` 返金コードが適用されます。

```json
{
  "returned_details": {
    "code": "account_closed" |
          "account_frozen" |
          "bank_account_restricted" |
          "bank_ownership_changed" |
          "could_not_process" |
          "invalid_account_number" |
          "incorrect_account_holder_name" |
          "invalid_currency" |
          "no_account" |
          "declined"
  }
}
```

また、テスト用の `Outbound Payment` で追跡詳細の転記をシミュレートするための、[テスト用更新エンドポイント](https://docs.stripe.com/api/treasury/outbound_payments/test_mode_update.md)も提供しています。`tracking_details` フィールドは、テスト用オブジェクトにのみ設定できます。

いずれのケースでも、Stripe は、該当するそれぞれの状態の移行に対して [Webhooks](https://docs.stripe.com/webhooks.md) をトリガーします。移行後に `OutboundPayment` を取得すると、想定される状態が返されます。

## OutboundPayment Webhook

Stripe は、[Webhook](https://docs.stripe.com/webhooks.md) エンドポイントに以下の `OutboundPayment` イベントを送信します。

- `OutboundPayment` 作成時の `treasury.outbound_payment.created`。
- `OutboundPayment` のステータスが変化した際の `treasury.outbound_payment.{{new_status}}`。ステータス値のオプションには以下が含まれます。
  - `treasury.outbound_payment.posted`
  - `treasury.outbound_payment.failed`
  - `treasury.outbound_payment.canceled`
  - `treasury.outbound_payment.returned`
- `OutboundPayment` の `expected_arrival_date` が変化した際の `treasury.outbound_payment.expected_arrival_date_updated`。
- `OutboundPayment` の追跡の詳細情報が更新された際の `treasury.outbound_payment.tracking_details_updated`。
