1.修改小程序pay/index.js 直接重定向不使用微信支付
if (res.code === 1) {
// wx.requestPayment({
// nonceStr: res.data.nonceStr,
// package: res.data.packageStr,
// paySign: res.data.paySign,
// timeStamp: res.data.timeStamp,
// signType: res.data.signType,
// success: function (res) {
// wx.showModal({
// title: "提示",
// content: "支付成功",
// success: function () {
// uni.redirectTo({
// url:
// "/pages/success/index?orderId=" +
// _this.orderId,
// });
// },
// });
// console.log("支付成功!");
// },
// });
直接重定向不使用微信支付
uni.redirectTo({url: '/pages/success/index?orderId=' + _this.orderId });
}
2.注释调微信支付接口
小程序注释了微信中的支付处理代码,那么payment里面的vo也就没用了,就可以更新数据库里的支付状态和时间了
public OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) throws Exception {
//"requestPayment:fail parameter error: parameter.tim…rameter.paySign should be String instead of Null;"
// 当前登录用户id
Long userId = BaseContext.getCurrentId();
User user = userMapper.getById(userId);
/* //调用微信支付接口,生成预支付交易单
JSONObject jsonObject = weChatPayUtil.pay(
ordersPaymentDTO.getOrderNumber(), //商户订单号
new BigDecimal(0.01), //支付金额,单位 元
"苍穹外卖订单", //商品描述
user.getOpenid() //微信用户的openid
);*/
// if (jsonObject.getString("code") != null && jsonObject.getString("code").equals("ORDERPAID")) {
// throw new OrderBusinessException("该订单已支付");
// }
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "ORDERPAID");
//
OrderPaymentVO vo = jsonObject.toJavaObject(OrderPaymentVO.class);
// vo.setPackageStr(jsonObject.getString("package"));
//为替代微信支付成功后的数据库订单状态更新,多定义一个方法进行修改
Integer OrderPaidStatus = Orders.PAID; //支付状态,已支付
Integer OrderStatus = Orders.TO_BE_CONFIRMED; //订单状态,待接单
//发现没有将支付时间 check_out属性赋值,所以在这里更新
LocalDateTime check_out_time = LocalDateTime.now();
orderMapper.updateStatus(OrderStatus, OrderPaidStatus, check_out_time, ORDERID);
return vo;
}
3.因为我们在这里更新了订单数据嘛,所以要在orderMapper里定义updateStatus方法
@Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where id = #{id}")
void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, Long id);
通过修改以上三个文件即可跳过微信支付,将订单数据更新到数据库
(不过这里我还想赘述一下支付成功后发生了什么,因为很容易搞混paySuccess这个方法干了什么)
在支付成功后服务器会向前台提醒,通过websocket双向通信向前台传输数据,通过调用paySuccess参数订单id,更新了订单状态和时间
@PutMapping("/payment")
@ApiOperation("订单支付")
public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
log.info("订单支付:{}", ordersPaymentDTO);
OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
log.info("生成预支付交易单:{}", orderPaymentVO);
return Result.success(orderPaymentVO);
}
/**
* 支付成功回调
* nofity下的方法
* @param request
*/
@RequestMapping("/paySuccess")
public void paySuccessNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
//读取数据
String body = readData(request);
log.info("支付成功回调:{}", body);
//数据解密
String plainText = decryptData(body);
log.info("解密后的文本:{}", plainText);
JSONObject jsonObject = JSON.parseObject(plainText);
String outTradeNo = jsonObject.getString("out_trade_no");//商户平台订单号
String transactionId = jsonObject.getString("transaction_id");//微信支付交易号
log.info("商户平台订单号:{}", outTradeNo);
log.info("微信支付交易号:{}", transactionId);
//业务处理,修改订单状态、来单提醒
orderService.paySuccess(outTradeNo);
//给微信响应
responseToWeixin(response);
}
//orderServiceImpl下的paySuccess方法
/**
* 支付成功,修改订单状态
*
* @param outTradeNo
*/
public void paySuccess(String outTradeNo) {
// 当前登录用户id
Long userId = BaseContext.getCurrentId();
// 根据订单号查询当前用户的订单
Orders ordersDB = orderMapper.getByNumberAndUserId(outTradeNo, userId);
// 根据订单id更新订单的状态、支付方式、支付状态、结账时间
Orders orders = Orders.builder()
.id(ordersDB.getId())
.status(Orders.TO_BE_CONFIRMED)
.payStatus(Orders.PAID)
.checkoutTime(LocalDateTime.now())
.build();
orderMapper.update(orders);
//通过websocket向客户端浏览器推送消息 type orderId content
Map map = new HashMap();
map.put("type",1); // 1表示来单提醒 2表示客户催单
map.put("orderId",ordersDB.getId());
map.put("content","订单号:" + outTradeNo);
String json = JSON.toJSONString(map);
webSocketServer.sendToAllClient(json);
}