目录:
(1)预约挂号-取消预约-接口开发
(2)预约挂号-取消预约-前端整合测试
(1)预约挂号-取消预约-接口开发
一:需求描述
取消订单分两种情况:
二:开发微信退款接口
参考文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
该接口需要使用证书,详情参考文档并下载证书
请下载的证书放在service-order模块/resources/cert文件夹下
在application.properties文件配置证书路径
weixin.cert= C:\\yygh_parent\\service\\service_order\\src\\main\\resources\\cert\\apiclient_cert.p12 |
首先在模块中引入证书:
然后再配置文件中配置证书的路径:
#微信退款证书
weixin.cert=D:\\AnZhuang\\Java项目\\尚医通代码\\yygh_parent\\service\\service_order\\src\\main\\resources\\cert\\apiclient_cert.p12
下面书写接口:更新表的信息:
第一个订单表,支付记录,退款表refund_info的信息:
退款我们是根据支付记录发起退款的
在PaymentService接口:书写获取支付记录的接口:
实现类实现:
/**
* 获取支付记录
* @param orderId
* @param paymentType
* @return
*/
@Override
public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("order_id",orderId);
wrapper.eq("payment_type",paymentType);
PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
return paymentInfo;
}
退款的实体类:
package com.atguigu.yygh.model.order;
import com.atguigu.yygh.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
/**
*
* RefundInfo
*
*
* @author qy
*/
@Data
@ApiModel(description = "RefundInfo")
@TableName("refund_info")
public class RefundInfo extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "对外业务编号")
@TableField("out_trade_no")
private String outTradeNo;
@ApiModelProperty(value = "订单编号")
@TableField("order_id")
private Long orderId;
@ApiModelProperty(value = "支付类型(微信 支付宝)")
@TableField("payment_type")
private Integer paymentType;
@ApiModelProperty(value = "交易编号")
@TableField("trade_no")
private String tradeNo;
@ApiModelProperty(value = "退款金额")
@TableField("total_amount")
private BigDecimal totalAmount;
@ApiModelProperty(value = "交易内容")
@TableField("subject")
private String subject;
@ApiModelProperty(value = "退款状态")
@TableField("refund_status")
private Integer refundStatus;
@ApiModelProperty(value = "回调时间")
@TableField("callback_time")
private Date callbackTime;
@ApiModelProperty(value = "回调信息")
@TableField("callback_content")
private String callbackContent;
}
添加退款记录
创建退款表的Mapper:RefundInfoMapper
package com.atguigu.yygh.order.mapper;
import com.atguigu.yygh.model.order.RefundInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface RefundInfoMapper extends BaseMapper {
}
RefundInfoService 接口:
package com.atguigu.yygh.order.service;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.model.order.RefundInfo;
import com.baomidou.mybatisplus.extension.service.IService;
public interface RefundInfoService extends IService {
/**
* 保存退款记录
* @param paymentInfo
*/
RefundInfo saveRefundInfo(PaymentInfo paymentInfo);
}
实现类:
package com.atguigu.yygh.order.service.impl;
import com.atguigu.yygh.enums.RefundStatusEnum;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.model.order.RefundInfo;
import com.atguigu.yygh.order.mapper.RefundInfoMapper;
import com.atguigu.yygh.order.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class RefundInfoServiceImpl
extends ServiceImpl implements RefundInfoService {
//保存退款记录
@Override
public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
//判断是否有重复数据添加
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("order_id",paymentInfo.getOrderId());
wrapper.eq("payment_type",paymentInfo.getPaymentType());
RefundInfo refundInfo = baseMapper.selectOne(wrapper);
if(refundInfo != null) {//有相同数据
return refundInfo;
}
//没有重复记录,添加记录
refundInfo = new RefundInfo();
refundInfo.setCreateTime(new Date());
refundInfo.setOrderId(paymentInfo.getOrderId());
refundInfo.setPaymentType(paymentInfo.getPaymentType());
refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
refundInfo.setSubject(paymentInfo.getSubject());
refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
baseMapper.insert(refundInfo);
return refundInfo;
}
}
1、在WeixinService添加接口
实现类:
//微信退款
@Override
public Boolean refund(Long orderId) {
try {
//获取支付记录信息
PaymentInfo paymentInfo = paymentService.getPaymentInfo(orderId, PaymentTypeEnum.WEIXIN.getStatus());
//添加信息到退款记录表
RefundInfo refundInfo = refundInfoService.saveRefundInfo(paymentInfo);
//判断当前订单数据是否已经退款
if(refundInfo.getRefundStatus().intValue() == RefundStatusEnum.REFUND.getStatus().intValue()) {
return true;
}
//还未退款,调用微信接口实现退款
//封装需要参数
Map paramMap = new HashMap<>();
paramMap.put("appid",ConstantPropertiesUtils.APPID); //公众账号ID
paramMap.put("mch_id",ConstantPropertiesUtils.PARTNER); //商户编号
paramMap.put("nonce_str",WXPayUtil.generateNonceStr());
paramMap.put("transaction_id",paymentInfo.getTradeNo()); //微信订单号
paramMap.put("out_trade_no",paymentInfo.getOutTradeNo()); //商户订单编号
paramMap.put("out_refund_no","tk"+paymentInfo.getOutTradeNo()); //商户退款单号
//实际开发的金额应该根据订单而确定,这里写死1分钱
//paramMap.put("total_fee",paymentInfoQuery.getTotalAmount().multiply(new BigDecimal("100")).longValue()+"");
// paramMap.put("refund_fee",paymentInfoQuery.getTotalAmount().multiply(new BigDecimal("100")).longValue()+"");
paramMap.put("total_fee","1");
paramMap.put("refund_fee","1");
String paramXml = WXPayUtil.generateSignedXml(paramMap,ConstantPropertiesUtils.PARTNERKEY);
//设置调用退款接口内容
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/secapi/pay/refund");
client.setXmlParam(paramXml);
client.setHttps(true);
//设置证书信息
client.setCert(true);
client.setCertPassword(ConstantPropertiesUtils.PARTNER);
client.post();
//接收返回数据
String xml = client.getContent();
Map resultMap = WXPayUtil.xmlToMap(xml);
if (null != resultMap && WXPayConstants.SUCCESS.equalsIgnoreCase(resultMap.get("result_code"))) {
refundInfo.setCallbackTime(new Date());
refundInfo.setTradeNo(resultMap.get("refund_id"));
refundInfo.setRefundStatus(RefundStatusEnum.REFUND.getStatus());
refundInfo.setCallbackContent(JSONObject.toJSONString(resultMap));
refundInfoService.updateById(refundInfo);
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
加一个证书的属性:
package com.atguigu.yygh.order.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConstantPropertiesUtils implements InitializingBean {
@Value("${weixin.appid}")
private String appid;
@Value("${weixin.partner}")
private String partner;
@Value("${weixin.partnerkey}")
private String partnerkey;
@Value("${weixin.cert}")
private String cert;
public static String APPID;
public static String PARTNER;
public static String PARTNERKEY;
public static String CERT;
@Override
public void afterPropertiesSet() throws Exception {
APPID = appid;
PARTNER = partner;
PARTNERKEY = partnerkey;
CERT = cert;
}
}
修改HttpClient类:
在OrderApiController中添加:继续添加方法:
//取消预约
@GetMapping("auth/cancelOrder/{orderId}")
public Result cancelOrder(@PathVariable Long orderId) {
Boolean isOrder = orderService.cancelOrder(orderId);
return Result.ok(isOrder);
}
OrderService接口:添加
//取消预约
Boolean cancelOrder(Long orderId);
医院那端的接口:
实现类:
//取消预约
@Override
public Boolean cancelOrder(Long orderId) {
//获取订单信息
OrderInfo orderInfo = baseMapper.selectById(orderId);
//判断是否取消
DateTime quitTime = new DateTime(orderInfo.getQuitTime());
if(quitTime.isBeforeNow()) {
throw new HospitalException(ResultCodeEnum.CANCEL_ORDER_NO);
}
//调用医院接口实现预约取消
SignInfoVo signInfoVo = hospitalFeignClient.getSignInfoVo(orderInfo.getHoscode());
if(null == signInfoVo) {
throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
}
Map reqMap = new HashMap<>();
reqMap.put("hoscode",orderInfo.getHoscode());
reqMap.put("hosRecordId",orderInfo.getHosRecordId());
reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
reqMap.put("sign", sign);
/* JSONObject result = HttpRequestHelper.sendRequest(reqMap,
signInfoVo.getApiUrl()+"/order/updateCancelStatus");*/
System.err.println(signInfoVo.getApiUrl()+"/order/updateCancelStatus");
JSONObject result = HttpRequestHelper.sendRequest(reqMap,
"http://localhost:9998/order/updateCancelStatus");
//根据医院接口返回数据
if(result.getInteger("code")!=200) {
throw new HospitalException(result.getString("message"), ResultCodeEnum.FAIL.getCode());
} else {
//判断当前订单是否可以取消 可以的话调用微信退款方法
if(orderInfo.getOrderStatus().intValue() == OrderStatusEnum.PAID.getStatus().intValue()) {
Boolean isRefund = weixinService.refund(orderId);
if(!isRefund) {
throw new HospitalException(ResultCodeEnum.CANCEL_ORDER_FAIL);
}
//更新订单状态
orderInfo.setOrderStatus(OrderStatusEnum.CANCLE.getStatus());
baseMapper.updateById(orderInfo);
//发送mq更新预约数量
OrderMqVo orderMqVo = new OrderMqVo();
orderMqVo.setScheduleId(orderInfo.getScheduleId());
//短信提示
MsmVo msmVo = new MsmVo();
msmVo.setPhone(orderInfo.getPatientPhone());
String reserveDate = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd") + (orderInfo.getReserveTime()==0 ? "上午": "下午");
Map param = new HashMap(){{
put("title", orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle());
put("reserveDate", reserveDate);
put("name", orderInfo.getPatientName());
}};
msmVo.setParam(param);
orderMqVo.setMsmVo(msmVo);
rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_ORDER, MqConst.ROUTING_ORDER, orderMqVo);
}
}
return true;
}
(2)预约挂号-取消预约-前端整合测试
在orderInfo.js中继续添加:
在show.vue:继续添加访问方法:
点击取消预约:
微信成功退款。
订单表状态变成-1:已经取消了
支付记录表:已经退款
退款记录表也成功添加记录: