繁体   English   中英

科尔多瓦Android Pay电子钱包请求未返回任何内容

[英]Cordova Android Pay Wallet Request Returns Nothing

我正在尝试编写一个插件,以检查用户手机上是否设置了Android Pay。 我已经按照Android教程的示例代码进行了操作,但是无法使其与Cordova一起使用。 当从下面的示例中调用canMakePayments函数时,什么也不会发生。 从Wallet.Payments.isReadyToPay的调用中看不到失败或成功。 任何建议将不胜感激。

package com.myapp.test;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface; 
import org.apache.cordova.CordovaWebView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;

import android.util.Log;
import android.support.annotation.NonNull;
import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.app.ProgressDialog;  

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.BooleanResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wallet.MaskedWallet;
import com.google.android.gms.wallet.MaskedWalletRequest;
import com.google.android.gms.wallet.Wallet;
import com.google.android.gms.wallet.WalletConstants;
import com.google.android.gms.wallet.fragment.SupportWalletFragment;
import com.google.android.gms.wallet.fragment.WalletFragmentInitParams;
import com.google.android.gms.wallet.fragment.WalletFragmentMode;
import com.google.android.gms.wallet.fragment.WalletFragmentOptions;
import com.google.android.gms.wallet.fragment.WalletFragmentStyle;

public class AndroidPay extends CordovaPlugin implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
    // actions
    private static final String CAN_MAKE_PAYMENTS = "canMakePayments";

    // Plugin tag name
    private static final String TAG = "AndroidPay";

    // Cordova callbacks
    CallbackContext canMakePaymentCallback = null;

    // Android Pay
    private SupportWalletFragment mWalletFragment;
    private GoogleApiClient mGoogleApiClient;
    public static final int WALLET_ENVIRONMENT = WalletConstants.ENVIRONMENT_TEST;


    /*
     *  Public function calls
     */


    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView); 
        Log.d(TAG, "Plugin initialized");
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        boolean validAction = true;

        if (action.equals(CAN_MAKE_PAYMENTS)) {
            canMakePayments(callbackContext);
        }
        else {
            validAction = false;
        }       
        return validAction;
    }

    /*
     *  Private function calls
     */

    private void canMakePayments(CallbackContext callbackContext) {
        Log.d(TAG, "canMakePayments");
        canMakePaymentCallback = callbackContext;       

        try {
            // Setup Google client
            mGoogleApiClient = new GoogleApiClient.Builder(this.cordova.getActivity())
                                .addApi(Wallet.API, new Wallet.WalletOptions.Builder()
                                    .setEnvironment(WALLET_ENVIRONMENT)
                                    .build())
                                .addConnectionCallbacks(this)
                                .addOnConnectionFailedListener(this)                            
                            .build();           

            // Check if user is ready to use Android Pay
            Wallet.Payments.isReadyToPay(mGoogleApiClient).setResultCallback(
                    new ResultCallback<BooleanResult>() {                   
                        @Override
                        public void onResult(@NonNull BooleanResult booleanResult) {    
                            if (booleanResult.getStatus().isSuccess()) {
                                if (booleanResult.getValue()) {
                                    // Show Android Pay buttons and user can make payments
                                    Log.d(TAG, "isReadyToPay:true");

                                    // TODO: Add logic
                                    canMakePaymentCallback.success();
                                } else {
                                    // Hide Android Pay buttons, user can't make payments
                                    Log.d(TAG, "isReadyToPay:false:" + booleanResult.getStatus());

                                    // TODO: Add logic
                                    canMakePaymentCallback.failure();
                                }
                            } else {
                                // Error making isReadyToPay call
                                Log.e(TAG, "isReadyToPay:" + booleanResult.getStatus());
                                canMakePaymentCallback.failure();
                            }
                        }
                    });         
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Exception: " + e.getMessage());
        }       

    }

    /**
     * Runs when a GoogleApiClient object successfully connects.
     */

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d(TAG, "***** onConnectionFailed:" + connectionResult.getErrorMessage());
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.d(TAG, "***** Connection suspended *****");
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Log.d(TAG, "***** Connected to GoogleApiClient *****");
    }
}

它不在开发人员文档中,但对我而言,解决此问题的方法是在调用Wallet.Payments.isReadyToPay之前先对Google客户端进行连接调用

mGoogleApiClient = new GoogleApiClient.Builder(this.cordova.getActivity())
                                .addApi(Wallet.API, new Wallet.WalletOptions.Builder()
                                    .setEnvironment(WALLET_ENVIRONMENT)
                                    .build())
                                .addConnectionCallbacks(this)
                                .addOnConnectionFailedListener(this)                            
                                .build();               

// Connect to google api client
mGoogleApiClient.connect();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM