WeChat 애플릿 결제 기능 개발 오류

高洛峰
풀어 주다: 2017-02-25 09:20:34
원래의
1339명이 탐색했습니다.

위챗 미니 프로그램 결제 기능 개발 오류 요약

위챗 미니 프로그램 결제가 드디어 함정을 제거하고 꽤 큰 함정이 있음을 발견하여 포스팅하겠습니다. 지금이고 앞으로도 관심 있는 학생들이 한 번 살펴보시기 바랍니다:

https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php? Chapter=7_4&index=2

문서를 보시면 업무프로세스를 보실 수 있습니다. 첫 번째 함정은 사용자의 openid를 얻는 것입니다. 매개변수는 URL 연결에 철자를 입력해야 합니다. 그렇지 않으면 {"errcode":40013,"errmsg":"잘못된 appid, 힌트: [ req_id: iil1ba0504ns86 ]"가 보고됩니다. }오류

 onLoad: function () {
  var that = this
  wx.login({
   success: function (res) {
    if (res.code) {
     //发起网络请求
     wx.request({
      url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxaacf22345345cfc7162fe3&secret=83ebd41c3e6f34a49b3a34578063434548ff3f71&js_code=' + res.code + '&grant_type=authorization_code',
      method: "POST",
      success: function (res) {
       that.setData({
        openid: res.data.openid
       })
      }
     })
    } else {
     console.log('获取用户登录态失败!' + res.errMsg)
    }
   }
  });
 }
로그인 후 복사

두 번째 함정은 결제 및 서명을 위한 통합 주문 인터페이스입니다. 이 함정은 더 많은 사람들이 문제를 겪는 이유입니다. 암호화는 종종 다음과 같이 사용됩니다. 서명 도구의 암호화된 서명이 다릅니다

서명 암호화 도구 주소: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php? Chapter=20_1

서명 및 암호화 시 utf-8로 변환해야 합니다. 암호화를 위해 자체 인터페이스를 사용하여 Digest.update(data.getBytes("utf-8"));

 // 统一下单接口获取sign(签名)
 paysignjsapi: function (appid, attach, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type, key) {
  var self = this;
  //加密签名
  wx.request({
   url: 'http://localhost:8080/XinXingWXApi/wxXcxApi/Md5Encrypt.do',
   method: 'GET',
   data: {
    appid: appid,
    attach: attach,
    body: body,
    mch_id: mch_id,
    nonce_str: nonce_str,
    notify_url: notify_url,
    openid: openid,
    out_trade_no: out_trade_no,
    spbill_create_ip: spbill_create_ip,
    total_fee: total_fee,
    trade_type: trade_type,
    key: key
   },
   //统一下单
   success: function (res) {
    var sign = res.data.strMd5
    var formData = "<xml>"
    formData += "<appid>" + appid + "</appid>" //appid 
    formData += "<attach>" + attach + "</attach>" //附加数据 
    formData += "<body>" + body + "</body>"    //标题
    formData += "<mch_id>" + mch_id + "</mch_id>" //商户号 
    formData += "<nonce_str>" + nonce_str + "</nonce_str>" //随机字符串,不长于32位。 
    formData += "<notify_url>" + notify_url + "</notify_url>" //异步接收微信支付结果通知的回调地址
    formData += "<openid>" + openid + "</openid>"  //用户Id
    formData += "<out_trade_no>" + out_trade_no + "</out_trade_no>" //商户订单号
    formData += "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>"
    formData += "<total_fee>" + total_fee + "</total_fee>" //金额
    formData += "<trade_type>" + trade_type + "</trade_type>"  //公共号支付
    formData += "<sign>" + sign + "</sign>"//签名
    formData += "</xml>"
로그인 후 복사

반환 데이터 분석 xml

 //请求统一下单接口
    wx.request({
     url: "https://api.mch.weixin.qq.com/pay/unifiedorder",
     method: &#39;POST&#39;,
     data: formData,
     success: function (data) {
      wx.request({
       url: "http://localhost:8080/XinXingWXApi/wxXcxApi/xmlAnalyze.do?strXml=" + data.data,
       method: &#39;POST&#39;,
       success: function (res) {
        var pk = &#39;prepay_id=&#39; + res.data.prepayId;
        var timeStamp = self.createTimeStamp();
        //获取支付签名,并支付
        self.getsignType(appid, timeStamp, nonce_str, pk, "MD5", key);
       }
      })
     }
    })
   }
  });
 }
로그인 후 복사

세 번째는 통화 결제입니다. 여기에도 몇 가지 함정이 있습니다. 첫 번째는 appid로 작성하면 작동하지 않는다는 것입니다. 두 번째는 prepay_id=wx2017011711060194dccf725232155886323일 때 결제 서명 오류가 보고된다는 것입니다. 또한 서명 인터페이스로 이동하여 서명이 일치하는지, 매개변수가 올바른지 확인해야 합니다. WeChat 결제를 호출할 때 appId

getsignType: function (appid, timeStamp, nonce_str, pk, signType, key) {
  var that = this;
  wx.request({
   url: "http://localhost:8080/XinXingWXApi/wxXcxApi/getSignType.hn",
   method: &#39;GET&#39;,
   data: {
    appId: appid,
    timeStamp: timeStamp,
    nonceStr: nonce_str,
    pk: pk,
    signType: signType,
    key: key
   },
   success: function (res) {
    console.log(res.data.paySign)
    var paySign = res.data.paySign
    //调用微信支付
    wx.requestPayment({
     &#39;appId&#39;: appid,
     &#39;timeStamp&#39;: timeStamp,
     &#39;nonceStr&#39;: nonce_str,
     &#39;package&#39;: pk,
     &#39;signType&#39;: &#39;MD5&#39;,
     &#39;paySign&#39;: paySign,
     &#39;success&#39;: function (res) {
      console.log(res);
      console.log(&#39;success&#39;);
     },
     &#39;fail&#39;: function (res) {
      console.log(res);
      console.log(&#39;fail&#39;);
     },
     &#39;complete&#39;: function (res) {
      // console.log(res);
      console.log(&#39;complete&#39;);
     }
    });
   }
  })
 }
로그인 후 복사

읽어주셔서 감사합니다. 모두에게 도움이 되기를 바랍니다. 이 사이트를 지원해 주셔서 감사합니다!

위챗 애플릿 결제 기능 개발 오류 관련 더 많은 글은 PHP 중국어 홈페이지를 주목해주세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!