微信支付開發(fā)流程,信支付開發(fā)流程
Jun 13, 2016 am 08:53 AM微信支付開發(fā)流程,信支付開發(fā)流程
授人以魚不如授人以漁
微信支付開發(fā)流程
下面以PHP語言為例,對微信支付的開發(fā)流程進(jìn)行一下說明。
1.獲取訂單信息
2.根據(jù)訂單信息和支付相關(guān)的賬號生成sign,并且生成支付參數(shù)
3.將支付參數(shù)信息POST到微信服務(wù)器,獲取返回信息
4.根據(jù)返回信息生成相應(yīng)的支付代碼(微信內(nèi)部)或是支付二維碼(非微信內(nèi)),完成支付。
下面分步驟的講一下:
1.微信支付中相關(guān)的必須的訂單參數(shù)有三個,分別是:body(商品名或訂單描述),out_trade_no(一般為訂單號)和total_fee(訂單金額,單位“分”,要注意單位問題),在不同的應(yīng)用中,首先要做的就是獲取訂單中的相關(guān)信息,為支付參數(shù)生成做準(zhǔn)備。
2.其他必須的支付參數(shù)有 appid(微信appid),mch_id(申請成功后告知),device_info(web端和微信端該參數(shù)都是統(tǒng)一的,為大寫的”WEB“),trade_type(根據(jù)使用場景不同,該值也是不同的,微信外部為”NATIVE“,微信內(nèi)部為”JSAPI“),nonce_str(32位隨機字符串),spbill_create_ip(發(fā)起支付的終端IP,即服務(wù)器IP),notify_url(支付回調(diào)地址,微信服務(wù)器通知網(wǎng)站支付完成與否,修改訂單狀態(tài)),sign(簽名),還有一個需要說明的地方,如果trade_type為JSAPI的話,openid為必填的參數(shù)。
簽名算法是比較容易出錯的地方,在于簽名步驟繁瑣,其實很關(guān)鍵的是,sign不參與簽名
?A:將1、2中提到的除sign外的參數(shù)賦值,放到一個數(shù)組array里面,按照字典順序排序,其實就是鍵值按照A—Z的順序進(jìn)行排序。
B:將數(shù)組轉(zhuǎn)換成字符串string,格式為 k1=v1&k2=v2&...kN=vN
C:在此string后加上KEY值(在微信支付商戶后臺用戶自己設(shè)定的)現(xiàn)在string =?k1=v1&k2=v2&...kN=vN&key=KEY。
D:string = md5(string)
E: sign =?strtoupper(string)
至此,sign生成完畢。
將sign添加到array數(shù)組里面生成新的數(shù)組。將該數(shù)組轉(zhuǎn)換為XML。至此,微信支付的參數(shù)準(zhǔn)備工作完成。
3.將2中生成的XML,使用POST的方式發(fā)送請求到微信(https://api.mch.weixin.qq.com/pay/unifiedorder),獲取返回的XML信息,將該信息轉(zhuǎn)換成數(shù)組格式方便操作。返回的XML信息如下:
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wx2421b1c4370ec43b]]></appid> <mch_id><![CDATA[10000100]]></mch_id> <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str> <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id> <trade_type><![CDATA[JSAPI]]></trade_type> </xml>
如果是trade_type==native支付的話,還會多一個參數(shù)code_url,該URL為微信掃碼支付的地址。
4.下面就是支付的過程了。
如果trade_type==native,那么使用一些方式將code_url轉(zhuǎn)換成二維碼,使用微信掃碼就可以了,如果是微信內(nèi)部點擊支付的話,需要調(diào)用微信js-sdk中的相關(guān)東西,這一步中最關(guān)鍵是生成一個json格式的字符串。
首先要生成轉(zhuǎn)換json字符串的數(shù)組array_jsapi。
A:該數(shù)組的參數(shù)包括:appId,timeStamp,nonceStr,package,signType(默認(rèn)為”MD5“),要注意大小寫和上面的數(shù)組里面是不一樣的。
B:使用該數(shù)組生成paySign參數(shù),簽名方式同上。
C:將paySign參數(shù)追加到array_jsapi數(shù)組中。
D:將該數(shù)組使用json_encode格式化為字符串js_string。
完成上面的工作,就可以在微信內(nèi)部進(jìn)行支付了。
下面為相關(guān)支付的示例代碼:
<script type='text/javascript'> function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', $js_string, function(res){ WeixinJSBridge.log(res.err_msg); if(res.err_msg=='get_brand_wcpay_request:ok') { alert('支付成功'); } else { alert('支付失敗'); } } ); } function callpay() { if (typeof WeixinJSBridge == 'undefined'){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } </script>
代碼中js_string即為我們生成的字符串。
HTML代碼中調(diào)用callpay()函數(shù)發(fā)起支付。
這樣微信支付的支付工作就完成了。
下面是回調(diào)工作,該功能確保訂單支付成功后,有正確的狀態(tài)顯示給用戶。
支付完成后,微信使用POST請求,將支付結(jié)果反饋給網(wǎng)站服務(wù)器,網(wǎng)站服務(wù)器獲取POST信息,根據(jù)支付成功與否,來確定是否修改訂單信息。
A:將POST參數(shù)中的sign去除,并且記錄下來該值。
B:對剩余的參數(shù)進(jìn)行簽名
C:將簽名結(jié)果和POST中的sign進(jìn)行比對,相同說明簽名正確,根據(jù)支付結(jié)果修改訂單狀態(tài)。
E:返回XML信息給微信,確保微信知道網(wǎng)站已經(jīng)收到該通知,避免微信再次推送POST,示例如下:
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> </xml>
如果失敗,則返回
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[失敗原因]]></return_msg> </xml>
至此,微信支付的整個開發(fā)介紹完畢。
有任何疑問,請聯(lián)系QQ:97695870
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.
