| | |
| | | package com.doumee.service.business.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | |
| | | import com.doumee.core.utils.Utils; |
| | | import com.doumee.core.wx.WxMiniConfig; |
| | | import com.doumee.core.wx.WxMiniUtilService; |
| | | import com.doumee.core.wx.WxPayProperties; |
| | | import com.doumee.dao.business.*; |
| | | import com.doumee.dao.business.MemberRidesMapper; |
| | | import com.doumee.dao.business.join.GoodsorderJoinMapper; |
| | |
| | | import com.doumee.service.business.MemberRidesService; |
| | | import com.doumee.service.business.PricingRuleService; |
| | | import com.doumee.service.business.RefundService; |
| | | import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; |
| | | import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest; |
| | | import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; |
| | | import com.github.binarywang.wxpay.exception.WxPayException; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiServiceExtension; |
| | | import com.wechat.pay.java.service.partnerpayments.jsapi.model.*; |
| | | import com.wechat.pay.java.service.refund.model.RefundNotification; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.checkerframework.checker.units.qual.A; |
| | |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class GoodsorderServiceImpl implements GoodsorderService { |
| | | |
| | | |
| | |
| | | closeGoodsorder( goodsorder,Constants.REFUND_TYPE.PLAT_FORCE.getKey()); |
| | | } |
| | | |
| | | /** |
| | | * 发起退款申请 |
| | | * @param goodsorder |
| | | * @param type |
| | | */ |
| | | @Transactional(rollbackFor = {Exception.class,BusinessException.class}) |
| | | @Override |
| | | public void closeGoodsorder( Goodsorder goodsorder ,int type ) { |
| | | |
| | | Refund r = refundMapper.selectOne(new QueryWrapper<Refund>().lambda() |
| | | .eq(Refund::getObjId,goodsorder.getId()) |
| | | .eq(Refund::getIsdeleted,Constants.ZERO) |
| | | .eq(Refund::getStatus,Constants.ZERO) |
| | | .last("limit 1" )); |
| | | if(r != null){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "该订单存在退款申请正在处理中,请稍后查看订单信息哦~"); |
| | | } |
| | | List<MemberRides> memberRides = getMemberRidesForClose(goodsorder.getId()); |
| | | //默认结算为押金金额 |
| | | BigDecimal closeMoney = new BigDecimal(0.00); |
| | |
| | | closeMoney = getCloseMoneyByRides(memberRides,true,goodsorder.getMoney()).getAmount(); |
| | | //实际结算价格,记录在最高车型记录上 |
| | | memberRides.get(0).setActualPrice(closeMoney); |
| | | } |
| | | int refundMoney =( Constants.formatDecimalNum(goodsorder.getMoney()).subtract(closeMoney)).intValue();//计算价格 |
| | | RefundDTO refundDTO = new RefundDTO(); |
| | | refundDTO.setOrderId(goodsorder.getId()); |
| | | refundDTO.setCanBalance(goodsorder.getMoney()); |
| | | refundDTO.setRefundAmount(new BigDecimal(refundMoney)); |
| | | refundDTO.setTotalAmount(goodsorder.getMoney()); |
| | | refundDTO.setMemberId(goodsorder.getMemberId()); |
| | | refundDTO.setReason(Constants.REFUND_TYPE.get(type).getName()); |
| | | refundDTO.setCreator(goodsorder.getEditor()); |
| | | refundDTO.setType(type); |
| | | wxMiniUtilService.wxRefund(refundDTO); |
| | | } |
| | | |
| | | /** |
| | | * 退款成功回调 |
| | | */ |
| | | @Transactional(rollbackFor = {Exception.class,BusinessException.class}) |
| | | @Override |
| | | public void closeGoodsorderDone(RefundNotification param) { |
| | | //查询退款申请订单 |
| | | Refund refund = refundMapper.selectById(param.getOutRefundNo()); |
| | | if(refund == null |
| | | || Constants.formatIntegerNum(refund.getIsdeleted()) == Constants.ONE |
| | | || Constants.formatIntegerNum(refund.getStatus()) == Constants.TWO){ |
| | | return; |
| | | } |
| | | if (!"SUCCESS".equals(param.getRefundStatus().name())) { |
| | | // 如果退款状态不正确,修改退款单状态 |
| | | refund.setStatus(Constants.ONE); |
| | | refund.setEditDate(new Date()); |
| | | refund.setOnlineOrderid(param.getTransactionId()); |
| | | refund.setDoneDate(refund.getEditDate()); |
| | | //更新退款单状态 |
| | | refundMapper.updateById(refund); |
| | | return; |
| | | } |
| | | refund.setOnlineOrderid(param.getTransactionId()); |
| | | doRefundTransactions(refund); |
| | | |
| | | Goodsorder goodsorder = goodsorderMapper.selectById(refund.getObjId()); |
| | | if(goodsorder ==null || Constants.formatIntegerNum(goodsorder.getCloseStatus()) != Constants.ZERO){ |
| | | return; |
| | | } |
| | | //进行订单结算业务处理 |
| | | List<MemberRides> memberRides = getMemberRidesForClose(refund.getObjId()); |
| | | //默认结算为押金金额 |
| | | if (!CollectionUtils.isEmpty(memberRides)){ |
| | | //骑行总时长 |
| | | for(MemberRides model :memberRides){ |
| | | MemberRides up = new MemberRides(); |
| | | up.setId(model.getId()); |
| | |
| | | memberRidesJoinMapper.updateById(up); |
| | | } |
| | | } |
| | | BigDecimal closeMoney =( Constants.formatDecimalNum(goodsorder.getMoney()).subtract(refund.getMoney())); |
| | | //退款乐行 |
| | | int type =Constants.formatIntegerNum(refund.getType()); |
| | | Goodsorder update = new Goodsorder(); |
| | | update.setId(goodsorder.getId()); |
| | | update.setStatus(Constants.GOODSORDER_STATUS.CLOSE.getKey()); |
| | | update.setCloseMoney(closeMoney); |
| | | update.setCloseDate(new Date()); |
| | | update.setCloseInfo(Constants.REFUND_TYPE.get(type).getInfo()); |
| | | update.setCloseInfo(Constants.REFUND_TYPE.get(refund.getType()).getInfo()); |
| | | update.setCloseUserId(goodsorder.getEditor()); |
| | | if(Constants.REFUND_TYPE.PLAT_FORCE.getKey() == type){ |
| | | //如果是强制结算 |
| | |
| | | //如果是平台自动结算 |
| | | update.setCloseType(Constants.TWO); |
| | | } |
| | | int refundMoney =( Constants.formatDecimalNum(goodsorder.getMoney()).subtract(closeMoney)).intValue();//计算价格 |
| | | if(refundMoney > 0){ |
| | | //如果有退款,申请微信退款,记录退款交易流水 |
| | | RefundDTO refundDTO = new RefundDTO(); |
| | | refundDTO.setOrderId(goodsorder.getId()); |
| | | refundDTO.setCanBalance(goodsorder.getMoney()); |
| | | refundDTO.setRefundAmount(new BigDecimal(refundMoney)); |
| | | refundDTO.setTotalAmount(goodsorder.getMoney()); |
| | | refundDTO.setMemberId(goodsorder.getMemberId()); |
| | | refundDTO.setReason(Constants.REFUND_TYPE.get(type).getName()); |
| | | refundDTO.setCreator(update.getEditor()); |
| | | refundDTO.setType(type); |
| | | Refund refund = wxMiniUtilService.wxRefund(refundDTO); |
| | | update.setCloseId(refund.getId()); |
| | | } |
| | | //存储 消费交易流水 |
| | | Transactions transactions = new Transactions(); |
| | | transactions.setId(Constants.getUUID()); |
| | |
| | | transactions.setCreateDate(new Date()); |
| | | transactions.setIsdeleted(Constants.ZERO); |
| | | transactions.setOrderId(goodsorder.getId()); |
| | | transactions.setMoney(goodsorder.getMoney().subtract(new BigDecimal(refundMoney))); |
| | | transactions.setMoney(goodsorder.getMoney().subtract(Constants.formatDecimalNum(refund.getMoney()))); |
| | | transactions.setType(Constants.TRANSACTIONS_TYPE.CONSUMPTION.getKey()); |
| | | transactions.setDoneDate(new Date()); |
| | | transactions.setTitle(Constants.TRANSACTIONS_TYPE.CONSUMPTION.getName()); |
| | |
| | | transactionsMapper.insert(transactions); |
| | | //修改订单信息 |
| | | goodsorderMapper.updateById(update); |
| | | } |
| | | |
| | | private void doRefundTransactions(Refund refund ) { |
| | | //存储交易流水表 |
| | | refund.setStatus(Constants.TWO); |
| | | refund.setEditDate(new Date()); |
| | | refund.setDoneDate(refund.getEditDate()); |
| | | //更新退款单状态 |
| | | refundMapper.updateById(refund); |
| | | |
| | | Transactions transactions = new Transactions(); |
| | | transactions.setId(Constants.getUUID()); |
| | | transactions.setMemberId(refund.getMemberId()); |
| | | transactions.setCreateDate(new Date()); |
| | | transactions.setIsdeleted(Constants.ZERO); |
| | | transactions.setOrderId(refund.getObjId()); |
| | | transactions.setMoney(refund.getMoney()); |
| | | transactions.setPreOrderid(refund.getObjId()); |
| | | transactions.setOnlineOrderid(refund.getId()); |
| | | transactions.setDoneDate(new Date()); |
| | | if(refund.getType().equals(Constants.REFUND_TYPE.PLAT_AUTO.getKey())||refund.getType().equals(Constants.REFUND_TYPE.PLAT_FORCE.getKey())){ |
| | | //平台自动退款 或 强制退款 |
| | | transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey()); |
| | | transactions.setTitle(Constants.REFUND_TYPE.PLAT_AUTO.getInfo()); |
| | | transactions.setContent(Constants.REFUND_TYPE.PLAT_AUTO.getInfo()); |
| | | }else if(refund.getType().equals(Constants.REFUND_TYPE.NORMAL.getKey())){ |
| | | //用户主动退款 |
| | | transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey()); |
| | | transactions.setTitle(Constants.REFUND_TYPE.NORMAL.getInfo()); |
| | | transactions.setContent(Constants.REFUND_TYPE.NORMAL.getInfo()); |
| | | }else if(refund.getType().equals(Constants.REFUND_TYPE.BACK.getKey())){ |
| | | //结算后退款 |
| | | transactions.setType(Constants.TRANSACTIONS_TYPE.REFUND.getKey()); |
| | | transactions.setTitle(Constants.REFUND_TYPE.BACK.getInfo()); |
| | | transactions.setContent(Constants.REFUND_TYPE.BACK.getInfo()); |
| | | } |
| | | transactions.setBalance(BigDecimal.ZERO); |
| | | transactions.setObjId(refund.getId()); |
| | | transactions.setObjType(Constants.ONE); |
| | | transactionsMapper.insert(transactions); |
| | | } |
| | | |
| | | private RidesDetailResponse getCloseMoneyByRides(List<MemberRides> memberRides,boolean isClose,BigDecimal yjMoney) { |
| | |
| | | refundDTO.setCreator(principal.getId()); |
| | | refundDTO.setReason(reason); |
| | | refundDTO.setType(Constants.REFUND_TYPE.BACK.getKey()); |
| | | Refund refund = wxMiniUtilService.wxRefund(refundDTO); |
| | | //发起退款申请 |
| | | wxMiniUtilService.wxRefund(refundDTO); |
| | | }else { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,退款金额不允许超过"+canBalance+"元"); |
| | | } |
| | |
| | | goodsorder.setPayWay(Constants.ZERO); |
| | | this.goodsorderMapper.insert(goodsorder); |
| | | //调起支付 |
| | | try { |
| | | Object response = null; |
| | | WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); |
| | | request.setBody("骑行押金"); |
| | | request.setAttach("createGoodsOrder"); |
| | | request.setOutTradeNo(goodsorder.getId()); |
| | | request.setTotalFee(goodsorder.getMoney().intValue()); |
| | | request.setSpbillCreateIp(this.getIpAddr()); |
| | | request.setTimeStart(DateUtil.DateToString(new Date(), "yyyyMMddHHmmss")); |
| | | request.setOpenid(member.getOpenid()); |
| | | response = WxMiniConfig.wxPayService.createOrder(request); |
| | | try { |
| | | response = getWxPayResponse(goodsorder,member.getOpenid()); |
| | | } catch (Exception e) { |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,发起支付失败~"); |
| | | } |
| | | |
| | | return response; |
| | | } catch (WxPayException e) { |
| | | e.printStackTrace(); |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),e.getMessage()); |
| | | } |
| | | |
| | | private Object getWxPayResponse(Goodsorder goodsorder,String openid) throws Exception { |
| | | //调起支付 |
| | | Object response = null; |
| | | PrepayRequest request = new PrepayRequest(); |
| | | request.setAttach("createGoodsOrder"); |
| | | request.setDescription("森林公园自行车骑行押金支付"); |
| | | request.setSpMchid(WxMiniConfig.wxProperties.getMchId()); |
| | | request.setSpAppid(WxMiniConfig.wxProperties.getAppId()); |
| | | request.setSubMchid(WxMiniConfig.wxProperties.getSubMchId()); |
| | | request.setSubAppid(WxMiniConfig.wxProperties.getSubAppId()); |
| | | Payer payer = new Payer(); |
| | | payer.setSubOpenid(openid); |
| | | request.setPayer(payer); |
| | | request.setOutTradeNo(goodsorder.getId()); |
| | | request.setNotifyUrl(WxMiniConfig.wxProperties.getNotifyUrl());//这个回调url必须是https开头的 |
| | | Amount amount = new Amount(); |
| | | amount.setTotal(goodsorder.getMoney().intValue()); |
| | | request.setAmount(amount); |
| | | // PrepayResponse res = WxMiniConfig.wxPayService.prepay(request); |
| | | // 跟之前下单示例一样,填充预下单参数 |
| | | PrepayWithRequestPaymentResponse resParam = WxMiniConfig.jsapiExtService.prepayWithRequestPayment(request,WxMiniConfig.wxProperties.getSubAppId()); |
| | | response =resParam; |
| | | |
| | | return response; |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 验证营业时间 |
| | |
| | | s.setEditDate(date); |
| | | s.setEditor(null); |
| | | s.setCloseDate(date); |
| | | |
| | | closeGoodsorder(s,Constants.REFUND_TYPE.PLAT_AUTO.getKey()); |
| | | }catch (BusinessException e){ |
| | | |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | |
| | | .eq("status",Constants.goodsorderStatus.pay) |
| | | .last(" limit 1 ")); |
| | | if(Objects.isNull(goodsorder)){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"无骑行订单记录"); |
| | | } |
| | | if(goodsorder == null ){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY ); |
| | | throw new BusinessException(ResponseStatus.NO_UNCLOSEED_ORDER.getCode(),"无骑行订单记录"); |
| | | } |
| | | if(Constants.formatIntegerNum(goodsorder.getStatus()) != Constants.GOODSORDER_STATUS.HAVING_PAY.getKey() ){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该订单当前状态不支持结算操作,请尝试刷新列表重试!" ); |
| | |
| | | public String payNotify(String preOrderId,String paymentNo){ |
| | | Goodsorder goodsorder = this.goodsorderMapper.selectById(preOrderId); |
| | | if(goodsorder.getStatus()==Constants.goodsorderStatus.pay){ |
| | | return WxPayNotifyResponse.success("处理成功!"); |
| | | return ("处理成功!"); |
| | | } |
| | | goodsorder.setOnlineOrderid(paymentNo); |
| | | goodsorder.setPayStatus(Constants.goodsorderPayStatus.pay); |
| | |
| | | transactions.setObjId(goodsorder.getId()); |
| | | transactions.setObjType(Constants.ZERO); |
| | | transactionsMapper.insert(transactions); |
| | | return WxPayNotifyResponse.success("处理成功!"); |
| | | return ("处理成功!"); |
| | | } |
| | | |
| | | |