Hao*_*Zha 5 android google-play-services google-pay
我按照Google Pay for Payments > Android教程中的确定使用 Google Pay API 付款的准备情况步骤来确定 Google Pay 钱包的准备情况。
在我将IsReadyToPayRequest对象existingPaymentMethodRequired中的属性设置为 之前,此方法工作正常。我的目的是确定钱包是否至少有一张卡,但我总是从请求中得到。我的环境是测试环境,Android手机注册了信用卡。有谁知道为什么?truefalse
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());
}
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
根据IsReadyToPayRequestexistingPaymentMethodRequired对象文档中属性的描述,在TEST 环境中它应该始终返回:true
注意:在TEST 环境中,如果您在IsReadyToPay() 请求中将existingPaymentMethodRequired 设置为true,则响应始终返回true。
你能确认它总是会false为你返回吗?即:文档是否不正确?
| 归档时间: |
|
| 查看次数: |
1690 次 |
| 最近记录: |