Search in sources :

Example 1 with WxPayException

use of com.github.binarywang.wxpay.exception.WxPayException in project leopard by tanhaichao.

the class WeixinPayClientImpl method micropay.

// String outTradeNo, String scene, String authCode, String subject, double totalAmount, String notifyUrl
@Override
public WxPayMicropayResult micropay(String outTradeNo, String scene, String authCode, String subject, double amount, String spbillCreateIp) throws WeixinPayException {
    // String paymentId = StringUtil.uuid();
    // weixinPayDao.add(paymentId, outTradeNo);
    // TODO
    int totalFee = (int) DecimalUtil.multiply(amount, 100);
    // 接口文档地址: https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
    // .appid(appId).mchId(mchId);
    WxPayMicropayRequest.Builder builder = WxPayMicropayRequest.newBuilder();
    // builder .nonceStr(nonceStr);
    // builder .sign(sign)
    builder.outTradeNo(outTradeNo);
    builder.totalFee(totalFee);
    builder.body(subject);
    builder.spbillCreateIp(spbillCreateIp);
    builder.authCode(authCode);
    WxPayMicropayRequest request = builder.build();
    try {
        return wxPayService.micropay(request);
    } catch (WxPayException e) {
        throw new WeixinPayException("访问刷卡支付接口出现异常.", e);
    }
}
Also used : WxPayException(com.github.binarywang.wxpay.exception.WxPayException) WxPayMicropayRequest(com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest)

Example 2 with WxPayException

use of com.github.binarywang.wxpay.exception.WxPayException in project fw-cloud-framework by liuweijw.

the class PayNotifyController method doNotifyResult.

public String doNotifyResult(HttpServletRequest request, HttpServletResponse response) {
    String logPrefix = "【微信支付回调通知】";
    log.info("====== 开始接收微信支付回调通知 ======");
    try {
        String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        log.info("{}通知请求数据:reqStr={}", logPrefix, xmlResult);
        if (StringHelper.isBlank(xmlResult)) {
            return WxPayNotifyResponse.fail("FAIL");
        }
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlResult);
        // 验证业务数据是否正确
        if (!PayConstant.RETURN_VALUE_SUCCESS.equalsIgnoreCase(result.getResultCode()) || !PayConstant.RETURN_VALUE_SUCCESS.equalsIgnoreCase(result.getResultCode())) {
            log.error("returnCode={},resultCode={},errCode={},errCodeDes={}", result.getReturnCode(), result.getResultCode(), result.getErrCode(), result.getErrCodeDes());
            return WxPayNotifyResponse.fail("notify data failed");
        }
        // 总金额
        Integer total_fee = result.getTotalFee();
        // 商户系统订单号
        String out_trade_no = result.getOutTradeNo();
        // 查询payOrder记录
        String payOrderId = out_trade_no;
        PayOrder payOrder = payOrderService.findPayOrderByOrderId(payOrderId);
        if (null == payOrder) {
            log.error("Can't found payOrder form db. payOrderId={}, ", payOrderId);
            return WxPayNotifyResponse.fail("未查询到相应订单信息");
        }
        // 查询payChannel记录
        String mchId = payOrder.getMch_id();
        String channelId = payOrder.getChannelId();
        PayChannel payChannel = payChannelService.findPayChannel(channelId, mchId);
        if (null == payChannel) {
            log.error("Can't found payChannel form db. mchId={} channelId={}, ", payOrderId, mchId, channelId);
            return WxPayNotifyResponse.fail("未查询到订单相关渠道信息");
        }
        WxPayConfig wxPayConfig = WxPayUtil.getWxPayConfig(payChannel.getParam(), result.getTradeType(), wxPayProperties.getCertRootPath(), wxPayProperties.getNotifyUrl());
        wxPayService.setConfig(wxPayConfig);
        // 签名校验
        result.checkResult(wxPayService, null, false);
        // 核对金额
        long wxPayAmt = new BigDecimal(total_fee).longValue();
        long dbPayAmt = payOrder.getAmount().longValue();
        if (dbPayAmt != wxPayAmt) {
            log.error("db payOrder record payPrice not equals total_fee. total_fee={},payOrderId={}", total_fee, payOrderId);
            return WxPayNotifyResponse.fail("支付金额不正确");
        }
        // 处理订单
        // 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
        int payStatus = payOrder.getStatus();
        if (payStatus == PayConstant.PAY_STATUS_COMPLETE) {
            // 处理完成
            log.info("====== 订单已经完成直接返回 ======");
            return WxPayNotifyResponse.success("OK");
        }
        if (payStatus != PayConstant.PAY_STATUS_SUCCESS) {
            boolean updatePayOrderRows = payOrderService.updatePayOrderStatus4Success(payOrder.getPayOrderId());
            if (!updatePayOrderRows) {
                log.error("{}更新支付状态失败,将payOrderId={},更新payStatus={}失败", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_FAILED);
                return WxPayNotifyResponse.fail("处理订单失败");
            }
            log.error("{}更新支付状态成功,将payOrderId={},更新payStatus={}成功", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
            payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
        }
        // 业务系统后端通知
        this.notifyService.notifyPayOrder(payOrder);
        log.info("====== 完成接收微信支付回调通知 ======");
        return WxPayNotifyResponse.success("OK");
    } catch (WxPayException e) {
        // 出现业务错误
        log.error("微信回调结果异常,异常原因" + e);
        log.info("{}请求数据result_code=FAIL", logPrefix);
        log.info("err_code:", e.getErrCode());
        log.info("err_code_des:", e.getErrCodeDes());
        return WxPayNotifyResponse.fail(e.getMessage());
    } catch (Exception e) {
        log.error("微信回调结果异常,异常原因" + e);
        return WxPayNotifyResponse.fail(e.getMessage());
    }
}
Also used : WxPayOrderNotifyResult(com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult) WxPayServiceImpl(com.github.binarywang.wxpay.service.impl.WxPayServiceImpl) PayOrder(com.github.liuweijw.business.pay.domain.PayOrder) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) PayChannel(com.github.liuweijw.business.pay.domain.PayChannel) BigDecimal(java.math.BigDecimal) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) WxPayConfig(com.github.binarywang.wxpay.config.WxPayConfig)

