|  |  | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.utils.ID; | 
 |  |  | import com.doumee.dao.business.ActionLogMapper; | 
 |  |  | import com.doumee.dao.business.RefundMapper; | 
 |  |  | import com.doumee.dao.business.TransactionsMapper; | 
 |  |  | import com.doumee.dao.business.model.Refund; | 
 |  |  | import com.doumee.dao.business.model.Transactions; | 
 |  |  | import com.doumee.dao.business.web.request.RefundDTO; | 
 |  |  | import com.doumee.service.business.RefundService; | 
 |  |  | import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest; | 
 |  |  | import com.github.binarywang.wxpay.bean.request.WxPayDownloadBillRequest; | 
 |  |  | import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; | 
 |  |  | import com.github.binarywang.wxpay.bean.result.WxPayBillResult; | 
 |  |  | import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; | 
 |  |  | import com.github.binarywang.wxpay.exception.WxPayException; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.checkerframework.checker.units.qual.A; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  | import org.yaml.snakeyaml.scanner.Constant; | 
 |  |  |  | 
 |  |  | import java.math.BigDecimal; | 
 |  |  | import java.security.KeyFactory; | 
 |  |  | import java.security.Signature; | 
 |  |  | import java.security.spec.PKCS8EncodedKeySpec; | 
 |  |  | import java.util.Base64; | 
 |  |  | import java.util.Date; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 微信小程序-公共方法 | 
 |  |  | 
 |  |  | @Slf4j | 
 |  |  | public class WxMiniUtilService { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private RefundMapper refundMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SystemDictDataBiz systemDictDataBiz; | 
 |  |  |     private TransactionsMapper transactionsMapper; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 订单微信退款 | 
 |  |  |      * orderNo:商户订单号 | 
 |  |  |      * totalPrice:订单总金额 | 
 |  |  |      * refundPrice;退款金额 | 
 |  |  |      */ | 
 |  |  |  | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public static String wxRefund(String orderNo, BigDecimal totalPrice, BigDecimal refundPrice) { | 
 |  |  |     public Refund wxRefund(RefundDTO refundDTO) { | 
 |  |  |         try { | 
 |  |  |             // 发送退款请求 | 
 |  |  |             String refNum = ID.nextGUID(); | 
 |  |  |             WxPayRefundRequest request = new WxPayRefundRequest(); | 
 |  |  |             request.setOutTradeNo(orderNo); | 
 |  |  |             request.setOutTradeNo(refundDTO.getOrderId()); | 
 |  |  |             request.setOutRefundNo(refNum); | 
 |  |  |            // request.setTotalFee(2); | 
 |  |  |           //  request.setRefundFee(1); | 
 |  |  |             request.setTotalFee(BaseWxPayRequest.yuanToFen(totalPrice.toString())); | 
 |  |  |             request.setRefundFee(BaseWxPayRequest.yuanToFen(refundPrice.toString())); | 
 |  |  | //            request.setTotalFee(BaseWxPayRequest.yuanToFen(refundDTO.getTotalAmount().toString())); | 
 |  |  | //            request.setRefundFee(BaseWxPayRequest.yuanToFen(refundDTO.getRefundAmount().toString())); | 
 |  |  |             System.out.println("实际总金额" + BaseWxPayRequest.yuanToFen(refundDTO.getTotalAmount().toString())); | 
 |  |  |             System.out.println("实际退款金额" + BaseWxPayRequest.yuanToFen(refundDTO.getRefundAmount().toString())); | 
 |  |  |  | 
 |  |  |             request.setTotalFee(1); | 
 |  |  |             request.setRefundFee(1); | 
 |  |  |             WxPayRefundResult response = WxMiniConfig.wxPayService.refund(request); | 
 |  |  |             if ("SUCCESS".equals(response.getReturnCode()) && "SUCCESS".equals(response.getResultCode())) { | 
 |  |  |                 return refNum; | 
 |  |  |             } else { | 
 |  |  |                 //存储退款记录 与 流水记录 | 
 |  |  |                 Refund refund = new Refund(); | 
 |  |  |                 refund.setId(Constants.getUUID()); | 
 |  |  |                 refund.setCreateDate(new Date()); | 
 |  |  |                 refund.setMemberId(refundDTO.getMemberId()); | 
 |  |  |                 refund.setMoney(refundDTO.getRefundAmount()); | 
 |  |  |                 refund.setOnlineOrderid(refNum); | 
 |  |  |                 refund.setPayWay(Constants.ZERO); | 
 |  |  |                 refund.setDoneDate(new Date()); | 
 |  |  |                 refund.setType(refundDTO.getType()); | 
 |  |  |                 refund.setObjId(refundDTO.getOrderId()); | 
 |  |  |                 refund.setReason(refundDTO.getReason()); | 
 |  |  |                 refund.setCanBalance(refundDTO.getCanBalance()); | 
 |  |  |                 refundMapper.insert(refund); | 
 |  |  |                 //存储交易流水表 | 
 |  |  |                 Transactions transactions = new Transactions(); | 
 |  |  |                 transactions.setId(Constants.getUUID()); | 
 |  |  |                 transactions.setMemberId(refundDTO.getMemberId()); | 
 |  |  |                 transactions.setCreateDate(new Date()); | 
 |  |  |                 transactions.setIsdeleted(Constants.ZERO); | 
 |  |  |                 transactions.setOrderId(refundDTO.getOrderId()); | 
 |  |  |                 transactions.setMoney(refundDTO.getRefundAmount()); | 
 |  |  |                 transactions.setType(refundDTO.getType()==Constants.TRANSACTIONS_TYPE.REFUND.getKey()?Constants.TRANSACTIONS_TYPE.PLATFORMREFUND.getKey():Constants.REFUND_TYPE.BACK.getKey()); | 
 |  |  |                 transactions.setPreOrderid(refundDTO.getOrderId()); | 
 |  |  |                 transactions.setOnlineOrderid(refNum); | 
 |  |  |                 transactions.setDoneDate(new Date()); | 
 |  |  |                 transactions.setTitle(Constants.TRANSACTIONS_TYPE.get(transactions.getType()).getName()); | 
 |  |  |                 transactions.setContent(Constants.TRANSACTIONS_TYPE.get(transactions.getType()).getInfo()); | 
 |  |  |                 transactions.setBalance(BigDecimal.ZERO); | 
 |  |  |                 transactions.setObjId(refund.getId()); | 
 |  |  |                 transactions.setObjType(Constants.ONE); | 
 |  |  |                 transactionsMapper.insert(transactions); | 
 |  |  |                 return refund; | 
 |  |  |             } else{ | 
 |  |  |                 throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),response.getErrCode() + response.getErrCodeDes()); | 
 |  |  |             } | 
 |  |  |         } catch (WxPayException e) { | 
 |  |  | 
 |  |  |         } | 
 |  |  |         throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"退款发生异常请联系管理员"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |