1、查询支付状态接口
(1)在TPayLogController 中添加查询支付状态方法。
java
@ApiOperation(value = "查询支付状态")
@GetMapping("queryOrderStatus/{orderNo}")
public R queryOrderStatus(@PathVariable String orderNo){
//1调用接口,查询订单状态
Map<String,String> map = payLogService.queryPayStatus(orderNo);
//2判断支付返回结果是否成功
if(map.get("trade_state").equals("SUCCESS")){//支付成功
//3支付成功,修改订单状态,插入支付日志
payLogService.updateOrderStatus(map);
return R.ok().message("支付成功");
}
return R.ok().code(25000).message("支付中");
}
(2)在TpayLogService中添加接口方法
java
Map<String, String> queryPayStatus(String orderNo);
void updateOrderStatus(Map<String, String> map);
(3)在TPayLogServiceImpl实现接口方法
java
//调用接口,查询订单状态
@Override
public Map<String, String> queryPayStatus(String orderNo) {
try {
//1封装参数
Map m = new HashMap<>();
m.put("appid", "wx74862e0dfcf69954");
m.put("mch_id", "1558950191");
m.put("out_trade_no", orderNo);
m.put("nonce_str", WXPayUtil.generateNonceStr());
//2设置请求、发送请求
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
client.setXmlParam(WXPayUtil.generateSignedXml(m,"T6m9iK73b0kn9g5v426MKfHQH7X8rKwb"));
client.setHttps(true);
client.post();
//3获取微信反馈结果xml
String content = client.getContent();
System.out.println("支付结果返回="+content);
//4转化xml成map
Map<String, String> map = WXPayUtil.xmlToMap(content);
return map;
} catch (Exception e) {
e.printStackTrace();
throw new GuliException(20001,"支付失败");
}
}
//支付成功,修改订单状态,插入支付日志
@Override
public void updateOrderStatus(Map<String, String> map) {
//1修改订单状态status改为1
//1.1根据订单号查询订单
QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
wrapper.eq("order_no",map.get("out_trade_no"));
TOrder order = orderService.getOne(wrapper);
//1.2判断订单
if(order==null){
throw new GuliException(20001,"订单失效");
}
//1.3更改状态
order.setStatus(1);//已支付
orderService.updateById(order);
//2支付日志表插入日志
TPayLog payLog = new TPayLog();
payLog.setOrderNo(order.getOrderNo());//支付订单号
payLog.setPayTime(new Date());
payLog.setPayType(1);//支付类型
payLog.setTotalFee(order.getTotalFee());//总金额(分)
payLog.setTradeState(map.get("trade_state"));//支付状态
payLog.setTransactionId(map.get("transaction_id"));
payLog.setAttr(JSONObject.toJSONString(map));
baseMapper.insert(payLog);//插入到支付日志表
}