Search in sources :

Example 6 with PayChannel

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

the class PayOrderController method validatePayOrderParams.

private R<Boolean> validatePayOrderParams(PayUnifiedOrder unifiedOrder, PayOrder payOrder, Map<String, String> params) {
    String preFix = "[统一下单提示] ";
    if (null == unifiedOrder)
        return new R<Boolean>().data(false).failure(preFix + "下单信息不能为空!");
    // 商户id
    String mchId = unifiedOrder.getMchId();
    if (StringHelper.isBlank(mchId))
        return new R<Boolean>().data(false).failure(preFix + "商户ID不存在!");
    // 商户订单号
    String mchOrderNo = unifiedOrder.getMchOrderNo();
    if (StringHelper.isBlank(mchOrderNo))
        return new R<Boolean>().data(false).failure(preFix + "商户订单号不存在!");
    // 渠道ID
    String channelId = unifiedOrder.getChannelId();
    if (StringHelper.isBlank(channelId))
        return new R<Boolean>().data(false).failure(preFix + "渠道ID不存在!");
    // 支付金额(单位分)
    Long amount = unifiedOrder.getAmount();
    if (null == amount || amount <= 0)
        return new R<Boolean>().data(false).failure(preFix + "支付金额(单位分)不正确!");
    // 币种
    String currency = unifiedOrder.getCurrency();
    if (StringHelper.isBlank(currency))
        return new R<Boolean>().data(false).failure(preFix + "币种未设置!");
    // 支付结果回调URL
    String notifyUrl = unifiedOrder.getNotifyUrl();
    if (StringHelper.isBlank(notifyUrl))
        return new R<Boolean>().data(false).failure(preFix + "支付回调未设置!");
    // 商品主题
    String subject = unifiedOrder.getSubject();
    if (StringHelper.isBlank(subject))
        return new R<Boolean>().data(false).failure(preFix + "商品主题未设置!");
    // 商品描述信息
    String body = unifiedOrder.getBody();
    if (StringHelper.isBlank(body))
        return new R<Boolean>().data(false).failure(preFix + "商品描述信息未设置!");
    // ip
    String ip = unifiedOrder.getIp();
    if (StringHelper.isBlank(ip))
        return new R<Boolean>().data(false).failure(preFix + "请求IP地址不正确!");
    String device = unifiedOrder.getDevice();
    if (StringHelper.isBlank(device))
        return new R<Boolean>().data(false).failure(preFix + "请求设备信息设置不正确!");
    String extra = unifiedOrder.getExtra();
    // 根据不同渠道,判断extra参数
    if (PayConstant.PAY_CHANNEL_WX_JSAPI.equalsIgnoreCase(channelId)) {
        if (StringHelper.isBlank(extra))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra]信息未设置!");
        JSONObject extraObject = JSON.parseObject(extra);
        if (null == extraObject || !extraObject.containsKey("openId"))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra.openId]信息未设置!");
        String openId = extraObject.getString("openId");
        if (StringHelper.isBlank(openId))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra.openId]信息未正确设置!");
    } else if (PayConstant.PAY_CHANNEL_WX_NATIVE.equalsIgnoreCase(channelId)) {
        if (StringHelper.isBlank(extra))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra]信息未设置!");
        JSONObject extraObject = JSON.parseObject(extra);
        if (null == extraObject || !extraObject.containsKey("productId"))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra.productId]信息未设置!");
        String productId = extraObject.getString("productId");
        if (StringHelper.isBlank(productId))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra.productId]信息未正确设置!");
    } else if (PayConstant.PAY_CHANNEL_WX_MWEB.equalsIgnoreCase(channelId)) {
        if (StringHelper.isBlank(extra))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra]信息未设置!");
        JSONObject extraObject = JSON.parseObject(WebUtils.buildURLDecoder(extra));
        if (null == extraObject || !extraObject.containsKey("sceneInfo"))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra.sceneInfo]信息未设置!");
        String sceneInfo = extraObject.getJSONObject("sceneInfo").toJSONString();
        if (StringHelper.isBlank(sceneInfo))
            return new R<Boolean>().data(false).failure(preFix + channelId + "[extra.sceneInfo]信息未正确设置!");
    }
    // 签名信息
    String sign = unifiedOrder.getSign();
    if (StringHelper.isBlank(sign))
        return new R<Boolean>().data(false).failure(preFix + "未签名!");
    // 查询商户信息
    PayMchInfo mchInfo = mchInfoService.findMchInfoByMchId(mchId);
    if (null == mchInfo)
        return new R<Boolean>().data(false).failure(preFix + "商户信息不存在!");
    if (mchInfo.getStatu().intValue() != 1)
        return new R<Boolean>().data(false).failure(preFix + "商户信息已失效!");
    if (StringHelper.isBlank(mchInfo.getReqKey()))
        return new R<Boolean>().data(false).failure(preFix + "商户请求私钥未设置!");
    // 查询商户对应的支付渠道
    PayChannel payChannel = payChannelService.findPayChannel(channelId, mchId);
    if (null == payChannel)
        return new R<Boolean>().data(false).failure(preFix + "商户渠道[channelId=" + channelId + ",mchId=" + mchId + "]信息不存在!");
    if (payChannel.getStatu().intValue() != 1)
        return new R<Boolean>().data(false).failure(preFix + "商户渠道[channelId=" + channelId + ",mchId=" + mchId + "]信息已经失效!");
    // 验证签名数据
    boolean verifyFlag = PayUtil.verifyPaySign((JSONObject) JSON.toJSON(unifiedOrder), mchInfo.getReqKey());
    if (!verifyFlag)
        return new R<Boolean>().data(false).failure(preFix + "下单信息验证签名失败!");
    payOrder.setPayOrderId(SequenceUtils.getInstance().generateBizSeqNo(BizConstant.PAY_ORDER_PREFIX));
    payOrder.setMch_id(mchInfo.getMchId());
    payOrder.setMchOrderNo(mchOrderNo);
    payOrder.setChannelId(channelId);
    payOrder.setAmount(amount);
    payOrder.setCurrency(currency);
    payOrder.setIp(ip);
    payOrder.setDevice(device);
    payOrder.setSubject(WebUtils.buildURLDecoder(subject));
    payOrder.setBody(WebUtils.buildURLDecoder(body));
    payOrder.setExtra(extra);
    payOrder.setChannelMchId(payChannel.getChannelMchId());
    payOrder.setParam1(unifiedOrder.getParam1());
    payOrder.setParam2(unifiedOrder.getParam2());
    payOrder.setNotifyUrl(notifyUrl);
    params.put("resKey", mchInfo.getResKey());
    params.put("channelParam", payChannel.getParam());
    // 支付宝支付回调业务地址--非必填
    params.put("returnUrl", unifiedOrder.getReturnUrl());
    return new R<Boolean>().data(true).success(preFix + "下单信息验证成功!");
}
Also used : R(com.github.liuweijw.commons.base.R) PayMchInfo(com.github.liuweijw.business.pay.domain.PayMchInfo) JSONObject(com.alibaba.fastjson.JSONObject) PayChannel(com.github.liuweijw.business.pay.domain.PayChannel)

Aggregations

PayChannel (com.github.liuweijw.business.pay.domain.PayChannel)6 PayOrder (com.github.liuweijw.business.pay.domain.PayOrder)4 WxPayConfig (com.github.binarywang.wxpay.config.WxPayConfig)2 PayMchInfo (com.github.liuweijw.business.pay.domain.PayMchInfo)2 R (com.github.liuweijw.commons.base.R)2 BigDecimal (java.math.BigDecimal)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)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 WxPayException (com.github.binarywang.wxpay.exception.WxPayException)1 WxPayServiceImpl (com.github.binarywang.wxpay.service.impl.WxPayServiceImpl)1 HashMap (java.util.HashMap)1