เมื่อสร้างบัตรและเข้ารหัสบัตรใน JWT แล้ว คุณก็พร้อมที่จะออกบัตรในแอป Android ได้ โดยคุณจะต้องตรวจสอบว่า Google Wallet API ใช้งานได้ในอุปกรณ์ของผู้ใช้ และแสดงบัตรดังกล่าวด้วยปุ่ม "เพิ่มลงใน Google Wallet" แล้วบันทึกบัตรลงใน Google Wallet เมื่อผู้ใช้แตะปุ่ม
สิ่งที่ต้องดำเนินการก่อน
ก่อนที่คุณจะพยายามออกบัตร โปรดตรวจสอบว่าคุณได้ทำสิ่งต่อไปนี้แล้ว
- ทําตามขั้นตอนทั้งหมดในคู่มือการเริ่มต้นใช้งาน
- สร้างชั้นเรียนบัตรอย่างน้อย 1 ชั้นเรียน
- สร้างออบเจ็กต์ Pass อย่างน้อย 1 รายการ
- เข้ารหัสคลาส Passes และออบเจ็กต์ Passes ใน 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 ในกิจกรรมหรือส่วนย่อย (เช่น องค์ประกอบ 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 ของผู้ใช้
คุณเพิ่ม GiftCardObject
ได้โดยส่ง 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