| | |
| | | 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 cn.hutool.core.date.DateUtil; |
| | | 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 static final String PAY_SUCCESS = "SUCCESS"; |
| | | private static final String PAY_USERPAYING = "USERPAYING"; |
| | | @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,paySecret)); |
| | | } catch (IllegalAccessException e) { |
| | | e.printStackTrace(); |
| | | throw new FebsException("签名异常"); |
| | | } |
| | | try { |
| | | WxPayMicropayResult micropay = wxPayService.micropay(request); |
| | | String returnCode = micropay.getReturnCode(); |
| | | String resultCode = micropay.getResultCode(); |
| | | String errCode = micropay.getErrCode(); |
| | | String outTradeNo = micropay.getOutTradeNo(); |
| | | String errCodeDes = micropay.getErrCodeDes(); |
| | | /** |
| | | * 交易成功判断条件:return_code和result_code都为SUCCESS且trade_type为MICROPAY |
| | | */ |
| | | |
| | | if(PAY_SUCCESS.equals(returnCode) && PAY_SUCCESS.equals(resultCode)){ |
| | | log.info("微信免密支付成功"); |
| | | return PAY_SUCCESS; |
| | | } else if (PAY_USERPAYING.equals(errCode)){//等待用户输入密码 |
| | | for(int i = 0; i < 4; i++){ |
| | | try { |
| | | Thread.sleep(5000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //调用微信的查询接口 |
| | | /** |
| | | * 交易成功判断条件: return_code、result_code和trade_state都为SUCCESS |
| | | */ |
| | | WxPayOrderQueryRequest wxPayOrderQueryRequest = new WxPayOrderQueryRequest(); |
| | | wxPayOrderQueryRequest.setOutTradeNo(outTradeNo); |
| | | WxPayOrderQueryResult wxPayOrderQueryResult = wxPayService.queryOrder(wxPayOrderQueryRequest); |
| | | String returnCodePaying = wxPayOrderQueryResult.getReturnCode(); |
| | | String resultCodePaying = wxPayOrderQueryResult.getResultCode(); |
| | | String tradeTypePaying = wxPayOrderQueryResult.getTradeType(); |
| | | if(PAY_SUCCESS.equals(tradeTypePaying)&& PAY_SUCCESS.equals(returnCodePaying)&& PAY_SUCCESS.equals(resultCodePaying)){ |
| | | log.info("微信加密支付成功!"); |
| | | return PAY_SUCCESS; |
| | | } |
| | | log.info("正在支付" + wxPayOrderQueryResult.getDetail()); |
| | | } |
| | | } |
| | | log.error("微信支付失败!"); |
| | | return errCodeDes; |
| | | } catch (WxPayException e) { |
| | | e.printStackTrace(); |
| | | throw new FebsException("支付异常, 请检查"); |
| | | throw new FebsException("支付异常"); |
| | | } |
| | | return null; |
| | | |
| | | } |
| | | |
| | | @Override |