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