การออกบัตรด้วย Android SDK

เมื่อสร้างบัตรและเข้ารหัสใน JWT แล้ว คุณก็พร้อมที่จะออกบัตรในแอป Android ของคุณ โดยจะต้องตรวจสอบว่า Google Wallet API พร้อมใช้งานในอุปกรณ์ของผู้ใช้ แสดงปุ่ม "เพิ่มลงใน Google Wallet" ให้ผู้ใช้ แล้วบันทึกบัตรลงใน Google Wallet ของผู้ใช้เมื่อแตะปุ่ม

ข้อกำหนดเบื้องต้น

ก่อนที่จะพยายามออกบัตร โปรดตรวจสอบว่าคุณได้ทำสิ่งต่อไปนี้แล้ว

1. ติดตั้ง Google Wallet Android SDK

หากต้องการใช้ Google Wallet Android SDK ให้เพิ่ม com.google.android.gms:play-services-pay ลงในส่วน dependencies ของไฟล์ build.gradle ระดับแอป

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

2. ตรวจสอบความพร้อมใช้งานของ Google Wallet API

ก่อนบันทึกออบเจ็กต์ใหม่ ให้ตรวจสอบว่า Google Wallet API พร้อมใช้งานในอุปกรณ์เป้าหมายโดยเรียกใช้เมธอดgetPayApiAvailabilityStatusในคลาสPayClient

เริ่มต้นด้วยการเพิ่มตัวแปรสมาชิกไปยัง กิจกรรมที่คุณจะแสดงปุ่ม และสร้างอินสแตนซ์เมื่อสร้างกิจกรรม

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, ผลลัพธ์ของกิจกรรม) และตรรกะการทำงานใน ViewModel (เช่น การสร้างอินสแตนซ์ไคลเอ็นต์ ทริกเกอร์การเรียกเครือข่าย)

จากนั้นใช้ 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 Wallet ไม่พร้อมใช้งานในประเทศของผู้ใช้

หาก API ไม่พร้อมใช้งาน ให้ซ่อนปุ่มและกลับไปใช้การผสานรวมอื่น (เช่น ใช้ลิงก์ JWT) โปรดทราบว่าผู้ใช้อาจมีสิทธิ์ใช้ API ในอนาคต

3. เพิ่มปุ่ม "เพิ่มลงใน Google Wallet"

Google Wallet มีปุ่มที่คุ้นเคยซึ่งคุณใช้เพื่อทริกเกอร์ ขั้นตอนการเพิ่มลงใน Google Wallet ในแอปพลิเคชันได้ ชิ้นงานเวกเตอร์สำหรับปุ่มมีอยู่ในหลักเกณฑ์เกี่ยวกับปุ่ม

คุณนำเข้าเนื้อหาเวกเตอร์ใน Android Studio ได้ที่File > New > Vector Asset เลือก "ไฟล์ในเครื่อง" ในวิซาร์ด แล้วตั้งชื่อ (เช่น add_to_google_wallet_button.xml) แล้วค้นหาไฟล์ในไดรฟ์ในเครื่องเพื่อนำเข้า

  • ปุ่ม "เพิ่มลงใน Google Wallet"
  • ปุ่มเพิ่มลงใน Google Wallet แบบย่อ

ตอนนี้คุณสามารถใช้ Drawable ที่นำเข้าเพื่อเพิ่มปุ่มลงในอินเทอร์เฟซผู้ใช้ได้แล้ว

    <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 Wallet ของผู้ใช้

คุณเพิ่ม GenericObject ได้โดยส่ง JWT ที่ไม่ได้ลงนามไปยังเมธอด savePasses คุณเริ่มการดำเนินการเพิ่มได้โดยคลิกปุ่ม Google Wallet

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