WeChat applet payment function development error

Source: Internet
Author: User
This article mainly introduces the summary of errors in the development of the mini-program payment function. For more information, see Summary of the development error of the mini-program payment function

The small program payment has finally completed the pitfall, and found that the pitfall is quite big. now I am posting a post, hoping that students who will enter the pitfall will have a look:

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

You can see the business process here when reading the document. The first pitfall is to obtain the user's openid. the parameter must be set to url connection. otherwise, {"errcode": 40013, "errmsg": "invalid appid, hints: [req_id: iil1ba0504ns86] "} error

OnLoad: function () {var that = this wx. login ({success: function (res) {if (res. code) {// initiate a network request 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 ('failed to get the user logon status! '+ Res. errMsg )}}});}

The second pitfall is the unified payment order interface. the signature pitfall is a problem that many people encounter. MD5 encryption is often different from the encrypted signature in the signature tool.

Signature encryption tool address: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php? Chapter = 20_1

When the signature is encrypted, it needs to be converted to utf-8. I use my own interface to encrypt digest. update (data. getBytes ("UTF-8 "));

// Obtain the sign (signature) paysignjsapi through the unified order interface: function (appid, attach, body, mch_id, nonce_str, yy_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type, key) {var self = this; // encrypt the signature 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, policy_url: policy_url, openid: openid, out_trade_no: out_trade_no, spbill_create_ip: spbill_create_ip, total_region: total_region, trade_type: trade_type, key: key}, // unified order success: function (res) {var sign = res. data. strMd5 var formData ="
 
  
"FormData + =" "+ appid +""// Appid formData + =" "+ attach +""// Add data formData + =""+ Body +""// Title formData + ="
  
   
"+ Mch_id +"
  "// Merchant ID formData + ="
  
   
"+ Nonce_str +"
  "// Random string, no longer than 32 characters. FormData + ="
  
   
"+ Notify_url +"
  "// Callback address for receiving the notification of the payment result asynchronously formData + ="
  
   
"+ Openid +"
  "// User ID formData + ="
  
   
"+ Out_trade_no +"
  "// Merchant Order No. formData + ="
  
   
"+ Spbill_create_ip +"
  "FormData + ="
  
   
"+ Total_detail +"
  "// Amount formData + ="
  
   
"+ Trade_type +"
  "// Public account payment formData + ="
  
   
"+ Sign +"
  "// Signature formData + ="
 "

Returned data parsing xml

// Request the unified order interface wx. request ({url: "https://api.mch.weixin.qq.com/pay/unifiedorder", method: 'post', data: formData, success: function (data) {wx. request ({url: "http: // localhost: 8080/XinXingWXApi/wxXcxApi/xmlAnalyze. do? StrXml = "+ data. data, method: 'post', success: function (res) {var pk = 'prepay _ id = '+ res. data. prepayId; var timeStamp = self. createTimeStamp (); // Obtain the payment signature and pay for self. getsignType (appid, timeStamp, nonce_str, pk, "MD5", key );}})}})}});}

The third is to call the payment, and there are also a few traps here. The first is that many appId statements cannot be written as appid, the second is to write the preoatid parameter format to prepay_id = wx2017011711060194dccf725232155886323. The third is to report the payment signature error when calling the payment. you also need to go to the signature interface to check whether the signature is consistent, check whether the parameter is correct. you must add the appId when calling the payment.

GetsignType: function (appid, timeStamp, nonce_str, pk, signType, key) {var that = this; wx. request ({url: "http: // localhost: 8080/XinXingWXApi/wxXcxApi/getSignType. hn ", method: 'GET', 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 // call to pay wx. requestPayment ({'appid ': appId, 'timestamp': timeStamp, 'noncestr': nonce_str, 'Package': pk, 'signtype': 'md5', 'paysign ': paySign, 'success': function (res) {console. log (res); console. log ('success');}, 'failed': function (res) {console. log (res); console. log ('fail ');}, 'complete': function (res) {// console. log (res); console. log ('complete ');}});}})}

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more articles about the development error of the mini-program payment function, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.