-
Notifications
You must be signed in to change notification settings - Fork 626
微信退款
sqmax edited this page Sep 18, 2018
·
1 revision
退款的API比较简单,只需要请求这个接口https://api.mch.weixin.qq.com/secapi/pay/refund
,请求体如下:
<xml>
<appid>wx2421b1c4370ec43b</appid>
<mch_id>10000100</mch_id>
<nonce_str>6cefdb308e1e2e8aabd48cf79e546a02</nonce_str>
<out_refund_no>1415701182</out_refund_no>
<out_trade_no>1415757673</out_trade_no>
<refund_fee>1</refund_fee>
<total_fee>1</total_fee>
<transaction_id></transaction_id>
<sign>FE56DD4AA85C0EECA82C35595A69E153</sign>
</xml>
可参见官方文档申请退款。
使用SDK方式对上面的请求接口请求体都作了封装,只需动态传入一些参数就行了,如下:
/**
* 退款
* @param orderDTO
*/
@Override
public RefundResponse refund(OrderDTO orderDTO) {
RefundRequest refundRequest=new RefundRequest();
refundRequest.setOrderId(orderDTO.getOrderId());
refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
log.info("【微信退款】 request={}",JsonUtil.toJson(refundRequest));
RefundResponse refundResponse=bestPayService.refund(refundRequest);
log.info("【微信退款】 response={}",JsonUtil.toJson(refundResponse));
return refundResponse;
}
这个方法在取消订单的业务方法里被调用,如下:
@Override
@Transactional
public OrderDTO cancel(OrderDTO orderDTO) {
OrderMaster orderMaster=new OrderMaster();
// BeanUtils.copyProperties(orderDTO,orderMaster);
//判断订单状态
if(!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())){
log.error("【取消订单】订单状态不正确, orderId={},orderStatus={}",orderDTO.getOrderId(),orderDTO.getOrderStatus());
throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
}
//修改订单状态
orderDTO.setOrderStatus(OrderStatusEnum.CANCLE.getCode());
BeanUtils.copyProperties(orderDTO,orderMaster);
OrderMaster updateResult=orderMasterRepository.save(orderMaster);
if(updateResult==null){
log.error("【取消订单】更新失败,orderMaster={}",orderMaster);
throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
}
//返还库存
if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
log.error("【取消订单】订单中无商品详情,orderDTO={}",orderDTO);
throw new SellException(ResultEnum.ORDER_DETAIL_EMPTY);
}
List<CartDTO> cartDTOList=orderDTO.getOrderDetailList().stream()
.map(e->new CartDTO(e.getProductId(),e.getProductQuantity()))
.collect(Collectors.toList());
productService.increaseStock(cartDTOList);
//如果已支付,需要退款
if(orderDTO.getPayStatus().equals(PayStatusEnum.SUCCESS.getCode())){
payService.refund(orderDTO);
}
return orderDTO;
}
目录