Search in sources :

Example 16 with BCPayResult

use of cn.beecloud.entity.BCPayResult 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)

Example 17 with BCPayResult

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

the class BCPay method reqAliPaymentViaAPP.

/**
 * 与服务器交互后下一步进入支付宝app支付
 *
 * @param responseMap 服务端返回参数
 */
private void reqAliPaymentViaAPP(final Map<String, Object> responseMap) {
    String orderString = (String) responseMap.get("order_string");
    PayTask aliPay = new PayTask(mContextActivity);
    String aliResult = aliPay.pay(orderString, false);
    // 解析ali返回结果
    Pattern pattern = Pattern.compile("resultStatus=\\{(\\d+?)\\}");
    Matcher matcher = pattern.matcher(aliResult);
    String resCode = "";
    if (matcher.find())
        resCode = matcher.group(1);
    String result;
    int errCode;
    String errMsg;
    // 9000-订单支付成功, 8000-正在处理中, 4000-订单支付失败, 6001-用户中途取消, 6002-网络连接出错
    String errDetail;
    if (resCode.equals("9000")) {
        result = BCPayResult.RESULT_SUCCESS;
        errCode = BCPayResult.APP_PAY_SUCC_CODE;
        errMsg = BCPayResult.RESULT_SUCCESS;
        errDetail = errMsg;
    } else if (resCode.equals("6001")) {
        result = BCPayResult.RESULT_CANCEL;
        errCode = BCPayResult.APP_PAY_CANCEL_CODE;
        errMsg = BCPayResult.RESULT_CANCEL;
        errDetail = errMsg;
    } else if (resCode.equals("8000")) {
        result = BCPayResult.RESULT_UNKNOWN;
        errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
        errMsg = BCPayResult.RESULT_PAYING_UNCONFIRMED;
        errDetail = "订单正在处理中,无法获取成功确认信息";
    } else {
        result = BCPayResult.RESULT_FAIL;
        errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
        errMsg = BCPayResult.FAIL_ERR_FROM_CHANNEL;
        if (resCode.equals("4000"))
            errDetail = "订单支付失败";
        else
            errDetail = "网络连接出错";
    }
    if (payCallback != null) {
        payCallback.done(new BCPayResult(result, errCode, errMsg, errDetail, BCCache.getInstance().billID));
    }
}
Also used : Pattern(java.util.regex.Pattern) PayTask(com.alipay.sdk.app.PayTask) Matcher(java.util.regex.Matcher) BCPayResult(cn.beecloud.entity.BCPayResult)

Example 18 with BCPayResult

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

the class BCPay method reqWXPaymentViaAPP.

/**
 * 与服务器交互后下一步进入微信app支付
 *
 * @param responseMap 服务端返回参数
 */
private void reqWXPaymentViaAPP(final Map<String, Object> responseMap) {
    // 获取到服务器的订单参数后,以下主要代码即可调起微信支付。
    PayReq request = new PayReq();
    request.appId = String.valueOf(responseMap.get("app_id"));
    request.partnerId = String.valueOf(responseMap.get("partner_id"));
    request.prepayId = String.valueOf(responseMap.get("prepay_id"));
    request.packageValue = String.valueOf(responseMap.get("package"));
    request.nonceStr = String.valueOf(responseMap.get("nonce_str"));
    request.timeStamp = String.valueOf(responseMap.get("timestamp"));
    request.sign = String.valueOf(responseMap.get("pay_sign"));
    if (wxAPI != null) {
        wxAPI.sendReq(request);
    } else {
        if (payCallback != null) {
            payCallback.done(new BCPayResult(BCPayResult.RESULT_FAIL, BCPayResult.APP_INTERNAL_EXCEPTION_ERR_CODE, BCPayResult.FAIL_EXCEPTION, "Error: 微信API为空, 请确认已经在需要调起微信支付的Activity中[成功]调用了BCPay.initWechatPay"));
        }
    }
}
Also used : PayReq(com.tencent.mm.opensdk.modelpay.PayReq) BCPayResult(cn.beecloud.entity.BCPayResult)

Example 19 with BCPayResult

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

the class BCPay method reqBaiduPaymentViaAPP.

/**
 * 与服务器交互后下一步进入百度app支付
 *
 * @param responseMap 服务端返回参数
 */
private void reqBaiduPaymentViaAPP(final Map<String, Object> responseMap) {
    String orderInfo = (String) responseMap.get("orderInfo");
    // Log.w(TAG, orderInfo);
    Map<String, String> map = new HashMap<String, String>();
    baiduPay = BaiduPay.getInstance();
    baiduPay.doPay(mContextActivity, orderInfo, new PayCallBack() {

        public void onPayResult(int stateCode, String payDesc) {
            // Log.w(TAG, "rsult=" + stateCode + "#desc=" + payDesc);
            String result;
            int errCode;
            String errMsg;
            String errDetail;
            switch(stateCode) {
                case // 需要到服务端验证支付结果
                PayCallBackManager.PayStateModle.PAY_STATUS_SUCCESS:
                    result = BCPayResult.RESULT_SUCCESS;
                    errCode = BCPayResult.APP_PAY_SUCC_CODE;
                    errMsg = BCPayResult.RESULT_SUCCESS;
                    errDetail = errMsg;
                    break;
                case // 需要到服务端验证支付结果
                PayCallBackManager.PayStateModle.PAY_STATUS_PAYING:
                    result = BCPayResult.RESULT_UNKNOWN;
                    errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
                    errMsg = BCPayResult.RESULT_PAYING_UNCONFIRMED;
                    errDetail = "订单正在处理中,无法获取成功确认信息";
                    break;
                case PayCallBackManager.PayStateModle.PAY_STATUS_CANCEL:
                    result = BCPayResult.RESULT_CANCEL;
                    errCode = BCPayResult.APP_PAY_CANCEL_CODE;
                    errDetail = errMsg = BCPayResult.RESULT_CANCEL;
                    break;
                case PayCallBackManager.PayStateModle.PAY_STATUS_NOSUPPORT:
                    result = BCPayResult.RESULT_FAIL;
                    errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
                    errMsg = BCPayResult.FAIL_ERR_FROM_CHANNEL;
                    errDetail = "不支持该种支付方式";
                    break;
                case PayCallBackManager.PayStateModle.PAY_STATUS_TOKEN_INVALID:
                    result = BCPayResult.RESULT_FAIL;
                    errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
                    errMsg = BCPayResult.FAIL_ERR_FROM_CHANNEL;
                    errDetail = "无效的登陆状态";
                    break;
                case PayCallBackManager.PayStateModle.PAY_STATUS_LOGIN_ERROR:
                    result = BCPayResult.RESULT_FAIL;
                    errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
                    errMsg = BCPayResult.FAIL_ERR_FROM_CHANNEL;
                    errDetail = "登陆失败";
                    break;
                case PayCallBackManager.PayStateModle.PAY_STATUS_ERROR:
                    result = BCPayResult.RESULT_FAIL;
                    errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
                    errMsg = BCPayResult.FAIL_ERR_FROM_CHANNEL;
                    errDetail = "支付失败";
                    break;
                case PayCallBackManager.PayStateModle.PAY_STATUS_LOGIN_OUT:
                    result = BCPayResult.RESULT_FAIL;
                    errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
                    errMsg = BCPayResult.FAIL_ERR_FROM_CHANNEL;
                    errDetail = "退出登录";
                    break;
                default:
                    result = BCPayResult.RESULT_FAIL;
                    errCode = BCPayResult.APP_INTERNAL_THIRD_CHANNEL_ERR_CODE;
                    errMsg = BCPayResult.FAIL_ERR_FROM_CHANNEL;
                    errDetail = "支付失败";
                    break;
            }
            if (payCallback != null) {
                payCallback.done(new BCPayResult(result, errCode, errMsg, errDetail + "#result=" + stateCode + "#desc=" + payDesc, BCCache.getInstance().billID));
            }
        }

        public boolean isHideLoadingDialog() {
            return true;
        }
    }, map);
}
Also used : PayCallBack(com.baidu.android.pay.PayCallBack) HashMap(java.util.HashMap) BCPayResult(cn.beecloud.entity.BCPayResult)

