苍穹外卖-day08-订单支付-跳过微信支付

由于微信支付需要商家认证才能开通,个人开发者只能模拟支付的流程,完成后续项目的学习。

解决思路:

后端: 点击 支付 -->调用支付成功接口-直接更新数据库订单状态

小程序: 点击 支付 -->直接跳转支付成功页面

1.修改 orderServiceImpl下的payment方法

跳过微信支付接口:
注释原有调用微信支付接口的代码
生成空JSON替换原接口生成预支付交易单
在这里插入图片描述
具体代码如下:

    public OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        // 当前登录用户id
        Long userId = BaseContext.getCurrentId();
        User user = userMapper.getById(userId);

//        //调用微信支付接口,生成预支付交易单
//        JSONObject jsonObject = weChatPayUtil.pay(
//                ordersPaymentDTO.getOrderNumber(), //商户订单号
//                new BigDecimal(0.01), //支付金额,单位 元
//                "苍穹外卖订单", //商品描述
//                user.getOpenid() //微信用户的openid
//        );

        //生成空JSON,跳过微信支付流程
        JSONObject jsonObject = new JSONObject();

        if (jsonObject.getString("code") != null && jsonObject.getString("code").equals("ORDERPAID")) {
            throw new OrderBusinessException("该订单已支付");
        }

        OrderPaymentVO vo = jsonObject.toJavaObject(OrderPaymentVO.class);
        vo.setPackageStr(jsonObject.getString("package"));
/*      // 模拟支付成功,更新数据库订单状态
        // 根据订单id更新订单的状态、支付方式、支付状态、结账时间
        // 根据订单号查询订单
        Orders ordersDB = orderMapper.getByNumberAndUserId(ordersPaymentDTO.getOrderNumber(), userId);
        Orders orders = Orders.builder()
                .id(ordersDB.getId())
                .status(Orders.TO_BE_CONFIRMED)
                .payStatus(Orders.PAID)
                .checkoutTime(LocalDateTime.now())
                .build();
        orderMapper.update(orders);*/

        return vo;
    }

2.修改OrderController

调用orderService.paySuccess方法,传入订单号ordersPaymentDTO.getOrderNumber()
直接更新数据库订单状态,模拟支付成功在这里插入图片描述
具体代码如下:

    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付:{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单:{}", orderPaymentVO);
        // 模拟支付成功,更新数据库订单状态 -此时没有回调
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success(orderPaymentVO);
    }

3.修改微信小程序代码

修改 pages.pay.index.js 文件,点击支付后直接跳转支付成功页面
搜索 handleSave方法
注释原有支付代码,补充为直接调用支付成功代码
在这里插入图片描述
在这里插入图片描述
具体代码如下:

  methods: _objectSpread(_objectSpread({},
  (0, _vuex.mapState)(['orderData'])), {}, {
    // 支付详情
    handleSave: function handleSave() {var _this = this;
      if (this.timeout) {
        (0, _api.cancelOrder)(this.orderId).then(function (res) {
        });
        uni.redirectTo({
          url: '/pages/details/index?orderId=' + this.orderId });

      } else {
        // 如果支付成功进入成功页
        clearTimeout(this.times);
        var params = {
          orderNumber: this.orderDataInfo.orderNumber,
          payMethod: this.activeRadio === 0 ? 1 : 2 };

        (0, _api.paymentOrder)(params).then(function (res) {
          if (res.code === 1) {
       //直接调用支付成功页面
       wx.showModal({
          title: '提示',
          content: '支付成功',
          success:function(){
            uni.redirectTo({url: '/pages/success/index?orderId=' + _this.orderId });
          }
        })
        console.log('支付成功!')

            // 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 });

          } else {
            wx.showModal({
              title: '提示',
              content: res.msg
            })
          }
        });
      }

    },

订单支付效果如图:
在这里插入图片描述在这里插入图片描述

至此,就可以愉快的接着做后续的项目啦~

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值