package com.doumee.core.wx; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.utils.ID; import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest; import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; import com.github.binarywang.wxpay.exception.WxPayException; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; /** * 微信小程序-公共方法 */ @Service @Slf4j public class WxMiniUtilService { /** * 订单微信退款 * orderNo:商户订单号 * totalPrice:订单总金额 * refundPrice;退款金额 */ @Transactional(rollbackFor = Exception.class) public static String wxRefund(String orderNo, BigDecimal totalPrice, BigDecimal refundPrice) { try { // 发送退款请求 String refNum = ID.nextGUID(); WxPayRefundRequest request = new WxPayRefundRequest(); request.setOutTradeNo(orderNo); request.setOutRefundNo(refNum); // request.setTotalFee(2); // request.setRefundFee(1); request.setTotalFee(BaseWxPayRequest.yuanToFen(totalPrice.toString())); request.setRefundFee(BaseWxPayRequest.yuanToFen(refundPrice.toString())); WxPayRefundResult response = WxMiniConfig.wxPayService.refund(request); if ("SUCCESS".equals(response.getReturnCode()) && "SUCCESS".equals(response.getResultCode())) { return refNum; } else { throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),response.getErrCode() + response.getErrCodeDes()); } } catch (WxPayException e) { e.printStackTrace(); } throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"退款发生异常请联系管理员"); } }