Example 20 with BCPayResult

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

the class BCWXWapPaymentActivity method onResume.

public void onResume() {
    super.onResume();
    synchronized (this) {
        if (firstIn) {
            firstIn = false;
            return;
        }
    }
    final ProgressDialog loadingDialog = new ProgressDialog(this);
    loadingDialog.setMessage("处理中,请稍候...");
    loadingDialog.setIndeterminate(true);
    loadingDialog.setCancelable(false);
    loadingDialog.show();
    BCCache.executorService.execute(new Runnable() {

        @Override
        public void run() {
            boolean found = false;
            // 总共查3次
            for (int i = 0; i < 3; i++) {
                BCQueryReqParams bcQueryReqParams;
                try {
                    bcQueryReqParams = new BCQueryReqParams(BCReqParams.BCChannelTypes.ALL);
                } catch (BCException e) {
                    Log.w(TAG, e.getMessage() + "");
                    return;
                }
                String queryURL = BCHttpClientUtil.getBillQueryURL() + "/" + BCCache.getInstance().billID + "?para=";
                BCHttpClientUtil.Response response = BCHttpClientUtil.httpGet(queryURL + bcQueryReqParams.transToEncodedJsonString());
                if (response.code == 200 || (response.code >= 400 && response.code < 500)) {
                    try {
                        BCQueryBillResult billResult = BCQueryBillResult.transJsonToResultObject(response.content);
                        // 如果查询到成功退出
                        if (billResult.getResultCode() == 0 && billResult.getBill().getPayResult() != null && billResult.getBill().getPayResult()) {
                            if (BCPay.payCallback != null) {
                                BCPay.payCallback.done(new BCPayResult(BCPayResult.RESULT_SUCCESS, BCPayResult.APP_PAY_SUCC_CODE, BCPayResult.RESULT_SUCCESS, "用户支付成功", BCCache.getInstance().billID));
                            }
                            found = true;
                            break;
                        }
                    } catch (JsonSyntaxException ex) {
                        Log.w(TAG, ex.getMessage() + "");
                    }
                }
                try {
                    // 0.5s后继续查
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    Log.w(TAG, e.getMessage() + "");
                }
            }
            if (!found) {
                // 如果一直没有查询到支付结果
                if (BCPay.payCallback != null) {
                    BCPay.payCallback.done(new BCPayResult(BCPayResult.RESULT_CANCEL, BCPayResult.APP_PAY_CANCEL_CODE, BCPayResult.RESULT_CANCEL, "用户未支付,或服务器通信延迟,如果用户确认已支付,请注意webhook推送", BCCache.getInstance().billID));
                }
            }
            loadingDialog.dismiss();
            finish();
        }
    });
}
Also used : BCQueryReqParams(cn.beecloud.entity.BCQueryReqParams) BCQueryBillResult(cn.beecloud.entity.BCQueryBillResult) JsonSyntaxException(com.google.gson.JsonSyntaxException) ProgressDialog(android.app.ProgressDialog) BCPayResult(cn.beecloud.entity.BCPayResult)

Aggregations

BCPayResult (cn.beecloud.entity.BCPayResult)20 BCCallback (cn.beecloud.async.BCCallback)10 BCResult (cn.beecloud.async.BCResult)10 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 HashMap (java.util.HashMap)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 BCReqParams (cn.beecloud.entity.BCReqParams)2 Gson (com.google.gson.Gson)2 Type (java.lang.reflect.Type)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ProgressDialog (android.app.ProgressDialog)1 View (android.view.View)1 TextView (android.widget.TextView)1 BCPayReqParams (cn.beecloud.entity.BCPayReqParams)1 BCQueryBillResult (cn.beecloud.entity.BCQueryBillResult)1 BCQueryReqParams (cn.beecloud.entity.BCQueryReqParams)1 PayTask (com.alipay.sdk.app.PayTask)1 PayCallBack (com.baidu.android.pay.PayCallBack)1