Sau khi tạo thẻ và mã hoá thẻ đó trong JWT, bạn đã sẵn sàng phát hành thẻ trong ứng dụng Android của mình. Để làm việc này, bạn cần kiểm tra xem Google Wallet API có trên thiết bị của người dùng hay không, cho người dùng thấy nút "Thêm vào Google Wallet", sau đó lưu thẻ vào Google Wallet của họ khi họ nhấn vào nút này.
Điều kiện tiên quyết
Trước khi thử phát hành thẻ và vé, hãy đảm bảo bạn đã hoàn tất các bước sau:
- Hoàn tất tất cả các bước trong Hướng dẫn tham gia.
- Tạo ít nhất một Lớp thẻ và vé.
- Tạo ít nhất một Đối tượng thẻ và vé.
- Mã hoá Passes Class và Passes Object trong JWT.
1. Cài đặt SDK Android của Google Wallet
Để sử dụng Google Wallet Android SDK, hãy thêm com.google.android.gms:play-services-pay
vào mục dependencies
của tệp build.gradle
ở cấp ứng dụng:
implementation "com.google.android.gms:play-services-pay:16.5.0"
2. Kiểm tra xem Google Wallet API có hoạt động hay không
Trước khi lưu đối tượng mới, hãy đảm bảo rằng API Google Wallet có trên thiết bị mục tiêu bằng cách gọi phương thức getPayApiAvailabilityStatus
trong lớp PayClient
.
Bắt đầu bằng cách thêm một biến thành viên vào hoạt động mà bạn sẽ hiện nút và khởi tạo biến đó khi hoạt động được tạo:
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
}
Nếu bạn đang sử dụng các mẫu thiết kế khác, hãy cân nhắc việc đặt logic nghiệp vụ dành riêng cho miền một cách thích hợp. Ví dụ: nếu bạn đang sử dụng mẫu MVVM, hãy đặt logic nghiệp vụ liên quan đến giao diện người dùng trong Activity hoặc Fragment (ví dụ: Các phần tử trên giao diện người dùng, kết quả hoạt động) và logic hoạt động trong mô hình khung hiển thị (ví dụ: khởi tạo ứng dụng, trình kích hoạt lệnh gọi mạng).
Tiếp theo, hãy dùng PayClient
để kiểm tra xem API có dùng được hay không:
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
});
}
Cuối cùng, hãy gọi phương thức mà bạn vừa xác định trong ứng dụng khi cần xác định trạng thái cung cấp của API.
Xử lý khi API không hoạt động
Một số lý do khiến API có thể không hoạt động là do phiên bản Android hoặc Dịch vụ Google Play đã lỗi thời, hoặc Google Wallet không hoạt động ở quốc gia của người dùng.
Nếu API không hoạt động, hãy cân nhắc việc ẩn nút và chuyển về một chế độ tích hợp khác (ví dụ: sử dụng đường liên kết JWT). Xin lưu ý rằng người dùng có thể đủ điều kiện sử dụng API này trong tương lai.
3. Thêm nút "Thêm vào Google Wallet"
Google Wallet cung cấp một nút quen thuộc mà bạn có thể dùng để kích hoạt quy trình Thêm vào Google Wallet trong ứng dụng của mình. Các thành phần vectơ cho nút có trong Nguyên tắc về nút.
Bạn có thể nhập tài sản vectơ trong Android Studio trong File > New > Vector Asset
. Chọn "Tệp cục bộ" trong trình hướng dẫn, thêm tên (ví dụ:
add_to_google_wallet_button.xml
) rồi tìm tệp trong ổ đĩa cục bộ để nhập.
Giờ đây, bạn có thể dùng đối tượng có thể vẽ đã nhập để thêm nút vào giao diện người dùng:
<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" />
Nút này có layout_height
là 48 dp và phải có chiều rộng ít nhất là 200 dp.
4. Thêm thẻ và vé vào Google Wallet của người dùng
Bạn có thể thêm TransitObject
bằng cách truyền một JWT chưa ký vào phương thức savePasses
.
Bạn có thể bắt đầu thao tác thêm do nhấp vào nút 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
}
Xử lý kết quả
Phương thức savePasses
sẽ kích hoạt quy trình lưu và gọi phương thức onActivityResult
sau khi quy trình lưu hoàn tất. Việc triển khai onActivityResult
sẽ tương tự như sau:
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
}
}
}
}
Khi thẻ được thêm thành công, resultCode
sẽ chứa giá trị của Activity.RESULT_OK
.