com.polyvi.xface.extension.uppay.XUPPayExt.java Source code

Java tutorial

Introduction

Here is the source code for com.polyvi.xface.extension.uppay.XUPPayExt.java

Source

/*
 Copyright 2012-2013, Polyvi Inc. (http://polyvi.github.io/openxface)
 This program is distributed under the terms of the GNU General Public License.
    
 This file is part of xFace.
    
 xFace is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 xFace is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with xFace.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.polyvi.xface.extension.uppay;

import java.util.Locale;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.polyvi.xface.util.XLog;
import com.unionpay.mobile.plugin.zl.UPLauncher;
import com.unionpay.mobile.uppay.v2.UPLauncherV2;

import android.content.Intent;

/**
 * XUPPayExt???????<br/>
 * dependent-libs: UPPayPluginEx.jar,UPPluginWidget.jar,armeabi/libCDZL.so,armeabi/libCdzlWidget.so
 */
public class XUPPayExt extends CordovaPlugin {
    /**
     * 1.?AndroidManifest.xml?<activity>
     * <activity android:name="com.unionpay.mobile.uppay.v2.MobileActivityEx"
     *      android:configChanges="orientation|keyboardHidden"
     *      android:screenOrientation="portrait"
     *      android:theme="@style/Transparent"
     *      android:windowSoftInputMode="adjustResize" />
     * <activity android:name="com.unionpay.mobile.plugin.zl.MobileActivity"
     *      android:configChanges="orientation|keyboardHidden"
     *      android:screenOrientation="portrait"
     *      android:theme="@style/Transparent"
     *      android:windowSoftInputMode="adjustResize" />
     * 2.?AndroidManifest.xml???
     * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     * <uses-permission android:name="android.permission.INTERNET" />
     * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     * <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
     * <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     * 3.res/valuesstyle.xml
     */
    private static final String CLASS_NAME = XUPPayExt.class.getSimpleName();
    private static final String KEY_PAY_RESULT = "pay_result";
    private static final String PAY_RESULT_SUCCESS = "success";

    private static final String COMMAND_START_PAY = "startPay";
    private static final String COMMAND_START_BALANCE_ENQUIRE = "startBalanceEnquire";

    /**
     * ????requestCodeUPPayPluginEx.jarUPPluginWidget.jar
     */
    private static final int UPPAY_PLUGIN_REQUEST_CODE = 100;

    private CallbackContext mCallbackCtx;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        // ?????
        mCallbackCtx = callbackContext;
        if (COMMAND_START_PAY.equals(action)) {
            String transSerialNumber = args.getString(0);
            // "00"??
            // "01"??
            // "99": pm?
            String mode = args.getString(1);
            JSONArray cards = null;
            if (!args.isNull(4)) {
                cards = args.getJSONArray(4);
            }
            // ?
            String reserved = null;
            cordova.setActivityResultCallback(this);
            UPLauncher.startUPPay(cordova.getActivity(), transSerialNumber, cards, mode, reserved);
        } else if (COMMAND_START_BALANCE_ENQUIRE.equals(action)) {
            String pan = args.getString(0);
            // "00":?
            // "01":?
            String mode = args.getString(1);
            // ?
            String reserved = null;
            cordova.setActivityResultCallback(this);
            UPLauncherV2.startBalanceEnquire(cordova.getActivity(), pan, mode, reserved);
        } else {
            return false;
        }
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        String payResult = "fail";
        if (intent != null) {
            if (UPPAY_PLUGIN_REQUEST_CODE == requestCode) {
                payResult = intent.getExtras().getString(KEY_PAY_RESULT).toLowerCase(Locale.ENGLISH);
                if (null != payResult) {
                    if (payResult.equals(PAY_RESULT_SUCCESS)) {
                        mCallbackCtx.success(PAY_RESULT_SUCCESS);
                    } else {
                        mCallbackCtx.error(payResult);
                    }
                } else {
                    // ?
                    String balanceAmount = intent.getExtras().getString("balance_amount");
                    // ??
                    String balanceAvailableAmount = intent.getExtras().getString("balance_available_amount");
                    JSONObject jsonObj = null;
                    if (null == balanceAmount && null == balanceAvailableAmount) {
                        XLog.e(CLASS_NAME, "Return params: balanceAmount or balanceAvailableAmount is null");
                        mCallbackCtx.error(payResult);
                    } else {
                        jsonObj = new JSONObject();
                        try {
                            jsonObj.put("balance", balanceAmount);
                            jsonObj.put("availableBalance", balanceAvailableAmount);
                            mCallbackCtx.success(jsonObj);
                        } catch (JSONException e) {
                            XLog.e(CLASS_NAME, "onActivityResult put element into jsonObj error!");
                            e.printStackTrace();
                            mCallbackCtx.error(payResult);
                        }
                    }
                }
            }
        }
    }
}