| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; |
| | | import com.github.binarywang.wxpay.bean.profitsharingV3.ProfitSharingReceiver; |
| | | import com.github.binarywang.wxpay.bean.profitsharingV3.ProfitSharingRequest; |
| | | import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; |
| | | import com.github.binarywang.wxpay.config.WxPayConfig; |
| | | import com.github.binarywang.wxpay.constant.WxPayConstants; |
| | | import com.github.binarywang.wxpay.exception.WxPayException; |
| | | import com.github.binarywang.wxpay.service.WxPayService; |
| | | import com.google.common.base.Joiner; |
| | | import com.google.gson.Gson; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.core.utils.DateUtils; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.common.security.utils.CodeFactoryUtil; |
| | | import com.ruoyi.common.security.utils.SecurityUtils; |
| | | import com.ruoyi.order.config.MyWxPayConfig; |
| | | import com.ruoyi.order.domain.dto.*; |
| | | import com.ruoyi.order.domain.pojo.account.OrderPayment; |
| | | import com.ruoyi.order.domain.pojo.account.OrderRefund; |
| | |
| | | import com.ruoyi.order.domain.pojo.order.PayRecord; |
| | | import com.ruoyi.order.domain.vo.*; |
| | | import com.ruoyi.order.mapper.order.OrderMapper; |
| | | import com.ruoyi.order.service.account.OrderPaymentService; |
| | | import com.ruoyi.order.service.account.PaymentMessageService; |
| | | import com.ruoyi.order.service.order.*; |
| | | import com.ruoyi.system.api.constant.AppErrorConstant; |
| | | import com.ruoyi.system.api.constant.DelayTaskEnum; |
| | |
| | | import com.ruoyi.system.api.domain.poji.member.MemberCoupon; |
| | | import com.ruoyi.system.api.domain.poji.member.MemberGiftRecord; |
| | | import com.ruoyi.system.api.domain.poji.shop.Shop; |
| | | import com.ruoyi.system.api.domain.poji.shop.ShopProportionVo; |
| | | import com.ruoyi.system.api.domain.poji.sys.SysUser; |
| | | import com.ruoyi.system.api.domain.vo.*; |
| | | import com.ruoyi.system.api.service.*; |
| | |
| | | @Autowired |
| | | private RedissonClient redissonClient; |
| | | |
| | | /** 商户号 */ |
| | | public static String merchantId = ""; |
| | | /** 商户API私钥路径 */ |
| | | public static String privateKeyPath = ""; |
| | | /** 商户证书序列号 */ |
| | | public static String merchantSerialNumber = ""; |
| | | /** 微信支付平台证书路径 */ |
| | | public static String wechatPayCertificatePath = ""; |
| | | /** 微信支付 APIv3 密钥 */ |
| | | public static String apiV3Key = ""; |
| | | /** 微信支付APPID */ |
| | | public static String appId=""; |
| | | /** 微信支付商户ID */ |
| | | public static String mchId=""; |
| | | /** 通知地址 */ |
| | | public static String notifyUrl=""; |
| | | @Autowired |
| | | private WxPayService wxService; |
| | | |
| | | /** |
| | | @Resource |
| | | private PaymentMessageService paymentMessageService; |
| | | |
| | | @Resource |
| | | private OrderPaymentService orderPaymentService; |
| | | |
| | | /** |
| | | * @description: buyGoods |
| | | * @param: appSureOrderDto |
| | | * @return: AppSureOrderVo |
| | |
| | | Integer serviceNum; |
| | | Integer goodsNum = 0; |
| | | String memberCouponId; |
| | | Goods goods; |
| | | Goods goods = null; |
| | | GoodsFile goodsFile; |
| | | BigDecimal goodsPrice; |
| | | BigDecimal goodsTotalPrice; |
| | |
| | | HashSet<String> goodsTypeSet = new HashSet<>(); |
| | | //使用优惠券 |
| | | StringJoiner memberCouponSJ = new StringJoiner(","); |
| | | List<String> goodsNameList = new ArrayList<>(); |
| | | // 遍历购买的商品列表 |
| | | for (AppBuyGoodsDto appBuyGoodsDto : appBuyGoodsDtoList) { |
| | | appSureOrderGoodsVo = new AppSureOrderGoodsVo(); |
| | |
| | | memberCouponId = appBuyGoodsDto.getMemberCouponId(); |
| | | // 获取商品信息 |
| | | goods = goodsMap.get(goodsId); |
| | | goodsNameList.add(goods.getGoodsName()); |
| | | // 设置商品信息 |
| | | appSureOrderGoodsVo.setGoodsId(goodsId); |
| | | appSureOrderGoodsVo.setGoodsName(goods.getGoodsName()); |
| | |
| | | } |
| | | appPlaceOrderVo.setOrderId(orderId); |
| | | appPlaceOrderVo.setOrderNo(orderNo); |
| | | // 调用支付 |
| | | /* CombineTransactionsJsRequest request=new CombineTransactionsJsRequest(); |
| | | request.setCombineAppid(); |
| | | request.setCombineMchid(); |
| | | profitsSharingService.combineTransactions(request);*/ |
| | | |
| | | //构建预下单支付对象 |
| | | /*PrepayRequest request = new PrepayRequest(); |
| | | Amount amount = new Amount(); |
| | | amount.setTotal(Integer.parseInt(order.getPayMoney().multiply(new BigDecimal(100)).toString())); |
| | | request.setAmount(amount); |
| | | request.setAppid(this.appId); |
| | | request.setMchid(this.mchId); |
| | | request.setDescription(order.getGoodsInfo()); |
| | | request.setNotifyUrl(this.notifyUrl); |
| | | request.setOutTradeNo(order.getOrderId()); |
| | | Payer payer = new Payer(); |
| | | payer.setOpenid("oLTPCuN5a-nBD4rAL_fa********"); |
| | | request.setPayer(payer);*/ |
| | | //返回前端唤醒支付结果信息 |
| | | /*PrepayWithRequestPaymentResponse res = this.getJsApIResponse(request); |
| | | appPlaceOrderVo.setAppId(this.appId); |
| | | appPlaceOrderVo.setTimeStamp(res.getTimeStamp()); |
| | | appPlaceOrderVo.setNonceStr(res.getNonceStr()); |
| | | appPlaceOrderVo.setPackageStr(res.getPackageVal()); |
| | | appPlaceOrderVo.setTradeType(res.getSignType()); |
| | | appPlaceOrderVo.setPaySign(res.getPaySign());*/ |
| | | try { |
| | | // 创建支付订单 |
| | | Long shopId = appPlaceOrderDto.getShopId(); |
| | | R<String> resultMch = remoteShopService.getShopSubMchId(shopId); |
| | | String subMchId = resultMch.getData(); |
| | | if(StringUtils.isEmpty(subMchId)){ |
| | | throw new ServiceException("获取微信商户号失败"); |
| | | } |
| | | |
| | | WxPayConfig config = wxService.getConfig(); |
| | | config.setSubMchId(subMchId); |
| | | |
| | | WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); |
| | | request.setTradeType(WxPayConstants.TradeType.JSAPI); |
| | | request.setSubMchId(subMchId); |
| | | // 商户订单号 |
| | | request.setOutTradeNo(orderNo); |
| | | // 订单总金额 |
| | | Integer totalFee = order.getPayMoney().multiply(new BigDecimal(100)).intValue(); |
| | | request.setTotalFee(totalFee); |
| | | request.setProductId(orderNo); |
| | | request.setNotifyUrl(config.getNotifyUrl()); |
| | | request.setProfitSharing("Y"); |
| | | request.setOpenid(appPlaceOrderDto.getOpenid()); |
| | | request.setSpbillCreateIp(appPlaceOrderDto.getSpbillCreateIp()); |
| | | // 商品描述 body |
| | | String body = "商品购买"; |
| | | if(null != goods){ |
| | | body = goods.getGoodsName() + "-" + body; |
| | | } |
| | | request.setBody(body); |
| | | |
| | | request.checkAndSign(config); |
| | | |
| | | // 请求参数 |
| | | Gson gson = new Gson(); |
| | | String payRequestJson = gson.toJson(request); |
| | | |
| | | WxPayMpOrderResult result = wxService.createOrder(request); |
| | | |
| | | String payResponseJson = gson.toJson(request); |
| | | |
| | | // 支付相关信息返回 |
| | | appPlaceOrderVo.setAppId(result.getAppId()); |
| | | appPlaceOrderVo.setMchId(config.getMchId()); |
| | | appPlaceOrderVo.setTimeStamp(result.getTimeStamp()); |
| | | appPlaceOrderVo.setNonceStr(result.getNonceStr()); |
| | | appPlaceOrderVo.setPackageStr(result.getPackageValue()); |
| | | appPlaceOrderVo.setSignType(result.getSignType()); |
| | | appPlaceOrderVo.setPaySign(result.getPaySign()); |
| | | appPlaceOrderVo.setPrepayId(result.getPackageValue()); |
| | | |
| | | //减去优惠券 |
| | | if(StringUtils.isNotBlank(memberCouponSJ.toString())){ |
| | | remoteCouponService.useMemberCoupon(memberCouponSJ.toString()); |
| | | } |
| | | //生成自动取消订单延时任务 |
| | | Integer delayTime = 30; |
| | | if(redisService.hasKey(SecurityConstant.AUTO_CANCEL_ORDER_TIME)){ |
| | | delayTime = redisService.getCacheObject(SecurityConstant.AUTO_CANCEL_ORDER_TIME); |
| | | } |
| | | redisService.setCacheObject(DelayTaskEnum.ORDER_AUTOMATIC_CANCEL.getCode() + "-" + orderId, orderId, delayTime.longValue(), TimeUnit.MINUTES); |
| | | DelayTask delayTask = new DelayTask(); |
| | | delayTask.setDelFlag(0); |
| | | delayTask.setCreateTime(new Date()); |
| | | delayTask.setEndTime(DateUtils.addMinutes(new Date(),delayTime)); |
| | | delayTask.setRedisKey(DelayTaskEnum.ORDER_AUTOMATIC_CANCEL.getCode() + "-" + orderId); |
| | | remoteConfigService.addDelayTask(delayTask); |
| | | appPlaceOrderVo.setEndTime(DateUtils.addMinutes(order.getCreateTime(),delayTime)); |
| | | |
| | | |
| | | //减去优惠券 |
| | | if(memberCouponSJ!=null&&StringUtils.isNotBlank(memberCouponSJ.toString())){ |
| | | remoteCouponService.useMemberCoupon(memberCouponSJ.toString()); |
| | | // 保存支付订单统一下单日志 |
| | | paymentMessageService.savePaymentMessage("1", 0L, payRequestJson, payResponseJson); |
| | | |
| | | // 保存支付订单统一下单支付记录 |
| | | orderPaymentService.saveOrderPayment(userId, shopId, orderId, order.getPayMoney(), |
| | | appPlaceOrderVo.getEndTime(), "Y", appPlaceOrderDto.getOpenid(), |
| | | Joiner.on(";").join(goodsNameList),result.getPackageValue()); |
| | | |
| | | return appPlaceOrderVo; |
| | | } catch (WxPayException e){ |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | //生成自动取消订单延时任务 |
| | | Integer delayTime = 30; |
| | | if(redisService.hasKey(SecurityConstant.AUTO_CANCEL_ORDER_TIME)){ |
| | | delayTime = redisService.getCacheObject(SecurityConstant.AUTO_CANCEL_ORDER_TIME); |
| | | } |
| | | redisService.setCacheObject(DelayTaskEnum.ORDER_AUTOMATIC_CANCEL.getCode() + "-" + orderId, orderId, delayTime.longValue(), TimeUnit.MINUTES); |
| | | DelayTask delayTask = new DelayTask(); |
| | | delayTask.setDelFlag(0); |
| | | delayTask.setCreateTime(new Date()); |
| | | delayTask.setEndTime(DateUtils.addMinutes(new Date(),delayTime)); |
| | | delayTask.setRedisKey(DelayTaskEnum.ORDER_AUTOMATIC_CANCEL.getCode() + "-" + orderId); |
| | | remoteConfigService.addDelayTask(delayTask); |
| | | appPlaceOrderVo.setEndTime(DateUtils.addMinutes(order.getCreateTime(),delayTime)); |
| | | return appPlaceOrderVo; |
| | | } |
| | | |
| | | |
| | |
| | | merVerifyOrderVo.setUserMobile(member.getMobile()); |
| | | merVerifyOrderVo.setCloseFlag(order.getCloseFlag()); |
| | | // |
| | | if(order.getOrderFrom()==2){ |
| | | |
| | | //profitsSharingService.applyProfitSharing(); |
| | | if(order.getOrderFrom() == 2){ |
| | | // profitsSharingService.applyProfitSharing(); |
| | | } |
| | | |
| | | //创建支付记录 |
| | |
| | | payRecord.setPayTime(new Date()); |
| | | payRecord.setPayType(2); |
| | | payRecordService.save(payRecord); |
| | | return merVerifyOrderVo; |
| | | |
| | | // TODO 核销完成 开始分账(平台收取服务费) |
| | | OrderPayment payment = orderPaymentService.getByOrderId(orderId); |
| | | String transactionId = payment.getTransactionId(); |
| | | |
| | | String platformTyMacId = MyWxPayConfig.getPlatformTyMacId(); |
| | | |
| | | R<String> resultMch = remoteShopService.getShopSubMchId(order.getShopId()); |
| | | String subMchId = resultMch.getData(); |
| | | if(StringUtils.isEmpty(subMchId)){ |
| | | throw new ServiceException("获取微信商户号失败"); |
| | | } |
| | | |
| | | R<ShopProportionVo> resultShopProportion = remoteShopService.getShopProportion(order.getShopId()); |
| | | ShopProportionVo shopProportion = resultShopProportion.getData(); |
| | | if(null == shopProportion){ |
| | | throw new ServiceException("获取商户分成失败"); |
| | | } |
| | | |
| | | ProfitSharingRequest request = new ProfitSharingRequest(); |
| | | request.setSubMchId(subMchId); |
| | | request.setTransactionId(transactionId); |
| | | request.setOutOrderNo(order.getOrderNo()); |
| | | List<ProfitSharingReceiver> receiverList = new ArrayList<>(); |
| | | ProfitSharingReceiver receiver = new ProfitSharingReceiver(); |
| | | // TODO 分账 |
| | | // receiver.set |
| | | // request.setReceivers(); |
| | | |
| | | // platformTyMacId |
| | | // wxService.getProfitSharingV3Service().profitSharing() |
| | | |
| | | return merVerifyOrderVo; |
| | | } |
| | | |
| | | /** |