【发布时间】:2021-03-20 19:22:02
【问题描述】:
我按照此处的说明确定 Google Pay 钱包是否准备就绪。 https://developers.google.com/pay/api/android/guides/tutorial#isreadytopay
在我将 true 设置为 existingPaymentMethodRequired 之前,这一切正常。我的目的是知道钱包是否至少有一张卡,但我总是从请求中得到false。我的环境是测试环境,安卓手机注册了信用卡。有谁知道为什么?文档在这里https://developers.google.com/pay/api/android/reference/request-objects#IsReadyToPayRequest
public static Optional<JSONObject> getIsReadyToPayRequest() {
try {
JSONObject isReadyToPayRequest = getBaseRequest();
isReadyToPayRequest.put(
"allowedPaymentMethods", new JSONArray().put(getBaseCardPaymentMethod()));
isReadyToPayRequest.put("existingPaymentMethodRequired", true);
return Optional.of(isReadyToPayRequest);
} catch (JSONException e) {
return Optional.empty();
}
}
private static JSONObject getCardPaymentMethod() throws JSONException {
JSONObject cardPaymentMethod = getBaseCardPaymentMethod();
cardPaymentMethod.put("tokenizationSpecification", getGatewayTokenizationSpecification());
return cardPaymentMethod;
}
private static JSONObject getBaseCardPaymentMethod() throws JSONException {
JSONObject cardPaymentMethod = new JSONObject();
cardPaymentMethod.put("type", "CARD");
JSONObject parameters = new JSONObject();
parameters.put("allowedAuthMethods", getAllowedCardAuthMethods());
parameters.put("allowedCardNetworks", getAllowedCardNetworks());
// Optionally, you can add billing address/phone number associated with a CARD payment method.
parameters.put("billingAddressRequired", true);
JSONObject billingAddressParameters = new JSONObject();
billingAddressParameters.put("format", "FULL");
parameters.put("billingAddressParameters", billingAddressParameters);
cardPaymentMethod.put("parameters", parameters);
return cardPaymentMethod;
}
private static JSONArray getAllowedCardAuthMethods() {
return new JSONArray()
.put("PAN_ONLY")
.put("CRYPTOGRAM_3DS");
}
private static JSONArray getAllowedCardNetworks() {
return new JSONArray()
.put("AMEX")
.put("DISCOVER")
.put("INTERAC")
.put("JCB")
.put("MASTERCARD")
.put("VISA");
}
private static JSONObject getBaseRequest() throws JSONException {
return new JSONObject().put("apiVersion", 2).put("apiVersionMinor", 0);
}
private void possiblyShowGooglePayButton() {
final Optional<JSONObject> isReadyToPayJson = PaymentsUtil.getIsReadyToPayRequest();
if (!isReadyToPayJson.isPresent()) {
return;
}
// The call to isReadyToPay is asynchronous and returns a Task. We need to provide an
// OnCompleteListener to be triggered when the result of the call is known.
IsReadyToPayRequest request = IsReadyToPayRequest.fromJson(isReadyToPayJson.get().toString());
Task<Boolean> task = paymentsClient.isReadyToPay(request);
task.addOnCompleteListener(this,
new OnCompleteListener<Boolean>() {
@Override
public void onComplete(@NonNull Task<Boolean> task) {
if (task.isSuccessful()) {
setGooglePayAvailable(task.getResult());
} else {
Log.w("isReadyToPay failed", task.getException());
}
}
});
}
【问题讨论】:
标签: android google-play-services google-pay