| | |
| | | import com.github.binarywang.wxpay.service.WxPayService; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.util.IpUtil; |
| | | import com.sandu.common.util.SpringContextHolder; |
| | | import com.sandu.ximon.admin.dto.wxquerydto.WxOrderDto; |
| | | import com.sandu.ximon.admin.pay.wxpay.UsrWxPayConfigService; |
| | | import com.sandu.ximon.admin.service.C3mOrderService; |
| | | import com.sandu.ximon.dao.domain.C3mOrder; |
| | | import com.sandu.ximon.dao.domain.WxConfigEntity; |
| | | import com.sandu.ximon.dao.enums.OrderStatus; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import me.chanjar.weixin.common.error.WxErrorException; |
| | |
| | | |
| | | /** |
| | | * 退款 |
| | | * |
| | | * @param totalAmount |
| | | * @param refundAmount |
| | | * @param outTradeNo |
| | | * @param poleId |
| | | * @return |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean refund(Double totalAmount, Double refundAmount, String outTradeNo, Long poleId) { |
| | | WxConfigEntity wxConfig = UsrWxPayConfigService.getBean().getConfigByPoleId(poleId); |
| | | public boolean refund(C3mOrder c3mOrder) { |
| | | |
| | | // WxPayRefundRequest refundRequest = new WxPayRefundRequest(); |
| | | // refundRequest.setOutTradeNo(orderId); |
| | | // refundRequest.setOutRefundNo(refundSn); |
| | | // refundRequest.setRefundFee(refundFee); |
| | | // refundRequest.setTotalFee(refundFee); |
| | | // refundRequest.setNotifyUrl(callbackUrl); |
| | | // try { |
| | | // WxPayRefundResult wxPayRefundResult = wxPayService.refund(refundRequest); |
| | | // if (WxPayConstants.ResultCode.SUCCESS.equals(wxPayRefundResult.getResultCode())) { |
| | | // return true; |
| | | // } else { |
| | | // log.error("微信退款失败,{}", wxPayRefundResult.getErrCodeDes()); |
| | | // throw new BusinessException("微信退款失败," + wxPayRefundResult.getErrCodeDes()); |
| | | // } |
| | | // } catch (WxPayException e) { |
| | | // log.error("微信退款失败,{}", e.getErrCodeDes()); |
| | | // throw new BusinessException("微信退款失败," + e.getErrCodeDes()); |
| | | // } |
| | | // |
| | | |
| | | |
| | | WxConfigEntity wxConfig = wxPayConfigService.getConfigByPoleId(c3mOrder.getPoleId()); |
| | | |
| | | if (wxConfig.getKeyPath() == null && wxConfig.getKeyPath().isEmpty()) { |
| | | throw new BusinessException("微信证书不存在,无法进行退款"); |
| | | } |
| | | //设置微信支付参数 |
| | | WxPayConfig payConfig = new WxPayConfig(); |
| | | |
| | | payConfig.setAppId(StringUtils.trimToNull(wxConfig.getAppappid())); |
| | | payConfig.setMchId(StringUtils.trimToNull(wxConfig.getMchId())); |
| | | payConfig.setMchKey(StringUtils.trimToNull(wxConfig.getPrivateKey())); |
| | | payConfig.setSubAppId(StringUtils.trimToNull(null)); |
| | | payConfig.setSubMchId(StringUtils.trimToNull(null)); |
| | | payConfig.setKeyPath(StringUtils.trimToNull(wxConfig.getKeyPath())); |
| | | wxPayService.setConfig(payConfig); |
| | | |
| | | |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String refundNo = sdf.format(new Date()); |
| | | WxPayRefundRequest refundRequest = new WxPayRefundRequest(); |
| | | //退款金额不能大于订单总金额 |
| | | if (refundAmount > totalAmount) { |
| | | if (c3mOrder.getRefundAmount() > c3mOrder.getTotalAmount()) { |
| | | throw new BusinessException("退款金额不能大于订单总金额"); |
| | | } |
| | | refundRequest.setOutTradeNo(outTradeNo); |
| | | System.out.println("" + c3mOrder.getRefundAmount()); |
| | | refundRequest.setOutTradeNo(c3mOrder.getOutTradeNo()); |
| | | refundRequest.setOutRefundNo(refundNo); |
| | | refundRequest.setTotalFee(Integer.valueOf(parseWxAmount((totalAmount + "")))); |
| | | refundRequest.setRefundFee(Integer.valueOf(parseWxAmount((refundAmount + "")))); |
| | | refundRequest.setTotalFee(Integer.valueOf(parseWxAmount((c3mOrder.getTotalAmount() + "")))); |
| | | refundRequest.setRefundFee(Integer.valueOf(parseWxAmount((c3mOrder.getRefundAmount() + "")))); |
| | | try { |
| | | WxPayRefundResult wxPayRefundResult = wxPayService.refund(refundRequest); |
| | | if (WxPayConstants.ResultCode.SUCCESS.equals(wxPayRefundResult.getResultCode())) { |
| | | c3mOrder.setOrderStatus(OrderStatus.REFUND.getStatus()); |
| | | c3mOrder.setChargingStates(1); |
| | | SpringContextHolder.getBean(C3mOrderService.class).updateById(c3mOrder); |
| | | return true; |
| | | } else { |
| | | log.error("微信退款失败,{}", wxPayRefundResult.getErrCodeDes()); |