android–Google Pay API获取收费金额

android–Google Pay API获取收费金额,第1张

概述我按照教程here集成谷歌付费API以获取我的应用程序的资金.到目前为止,我已成功设法收费,但我发现很难获得成功转账的收费金额.我使用的谷歌钱包依赖是implementation'com.google.android.gms:play-services-wallet:15.0.1'当我从另一个片段发出请求时,我想获得MainActivity’

我按照教程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()

@OverrIDepublic 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.    }}

我希望这能帮到您.

总结

以上是内存溢出为你收集整理的android – Google Pay API获取收费金额全部内容,希望文章能够帮你解决android – Google Pay API获取收费金额所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116728.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存