6

我正在按照此处的教程集成 google pay API 以获取我的应用程序的资金。到目前为止,我已经成功地进行了收费,但我发现在成功转账时很难获得收费金额。

我使用的谷歌钱包依赖是

implementation 'com.google.android.gms:play-services-wallet:15.0.1'

当我向另一个人提出请求时,我想在MainActivity's中获得收费金额。但只有电子邮件地址和帐单状态。我能想到的唯一合乎逻辑的方法是以某种方式将充电量手动注入,但我似乎找不到解决方法。onActivityResultFragmentIntent dataIntent data

我试图在这里设置一个参数,但它没有显示在Intent我收到的onActivityResult.

任何人都可以在这里帮我一把吗?

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);


    }
4

2 回答 2

1

请注意,支付令牌的检索并不意味着支付交易成功发生。只有当您通过支付处理器或网关提交订单以进行处理时,才会发生这种情况。发生这种情况时,这通常包括订单 ID 和最终收费金额。

我建议这是一种更具体的方式来说明成功的交易。

于 2019-01-06T14:52:31.517 回答
0

正如您在评论中提到的,电子邮件和付款状态是由 Google 电子钱包设置的,所以我不确定您是否可以修改该意图。

解决方案:

一种选择是sharedPreference用于此目的。在requestPayment()方法中设置,chargeAmount如果付款成功,则从. 你完成了。SharedPreferenceMainActivity#onActivityResult()chargeAmountSharedPreference

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

我希望这能帮到您。

于 2018-07-03T05:15:07.410 回答