Search in sources :

Example 1 with WxPayService

use of com.github.binarywang.wxpay.service.WxPayService in project weixin-java-pay-demo by binarywang.

the class WxPayConfiguration method wxPayService.

@Bean
public // @ConditionalOnMissingBean
WxPayService wxPayService(WxPayConfig payConfig) {
    WxPayService wxPayService = new WxPayServiceImpl();
    wxPayService.setConfig(payConfig);
    return wxPayService;
}
Also used : WxPayService(com.github.binarywang.wxpay.service.WxPayService) WxPayServiceImpl(com.github.binarywang.wxpay.service.impl.WxPayServiceImpl) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with WxPayService

use of com.github.binarywang.wxpay.service.WxPayService 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 3 with WxPayService

use of com.github.binarywang.wxpay.service.WxPayService 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 4 with WxPayService

use of com.github.binarywang.wxpay.service.WxPayService in project summer by foxsugar.

the class WechatPayConfig method wxPayService.

@Bean(name = "wxPayService")
@Primary
public WxPayService wxPayService() {
    WxPayService wxPayService = new WxPayServiceImpl();
    wxPayService.setConfig(wxPayConfig());
    return wxPayService;
}
Also used : WxPayService(com.github.binarywang.wxpay.service.WxPayService) WxPayServiceImpl(com.github.binarywang.wxpay.service.impl.WxPayServiceImpl) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 5 with WxPayService

use of com.github.binarywang.wxpay.service.WxPayService in project summer by foxsugar.

the class WechatPayConfig method wxAppPayService.

@Bean(name = "wxAppPayService")
public WxPayService wxAppPayService() {
    WxPayService wxPayService = new WxPayServiceImpl();
    wxPayService.setConfig(wxAppPayConfig());
    return wxPayService;
}
Also used : WxPayService(com.github.binarywang.wxpay.service.WxPayService) WxPayServiceImpl(com.github.binarywang.wxpay.service.impl.WxPayServiceImpl) Bean(org.springframework.context.annotation.Bean)

Aggregations

WxPayService (com.github.binarywang.wxpay.service.WxPayService)7 WxPayServiceImpl (com.github.binarywang.wxpay.service.impl.WxPayServiceImpl)7 Bean (org.springframework.context.annotation.Bean)5 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)3 WxPayConfig (com.github.binarywang.wxpay.config.WxPayConfig)2 WxPayException (com.github.binarywang.wxpay.exception.WxPayException)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 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 Primary (org.springframework.context.annotation.Primary)1