Google Pay API收取费用

Faw*_*zan 5 android google-pay

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

我正在使用的Google钱包依赖项是

implementation 'com.google.android.gms:play-services-wallet:15.0.1'
Run Code Online (Sandbox Code Playgroud)

我要从另一个人提出请求时收取中MainActivity的。但是仅有电子邮件地址和帐单状态。我能想到的唯一合乎逻辑的方法是以某种方式向其注入收费金额,但我似乎找不到解决方法。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);


    }
Run Code Online (Sandbox Code Playgroud)

Vik*_*ngh 0

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

解决方案:

一种选择是用于sharedPreference此目的。在requestPayment()方法中设置chargeAmountSharedPreference如果MainActivity#onActivityResult()支付成功则从chargeAmount中获取SharedPreference。你完成了。

public void requestPayment(BigDecimal amount) {

    ...........

    String chargeAmount = amount.divide(MICROS).setScale(2, RoundingMode.HALF_EVEN).toString();
    // set chargeAmount to SharedPreference here

    .............
}
Run Code Online (Sandbox Code Playgroud)

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您。