苍穹外卖--处理支付功能的操作

This commit is contained in:
2025-11-25 14:50:26 +08:00
parent bafbea94df
commit 0ce2d90e9a
7 changed files with 87 additions and 13 deletions

View File

@@ -1,18 +1,17 @@
package com.sky.controller.user;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
@RestController
@Slf4j
@@ -32,4 +31,21 @@ public class OrderController {
OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
return Result.success(orderSubmitVO);
}
/**
* 订单支付
* 返回支付参数给小程序,让小程序端能调起微信支付 第8步
* @param ordersPaymentDTO
* @return
*/
@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);
}
}

View File

@@ -4,6 +4,9 @@ import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Update;
import java.time.LocalDateTime;
@Mapper
public interface OrderMapper {
@@ -16,4 +19,8 @@ public interface OrderMapper {
"#{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
@Options(useGeneratedKeys = true,keyProperty = "id")
void insert(Orders orders);
//处理支付pem问题
@Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus},checkout_time = #{checkOutTime} where id = #{id}")
void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, Long id);
}

View File

@@ -18,4 +18,7 @@ public interface UserMapper {
"values (#{openid},#{name},#{phone},#{sex},#{idNumber},#{avatar},#{createTime})")
@Options(useGeneratedKeys = true,keyProperty = "id")//主键回显得到新用户的主键id
void insert(User user);
@Select("select * from user where id = #{id} ")
User getById(Long id);
}

View File

@@ -1,6 +1,8 @@
package com.sky.service;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
public interface OrderService {
@@ -10,4 +12,11 @@ public interface OrderService {
* @return
*/
OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO);
/**
* 支付功能
* @param ordersPaymentDTO
* @return
*/
OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) throws Exception;
}

View File

@@ -1,22 +1,20 @@
package com.sky.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.entity.*;
import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.mapper.*;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -39,6 +37,11 @@ public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDetailMapper orderDetailMapper;
@Autowired
private UserMapper userMapper;
private Orders orders;//用来记录对应要支付的订单
/**
* 用户下单
* @param ordersSubmitDTO
@@ -73,6 +76,8 @@ public class OrderServiceImpl implements OrderService {
orderMapper.insert(orders);
this.orders = orders;
//3.向订单明细表插入N条商品明细记录
ArrayList<OrderDetail> orderDetails = new ArrayList<>();//订单明细集合
for (ShoppingCart shoppingCart : cartList) {
@@ -97,4 +102,26 @@ public class OrderServiceImpl implements OrderService {
.orderTime(orders.getOrderTime()).build();
return submitVO;
}
/**
* 订单支付
*
* @param ordersPaymentDTO
* @return
*/
public OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) throws Exception {
// 当前登录用户id
Long userId = BaseContext.getCurrentId();
User user = userMapper.getById(userId);
//处理支付pem问题
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; //订单状态,待接单
LocalDateTime check_out_time = LocalDateTime.now(); //更新支付时间
orderMapper.updateStatus(OrderStatus, OrderPaidStatus, check_out_time, this.orders.getId());
return vo;
}
}