> For the complete documentation index, see [llms.txt](https://docs.payments.thalescloud.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.payments.thalescloud.io/nfc-wallet-sdk-android/ja/implement-nfc-wallet/make-payment/implement-contactless-payments/2.-implement-contactless-payment-callbacks.md).

# 2. 非接触決済のコールバックを実装する

## 概要

APDU 処理中、NFC Wallet SDK は支払いライフサイクルのイベントであなたのアプリケーションを呼び出します。

実装する `ContactlessPaymentServiceListener` をデジタルウォレットアプリケーションで実装して、次を行います:

* 非接触取引の開始と完了を検出します。
* CDCVM が必要な場合にステップアップ認証をトリガーします。
* エラーを処理し、次の取引に備えて回復します。

## SDK 連携

### 実装する `ContactlessPaymentServiceListener`

リスナーインスタンスを作成し、必要なコールバックを実装します。

```java
// HCE サービス用のリスナーをインスタンス化します。
PaymentServiceListener myPaymentListener = new ContactlessPaymentServiceListener() {

    @Override
    public void onTransactionStarted() {
        /*
         * POS 端末との最初の APDU が交換されたときにトリガーされます。
         */
    }

    @Override
    public void onAuthenticationRequired(
            PaymentService activatedPaymentService,
            CHVerificationMethod chVerificationMethod,
            long cvmResetTimeout) {
        /*
         * エンドユーザーによる認証（CDCVM）が必要なときにトリガーされます。
         *
         * 必要な方法を判断するには chVerificationMethod を使用します。
         * cvmResetTimeout は一部の方法（たとえばウォレット PIN）にのみ適用されます。
         * これを使用して、検証がどのくらいの時間有効かをエンドユーザーに伝えます。
         */
    }

    @Override
    public void onReadyToTap(PaymentService paymentService) {
        /*
         * 認証成功後にトリガーされます。
         * cvmResetTimeout が期限切れになる前に、エンドユーザーにもう一度タップするよう促します。
         */
    }

    @Override
    public void onTransactionCompleted(TransactionContext transactionContext) {
        /*
         * 取引が正常に完了したときにトリガーされます。
         * TransactionContext を使用して詳細（金額、日付など）を表示します。
         */
    }

    @Override
    public void onTransactionInterrupted() {
        /*
         * POS 端末への NFC リンクが中断されたときにトリガーされます。
         * エンドユーザーにもう一度タップするよう促します。
         * オプションのコールバック
         */
    }

    @Override
    public void onError(
            TransactionContext transactionContext,
            PaymentServiceErrorCode paymentServiceErrorCode,
            String message) {
        /*
         * 取引を正常に完了できないときにトリガーされます。
         */
    }

    @Override
    public void onFirstTapCompleted() {
        /*
         * 1 回目のタップの APDU 処理が完了したことを示します。
         * PFP ベースの取引でのみサポートされます。
         */
    }

    @Override
    public void onNextTransactionReady(
            DeactivationStatus deactivationStatus,
            DigitalizedCardStatus digitalizedCardStatus,
            DigitalizedCard digitalizedCard) {
        /*
         * 取引完了後にトリガーされます。
         * カードの状態を確認し、次の支払いに備えるために使用します。
         */
    }
};
```

### HCE サービスからリスナーを返します

拡張しているクラスで `AsyncHCEService`、からリスナーを返します `setupListener()`.

```java
public class MyHCEService extends AsyncHCEService {

    @Override
    public PaymentServiceListener setupListener() {
        return myPaymentListener;
    }
}
```

## コールバックフロー（通常）

コールバックは通常この順序でトリガーされます。

`onTransactionInterrupted()` は、 `onTransactionStarted()`.

1. `onTransactionStarted()` の後、POS 端末との最初の APDU 交換後にいつでも発生する可能性があります。
2. `onAuthenticationRequired()` エンドユーザーが CDCVM を完了する必要があるとき。
3. `onReadyToTap()` 認証成功後、2 回目のタップを要求するため。
4. `onTransactionCompleted()` 取引が正常に完了したとき。
5. `onTransactionInterrupted()` （オプション）POS 端末への NFC リンクが切断されたとき。を参照してください [POS 端末の切断を処理する](#handle-pos-terminal-disconnects-optional).
6. `onNextTransactionReady()` SDK が次の取引の準備ができたとき。

エラーが発生した場合、SDK は `onError()` をトリガーし、フローを完了しません。

## POS 端末の切断を処理する（オプション）

SDK は、POS 端末への NFC リンクが中断されたときに通知できます。

使用する `onTransactionInterrupted()` エンドユーザーにもう一度タップするよう促します。

非接触支払いを開始する前に、再試行動作を設定します:

* `PaymentSetting.setRetryLimit(int)`

  取引が失敗する前に許容される POS 端末の切断回数を設定します。

  デフォルトは `0`.

  で、 `0`のとき、SDK は `onError()` 最初の切断でトリガーします。
* `PaymentSetting.setTransactionRetryTimeout(long)`

  切断後に POS 端末が再試行できる時間を設定します。

  この間隔中に APDU が受信されない場合、SDK は `onError()`.

  範囲は `500` から `10000` ミリ秒です。

  デフォルトは `2000` ミリ秒です。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.payments.thalescloud.io/nfc-wallet-sdk-android/ja/implement-nfc-wallet/make-payment/implement-contactless-payments/2.-implement-contactless-payment-callbacks.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
