About calling the WeChat payment function and other precautions in uniapp

Look at the code first and copy and use it. Of course, it may not be universal. It is recommended that you read it and sort out your ideas. In general, the ideas are very important, followed by the code.

(Prior to this, you must first make sure that the main body of your Mini Program is a merchant before you can initiate WeChat Pay)

<script>
  //就一般思路而言  您首先将要做的是 根据商品订单或者其他信息创建一个订单
  // 比如创建订单接口({activity_id:_this.activity_id})
  // 然后调用你们后台的支付接口等待返回数据   如下
  export default {
    
    
  	name: 'paymoney',
    data(){
    
    
      return{
    
    
        Appid:'这是您的Appid',
        orderid:'订单id'
      }
    },
    methods:{
    
    
      payMoney(){
    
    
        OrderPay({
    
    
          order_id: this.orderid
        }).then(res => {
    
    
          if (res.code == 1) {
    
    
            //判断接口正确后调用如下方法  requestPayment  其中有六个参数
            uni.requestPayment({
    
    
              appId: _this.Appid,   //您的appid
              timeStamp: res.data.timeStamp,  //时间戳 ,由后台返回
              nonceStr: res.data.nonceStr,//随机字符串,由后台返回
              package: res.data.package, //统一下单的参数
              // 签名算法 一般是MD5 这里使用三目运算意思的
              // 后端返回signType了吗?返回了用返回的。 : 没有返回 用MD5
              signType: res.data.signType ? res.data.signType : 'MD5',
              paySign: res.data.paySign,  //支付签名
              success(res) {
    
    
                //支付成功
                wx.showToast({
    
    
                  title: '支付成功',
                  icon: 'none',
                  duration: 2000,
                })
              },
              fail(err) {
    
    
                //支付失败
                wx.showToast({
    
    
                  title: err.msg,
                  icon: 'none',
                  duration: 500,
                })
              }
            })
          }
        })
      }
    }
  }
</script>

Additional and useful information that may appear has been commented.
Other questions about uniapp or areas that you do not understand this method can leave a message, I will reply and help you solve it as soon as possible.

Guess you like

Origin blog.csdn.net/weixin_47821281/article/details/108605734