Search in sources :

Example 6 with WxPayConfig

use of com.github.binarywang.wxpay.config.WxPayConfig in project fw-cloud-framework by liuweijw.

the class PaySendRedpackController method validateSendRedpackReqParams.

private R<Boolean> validateSendRedpackReqParams(PaySendRedpackReqBean sendRedpackReqBean, PaySendRedpack paySendRedpack, WxPaySendRedpackRequestBuilder wxPaySendRedpackRequestBuilder) {
    if (null == sendRedpackReqBean)
        return new R<Boolean>().data(false).failure("请求[/pay/order/sendRedpack]参数不能为空!");
    // 是否是发放普通
    boolean isCommonRedPack = sendRedpackReqBean.getRedPackType().intValue() == 0;
    String preFix = isCommonRedPack ? "【发放普通红包】" : "【发放裂变红包】";
    // 商户id
    String mchId = sendRedpackReqBean.getMchId();
    if (StringHelper.isBlank(mchId))
        return new R<Boolean>().data(false).failure(preFix + "商户ID不存在!");
    // 商户订单号
    String mchOrderNo = sendRedpackReqBean.getMchOrderNo();
    if (StringHelper.isBlank(mchOrderNo))
        return new R<Boolean>().data(false).failure(preFix + "商户订单号不存在!");
    // 渠道ID
    String channelId = sendRedpackReqBean.getChannelId();
    if (StringHelper.isBlank(channelId))
        return new R<Boolean>().data(false).failure(preFix + "渠道ID不存在!");
    // 红包发放总金额(单位分)
    Integer amount = sendRedpackReqBean.getTotalAmount();
    if (null == amount || amount <= 0)
        return new R<Boolean>().data(false).failure(preFix + "红包发放总金额(单位分)不正确!");
    if (amount < 100 || amount > 20000)
        return new R<Boolean>().data(false).failure(preFix + "每个红包的平均金额必须在1.00元到200.00元之间!");
    // 红包发放总人数
    Integer totalNum = sendRedpackReqBean.getTotalNum();
    if (null == totalNum || totalNum <= 0)
        return new R<Boolean>().data(false).failure(preFix + "红包发放总人数设置不正确!");
    // 红包祝福语
    String wishing = sendRedpackReqBean.getWishing();
    if (StringHelper.isBlank(wishing))
        return new R<Boolean>().data(false).failure(preFix + "红包祝福语未设置!");
    // ip
    String ip = sendRedpackReqBean.getIp();
    if (StringHelper.isBlank(ip))
        return new R<Boolean>().data(false).failure(preFix + "请求IP地址不正确!");
    // 活动名称
    String actName = sendRedpackReqBean.getActName();
    if (StringHelper.isBlank(actName))
        return new R<Boolean>().data(false).failure(preFix + "活动名称未设置!");
    // 备注
    String remark = sendRedpackReqBean.getRemark();
    if (StringHelper.isBlank(remark))
        return new R<Boolean>().data(false).failure(preFix + "备注信息未设置!");
    // 场景id
    String sceneId = sendRedpackReqBean.getSceneId();
    if (StringHelper.isBlank(sceneId) || !checkSceneId(sceneId))
        return new R<Boolean>().data(false).failure(preFix + "场景id不正确!");
    // 签名信息
    String sign = sendRedpackReqBean.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 + "]信息已经失效!");
    WxPayConfig wxPayConfig = WxPayUtil.getWxPayConfig(payChannel.getParam(), null, wxPayProperties.getCertRootPath(), wxPayProperties.getNotifyUrl());
    if (null == wxPayConfig)
        return new R<Boolean>().data(false).failure(preFix + "商户渠道[channelId=" + channelId + ",mchId=" + mchId + "]信息param设置不正确!");
    // 验证签名数据
    boolean verifyFlag = PayUtil.verifyPaySign((JSONObject) JSON.toJSON(sendRedpackReqBean), mchInfo.getReqKey());
    if (!verifyFlag)
        return new R<Boolean>().data(false).failure(preFix + "验证签名失败!");
    paySendRedpack.setSendOrderId(SequenceUtils.getInstance().generateBizSeqNo("SRP"));
    paySendRedpack.setWxPayConfig(wxPayConfig);
    paySendRedpack.setResKey(mchInfo.getResKey());
    paySendRedpack.setRedPackType(isCommonRedPack ? 0 : 1);
    paySendRedpack.setChannelId(channelId);
    // 商户订单号
    paySendRedpack.setMchOrderNo(mchOrderNo);
    // 商户号
    paySendRedpack.setMchId(wxPayConfig.getMchId());
    paySendRedpack.setOpenId(sendRedpackReqBean.getOpenId());
    // 付款金额
    paySendRedpack.setTotalAmount(sendRedpackReqBean.getTotalAmount());
    // 红包发放总人数
    paySendRedpack.setTotalNum(sendRedpackReqBean.getTotalNum());
    if (!isCommonRedPack) {
        // 红包金额设置方式 裂变红包才进行设置
        paySendRedpack.setAmtType("ALL_RAND");
    } else {
        paySendRedpack.setAmtType(null);
    }
    // 红包祝福语
    paySendRedpack.setWishing(sendRedpackReqBean.getWishing());
    paySendRedpack.setIp(ip);
    // 活动名称
    paySendRedpack.setActName(sendRedpackReqBean.getActName());
    // 备注
    paySendRedpack.setRemark(sendRedpackReqBean.getRemark());
    // 场景id
    paySendRedpack.setSceneId(sceneId);
    if (!PublicHelper.isEmpty(sendRedpackReqBean.getRiskInfo()))
        // 活动信息
        paySendRedpack.setRiskInfo(sendRedpackReqBean.getRiskInfo());
    if (!PublicHelper.isEmpty(sendRedpackReqBean.getConsumeMchId()))
        // 资金授权商户号
        paySendRedpack.setConsumeMchId(sendRedpackReqBean.getConsumeMchId());
    wxPaySendRedpackRequestBuilder.mchBillNo(paySendRedpack.getMchOrderNo()).wxAppid(wxPayConfig.getAppId()).sendName(// 商户名称 来自商户数据库
    mchInfo.getMchName()).reOpenid(paySendRedpack.getOpenId()).totalAmount(paySendRedpack.getTotalAmount()).totalNum(paySendRedpack.getTotalNum()).amtType(paySendRedpack.getAmtType()).wishing(paySendRedpack.getWishing()).clientIp(paySendRedpack.getIp()).actName(paySendRedpack.getActName()).remark(paySendRedpack.getRemark()).sceneId(paySendRedpack.getSceneId()).riskInfo(paySendRedpack.getRiskInfo()).consumeMchId(paySendRedpack.getConsumeMchId()).build();
    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) PayChannel(com.github.liuweijw.business.pay.domain.PayChannel) WxPayConfig(com.github.binarywang.wxpay.config.WxPayConfig)

