DaZeng:小程序支付调用

18 篇文章 0 订阅
2 篇文章 0 订阅

点击支付按钮

<button class="user-btn" bindtap="allowInv">支付</button>
  allowInv: async function () {
    const { payFee } = this.data
    this.setData({ loading: true })
    try {
      if (payFee > 0) { 
      	//需要支付费用的处理...
        const oid = await this.createOrder()
        const payData = await this.invokeOrder(oid)
        await this.payOrder(payData)
        await this.getOrderResult(oid)
        this.toShop()
      } else {
      	//不需要支付费用的处理...
      }
    } catch (ex) {
      //失败
      if (ex.message) {
        //错误信息处理
        app.showErrorMsg(ex.message)
      }
    }
    this.setData({ loading: false })
  },

支付处理

在这里插入图片描述

  • serviceApp.getMutation是自己封装的请求调用函数,可自定义修改为自己的接口请求,主要就是去接收后台res的返回值
  createOrder: async function () {
    const { userId, levelId } = this.data
    //需要会费
    const res = await throwServiceMutation(gql_create, { levelId, sellerId: userId })
    console.log('创建订单', res);
    return res.wxAppPay.create
  },
  invokeOrder: async function (oid) {
    const res = await throwServiceMutation(gql_invoke, { oid })
    console.log('创建微信支付需要的信息', res);
    const script = res.wxAppPay.invoke.script
    const data = script && JSON.parse(script)
    return data
  },
  payOrder: async function (options) {
    return new Promise((resolve, reject) => {
      const { nonceStr, paySign, signType, timestamp } = options
      wx.requestPayment({
        /** 随机字符串,长度为32个字符以下 */
        nonceStr: nonceStr,
        /** 统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*** */
        package: options.package,
        /** 签名,具体见微信支付文档 */
        paySign: paySign,
        /** 时间戳,从 1970 年 1 月 1 日 00:00:00 至今的秒数,即当前的时间 */
        timeStamp: timestamp.toString(),
        signType: signType,
        success: (res) => {
          console.log('支付成功', res);
          resolve(res)
        },
        fail: (err) => {
          reject(new Error('支付失败'))
        }
      })
    })
  },
  getOrderResult: async function (oid) {
    this.invokeResultCount++   /**支付接口调用次数从0开始,在Page中声明 */
    let res = await serviceApp.getMutation({
      mutation: gql_result,
      variables: { oid },
      hideError: true
    })
    console.log('获取结果', res);
    if (isErrorCode(res, 101)) { //没有拿到结果间隔2s再去获取支付结果 - 最多调用10次
      if (this.invokeResultCount < 10) {
        await setTimeoutAsync(() => { }, 2000)
        res = await this.getOrderResult(oid)
      }
    } else if (!res || res.errors) {
      throw new Error('获取支付结果失败')
    }
    return res
 },
  toShop: function () {
    wx.redirectTo({
      url: `xxxx`,
      success: () => {
        wx.showToast('支付完成,加入成功')
      }
    })
  }
--------------------------------------------
async function throwServiceMutation(mutation, variables) {
  const res = await serviceApp.getMutation({
    mutation, variables, hideError: true
  })
  if (res && !res.errors) {
    return res
  } else {
    throw new Error(err)
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Da Zeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值