Example 3 with WxPayException

use of com.github.binarywang.wxpay.exception.WxPayException in project fw-cloud-framework by liuweijw.

the class WxSendRedpackServiceImpl method sendRedpack.

@Override
public R<Map<String, Object>> sendRedpack(PaySendRedpack paySendRedpack, WxPaySendRedpackRequest sendRedpackRequest) {
    Map<String, Object> returnMap = new HashMap<String, Object>();
    // 设置会出现签名加密返回
    returnMap.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_SUCCESS);
    returnMap.put("return_code", PayConstant.RETURN_VALUE_FAIL);
    returnMap.put("return_msg", "请求出现异常!");
    boolean isCommonRedPack = paySendRedpack.getRedPackType().intValue() == 0;
    String resKey = paySendRedpack.getResKey();
    String logPrefix = isCommonRedPack ? "【发放普通红包】" : "【发放裂变红包】";
    try {
        log.info(logPrefix + "请求:" + sendRedpackRequest.toString());
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(paySendRedpack.getWxPayConfig());
        WxPaySendRedpackResult paySendRedpackResult = wxPayService.sendRedpack(sendRedpackRequest);
        paySendRedpack.setReturnCode(paySendRedpackResult.getReturnCode());
        paySendRedpack.setReturnMsg(paySendRedpackResult.getReturnMsg());
        paySendRedpack.setResultCode(paySendRedpackResult.getResultCode());
        paySendRedpack.setErrCode(paySendRedpackResult.getErrCode());
        paySendRedpack.setErrCodeDes(paySendRedpackResult.getErrCodeDes());
        // 订单流水号
        returnMap.put("send_order_id", paySendRedpack.getSendOrderId());
        // 订单编号
        returnMap.put("mch_order_no", paySendRedpack.getMchOrderNo());
        returnMap.put("return_code", paySendRedpackResult.getReturnCode());
        returnMap.put("return_msg", paySendRedpackResult.getReturnMsg());
        returnMap.put("result_code", paySendRedpackResult.getResultCode());
        returnMap.put("err_code", paySendRedpackResult.getErrCode());
        returnMap.put("err_code_des", paySendRedpackResult.getErrCodeDes());
        if (PayConstant.RETURN_VALUE_SUCCESS.equals(paySendRedpackResult.getReturnCode()) && PayConstant.RETURN_VALUE_SUCCESS.equals(paySendRedpackResult.getResultCode())) {
            paySendRedpack.setWxTotalAmount(paySendRedpackResult.getTotalAmount());
            paySendRedpack.setSendListid(paySendRedpackResult.getSendListid());
            paySendRedpack.setSendTime(paySendRedpackResult.getSendTime());
            returnMap.put("total_amount", paySendRedpackResult.getTotalAmount());
            returnMap.put("send_listid", paySendRedpackResult.getSendListid());
            returnMap.put("send_time", paySendRedpackResult.getSendTime());
            log.info(logPrefix + "响应:" + JSON.toJSONString(returnMap));
            log.info(logPrefix + "结果:" + PayConstant.RETURN_VALUE_SUCCESS);
        } else {
            log.info(logPrefix + "响应:" + JSON.toJSONString(returnMap));
            log.info(logPrefix + "结果:" + PayConstant.RETURN_VALUE_FAIL);
        }
        // 保存 发送明细数据入库
        paySendRedpackRepository.saveAndFlush(paySendRedpack);
        return new R<Map<String, Object>>().data(PayUtil.makeRetData(returnMap, resKey)).success();
    } catch (WxPayException e) {
        e.printStackTrace();
        return new R<Map<String, Object>>().data(PayUtil.makeRetData(returnMap, resKey)).failure(logPrefix + "请求异常:" + e.toString());
    }
}
Also used : WxPayService(com.github.binarywang.wxpay.service.WxPayService) WxPaySendRedpackResult(com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult) R(com.github.liuweijw.commons.base.R) HashMap(java.util.HashMap) WxPayServiceImpl(com.github.binarywang.wxpay.service.impl.WxPayServiceImpl) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with WxPayException

