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

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; package com.sky.controller.user;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO; import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.Result; import com.sky.result.Result;
import com.sky.service.OrderService; import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO; import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@Slf4j @Slf4j
@@ -32,4 +31,21 @@ public class OrderController {
OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO); OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
return Result.success(orderSubmitVO); 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.Insert;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Update;
import java.time.LocalDateTime;
@Mapper @Mapper
public interface OrderMapper { public interface OrderMapper {
@@ -16,4 +19,8 @@ public interface OrderMapper {
"#{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})") "#{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
@Options(useGeneratedKeys = true,keyProperty = "id") @Options(useGeneratedKeys = true,keyProperty = "id")
void insert(Orders orders); 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})") "values (#{openid},#{name},#{phone},#{sex},#{idNumber},#{avatar},#{createTime})")
@Options(useGeneratedKeys = true,keyProperty = "id")//主键回显得到新用户的主键id @Options(useGeneratedKeys = true,keyProperty = "id")//主键回显得到新用户的主键id
void insert(User user); 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; package com.sky.service;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO; import com.sky.dto.OrdersSubmitDTO;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO; import com.sky.vo.OrderSubmitVO;
public interface OrderService { public interface OrderService {
@@ -10,4 +12,11 @@ public interface OrderService {
* @return * @return
*/ */
OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO); OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO);
/**
* 支付功能
* @param ordersPaymentDTO
* @return
*/
OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) throws Exception;
} }

View File

@@ -1,22 +1,20 @@
package com.sky.service.impl; package com.sky.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant; import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext; import com.sky.context.BaseContext;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO; import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook; import com.sky.entity.*;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.exception.AddressBookBusinessException; import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.ShoppingCartBusinessException; import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.AddressBookMapper; import com.sky.mapper.*;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.OrderService; import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO; import com.sky.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -39,6 +37,11 @@ public class OrderServiceImpl implements OrderService {
@Autowired @Autowired
private OrderDetailMapper orderDetailMapper; private OrderDetailMapper orderDetailMapper;
@Autowired
private UserMapper userMapper;
private Orders orders;//用来记录对应要支付的订单
/** /**
* 用户下单 * 用户下单
* @param ordersSubmitDTO * @param ordersSubmitDTO
@@ -73,6 +76,8 @@ public class OrderServiceImpl implements OrderService {
orderMapper.insert(orders); orderMapper.insert(orders);
this.orders = orders;
//3.向订单明细表插入N条商品明细记录 //3.向订单明细表插入N条商品明细记录
ArrayList<OrderDetail> orderDetails = new ArrayList<>();//订单明细集合 ArrayList<OrderDetail> orderDetails = new ArrayList<>();//订单明细集合
for (ShoppingCart shoppingCart : cartList) { for (ShoppingCart shoppingCart : cartList) {
@@ -97,4 +102,26 @@ public class OrderServiceImpl implements OrderService {
.orderTime(orders.getOrderTime()).build(); .orderTime(orders.getOrderTime()).build();
return submitVO; 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;
}
} }

View File

@@ -18,3 +18,10 @@ sky:
wechat: wechat:
appid: wx3a8ed8d8e9c9552a appid: wx3a8ed8d8e9c9552a
secret: 8ada060abc5326be3a446a75df4c77b3 secret: 8ada060abc5326be3a446a75df4c77b3
mchid: 1561414331
mchSerialNo: 4B3B3DC35414AD50B1B755BAF8DE9CC7CF407606
apiV3Key: CZBK51236435wxpay435434323FFDuv3
#通知程序支付成功接口
notifyUrl: https://393bae3e.r12.vip.cpolar.cn/notify/paySuccess
#通知程序退款成功接口
refundNotifyUrl: https://393bae3e.r12.vip.cpolar.cn/notify/refundSuccess

View File

@@ -52,3 +52,8 @@ sky:
wechat: wechat:
appid: ${sky.wechat.appid} appid: ${sky.wechat.appid}
secret: ${sky.wechat.secret} secret: ${sky.wechat.secret}
mchid: ${sky.wechat.mchid}
mchSerialNo: ${sky.wechat.mchSerialNo}
apiV3Key: ${sky.wechat.apiV3Key}
notifyUrl: ${sky.wechat.notifyUrl}
refundNotifyUrl: ${sky.wechat.refundNotifyUrl}