Android SDK による問題パス

パスを作成して JWT でエンコードしたら、Android アプリでパスを発行できます。そのためには、ユーザーのデバイスで Google Wallet API を使用できることを確認し、「Google ウォレットに追加」と表示する必要があります。ボタンをタップし、ボタンをタップしたら Google ウォレットにパスを保存します。

前提条件

パスを発行する前に、以下の手順を完了していることを確認してください。

で確認できます。

1. Google ウォレット Android SDK をインストールする

Google ウォレット Android SDK を使用するには、アプリレベルの build.gradle ファイルの dependencies セクションに com.google.android.gms:play-services-pay を追加します。

  implementation "com.google.android.gms:play-services-pay:16.5.0"

2. Google Wallet API の提供状況を確認する

新しいオブジェクトを保存する前に、Google Wallet API が PayClient クラスの getPayApiAvailabilityStatus メソッドを呼び出して、対象デバイスで利用できます。

まず、メンバー変数を このアクティビティでは、ボタンを表示し、アクティビティが 作成:

Kotlin

import com.google.android.gms.pay.PayClient

private lateinit var walletClient: PayClient

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  walletClient = Pay.getClient(this)

  // Additional logic in your onCreate method
}

Java

import com.google.android.gms.pay.PayClient;

private final PayClient walletClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  walletClient = Pay.getClient(application);

  // Additional logic in your onCreate method
}

他の設計パターンを使用する場合は、ドメイン固有のビジネス ロジックを適切に配置することを検討してください。たとえば MVVM パターンを使用する場合は UI 関連のビジネス ロジックを Activity または Fragment に配置(例:UI 要素 オペレーション ロジック( インスタンス化、ネットワーク呼び出しトリガーなど)が含まれます。

次に、PayClient を使用して、API が使用可能かどうかを確認します。

Kotlin

import com.google.android.gms.pay.PayApiAvailabilityStatus

private fun fetchCanUseGoogleWalletApi() {
  walletClient
    .getPayApiAvailabilityStatus(PayClient.RequestType.SAVE_PASSES)
    .addOnSuccessListener { status ->
      if (status == PayApiAvailabilityStatus.AVAILABLE) {
        // The API is available, show the button in your UI
      } else {
        // The user or device is not eligible for using the Pay API
      }
    }
    .addOnFailureListener {
      // Hide the button and optionally show an error message
    }
}

Java

import com.google.android.gms.pay.PayApiAvailabilityStatus;

private void fetchCanAddPassesToGoogleWallet() {
  walletClient
    .getPayApiAvailabilityStatus(PayClient.RequestType.SAVE_PASSES)
    .addOnSuccessListener(status -> {
      if (status == PayApiAvailabilityStatus.AVAILABLE) {
        // The API is available, show the button in your UI
      } else {
        // The user or device is not eligible for using the Pay API
      };
    })
    .addOnFailureListener(exception -> {
      // Google Play Services is too old, or API availability not verified
      // Hide the button and optionally show an error message
    });
}

最後に、API が使用可能かどうかを判断する際にアプリケーションで定義したメソッドを呼び出します。

API が使用できない場合の処理

API が使用できない理由としては、Android や Google Play 開発者サービスのバージョンが古い、または Google ウォレットが 利用できない状態です。

API が使用できない場合 ボタンを非表示にして、別の統合(例: JWT リンクを使用)。なお、今後ユーザーが API を使用できるようになる可能性もあります。

3. [Google ウォレットに追加] をボタン

Google ウォレットには使い慣れたボタンがあり、これを使用して アプリ内の Google ウォレットへの追加フロー。ボタンのベクター アセットは次のとおりです。 については、 ボタンに関するガイドライン

ベクター アセットは、Android Studio で File > New > Vector Asset を使用してインポートできます。[ローカル ファイル] を選択ウィザードで、名前(例: add_to_google_wallet_button.xml)を指定し、ローカル ドライブでそのファイルを探してインポートします。

  • [Google ウォレットに追加] ボタン
  • [Google ウォレットに追加] ボタンを縮小しました

これで、インポートしたドローアブルを使用してユーザー インターフェースにボタンを追加できます。

    <ImageButton
        android:id="@+id/addToGoogleWalletButton"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:minWidth="200dp"
        android:clickable="true"
        android:src="@drawable/add_to_google_wallet_button" />

ボタンの layout_height は 48 dp とし、幅は 200 dp 以上にする必要があります。

4. ユーザーの Google ウォレットにパスを追加する

FlightObject を追加するには、無署名の JWT を savePasses メソッドに渡します。 [Google ウォレット] をクリックすると、追加操作を開始できます。 ] ボタンを離します。

Kotlin

import android.os.Bundle
import android.view.View
import com.google.android.gms.samples.wallet.databinding.ActivityCheckoutBinding

private val addToGoogleWalletRequestCode = 1000

private lateinit var layout: ActivityCheckoutBinding
private lateinit var addToGoogleWalletButton: View

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  // Use view binding to access the UI elements
  layout = ActivityCheckoutBinding.inflate(layoutInflater)
  setContentView(layout.root)

  addToGoogleWalletButton = layout.addToGoogleWalletButton
  addToGoogleWalletButton.setOnClickListener {
    walletClient.savePasses(newObjectJson, this, addToGoogleWalletRequestCode)
  }

  // Additional logic in your onCreate method
}

Java

import android.os.Bundle;
import android.view.View;
import com.google.android.gms.samples.wallet.databinding.ActivityCheckoutBinding;

private static final int ADD_TO_GOOGLE_WALLET_REQUEST_CODE = 999;

private ActivityCheckoutBinding layout:
private View addToGoogleWalletButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // Use view binding to access the UI elements
  layout = ActivityCheckoutBinding.inflate(getLayoutInflater());
  setContentView(layout.getRoot());

  addToGoogleWalletButton = layout.addToGoogleWalletButton;
  addToGoogleWalletButton.setOnClickListener(v -> {
    walletClient.savePasses(newObjectJson, this, ADD_TO_GOOGLE_WALLET_REQUEST_CODE);
  });

  // Additional logic in your onCreate method
}

結果の処理

savePasses メソッドは保存フローをトリガーし、 保存フローが完了した後の onActivityResult メソッド。onActivityResult の実装は次のようになります。

Kotlin

import android.content.Intent

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  super.onActivityResult(requestCode, resultCode, data)

  if (requestCode == addToGoogleWalletRequestCode) {
    when (resultCode) {
      RESULT_OK -> {
        // Pass saved successfully
      }

      RESULT_CANCELED -> {
        // Save operation canceled
      }

      PayClient.SavePassesResult.SAVE_ERROR -> data?.let { intentData ->
        val errorMessage = intentData.getStringExtra(PayClient.EXTRA_API_ERROR_MESSAGE)
        // Handle error
      }

      else -> {
          // Handle unexpected (non-API) exception
      }
    }
  }
}

Java

import android.content.Intent;

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  if (requestCode == ADD_TO_GOOGLE_WALLET_REQUEST_CODE) {
    switch (resultCode) {
      case RESULT_OK: {
        // Pass saved successfully
        break;
      }

      case RESULT_CANCELED: {
        // Save operation canceled
        break;
      }

      case PayClient.SavePassesResult.SAVE_ERROR: {
        if (data != null) {
          String apiErrorMessage = data.getStringExtra(PayClient.EXTRA_API_ERROR_MESSAGE);
          // Handle error
        }
        break;
      }

      default: {
        // Handle unexpected (non-API) exception
      }
    }
  }
}

パスの追加が成功すると、resultCode には Activity.RESULT_OK の値が含まれます。