• <del id="4w2oc"><table id="4w2oc"></table></del>
    <nav id="4w2oc"><dd id="4w2oc"></dd></nav>
    '+obj.body+'<\/body> ' +\r\n ''+config.mch_id+'<\/mch_id> ' +\r\n ''+obj.nonce_str+'<\/nonce_str> ' +\r\n ''+obj.notify_url+'<\/notify_url>' +\r\n ''+obj.openid+'<\/openid> ' +\r\n ''+obj.out_trade_no+'<\/out_trade_no>'+\r\n ''+obj.spbill_create_ip+'<\/spbill_create_ip> ' +\r\n ''+obj.total_fee+'<\/total_fee> ' +\r\n ''+obj.trade_type+'<\/trade_type> ' +\r\n ''+obj.sign+'<\/sign> ' + \/\/ 此處必帶簽名, 否者微信在驗(yàn)證數(shù)據(jù)的時候是不通過的\r\n '<\/xml>';<\/pre><\/p>The next step is to request the api to get the value of prepay_id, and request the xml data obtained above The following api is sent to WeChat. WeChat will return the value you want after verifying that the data is OK.

    api: https:\/\/api.mch.weixin.qq.com\/pay\/unifiedorder<\/p> 6. After obtaining the prepay_id, can I directly call WeChat payment in the h5 section? The answer is not yet.

    <\/p> After obtaining the prepay_id, the parameters for h5 to call WeChat’s payment function are as follows:

    {\r\n \"appId\" : \"wx2421b1c4370ec43b\", \/\/公眾號名稱,由商戶傳入 \r\n \"timeStamp\":\" 1395712654\",  \/\/時間戳,自1970年以來的秒數(shù) \r\n \"nonceStr\" : \"e61463f8efa94090b1f366cccfbbb444\", \/\/隨機(jī)串 \r\n \"package\" : \"prepay_id=u802345jgfjsdfgsdg888\", \r\n \"signType\" : \"MD5\",  \/\/微信簽名方式:\r\n}<\/pre>

    有了這樣的參數(shù), 那么你還需要吧所有參與的參數(shù)做簽名。簽名規(guī)跟上面的一樣,生成了簽名后需要吧簽名的參數(shù) paySign 賦給h5 呼起微信的支付功能的參數(shù)(也就是微信的簽名不參與簽名的生成) 最后的參數(shù)是這樣子的:<\/p>

    {\r\n \"appId\" : \"wx2421b1c4370ec43b\", \/\/公眾號名稱,由商戶傳入 \r\n \"timeStamp\":\" 1395712654\",  \/\/時間戳,自1970年以來的秒數(shù) \r\n \"nonceStr\" : \"e61463f8efa94090b1f366cccfbbb444\", \/\/隨機(jī)串 \r\n \"package\" : \"prepay_id=u802345jgfjsdfgsdg888\", \r\n \"signType\" : \"MD5\",  \/\/微信簽名方式:\r\n \"paySign\" : \"70EA570631E4BB79628FBCA90534C63FF7FADD89\" \/\/微信簽名\r\n}<\/pre>

    如果你的各個環(huán)節(jié)都沒有問題, 那么得到了這樣參數(shù)后你就可以正常的調(diào)用起微信的支付功能, 跟原生的功能是沒有任何的差別的,(估計(jì)你現(xiàn)在的心里也特高興吧, 沒有 app 竟然可以使用 app 的功能,就是這么的神奇)。<\/p>

    七.支付完成的回調(diào)<\/p>

    微信支付完了后會在 h5 頁面的微信支付的回調(diào)函數(shù)里面放回值, res.err_msg == \"get_brand_wcpay_request:ok\" ,這樣就是成功了, 但是不是就完事兒了呢 ? 也不是,為什么呢? 微信真的收到錢了么? 收到的錢是不是你傳遞給微信的值呢 ?你還需要將支付的結(jié)果寫數(shù)據(jù)庫什么的,這些都是未知。還記的在統(tǒng)一下單接口中有個必須參數(shù)就是 notify_url : NOTIFY_URL,\/\/ 微信付款后的回調(diào)地址 這個地址是用戶傳遞給微信的, 微信在收到用戶的付款后會以 post 的方式請求這個接口,微信會傳遞用戶付款的信息過來, 不過是 xml 格式的。 類系這樣的 xml 格式:<\/p>

    \r\n <\/appid>\r\n <\/attach>\r\n <\/bank_type>\r\n <\/fee_type>\r\n <\/is_subscribe>\r\n <\/mch_id>\r\n <\/nonce_str>\r\n <\/openid>\r\n <\/out_trade_no>\r\n <\/result_code>\r\n <\/return_code>\r\n <\/sign>\r\n <\/sub_mch_id>\r\n <\/time_end>\r\n 1<\/total_fee>\r\n <\/trade_type>\r\n <\/transaction_id>\r\n<\/xml><\/pre>

    根據(jù)自己的業(yè)務(wù)邏輯解析這個 xml 格式的數(shù)據(jù)就好了。 注意:這里你在獲取到數(shù)據(jù)后微信需要得到你的回應(yīng), 如果你一直不回應(yīng)微信, 微信會請求你好幾次, 這樣估計(jì)你的邏輯會有問題吧,所以你需要給微信返回 xml 的格式的 回應(yīng)。<\/p>

    \r\n <\/return_code>\r\n <\/return_msg>\r\n<\/xml><\/pre>

    小坑:node ,express 框架開發(fā), 如果你在微信的支付成功后的回調(diào)中沒有獲取到任何 xml 的值, 那么你需要安裝一組件:body-parser-xml, 你可以使用 npm install body-parser-xml --save 安裝, 在 app.js 里面require('body-parser-xml')(bodyParser);,使用中間件的方式<\/p>

    \/\/ 解決微信支付通知回調(diào)數(shù)據(jù)\r\napp.use(bodyParser.xml({\r\n limit: '2MB', \/\/ Reject payload bigger than 1 MB\r\n xmlParseOptions: {\r\n normalize: true, \/\/ Trim whitespace inside text nodes\r\n normalizeTags: true, \/\/ Transform tags to lowercase\r\n explicitArray: false \/\/ Only put nodes in array if >1\r\n }\r\n}));<\/pre>

    這樣你就可以正常的獲取到微信的 xml 數(shù)據(jù)了。<\/p>

    使用方法:<\/p>

    pay.getAccessToken({\r\n notify_url : 'http:\/\/demo.com\/', \/\/微信支付完成后的回調(diào)\r\n out_trade_no : new Date().getTime(), \/\/訂單號\r\n attach : '名稱',\r\n body : '購買信息',\r\n total_fee : '1', \/\/ 此處的額度為分\r\n spbill_create_ip : req.connection.remoteAddress,\r\n }, function (error, responseData) {\r\n res.render('payment', {\r\n  title : '微信支付',\r\n  wxPayParams : JSON.stringify(responseData),\r\n  \/\/userInfo : userInfo\r\n });\r\n });<\/pre><\/p>\n

    就到這里吧, 感覺也差不多了。如有不對的地方還請指正。<\/p>\n

    更多nodejs微信公眾號支付開發(fā)相關(guān)文章請關(guān)注PHP中文網(wǎng)!
    <\/p>\n


    <\/p>"}

    国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

    Home WeChat Applet WeChat Development Nodejs WeChat public account payment development

    Nodejs WeChat public account payment development

    Jan 04, 2017 pm 04:52 PM

    odeJs WeChat public account function development, mobile H5 page calls WeChat’s payment function. In the past few days, node and h5 pages have been used to call WeChat's payment function according to the company's needs to complete payment requirements. Now let’s go through the development process again to help more developers successfully complete the development of WeChat payment functions. (WeChat does not yet provide node payment function)

    1. Request CODE

    The purpose of requesting code is to obtain the user's openid (the user's unique identification relative to the current official account) and access_token. Requested API: https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
    This api requires attention to several parameters:

    1. appid The appid of the official account can be seen in the official account
    2. redirect_uri Customized WeChat callback address. WeChat will jump to the redirect_uri address you defined after you request the above address. With code, the redirect_url here requires **url_encode** *php*. If your program is node, you need to use **encodeURLComponent(url)** encoding
    3. response_type=code, there is nothing to say about this. Fixed response_type=code, for detailed instructions, please refer to the instructions on the WeChat official website
    4. scope=snsapi_userinfo, just write it like this, for detailed instructions, please check the instructions on the WeChat official website
    5. state=STATE, just write it like this , for detailed instructions, you can view the instructions on the WeChat official website
    6. wechat_redirect is fixed like this. For detailed instructions, you can view the instructions on the WeChat official website
    ps: Official website link:

    2. Get access_token through code, openid

    The value of code has been obtained in the first step, then you need to obtain the value of access_token and openid through code. The requested api
    API https://api.weixin.qq .com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
    Parameter description of api here:

    1. appid WeChat official account id, obtained from WeChat official account background
    2. Secret is the key of the WeChat official account. Get the code from the WeChat official account background. The first step is to get the code used.
    4. Just fix grant_type=authorization_code.

    3. Call the interface

    access_token through access_token to perform subsequent functions. You can refer to the official example:

    https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t= resource/res_list&verify=1&id=open1419316518&lang=zh_CN

    4. Call up the payment API on the web page

    When you see this, you feel that it is almost over. As long as the web page calls the WeChat payment function, it will be over. ? No, not even close

    Open the H5 webpage in the WeChat browser and execute JS to activate the payment. The input and output data format of the interface is JSON.
    Note: WeixinJSBridge built-in objects are not valid in other browsers.
    The sample code is as follows:

    function onBridgeReady(){
     WeixinJSBridge.invoke(
     &#39;getBrandWCPayRequest&#39;, {
      "appId" : "wx2421b1c4370ec43b", //公眾號名稱,由商戶傳入 
      "timeStamp":" 1395712654",  //時間戳,自1970年以來的秒數(shù) 
      "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //隨機(jī)串 
      "package" : "prepay_id=u802345jgfjsdfgsdg888", 
      "signType" : "MD5",  //微信簽名方式: 
      "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信簽名 
     },
     function(res){ 
      if(res.err_msg == "get_brand_wcpay_request:ok" ) {} // 使用以上方式判斷前端返回,微信團(tuán)隊(duì)鄭重提示:res.err_msg將在用戶支付成功后返回 ok,但并不保證它絕對可靠。 
     }
     ); 
    }
    if (typeof WeixinJSBridge == "undefined"){
     if( document.addEventListener ){
     document.addEventListener(&#39;WeixinJSBridgeReady&#39;, onBridgeReady, false);
     }else if (document.attachEvent){
     document.attachEvent(&#39;WeixinJSBridgeReady&#39;, onBridgeReady); 
     document.attachEvent(&#39;onWeixinJSBridgeReady&#39;, onBridgeReady);
     }
    }else{
     onBridgeReady();
    }

    See the above code, then if you want to call the payment function of WeChat, you need to pass parameters,

    {
     "appId" : "wx2421b1c4370ec43b", //公眾號名稱,由商戶傳入 
     "timeStamp":" 1395712654",  //時間戳,自1970年以來的秒數(shù) 
     "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //隨機(jī)串 
     "package" : "prepay_id=u802345jgfjsdfgsdg888", 
     "signType" : "MD5",  //微信簽名方式: 
     "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信簽名 
    }

    Parameter description:

    1. appId //Official account name, passed in by the merchant

    2. timeStamp //Timestamp, the number of seconds since 1970 Special attention needs to be paid here, it needs to be in the timestamp format of a string, which means it must be " ” Quotation marks
    3. nonceStr // Random string 32 bits, method will be provided later
    4. signType // WeChat signature method: MD5
    5. paySign // WeChat signature, then
    6 . **package** //This is the most important. Where did you get it? Next.
    ps: Official website interface description
    https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6

    5. Get the package from WeChat’s unified ordering interface obtains prepay_id

    Official api:

    https://api.mch.weixin.qq.com/pay/unifiedorder

    There are a lot of request parameters, but Some of them are not necessary. The following are the required parameters.

    {
     appid : APPID,
     attach : ATTACH,
     body : BODY,
     mch_id : MCH_ID,
     nonce_str: NONCE_STR,
     notify_url : NOTIFY_URL,// 微信付款后的回調(diào)地址
     openid : OPENID,
     out_trade_no : OUT_TRADE_NO ,//new Date().getTime(), //訂單號
     spbill_create_ip : SPBILL_CREATE_IP , //客戶端的 ip
     total_fee : TOTAL_FEE, //商品的價格, 此處需要注意的是這個價格是以分算的, 那么一般是元, 你需要轉(zhuǎn)換為 RMB 的元
     trade_type : &#39;JSAPI&#39;,
    }

    WeChat’s unified ordering interface requires the transmission of xml data, and the data also needs to be signed, so first sign the data.

    For signature rules, please refer to the signature rules given by WeChat (the signature method will be given later)
    WeChat official signature rules:
    https://pay.weixin.qq.com/wiki/doc/api/ jsapi.php?chapter=4_3

    After generating the signature, you need to assemble the data into xml format:

    var body = &#39;<xml> &#39; +
     &#39;<appid>&#39;+config.wxappid+&#39;</appid> &#39; +
     &#39;<attach>&#39;+obj.attach+&#39;</attach> &#39; +
     &#39;<body>&#39;+obj.body+&#39;</body> &#39; +
     &#39;<mch_id>&#39;+config.mch_id+&#39;</mch_id> &#39; +
     &#39;<nonce_str>&#39;+obj.nonce_str+&#39;</nonce_str> &#39; +
     &#39;<notify_url>&#39;+obj.notify_url+&#39;</notify_url>&#39; +
     &#39;<openid>&#39;+obj.openid+&#39;</openid> &#39; +
     &#39;<out_trade_no>&#39;+obj.out_trade_no+&#39;</out_trade_no>&#39;+
     &#39;<spbill_create_ip>&#39;+obj.spbill_create_ip+&#39;</spbill_create_ip> &#39; +
     &#39;<total_fee>&#39;+obj.total_fee+&#39;</total_fee> &#39; +
     &#39;<trade_type>&#39;+obj.trade_type+&#39;</trade_type> &#39; +
     &#39;<sign>&#39;+obj.sign+&#39;</sign> &#39; + // 此處必帶簽名, 否者微信在驗(yàn)證數(shù)據(jù)的時候是不通過的
     &#39;</xml>&#39;;

    The next step is to request the api to get the value of prepay_id, and request the xml data obtained above The following api is sent to WeChat. WeChat will return the value you want after verifying that the data is OK.

    api: https://api.mch.weixin.qq.com/pay/unifiedorder

    6. After obtaining the prepay_id, can I directly call WeChat payment in the h5 section? The answer is not yet.

    After obtaining the prepay_id, the parameters for h5 to call WeChat’s payment function are as follows:

    {
     "appId" : "wx2421b1c4370ec43b", //公眾號名稱,由商戶傳入 
     "timeStamp":" 1395712654",  //時間戳,自1970年以來的秒數(shù) 
     "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //隨機(jī)串 
     "package" : "prepay_id=u802345jgfjsdfgsdg888", 
     "signType" : "MD5",  //微信簽名方式:
    }

    有了這樣的參數(shù), 那么你還需要吧所有參與的參數(shù)做簽名。簽名規(guī)跟上面的一樣,生成了簽名后需要吧簽名的參數(shù) paySign 賦給h5 呼起微信的支付功能的參數(shù)(也就是微信的簽名不參與簽名的生成)
    最后的參數(shù)是這樣子的:

    {
     "appId" : "wx2421b1c4370ec43b", //公眾號名稱,由商戶傳入 
     "timeStamp":" 1395712654",  //時間戳,自1970年以來的秒數(shù) 
     "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //隨機(jī)串 
     "package" : "prepay_id=u802345jgfjsdfgsdg888", 
     "signType" : "MD5",  //微信簽名方式:
     "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信簽名
    }

    如果你的各個環(huán)節(jié)都沒有問題, 那么得到了這樣參數(shù)后你就可以正常的調(diào)用起微信的支付功能, 跟原生的功能是沒有任何的差別的,(估計(jì)你現(xiàn)在的心里也特高興吧, 沒有 app 竟然可以使用 app 的功能,就是這么的神奇)。

    七.支付完成的回調(diào)

    微信支付完了后會在 h5 頁面的微信支付的回調(diào)函數(shù)里面放回值,
    res.err_msg == "get_brand_wcpay_request:ok" ,這樣就是成功了, 但是不是就完事兒了呢 ? 也不是,為什么呢? 微信真的收到錢了么? 收到的錢是不是你傳遞給微信的值呢 ?你還需要將支付的結(jié)果寫數(shù)據(jù)庫什么的,這些都是未知。還記的在統(tǒng)一下單接口中有個必須參數(shù)就是 notify_url : NOTIFY_URL,// 微信付款后的回調(diào)地址 這個地址是用戶傳遞給微信的, 微信在收到用戶的付款后會以 post 的方式請求這個接口,微信會傳遞用戶付款的信息過來, 不過是 xml 格式的。
    類系這樣的 xml 格式:

    <xml>
     <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
     <attach><![CDATA[支付測試]]></attach>
     <bank_type><![CDATA[CFT]]></bank_type>
     <fee_type><![CDATA[CNY]]></fee_type>
     <is_subscribe><![CDATA[Y]]></is_subscribe>
     <mch_id><![CDATA[10000100]]></mch_id>
     <nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>
     <openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>
     <out_trade_no><![CDATA[1409811653]]></out_trade_no>
     <result_code><![CDATA[SUCCESS]]></result_code>
     <return_code><![CDATA[SUCCESS]]></return_code>
     <sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>
     <sub_mch_id><![CDATA[10000100]]></sub_mch_id>
     <time_end><![CDATA[20140903131540]]></time_end>
     <total_fee>1</total_fee>
     <trade_type><![CDATA[JSAPI]]></trade_type>
     <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>
    </xml>

    根據(jù)自己的業(yè)務(wù)邏輯解析這個 xml 格式的數(shù)據(jù)就好了。
    注意:這里你在獲取到數(shù)據(jù)后微信需要得到你的回應(yīng), 如果你一直不回應(yīng)微信, 微信會請求你好幾次, 這樣估計(jì)你的邏輯會有問題吧,所以你需要給微信返回 xml 的格式的 回應(yīng)。

    <xml>
     <return_code><![CDATA[SUCCESS]]></return_code>
     <return_msg><![CDATA[OK]]></return_msg>
    </xml>

    小坑:node ,express 框架開發(fā), 如果你在微信的支付成功后的回調(diào)中沒有獲取到任何 xml 的值, 那么你需要安裝一組件:body-parser-xml, 你可以使用 npm install body-parser-xml --save 安裝, 在 app.js 里面require('body-parser-xml')(bodyParser);,使用中間件的方式

    // 解決微信支付通知回調(diào)數(shù)據(jù)
    app.use(bodyParser.xml({
     limit: &#39;2MB&#39;, // Reject payload bigger than 1 MB
     xmlParseOptions: {
     normalize: true, // Trim whitespace inside text nodes
     normalizeTags: true, // Transform tags to lowercase
     explicitArray: false // Only put nodes in array if >1
     }
    }));

    這樣你就可以正常的獲取到微信的 xml 數(shù)據(jù)了。

    使用方法:

    pay.getAccessToken({
     notify_url : &#39;http://demo.com/&#39;, //微信支付完成后的回調(diào)
     out_trade_no : new Date().getTime(), //訂單號
     attach : &#39;名稱&#39;,
     body : &#39;購買信息&#39;,
     total_fee : &#39;1&#39;, // 此處的額度為分
     spbill_create_ip : req.connection.remoteAddress,
     }, function (error, responseData) {
     res.render(&#39;payment&#39;, {
      title : &#39;微信支付&#39;,
      wxPayParams : JSON.stringify(responseData),
      //userInfo : userInfo
     });
     });

    就到這里吧, 感覺也差不多了。如有不對的地方還請指正。

    更多nodejs微信公眾號支付開發(fā)相關(guān)文章請關(guān)注PHP中文網(wǎng)!


    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)