2017-08-01 63 views
0

我正在尝试编写一个插件,用于检查用户手机上是否安装了Android Pay。我使用他们的示例代码跟踪了Android教程,但无法使其与Cordova一起工作。从下面的示例调用函数canMakePayments时,什么也不会发生。我没有看到从拨打电子邮件到Wallet.Payments.isReadyToPay失败或成功。任何意见将不胜感激。Cordova Android Pay钱包请求无返回

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 *****"); 
    } 
} 

回答

0

这不是开发者文档中,但什么固定的这对我来说是要对谷歌客户机的连接呼叫呼叫Wallet.Payments.isReadyToPay

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