Example 7 with WxPayConfig

use of com.github.binarywang.wxpay.config.WxPayConfig 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)

Example 8 with WxPayConfig

use of com.github.binarywang.wxpay.config.WxPayConfig in project summer by foxsugar.

the class WechatPayConfig method wxPayConfig.

@Bean
public WxPayConfig wxPayConfig() {
    // WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
    WxPayConfig wxPayConfig = new WxPayConfig();
    wxPayConfig.setAppId(accountConfig.getMpAppId());
    wxPayConfig.setMchId(accountConfig.getMchId());
    wxPayConfig.setMchKey(accountConfig.getMchKey());
    // wxPayConfig.setSubAppId(accountConfig.getsubAppId);
    // wxPayConfig.setSubMchId(this.subMchId);
    wxPayConfig.setKeyPath(accountConfig.getKeyPath());
    return wxPayConfig;
}
Also used : WxPayConfig(com.github.binarywang.wxpay.config.WxPayConfig) Bean(org.springframework.context.annotation.Bean)

Example 9 with WxPayConfig

use of com.github.binarywang.wxpay.config.WxPayConfig in project litemall by linlinjava.

the class WeixinConfig method wxPayConfig.

@Bean
public WxPayConfig wxPayConfig() {
    WxPayConfig payConfig = new WxPayConfig();
    payConfig.setAppId(WX_AppId);
    payConfig.setMchId(WX_MchId);
    payConfig.setMchKey(WX_MchKey);
    payConfig.setSubAppId(null);
    payConfig.setSubMchId(null);
    payConfig.setKeyPath(WX_KeyPath);
    return payConfig;
}
Also used : WxPayConfig(com.github.binarywang.wxpay.config.WxPayConfig) Bean(org.springframework.context.annotation.Bean)

Example 10 with WxPayConfig

use of com.github.binarywang.wxpay.config.WxPayConfig in project litemall by linlinjava.

the class WxPayConfiguration method config.

@Bean
@ConditionalOnMissingBean
public WxPayConfig config() {
    WxPayConfig payConfig = new WxPayConfig();
    payConfig.setAppId(this.properties.getAppId());
    payConfig.setMchId(this.properties.getMchId());
    payConfig.setMchKey(this.properties.getMchKey());
    payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
    payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
    payConfig.setKeyPath(this.properties.getKeyPath());
    return payConfig;
}
Also used : WxPayConfig(com.github.binarywang.wxpay.config.WxPayConfig) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

WxPayConfig (com.github.binarywang.wxpay.config.WxPayConfig)11 Bean (org.springframework.context.annotation.Bean)6 WxPayServiceImpl (com.github.binarywang.wxpay.service.impl.WxPayServiceImpl)4 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)3 JSONObject (com.alibaba.fastjson.JSONObject)2 WxPayException (com.github.binarywang.wxpay.exception.WxPayException)2 WxPayService (com.github.binarywang.wxpay.service.WxPayService)2 PayChannel (com.github.liuweijw.business.pay.domain.PayChannel)2 R (com.github.liuweijw.commons.base.R)2 WxPayOrderNotifyResult (com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult)1 WxPayUnifiedOrderRequest (com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest)1 WxPayUnifiedOrderResult (com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult)1 PayMchInfo (com.github.liuweijw.business.pay.domain.PayMchInfo)1 PayOrder (com.github.liuweijw.business.pay.domain.PayOrder)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 PostConstruct (javax.annotation.PostConstruct)1