我正在学习教程 here集成谷歌支付 API 以获得我的应用程序的资金。到目前为止,我已经成功地进行了收费,但我发现很难在成功转帐时获得收费金额。
我使用的谷歌钱包依赖是
implementation 'com.google.android.gms:play-services-wallet:15.0.1'
当我从另一个 Fragment
发出请求时,我想在 MainActivity
的 onActivityResult
中获取收费金额。但是 Intent 数据
只有电子邮件地址和账单状态。我能想到的唯一合乎逻辑的方法是以某种方式将收费金额 manullay 注入(inject) Intent data
但我似乎找不到解决方法。
我试图在这里设置一个参数,但它没有出现在我在 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);
}
最佳答案
请注意,检索到支付 token 并不意味着支付交易成功。只有当您通过支付处理器或网关提交订单进行处理时,才会发生这种情况。发生这种情况时,这通常包括订单 ID 和最终收费金额。
我建议将此作为一种更具体的方式来说明成功的交易。
关于android - Google Pay API 获取扣款金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51104035/