Day8:用户下单、微信支付
- Day8:用户下单、微信支付
- a. 用户下单
- b. 微信支付
Day8:用户下单、微信支付
a. 用户下单
创建OrderController并提供用户下单方法:
/*** 用户下单* @param ordersSubmitDTO* @return*/
@PostMapping("/submit")
@ApiOperation("用户下单")
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){log.info("用户下单,参数为:{}",ordersSubmitDTO);OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);return Result.success(orderSubmitVO);
}
创建OrderServiceImpl,并实现用户下单方法,及其父类接口
@Service
public class OrderServiceImpl implements OrderService {@Autowiredprivate OrderMapper orderMapper;@Autowiredprivate OrderDetailMapper orderDetailMapper;@Autowiredprivate AddressBookMapper addressBookMapper;@Autowiredprivate ShoppingCartMapper shoppingCartMapper;/*** 用户下单* @param ordersSubmitDTO* @return*/@Overridepublic OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {// 处理各种业务异常(地址为空,购物车数据为空)AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());if (addressBook == null) {throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);}// 查询当前用户的购物车数据Long userId = BaseContext.getCurrentId();ShoppingCart shoppingCart = new ShoppingCart();shoppingCart.setUserId(userId);List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);if (shoppingCartList == null || shoppingCartList.size() == 0){throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);}// 向订单表插入1条数据Orders orders = new Orders();BeanUtils.copyProperties(ordersSubmitDTO, orders);orders.setOrderTime(LocalDateTime.now());orders.setPayStatus(Orders.UN_PAID);orders.setStatus(Orders.PENDING_PAYMENT);orders.setNumber(String.valueOf(System.currentTimeMillis()));orders.setPhone(addressBook.getPhone());orders.setConsignee(addressBook.getConsignee());orders.setUserId(userId);orderMapper.insert(orders);List<OrderDetail> orderDetailList = new ArrayList<>();// 向订单明细表插入n条数据for (ShoppingCart cart : shoppingCartList) {OrderDetail orderDetail = new OrderDetail(); // 订单明细BeanUtils.copyProperties(cart, orderDetail);orderDetail.setOrderId(orders.getId()); // 设置当前订单明细关联的订单idorderDetailList.add(orderDetail);}orderDetailMapper.insertBatch(orderDetailList);// 下单成功,清空用户购物车shoppingCartMapper.deleteByUserId(userId);// 封装VO返回结果OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder().id(orders.getId()).orderTime(orders.getOrderTime()).orderNumber(orders.getNumber()).orderAmount(orders.getAmount()).build();return orderSubmitVO;}
}
创建OrderMapper接口和对应的xml映射文件:
// OrderMapper.java
@Mapper
public interface OrderMapper {/*** 插入订单数据* @param orders*/void insert(Orders orders);
}// OrderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.OrderDetailMapper"><insert id="insertBatch">insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount)VALUES<foreach collection="orderDetailList" item="od" separator=",">(#{od.name}, #{od.image}, #{od.orderId}, #{od.dishId}, #{od.setmealId}, #{od.dishFlavor}, #{od.number}, #{od.amount})</foreach></insert>
</mapper>
创建OrderDetailMapper接口和对应的xml映射文件:
// OrderDetailMapper.java
@Mapper
public interface OrderDetailMapper {/*** 批量插入订单明细数据* @param orderDetailList*/void insertBatch(List<OrderDetail> orderDetailList);
}// OrderDetailMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.OrderDetailMapper"><insert id="insertBatch">insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount)VALUES<foreach collection="orderDetailList" item="od" separator=",">(#{od.name}, #{od.image}, #{od.orderId}, #{od.dishId}, #{od.setmealId}, #{od.dishFlavor}, #{od.number}, #{od.amount})</foreach></insert>
</mapper>
b. 微信支付
注意:个人小程序无法申请微信支付功能
获取微信支付平台证书、商户私钥文件:
这两个文件需在小程序网站上下载
微信支付相关配置:
在application.yml中,写入相关读取配置,并在application-dev.yml中写入具体参数
# application.yml
sky: wechat:appid: ${sky.wechat.appid}secret: ${sky.wechat.secret}mchid: ${sky.wechat.mchid}mchSerialNo: ${sky.wechat.mchSerialNo}privateKeyFilePath: ${sky.wechat.privateKeyFilePath}apiV3Key: ${sky.wechat.apiV3Key}weChatPayCertFilePath: ${sky.wechat.weChatPayCertFilePath}notifyUrl: ${sky.wechat.notifyUrl}refundNotifyUrl: ${sky.wechat.refundNotifyUrl}