美文网首页
RN 微信支付

RN 微信支付

作者: Bana | 来源:发表于2018-09-17 15:29 被阅读143次

    1.使用库:react-native-wechat
    2.请参考 github 搭建微信开发环境
    3.首页注册 appID

    WeChat.registerApp(WXAppId);
    

    4.调用微信 api。统一下单接口,获取到 预支付id,后RN调用WeChat的pay

    new Date().getTime() 方法在ios正式版会异常,最好用moment插件 moment().unix()

     //const currentTimestamp = parseInt((new Date().getTime())/1000);
    const currentTimestamp = moment().unix();
        let noceStr = Math.random().toString().split('.')[1]
        let signStr = `appid=${WXAppId}&noncestr=${noceStr}&package=Sign=WXPay`
          + `&partnerid=${SHId}&prepayid=${payData.prepayId}`
          + `&timestamp=${currentTimestamp}`
          + `&key=${SHKey}`;
        let signMd5 = md5.hex_md5(signStr).toUpperCase();
    
    
        console.log("微信支付签名信息:" + signStr)
        console.log("微信支付签名:" + signMd5)
        let payObject = {
          appId: WXAppId,  //appid
          partnerId: SHId,   //商户号
          prepayId: payData.prepayId, //商家预支付id
          nonceStr: noceStr,  //随机字符串
          timeStamp: currentTimestamp, //时间戳
          package: "Sign=WXPay", //商家指定签名
          sign: signMd5 //签名
        }
        console.log("微信支付信息:" + JSON.stringify(payObject))
        WeChat.pay(payObject).then((success) => {
          console.log("支付成功" + success)
          DeviceEventEmitter.emit("GetOrderCount")
          Toast.success('支付成功');
        }).catch((error) => {
          if (error.errCode == -2) {
            Toast.fail('支付失败:用户取消');
          }else{
            Toast.fail('支付失败');
          }
        })
    

    PS:时间戳这个东西 精确到秒,不是毫秒!!!
    currentTimestamp 千万不能调用toString() 方法,因为在ios正式版会有闪退的

    相关文章

      网友评论

          本文标题:RN 微信支付

          本文链接:https://www.haomeiwen.com/subject/nbolnftx.html