我按照教程here集成谷歌付费API以获取我的应用程序的资金.到目前为止,我已成功设法收费,但我发现很难获得成功转账的收费金额.
我使用的谷歌钱包依赖是
implementation 'com.google.android.gms:play-services-wallet:15.0.1'
当我从另一个片段发出请求时,我想获得MainActivity’sonActivityResult中的收费金额.但是Intent数据只有电子邮件地址和账单状态.我能想到的唯一合乎逻辑的方法是以某种方式将收费数量manullay注入Intent数据,但我似乎无法找到解决方法.
我试图在这里设置一个参数,但它没有出现在我在onActivityResult中收到的Intent中.
任何人都可以帮我一把吗?
public void requestPayment(BigDecimal amount) {
// disables the button to prevent multiple clicks.
//mGooglePayButton.setClickable(false);
// The price provided to the API should include taxes and shipping.
// This price is not displayed to the user.
String chargeAmount = amount.divide(MICROS).setScale(2, RoundingMode.HALF_EVEN).toString();
// String price = PaymentsUtil.microsToString(chargeAmount);
TransactionInfo transaction = PaymentsUtil.createTransaction(chargeAmount);
Parcel parcel = Parcel.obtain();
parcel.writeString(chargeAmount);
parcel.recycle();
transaction.writetoParcel(parcel, 0);
PaymentDataRequest request = PaymentsUtil.createPaymentDataRequest(transaction);
Task<PaymentData> futurePaymentData = mPaymentsClient.loadPaymentData(request);
// Since loadPaymentData may show the UI asking the user to select a payment method, we use
// AutoResolveHelper to wait for the user interacting with it. Once completed,
// onActivityResult will be called with the result.
AutoResolveHelper.resolveTask(futurePaymentData, Objects.requireNonNull(getActivity()), LOAD_PAYMENT_DATA_REQUEST_CODE);
}
解决方法:
正如您在评论中提到的,电子邮件和付款状态由Google电子钱包设置,因此我不确定您是否可以修改该Intent.
解:
一种选择是使用sharedPreference来实现此目的.在requestPayment()方法中,在SharedPreference和MainActivity中设置chargeAmount#onActivityResult()如果付款成功,则从SharedPreference获取chargeAmount.你完成了.
public void requestPayment(BigDecimal amount) {
...........
String chargeAmount = amount.divide(MICROS).setScale(2, RoundingMode.HALF_EVEN).toString();
// set chargeAmount to SharedPreference here
.............
}
MainActivity#onActivityResult()
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case LOAD_PAYMENT_DATA_REQUEST_CODE:
switch (resultCode) {
case Activity.RESULT_OK:
// get chargeAmount from SharedPreference here
PaymentData paymentData = PaymentData.getFromIntent(data);
String token = paymentData.getPaymentMethodToken().getToken();
break;
case Activity.RESULT_CANCELED:
break;
case AutoResolveHelper.RESULT_ERROR:
Status status = AutoResolveHelper.getStatusFromIntent(data);
// Log the status for debugging.
// Generally, there is no need to show an error to
// the user as the Google Pay API will do that.
break;
default:
// Do nothing.
}
break;
default:
// Do nothing.
}
}
我希望这能帮到您.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。