Search in sources :

Example 1 with BCPayReqParams

use of cn.beecloud.entity.BCPayReqParams in project ttdj by soonphe.

the class BCPay method reqPaymentAsync.

/**
 * 支付调用总接口
 *
 * @param channelType  支付类型  对于支付手机APP端目前只支持WX_APP, ALI_APP, UN_APP, BD_APP
 * @param billTitle    商品描述, 32个字节内, 汉字以2个字节计
 * @param billTotalFee 支付金额,以分为单位,必须是正整数
 * @param billNum      商户自定义订单号
 * @param billTimeout  订单超时时间,以秒为单位,可以为null
 * @param optional     为扩展参数,可以传入任意数量的key/value对来补充对业务逻辑的需求
 * @param analysis     为扩展参数,用于后期的分析,当前仅支持以category为key的分类分析
 * @param callback     支付完成后的回调函数
 * @see cn.beecloud.entity.BCReqParams.BCChannelTypes
 */
private void reqPaymentAsync(final BCReqParams.BCChannelTypes channelType, final String billTitle, final Integer billTotalFee, final String billNum, final Integer billTimeout, final String notifyUrl, final String returnUrl, final String cardNum, final String qrCodeMode, final String buyerId, final String couponId, final Map<String, String> optional, final Map<String, String> analysis, final BCCallback callback) {
    if (callback == null) {
        Log.w(TAG, "请初始化callback");
        return;
    }
    payCallback = callback;
    BCCache.executorService.execute(new Runnable() {

        @Override
        public void run() {
            // 校验并准备公用参数
            BCPayReqParams parameters;
            try {
                parameters = new BCPayReqParams(channelType);
            } catch (BCException e) {
                callback.done(new BCPayResult(BCPayResult.RESULT_FAIL, BCPayResult.APP_INTERNAL_EXCEPTION_ERR_CODE, BCPayResult.FAIL_EXCEPTION, e.getMessage()));
                return;
            }
            String paramValidRes = BCValidationUtil.prepareParametersForPay(billTitle, billTotalFee, billNum, optional, parameters);
            if (paramValidRes != null) {
                callback.done(new BCPayResult(BCPayResult.RESULT_FAIL, BCPayResult.APP_INTERNAL_PARAMS_ERR_CODE, BCPayResult.FAIL_INVALID_PARAMS, paramValidRes));
                return;
            }
            parameters.billTimeout = billTimeout;
            parameters.analysis = analysis;
            parameters.notifyUrl = notifyUrl;
            parameters.returnUrl = returnUrl;
            parameters.cardNum = cardNum;
            parameters.qrPayMode = qrCodeMode;
            parameters.buyerId = buyerId;
            parameters.couponId = couponId;
            String payURL = BCHttpClientUtil.getBillPayURL();
            BCHttpClientUtil.Response response = BCHttpClientUtil.httpPost(payURL, parameters.transToBillReqMapParams());
            if (response.code == 200 || (response.code >= 400 && response.code < 500)) {
                String ret = response.content;
                // 反序列化json串
                Gson res = new Gson();
                Type type = new TypeToken<Map<String, Object>>() {
                }.getType();
                Map<String, Object> responseMap;
                try {
                    responseMap = res.fromJson(ret, type);
                } catch (JsonSyntaxException ex) {
                    callback.done(new BCPayResult(BCPayResult.RESULT_FAIL, BCPayResult.APP_INTERNAL_EXCEPTION_ERR_CODE, BCPayResult.FAIL_EXCEPTION, "JsonSyntaxException or Network Error:" + response.code + " # " + response.content));
                    return;
                }
                // 判断后台返回结果
                Integer resultCode = ((Double) responseMap.get("result_code")).intValue();
                if (resultCode == 0) {
                    if (mContextActivity != null) {
                        BCCache.getInstance().billID = (String) responseMap.get("id");
                        // 如果是测试模式
                        if (BCCache.getInstance().isTestMode) {
                            reqTestModePayment(billTitle, billTotalFee);
                            return;
                        }
                        // 针对不同的支付渠道调用不同的API
                        switch(channelType) {
                            case WX_APP:
                                reqWXPaymentViaAPP(responseMap);
                                break;
                            case ALI_APP:
                            case // ISV通道走官方的ALI jar,其他qrcode封装的在另一个工程
                            BC_ALI_APP:
                                reqAliPaymentViaAPP(responseMap);
                                break;
                            case UN_APP:
                            case BC_APP:
                                reqUnionPaymentViaAPP(responseMap);
                                break;
                            case BD_APP:
                                reqBaiduPaymentViaAPP(responseMap);
                                break;
                            case BC_WX_WAP:
                                reqWXWapPaymentViaWebView(responseMap);
                                break;
                            default:
                                callback.done(new BCPayResult(BCPayResult.RESULT_UNKNOWN, resultCode, String.valueOf(responseMap.get("result_msg")), String.valueOf(responseMap.get("err_detail")), String.valueOf(responseMap.get("id")), responseMap.get("url") == null ? null : String.valueOf(responseMap.get("url")), responseMap.get("html") == null ? null : String.valueOf(responseMap.get("html"))));
                        }
                    } else {
                        callback.done(new BCPayResult(BCPayResult.RESULT_FAIL, BCPayResult.APP_INTERNAL_EXCEPTION_ERR_CODE, BCPayResult.FAIL_EXCEPTION, "Context-Activity NP-Exception"));
                    }
                } else {
                    // 返回后端传回的错误信息
                    String serverMsg = String.valueOf(responseMap.get("result_msg"));
                    String serverDetail = String.valueOf(responseMap.get("err_detail"));
                    callback.done(new BCPayResult(BCPayResult.RESULT_FAIL, resultCode, serverMsg, serverDetail));
                }
            } else {
                callback.done(new BCPayResult(BCPayResult.RESULT_FAIL, BCPayResult.APP_INTERNAL_NETWORK_ERR_CODE, BCPayResult.FAIL_NETWORK_ISSUE, "Network Error:" + response.code + " # " + response.content));
            }
        }
    });
}
Also used : Gson(com.google.gson.Gson) BCPayResult(cn.beecloud.entity.BCPayResult) BCPayReqParams(cn.beecloud.entity.BCPayReqParams) Type(java.lang.reflect.Type) JsonSyntaxException(com.google.gson.JsonSyntaxException) TypeToken(com.google.gson.reflect.TypeToken) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BCPayReqParams (cn.beecloud.entity.BCPayReqParams)1 BCPayResult (cn.beecloud.entity.BCPayResult)1 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 TypeToken (com.google.gson.reflect.TypeToken)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1