Search in sources :

Example 1 with PayOrder

use of com.github.liuweijw.business.pay.domain.PayOrder 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 2 with PayOrder

use of com.github.liuweijw.business.pay.domain.PayOrder in project fw-cloud-framework by liuweijw.

the class PayNotifyController method notifyTest.

@RequestMapping("/notifyTest/{payOrderId}")
@ResponseBody
public String notifyTest(@PathVariable String payOrderId) {
    String logPrefix = "【微信支付回调通知】";
    log.info("====== 开始接收微信支付回调通知 ======");
    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("未查询到订单相关渠道信息");
    }
    // 处理订单
    // 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_SUCCESS);
            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");
}
Also used : PayOrder(com.github.liuweijw.business.pay.domain.PayOrder) PayChannel(com.github.liuweijw.business.pay.domain.PayChannel) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with PayOrder

use of com.github.liuweijw.business.pay.domain.PayOrder in project fw-cloud-framework by liuweijw.

the class PayAliNotifyController method notifyTest.

@RequestMapping("/notifyTest/{payOrderId}")
@ResponseBody
public String notifyTest(@PathVariable String payOrderId) {
    String logPrefix = "【支付宝支付回调通知】";
    log.info("====== 开始接收支付宝支付回调通知 ======");
    PayOrder payOrder = payOrderService.findPayOrderByOrderId(payOrderId);
    if (null == payOrder) {
        log.error("Can't found payOrder form db. payOrderId={}, ", payOrderId);
        return PayConstant.RETURN_ALIPAY_VALUE_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 PayConstant.RETURN_ALIPAY_VALUE_FAIL;
    }
    // 处理订单
    // 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
    int payStatus = payOrder.getStatus();
    if (payStatus == PayConstant.PAY_STATUS_COMPLETE) {
        // 处理完成
        log.info("====== 订单已经完成直接返回 ======");
        return PayConstant.RETURN_ALIPAY_VALUE_SUCCESS;
    }
    if (payStatus != PayConstant.PAY_STATUS_SUCCESS) {
        boolean updatePayOrderRows = payOrderService.updatePayOrderStatus4Success(payOrder.getPayOrderId());
        if (!updatePayOrderRows) {
            log.error("{}更新支付状态失败,将payOrderId={},更新失败", logPrefix, payOrder.getPayOrderId());
            return PayConstant.RETURN_ALIPAY_VALUE_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 PayConstant.RETURN_ALIPAY_VALUE_SUCCESS;
}
Also used : PayOrder(com.github.liuweijw.business.pay.domain.PayOrder) PayChannel(com.github.liuweijw.business.pay.domain.PayChannel) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with PayOrder

use of com.github.liuweijw.business.pay.domain.PayOrder in project fw-cloud-framework by liuweijw.

the class PayAliNotifyController method doNotifyResult.

public String doNotifyResult(HttpServletRequest request, HttpServletResponse response) {
    String logPrefix = "【支付宝支付回调通知】";
    log.info("====== 开始接收支付宝支付回调通知 ======");
    try {
        // 获取支付宝POST过来反馈信息
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = iter.next();
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
            params.put(name, valueStr);
        }
        log.info("{}通知请求数据:reqStr={}", logPrefix, params);
        if (params.isEmpty()) {
            log.error("{}请求参数为空", logPrefix);
            return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
        }
        // 商户订单号
        String out_trade_no = params.get("out_trade_no");
        // 支付金额
        String total_amount = params.get("total_amount");
        if (StringUtils.isEmpty(out_trade_no)) {
            log.error("{}AliPay Notify parameter out_trade_no is empty. out_trade_no={}", logPrefix, out_trade_no);
            return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
        }
        if (StringUtils.isEmpty(total_amount)) {
            log.error("{}AliPay Notify parameter total_amount is empty. total_fee={}", logPrefix, total_amount);
            return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
        }
        // 查询payOrder记录
        String payOrderId = out_trade_no;
        PayOrder payOrder = payOrderService.findPayOrderByOrderId(payOrderId);
        if (null == payOrder) {
            log.error("{}Can't found payOrder form db. payOrderId={}, ", logPrefix, payOrderId);
            return PayConstant.RETURN_ALIPAY_VALUE_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={}, ", logPrefix, payOrderId, mchId, channelId);
            return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
        }
        boolean verify_result = false;
        try {
            verify_result = AlipaySignature.rsaCheckV1(params, AlipayUtil.init(payChannel.getParam()).getAlipayPublicKey(), AlipayConfig.CHARSET, "RSA2");
        } catch (AlipayApiException e) {
            log.error("{}AlipaySignature.rsaCheckV1 error:" + e, logPrefix);
        }
        // 验证签名
        if (!verify_result)
            return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
        // 核对金额
        long aliPayAmt = new BigDecimal(total_amount).movePointRight(2).longValue();
        long dbPayAmt = payOrder.getAmount().longValue();
        if (dbPayAmt != aliPayAmt) {
            log.error("{}db payOrder record payPrice not equals total_fee. total_fee={},payOrderId={}", logPrefix, aliPayAmt, payOrderId);
            return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
        }
        // 处理订单
        // 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
        int payStatus = payOrder.getStatus();
        if (payStatus == PayConstant.PAY_STATUS_COMPLETE) {
            // 处理完成
            log.info("====== 订单已经完成直接返回 ======");
            return PayConstant.RETURN_ALIPAY_VALUE_SUCCESS;
        }
        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 PayConstant.RETURN_ALIPAY_VALUE_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 PayConstant.RETURN_ALIPAY_VALUE_SUCCESS;
    } catch (Exception e) {
        log.error("支付宝回调结果异常,异常原因" + e);
        return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
    }
}
Also used : HashMap(java.util.HashMap) PayOrder(com.github.liuweijw.business.pay.domain.PayOrder) PayChannel(com.github.liuweijw.business.pay.domain.PayChannel) BigDecimal(java.math.BigDecimal) AlipayApiException(com.alipay.api.AlipayApiException) AlipayApiException(com.alipay.api.AlipayApiException)

Example 5 with PayOrder

use of com.github.liuweijw.business.pay.domain.PayOrder in project fw-cloud-framework by liuweijw.

the class PayOrderController method create.

/**
 * 统一下单接口: 1) 先验证接口参数以及签名信息 2) 验证通过创建支付订单 3) 根据商户选择渠道,调用支付服务进行下单 4) 返回下单数据
 */
@RequestMapping(value = "/create")
public R<Map<String, Object>> create(@RequestBody PayUnifiedOrder unifiedOrder) {
    PayOrder payOrder = new PayOrder();
    Map<String, String> params = new HashMap<>();
    R<Boolean> validateResult = this.validatePayOrderParams(unifiedOrder, payOrder, params);
    if (!validateResult.getData()) {
        log.error(JSON.toJSONString(validateResult));
        return new R<Map<String, Object>>().data(PayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "订单校验失败", null, null)).failure();
    }
    PayOrder resultOrder = payOrderService.updateOrSavePayOrder(payOrder);
    if (null == resultOrder)
        return new R<Map<String, Object>>().data(PayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "订单保存失败", null, null)).failure();
    switch(resultOrder.getChannelId()) {
        case PayConstant.PAY_CHANNEL_WX_APP:
            return wxUnifiedOrderService.doWxUnifiedOrderRequest(PayConstant.WxConstant.TRADE_TYPE_APP, payOrder, params);
        case PayConstant.PAY_CHANNEL_WX_JSAPI:
            return wxUnifiedOrderService.doWxUnifiedOrderRequest(PayConstant.WxConstant.TRADE_TYPE_JSPAI, payOrder, params);
        case PayConstant.PAY_CHANNEL_WX_NATIVE:
            return wxUnifiedOrderService.doWxUnifiedOrderRequest(PayConstant.WxConstant.TRADE_TYPE_NATIVE, payOrder, params);
        case PayConstant.PAY_CHANNEL_WX_MWEB:
            return wxUnifiedOrderService.doWxUnifiedOrderRequest(PayConstant.WxConstant.TRADE_TYPE_MWEB, payOrder, params);
        case PayConstant.PAY_CHANNEL_ALIPAY_MOBILE:
        case PayConstant.PAY_CHANNEL_ALIPAY_PC:
        case PayConstant.PAY_CHANNEL_ALIPAY_WAP:
        case PayConstant.PAY_CHANNEL_ALIPAY_QR:
            return aliUnifiedOrderService.doAliUnifiedOrderRequest(resultOrder.getChannelId(), payOrder, params);
        default:
            break;
    }
    return new R<Map<String, Object>>().data(PayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "不支持的支付渠道类型[channelId=" + resultOrder.getChannelId() + "]", null, null)).failure();
}
Also used : R(com.github.liuweijw.commons.base.R) PayOrder(com.github.liuweijw.business.pay.domain.PayOrder) HashMap(java.util.HashMap) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PayOrder (com.github.liuweijw.business.pay.domain.PayOrder)5 PayChannel (com.github.liuweijw.business.pay.domain.PayChannel)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AlipayApiException (com.alipay.api.AlipayApiException)1 WxPayOrderNotifyResult (com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult)1 WxPayConfig (com.github.binarywang.wxpay.config.WxPayConfig)1 WxPayException (com.github.binarywang.wxpay.exception.WxPayException)1 WxPayServiceImpl (com.github.binarywang.wxpay.service.impl.WxPayServiceImpl)1 R (com.github.liuweijw.commons.base.R)1 Map (java.util.Map)1