use of com.github.binarywang.wxpay.exception.WxPayException in project fw-cloud-framework by liuweijw.

the class WxUnifiedOrderServiceImpl method doWxUnifiedOrderRequest.

@Override
public R<Map<String, Object>> doWxUnifiedOrderRequest(String tradeType, PayOrder payOrder, Map<String, String> params) {
    try {
        String logPrefix = "【微信支付统一下单】";
        if (null == payOrder || null == params || StringHelper.isBlank(tradeType) || StringHelper.isBlank(params.get("resKey")) || StringHelper.isBlank(params.get("channelParam")))
            return new R<Map<String, Object>>().data(PayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "", PayConstant.RETURN_VALUE_FAIL, PayEnum.ERR_0001)).failure();
        String resKey = params.get("resKey");
        WxPayConfig wxPayConfig = WxPayUtil.getWxPayConfig(params.get("channelParam"), tradeType, wxPayProperties.getCertRootPath(), wxPayProperties.getNotifyUrl());
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = buildUnifiedOrderRequest(payOrder, wxPayConfig);
        String payOrderId = payOrder.getPayOrderId();
        WxPayUnifiedOrderResult wxPayUnifiedOrderResult = null;
        try {
            wxPayUnifiedOrderResult = wxPayService.unifiedOrder(wxPayUnifiedOrderRequest);
            log.info("{} >>> 下单成功", logPrefix);
            Map<String, Object> map = PayUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_SUCCESS, null);
            map.put("payOrderId", payOrderId);
            map.put("prepayId", wxPayUnifiedOrderResult.getPrepayId());
            boolean result = payOrderService.updatePayOrderStatus4Paying(payOrderId, wxPayUnifiedOrderResult.getPrepayId());
            log.info("更新第三方支付订单号:payOrderId={},prepayId={},result={}", payOrderId, wxPayUnifiedOrderResult.getPrepayId(), result);
            switch(tradeType) {
                case PayConstant.WxConstant.TRADE_TYPE_NATIVE:
                    {
                        // 二维码支付链接
                        map.put("codeUrl", wxPayUnifiedOrderResult.getCodeURL());
                        break;
                    }
                case PayConstant.WxConstant.TRADE_TYPE_APP:
                    {
                        Map<String, String> payInfo = new HashMap<>();
                        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
                        String nonceStr = String.valueOf(System.currentTimeMillis());
                        // APP支付绑定的是微信开放平台上的账号,APPID为开放平台上绑定APP后发放的参数
                        String appId = wxPayConfig.getAppId();
                        Map<String, String> configMap = new HashMap<>();
                        // 此map用于参与调起sdk支付的二次签名,格式全小写,timestamp只能是10位,格式固定,切勿修改
                        String partnerId = wxPayConfig.getMchId();
                        configMap.put("prepayid", wxPayUnifiedOrderResult.getPrepayId());
                        configMap.put("partnerid", partnerId);
                        String packageValue = "Sign=WXPay";
                        configMap.put("package", packageValue);
                        configMap.put("timestamp", timestamp);
                        configMap.put("noncestr", nonceStr);
                        configMap.put("appid", appId);
                        // 此map用于客户端与微信服务器交互
                        payInfo.put("sign", SignUtils.createSign(configMap, null, wxPayConfig.getMchKey(), new String[0]));
                        payInfo.put("prepayId", wxPayUnifiedOrderResult.getPrepayId());
                        payInfo.put("partnerId", partnerId);
                        payInfo.put("appId", appId);
                        payInfo.put("packageValue", packageValue);
                        payInfo.put("timeStamp", timestamp);
                        payInfo.put("nonceStr", nonceStr);
                        map.put("payParams", payInfo);
                        break;
                    }
                case PayConstant.WxConstant.TRADE_TYPE_JSPAI:
                    {
                        Map<String, String> payInfo = new HashMap<>();
                        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
                        String nonceStr = String.valueOf(System.currentTimeMillis());
                        payInfo.put("appId", wxPayUnifiedOrderResult.getAppid());
                        // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                        payInfo.put("timeStamp", timestamp);
                        payInfo.put("nonceStr", nonceStr);
                        payInfo.put("package", "prepay_id=" + wxPayUnifiedOrderResult.getPrepayId());
                        payInfo.put("signType", WxPayConstants.SignType.MD5);
                        payInfo.put("paySign", SignUtils.createSign(payInfo, null, wxPayConfig.getMchKey(), new String[0]));
                        map.put("payParams", payInfo);
                        break;
                    }
                case PayConstant.WxConstant.TRADE_TYPE_MWEB:
                    {
                        // h5支付链接地址
                        map.put("payUrl", wxPayUnifiedOrderResult.getMwebUrl());
                        break;
                    }
            }
            return new R<Map<String, Object>>().data(PayUtil.makeRetData(map, resKey)).success();
        } catch (WxPayException e) {
            log.error(e + "下单失败");
            // 出现业务错误
            log.info("{}下单返回失败", logPrefix);
            log.info("err_code:{}", e.getErrCode());
            log.info("err_code_des:{}", e.getErrCodeDes());
            return new R<Map<String, Object>>().data(PayUtil.makeRetData(PayUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_FAIL, "0111", "调用微信支付失败," + e.getErrCode() + ":" + e.getErrCodeDes()), resKey)).failure();
        }
    } catch (Exception e) {
        log.error("微信支付统一下单异常" + e);
        return new R<Map<String, Object>>().data(PayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "", PayConstant.RETURN_VALUE_FAIL, PayEnum.ERR_0001)).failure();
    }
}
Also used : WxPayUnifiedOrderResult(com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult) WxPayServiceImpl(com.github.binarywang.wxpay.service.impl.WxPayServiceImpl) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) WxPayUnifiedOrderRequest(com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest) WxPayException(com.github.binarywang.wxpay.exception.WxPayException) WxPayService(com.github.binarywang.wxpay.service.WxPayService) R(com.github.liuweijw.commons.base.R) WxPayConfig(com.github.binarywang.wxpay.config.WxPayConfig) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

WxPayException (com.github.binarywang.wxpay.exception.WxPayException)4 WxPayServiceImpl (com.github.binarywang.wxpay.service.impl.WxPayServiceImpl)3 WxPayConfig (com.github.binarywang.wxpay.config.WxPayConfig)2 WxPayService (com.github.binarywang.wxpay.service.WxPayService)2 R (com.github.liuweijw.commons.base.R)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JSONObject (com.alibaba.fastjson.JSONObject)1 WxPayOrderNotifyResult (com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult)1 WxPayMicropayRequest (com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest)1 WxPayUnifiedOrderRequest (com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest)1 WxPaySendRedpackResult (com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult)1 WxPayUnifiedOrderResult (com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult)1 PayChannel (com.github.liuweijw.business.pay.domain.PayChannel)1 PayOrder (com.github.liuweijw.business.pay.domain.PayOrder)1 BigDecimal (java.math.BigDecimal)1