


A brief discussion on the process of developing WeChat payment using PHP, a brief discussion on the php payment process
The following uses PHP language as an example to explain the development process of WeChat payment.
1. Get order information
2. Generate sign based on order information and payment-related account number, and generate payment parameters
3. POST the payment parameter information to the WeChat server and obtain the return information
4. Generate the corresponding payment code (within WeChat) or payment QR code (not within WeChat) based on the returned information to complete the payment.
The following is a step-by-step explanation:
1. There are three necessary order parameters related to WeChat payment, namely: body (product name or order description), out_trade_no (usually order number) and total_fee (order amount, unit "cent", pay attention to the unit Question), in different applications, the first thing to do is to obtain the relevant information in the order to prepare for the generation of payment parameters.
2. Other necessary payment parameters include appid (WeChat appid), mch_id (notified after the application is successful), device_info (the parameters on the web and WeChat sides are the same, capitalized "WEB"), trade_type (according to This value is also different in different usage scenarios. It is "NATIVE" outside WeChat and "JSAPI" inside WeChat), nonce_str (32-bit random string), spbill_create_ip (the terminal IP that initiates the payment, that is, the server IP), notify_url (payment Callback address, the WeChat server notifies the website whether the payment is completed or not, modify the order status), sign (signature), and there is another point that needs to be explained. If trade_type is JSAPI, openid is a required parameter.
The signature algorithm is prone to errors because the signing steps are cumbersome. In fact, the most important thing is that sign does not participate in the signature
A: Assign the parameters mentioned in 1 and 2 except sign into an array array, and sort them in dictionary order. In fact, the key values are sorted in the order of A-Z.
B: Convert the array into a string string in the format k1=v1&k2=v2&...kN=vN
C: Add the KEY value after this string (set by the user in the WeChat payment merchant backend). Now string = k1=v1&k2=v2&...kN=vN&key=KEY.
D:string = md5(string)
E: sign = strtoupper(string)
At this point, the sign is generated.
Add sign to the array array to generate a new array. Convert this array to XML. At this point, the parameter preparation work for WeChat payment is completed.
3. Use POST to send the XML generated in 2 to WeChat (https://api.mch.weixin.qq.com/pay/unifiedorder), obtain the returned XML information, and convert the information into array format for easy operation. The returned XML information is as follows:
<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>
If it is trade_type==native payment, there will be an additional parameter code_url, which is the address of WeChat scan code payment.
4. The following is the payment process.
If trade_type==native, then use some methods to convert the code_url into a QR code, and just use WeChat to scan the code. If it is click-to-pay within WeChat, you need to call the relevant things in WeChat js-sdk. This step The most important thing is to generate a string in json format.
First, generate the array_jsapi that converts the json string.
A: The parameters of this array include: appId, timeStamp, nonceStr, package, signType (default is "MD5"). Please note that the case is different from the above array.
B: Use this array to generate paySign parameters. The signature method is the same as above.
C: Append the paySign parameter to the array_jsapi array.
D: Format the array into a string js_string using json_encode.
After completing the above work, you can make payment within WeChat.
The following is a sample code for related payments:
<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>
The js_string in the code is the string we generated.
Call the callpay() function in HTML code to initiate payment.
In this way, the payment work of WeChat Pay is completed.
The following is the callback work. This function ensures that the correct status is displayed to the user after the order payment is successful.
After the payment is completed, WeChat uses a POST request to feedback the payment results to the website server. The website server obtains the POST information and determines whether to modify the order information based on whether the payment is successful or not.
A: Remove the sign in the POST parameter and record the value.
B: Sign the remaining parameters
C: Compare the signature result with the sign in POST. If the signature is the same, it means the signature is correct. Modify the order status according to the payment result.
E: Return XML information to WeChat to ensure that WeChat knows that the website has received the notification and prevent WeChat from pushing POST again. The example is as follows:
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> </xml>
If failed, return
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[失败原因]]></return_msg> </xml>
At this point, the entire development of WeChat Payment is introduced.

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
