เมื่อสร้างบัตรและเข้ารหัสบัตรใน JWT แล้ว คุณก็พร้อมที่จะออกบัตรในแอป Android โดยจะต้องตรวจสอบว่า Google Wallet API พร้อมใช้งานในอุปกรณ์ของผู้ใช้ แสดงปุ่ม "เพิ่มลงใน Google Wallet" ให้ผู้ใช้เห็น แล้วบันทึกบัตรลงใน Google Wallet ของผู้ใช้เมื่อผู้ใช้แตะปุ่ม
ข้อกำหนดเบื้องต้น
โปรดตรวจสอบว่าคุณได้ทำสิ่งต่อไปนี้เสร็จสมบูรณ์แล้วก่อนที่จะพยายามออกบัตร
- ทำตามขั้นตอนทั้งหมดในคู่มือการเตรียมความพร้อมผู้ใช้งานใหม่
- สร้างคลาสบัตรอย่างน้อย 1 คลาส
- สร้างออบเจ็กต์บัตรอย่างน้อย 1 รายการ
- เข้ารหัสคลาสบัตรและออบเจ็กต์บัตรใน JWT
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 ในกิจกรรมหรือ Fragment (เช่น องค์ประกอบ UI, ผลลัพธ์ของกิจกรรม) และตรรกะการทำงานใน View Model (เช่น การสร้างอินสแตนซ์ของไคลเอ็นต์, ทริกเกอร์การเรียกเครือข่าย)
จากนั้นใช้ 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 Services เวอร์ชันเก่า หรือ Google Wallet ไม่พร้อมใช้งานในประเทศของผู้ใช้
หาก API ไม่พร้อมใช้งาน ให้ซ่อนปุ่มและกลับไปใช้การผสานรวมอื่น (เช่น ใช้ลิงก์ JWT) โปรดทราบว่าผู้ใช้อาจมีสิทธิ์ใช้ API ในอนาคต
3. เพิ่มปุ่ม "เพิ่มลงใน Google Wallet"
Google Wallet มีปุ่มที่คุ้นเคยซึ่งคุณใช้เพื่อทริกเกอร์โฟลว์การเพิ่มลงใน Google Wallet ในแอปพลิเคชันได้ ชิ้นงานเวกเตอร์สำหรับปุ่มมี อยู่ใน หลักเกณฑ์ของปุ่ม
คุณสามารถนำเข้าชิ้นงานเวกเตอร์ใน Android Studio ได้ที่ File > New > Vector Asset เลือก "Local file" ในวิซาร์ด ตั้งชื่อ (เช่น
add_to_google_wallet_button.xml) และค้นหาไฟล์ในไดรฟ์ภายในเครื่องเพื่อนำเข้า
ตอนนี้คุณสามารถใช้ 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 ของผู้ใช้
คุณเพิ่ม TransitObject ได้โดยส่ง 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

