| | |
| | | package cc.mrbird.febs.pay.service.impl; |
| | | |
| | | import cc.mrbird.febs.common.enumerates.DataDictionaryEnum; |
| | | import cc.mrbird.febs.common.enumerates.OrderStatusEnum; |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cc.mrbird.febs.mall.entity.MallMember; |
| | | import cc.mrbird.febs.mall.entity.MallOrderInfo; |
| | | import cc.mrbird.febs.mall.mapper.DataDictionaryCustomMapper; |
| | | import cc.mrbird.febs.mall.mapper.MallOrderInfoMapper; |
| | | import cc.mrbird.febs.pay.model.BrandWCPayRequestData; |
| | | import cc.mrbird.febs.pay.model.JsApiPayBusiness; |
| | | import cc.mrbird.febs.pay.model.JsApiPayReqData; |
| | | import cc.mrbird.febs.pay.model.JsApiPayResData; |
| | | import cc.mrbird.febs.pay.service.IXcxCodePayService; |
| | | import cc.mrbird.febs.pay.util.RandomStringGenerator; |
| | | import cc.mrbird.febs.pay.util.Signature; |
| | | import cc.mrbird.febs.pay.util.WebUtil; |
| | | import cn.hutool.core.date.DateField; |
| | | import cn.hutool.core.date.DatePattern; |
| | | import cc.mrbird.febs.pay.util.WeixinServiceUtil; |
| | | import cc.mrbird.febs.rabbit.producter.AgentProducer; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult; |
| | | import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest; |
| | | import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; |
| | | import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest; |
| | | import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult; |
| | | import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult; |
| | | import com.github.binarywang.wxpay.config.WxPayConfig; |
| | | import com.github.binarywang.wxpay.exception.WxPayException; |
| | | import com.github.binarywang.wxpay.service.WxPayService; |
| | | import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dom4j.Document; |
| | | import org.dom4j.DocumentHelper; |
| | | import org.dom4j.Element; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | |
| | | public class xcxCodePayServiceImpl implements IXcxCodePayService { |
| | | |
| | | private final MallOrderInfoMapper mallOrderInfoMapper; |
| | | private final WxPayService wxService; |
| | | |
| | | private final DataDictionaryCustomMapper dataDictionaryCustomMapper; |
| | | private final WeixinServiceUtil weixinServiceUtil; |
| | | private final AgentProducer agentProducer; |
| | | private final WxPayService wxPayService; |
| | | private static final String PAY_SUCCESS = "SUCCESS"; |
| | | private static final String PAY_WAITING = "等待用户输入密码"; |
| | | private static final String PAY_USERPAYING = "USERPAYING"; |
| | | private static final String PAY_FAIL = "FAIL"; |
| | | private static final String TRADE_TYPE = "MICROPAY"; |
| | | @Override |
| | | public String pay(MallOrderInfo mallOrderInfo) { |
| | | WxPayMicropayRequest request = new WxPayMicropayRequest(); |
| | | request |
| | | request.setAppid(wxPayProperties.getAppId()); |
| | | request.setMchId(wxPayProperties.getMchId()); |
| | | request.setNotifyUrl(wxPayProperties.getNoticeUrl()); |
| | | request.setBody(orderInfo.getName()); |
| | | request.setOutTradeNo(orderInfo.getOrderNo()); |
| | | request.setTimeExpire(DateUtil.format(DateUtil.offset(new Date(), DateField.MINUTE, 15), DatePattern.PURE_DATETIME_PATTERN)); |
| | | request.setSpbillCreateIp("127.0.0.1"); |
| | | request.setTradeType("APP"); |
| | | |
| | | // if ("dev".equals(active) || "test".equals(active)) { |
| | | if ("dev".equals(active)) { |
| | | request.setTotalFee(1); |
| | | } else { |
| | | request.setTotalFee(orderInfo.getAmount().add(orderInfo.getCarriage()).multiply(new BigDecimal(100)).intValue()); |
| | | } |
| | | // String appId = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | // DataDictionaryEnum.XCX_APP_ID.getType(), |
| | | // DataDictionaryEnum.XCX_APP_ID.getCode() |
| | | // ).getValue(); |
| | | // String mchId = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | // DataDictionaryEnum.XCX_MCH_ID.getType(), |
| | | // DataDictionaryEnum.XCX_MCH_ID.getCode() |
| | | // ).getValue(); |
| | | // String paySecret = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | // DataDictionaryEnum.XCX_MCH_KEY.getType(), |
| | | // DataDictionaryEnum.XCX_MCH_KEY.getCode() |
| | | // ).getValue(); |
| | | // String keyPath = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | // DataDictionaryEnum.XCX_MCH_KEY_PATH_P12.getType(), |
| | | // DataDictionaryEnum.XCX_MCH_KEY_PATH_P12.getCode() |
| | | // ).getValue(); |
| | | // String notifyUrl = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | // DataDictionaryEnum.XCX_NOTICE_URL.getType(), |
| | | // DataDictionaryEnum.XCX_NOTICE_URL.getCode() |
| | | // ).getValue(); |
| | | // WxPayConfig payConfig = new WxPayConfig(); |
| | | // payConfig.setAppId(StrUtil.trim(appId)); |
| | | // payConfig.setMchId(StrUtil.trim(mchId)); |
| | | // payConfig.setMchKey(StrUtil.trim(paySecret)); |
| | | // payConfig.setKeyPath(StrUtil.trim(keyPath)); |
| | | // payConfig.setNotifyUrl(StrUtil.trim(notifyUrl)); |
| | | |
| | | // 可以指定是否使用沙箱环境 |
| | | // payConfig.setUseSandboxEnv(false); |
| | | // |
| | | // WxPayService wxPayService = new WxPayServiceImpl(); |
| | | // wxPayService.setConfig(payConfig); |
| | | WxPayMicropayRequest request = new WxPayMicropayRequest(); |
| | | /** |
| | | * |
| | | Map<String, String> map = new HashMap<>(16); |
| | | map.put("attach", "订单额外描述"); |
| | | map.put("auth_code", auth_code); |
| | | map.put("body", "付款码支付测试"); |
| | | map.put("device_info", "1000"); |
| | | map.put("nonce_str", WXPayUtil.generateNonceStr()); |
| | | map.put("out_trade_no", out_trade_no); |
| | | map.put("spbill_create_ip", "14.17.22.52"); |
| | | map.put("total_fee", "2"); |
| | | //生成签名 |
| | | String sign = WXPayUtil.generateSignature(map, config.getKey()); |
| | | map.put("sign", sign); |
| | | */ |
| | | // request.setAppid(appId); |
| | | // request.setMchId(mchId); |
| | | request.setNonceStr(RandomStringGenerator.getRandomStringByLength(32)); |
| | | request.setBody(mallOrderInfo.getName()); |
| | | request.setOutTradeNo(mallOrderInfo.getOrderNo()); |
| | | int totalFee = mallOrderInfo.getAmount().subtract(mallOrderInfo.getCarriage()).multiply(new BigDecimal(100)).intValue(); |
| | | request.setTotalFee(totalFee); |
| | | request.setSpbillCreateIp(weixinServiceUtil.getIpAddr(WebUtil.getRequest())); |
| | | request.setAuthCode(mallOrderInfo.getTakeCode()); |
| | | |
| | | try { |
| | | WxPayMicropayResult micropay = wxService.micropay(request); |
| | | return JSONObject.toJSONString(micropay); |
| | | request.setSign(Signature.getSign(request,wxPayService.getConfig().getMchKey())); |
| | | } catch (IllegalAccessException e) { |
| | | e.printStackTrace(); |
| | | throw new FebsException("签名异常"); |
| | | } |
| | | WxPayMicropayResult micropay = null; |
| | | String errCode = null; |
| | | String errCodeDes = null; |
| | | try { |
| | | micropay = wxPayService.micropay(request); |
| | | |
| | | } catch (WxPayException e) { |
| | | e.printStackTrace(); |
| | | throw new FebsException("支付异常, 请检查"); |
| | | } |
| | | return null; |
| | | errCode = e.getErrCode(); |
| | | errCodeDes = e.getErrCodeDes(); |
| | | // throw new FebsException(e.getErrCodeDes()); |
| | | }finally { |
| | | if (PAY_USERPAYING.equals(errCode)){//等待用户输入密码 |
| | | for(int i = 0; i < 5; i++){ |
| | | try { |
| | | Thread.sleep(3000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | String outTradeNo = mallOrderInfo.getOrderNo(); |
| | | WxPayOrderQueryRequest wxPayOrderQueryRequest = new WxPayOrderQueryRequest(); |
| | | wxPayOrderQueryRequest.setOutTradeNo(outTradeNo); |
| | | log.info("付款码支付订单查询"); |
| | | WxPayOrderQueryResult wxPayOrderQueryResult = null; |
| | | try { |
| | | wxPayOrderQueryResult = wxPayService.queryOrder(wxPayOrderQueryRequest); |
| | | } catch (WxPayException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | String returnCodePaying = wxPayOrderQueryResult.getReturnCode(); |
| | | String tradeTypePaying = wxPayOrderQueryResult.getTradeType(); |
| | | String resultCodePaying = wxPayOrderQueryResult.getResultCode(); |
| | | String tradeStateDesc = wxPayOrderQueryResult.getTradeStateDesc(); |
| | | /** |
| | | * 交易成功判断条件:return_code和result_code都为SUCCESS且trade_type为MICROPAY |
| | | */ |
| | | if(PAY_SUCCESS.equals(resultCodePaying)&& PAY_SUCCESS.equals(returnCodePaying)&& TRADE_TYPE.equals(tradeTypePaying)){ |
| | | log.info("微信加密支付成功!"); |
| | | agentProducer.sendSyAppOrderPayDoneQueue(outTradeNo); |
| | | return tradeStateDesc; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if(ObjectUtil.isEmpty(micropay)){ |
| | | return errCodeDes; |
| | | } |
| | | String returnCode = micropay.getReturnCode(); |
| | | String resultCode = micropay.getResultCode(); |
| | | String outTradeNo = micropay.getOutTradeNo(); |
| | | |
| | | if(PAY_SUCCESS.equals(returnCode) && PAY_SUCCESS.equals(resultCode)){ |
| | | log.info("微信免密支付成功"); |
| | | MallOrderInfo orderInfo = mallOrderInfoMapper.selectByOrderNo(mallOrderInfo.getOrderNo()); |
| | | if(ObjectUtil.isNotEmpty(orderInfo)){ |
| | | if (OrderStatusEnum.FINISH.getValue() != orderInfo.getStatus()) { |
| | | orderInfo.setStatus(OrderStatusEnum.FINISH.getValue()); |
| | | orderInfo.setPayResult(1); |
| | | orderInfo.setPayTime(DateUtil.date()); |
| | | mallOrderInfoMapper.updateById(orderInfo); |
| | | } |
| | | } |
| | | agentProducer.sendSyAppOrderPayDoneQueue(outTradeNo); |
| | | return PAY_SUCCESS; |
| | | } else if(PAY_FAIL.equals(resultCode)){ |
| | | return errCodeDes; |
| | | } |
| | | return errCodeDes; |
| | | } |
| | | |
| | | @Override |
| | |
| | | return; |
| | | } |
| | | orderInfo.setStatus(OrderStatusEnum.FINISH.getValue()); |
| | | orderInfo.setPayResult("1"); |
| | | orderInfo.setPayResult(1); |
| | | orderInfo.setPayTime(DateUtil.date()); |
| | | orderInfo.setPayTradeNo(params.get("transaction_id")); |
| | | mallOrderInfoMapper.updateById(orderInfo); |