微信支付開發(fā)流程,信支付開發(fā)流程
Jun 13, 2016 am 08:53 AM微信支付開發(fā)流程,信支付開發(fā)流程
授人以魚不如授人以漁
微信支付開發(fā)流程
下面以PHP語言為例,對微信支付的開發(fā)流程進行一下說明。
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的順序進行排序。
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)部進行支付了。
下面為相關(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ù)進行簽名
C:將簽名結(jié)果和POST中的sign進行比對,相同說明簽名正確,根據(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

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

USDT cash exchange needs to be operated through a trading platform that supports fiat currency withdrawal. 1. Prepare a trading platform account that supports fiat currency withdrawal (such as Binance, Ouyi, Huobi, etc.); 2. Complete KYC real-name authentication; 3. Bind bank cards or Alipay and other payment methods; 4. Log in to the account and ensure that USDT is in the fund account or spot account; 5. Enter the OTC or fiat currency area and choose to sell USDT; 6. Set the sales amount and match the buyer; 7. After confirming the other party’s payment, click “Confirm Coin Delivery” to complete the transaction. Withdrawal methods include OTC fiat currency transactions, bank card withdrawals and third-party payments, among which OTC is more efficient. Notes include making sure to verify that the money is received before releasing, avoiding frequent large withdrawals, and contacting customer service in time when encountering abnormalities. The key to the entire process is to choose

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.
