一、创建支付模块和准备

1、在service模块下创建子模块service_order
image.png
2、在service_order模块中引入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.github.wxpay</groupId>
  4. <artifactId>wxpay-sdk</artifactId>
  5. <version>0.0.3</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>fastjson</artifactId>
  10. </dependency>
  11. </dependencies>

3、创建支付相关的表
image.png
4、使用代码生成器生成相关代码
image.png
5、编写application.properties配置文件

# 服务端口
server.port=8007
# 服务名
spring.application.name=service-order

# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath:com/atguigu/orderservice/mapper/xml/*.xml

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

#开启熔断机制
feign.hystrix.enabled=true
# 设置hystrix超时时间,默认1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000

二、开发创建订单接口

1、编写订单controller

@RestController
@RequestMapping("/orderservice/order")
@CrossOrigin
public class TOrderController {


    @Autowired
    private TOrderService orderService;
    //根据课程id和用户id创建订单,返回订单id
    @PostMapping("createOrder/{courseId}")
    public R save(@PathVariable String courseId, HttpServletRequest request) {
        String orderId = orderService.saveOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderId);
    }

}

2、在service_edu创建接口

(1)实现根据课程id获取课程信息,返回课程信息对象

//根据课程id查询课程信息
@GetMapping("getDto/{courseId}")
public com.atguigu.commonutils.vo.CourseInfoForm getCourseInfoDto(@PathVariable String courseId) {
        CourseInfoForm courseInfoForm = courseService.getCourseInfo(courseId);
        com.atguigu.commonutils.vo.CourseInfoForm courseInfo = new com.atguigu.commonutils.vo.CourseInfoForm();
        BeanUtils.copyProperties(courseInfoForm,courseInfo);
        return courseInfo;

}

3、在service_ucenter创建接口

(1)实现用户id获取用户信息,返回用户信息对象

//根据token字符串获取用户信息
@PostMapping("getInfoUc/{id}")
public com.atguigu.commonutils.vo.UcenterMember getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        com.atguigu.commonutils.vo.UcenterMember memeber = new com.atguigu.commonutils.vo.UcenterMember();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;

}

4、编写订单service
(1)在service_order模块创建接口,实现远程调用
image.png
EduClient

@Component
@FeignClient("service-edu")
public interface EduClient {
    //根据课程id查询课程信息
    @GetMapping("/eduservice/course/getDto/{courseId}")
    public com.atguigu.commonutils.vo.CourseInfoForm getCourseInfoDto(@PathVariable("courseId") String courseId);

}

UcenterClient

@Component
@FeignClient("service-ucenter")
public interface UcenterClient {
    //根据课程id查询课程信息
    @PostMapping("/ucenterservice/member/getInfoUc/{id}")
    public com.atguigu.commonutils.vo.UcenterMember getInfo(@PathVariable("id") String id);

}



(2)在service_order模块编写创建订单service

@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {


    @Autowired
    private EduClient eduClient;


    @Autowired
    private UcenterClient ucenterClient;


    //创建订单
    @Override
    public String saveOrder(String courseId, String memberId) {
        //远程调用课程服务,根据课程id获取课程信息
        CourseInfoForm courseDto = eduClient.getCourseInfoDto(courseId);

        //远程调用用户服务,根据用户id获取用户信息
        UcenterMember ucenterMember = ucenterClient.getInfo(memberId);


        //创建订单
        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName("test");
        order.setTotalFee(courseDto.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterMember.getMobile());
        order.setNickname(ucenterMember.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);


        return order.getOrderNo();
    }

}

三、开发获取订单接口

1、在订单controller创建根据id获取订单信息接口

  @GetMapping("getOrder/{orderId}")
  public R get(@PathVariable String orderId) {
      QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
      wrapper.eq("order_no",orderId);
      TOrder order = orderService.getOne(wrapper);
      return R.ok().data("item", order);
    }