> 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ウォレット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() {
        /*
         * 最初のタップの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()` 認証が成功した後に、再タップを要求するために。
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.
