Analysis of PHP WeChat payment notification Handling Methods

Source: Internet
Author: User
Tags openid
This article is a supplement to the previous article. It is mainly because the official website does not have a demo about the notification. It is really uncomfortable to touch the stone and cross the river. It is convenient for you to develop and release it for you.

This article is a supplement to the previous article. It is mainly because the official website does not have a demo about the notification. It is really uncomfortable to touch the stone and cross the river. It is convenient for you to develop and release it for you.

For the implementation of the notification mechanism, only official documents do not have demo code. For those who have not done so, it takes a lot of time for testing.

As mentioned in the document, the structure of the data sent from each notification is complicated. It is a multi-segment data. In addition to the POST data, it also needs to take other data.

The following describes the values of php: // input and $ _ POST:

The Code is as follows:


1. When the value of Content-Type is application/x-www-form-urlencoded, php will fill in the corresponding data of the http Request body to the array $ _ POST, the data filled in the $ _ POST array is the result of urldecode () parsing. (In fact, in addition to the Content-Type, there is also multipart/form-data indicating that the data is form data. We will introduce it later)
2, php: // input data, as long as the Content-Type is not multipart/form-data (this condition will be described later ). Php: // the input data is consistent with the part of the http entity body. The consistent data Length is specified by Content-Length.
3. Only when the Content-Type is application/x-www-form-urlencoded and the submission method is POST, $ _ POST data and php: // The input data is "consistent" (with quotation marks, indicating that the formats are inconsistent and the content is consistent. In other cases, they are inconsistent.
4. php: // input cannot read $ _ GET data. The reason is that the $ _ GET data is written in the PATH field of the http Request header as query_path, rather than in the http Request body.

This also helps us understand why the xml_rpc server reads data through file_get_contents ('php: // input', 'R '). Instead of reading from $ _ POST, it is because the xml_rpc data Type is xml, and its Content-Type is text/xml.
5. php: // input has encountered multipart/form-data. Please refer to RFC1867's description. Multipart/form-data also means to submit form data using the POST method. It is also accompanied by file upload, so it will be different from the data format of application/x-www-form-urlencoded. It is passed to the server in a more reasonable and efficient data format. When the Content-Type is multipart/form-data, php: // input is empty even if there is data in the http Request body. PHP does not enter the data in php at this time: // input stream. Therefore, php: // input cannot be used to read data from enctype = multipart/form-data.

6. when the Content-Type is application/x-www-form-urlencoded, the php: // input and $ _ POST data are "consistent". When the Content-Type is another Content-Type, php: // The input and $ _ POST data are inconsistent. Because only when the Content-Type is application/x-www-form-urlencoded or multipart/form-data, PHP will fill in the corresponding part of the body of the http request packet in the $ _ POST global variable. PHP will ignore this in other cases. Php: // input is not empty except for multipart/form-data.

These two methods are used to read the transmitted data.

Get $ POST first. This is a regular payment notification, as shown in the following figure:

The Code is as follows:


Array (
'Bank _ type' => '123 ',
'Discount' => '0 ',
'Partition _ type' => '1 ',
'Input _ charset' => 'utf-8 ',
'Your Y _ id' => 'yano6cznonzk0agb8n?ggvuwssjt7ze7gwrars0r _ done ',
'Out _ trade_no '=> '123 ',
'Parts' => '12xxxxxxxx ',
'Product _ period' => '123 ',
'Sign' => '545fa0e8b594bbxxxx48xx142f084ty ',
'Sign _ type' => 'md5 ',
'Time _ end' => '123 ',
'Total _ 0000' => '123 ',
'Trade _ mode' => '1 ',
'Trade _ state' => '0 ',
'Transaction _ id' => '12xxx449012014xxx33174005xx ',
'Transport _ upload' => '0 ',
)

Use file_get_contents ('php: // input') to read additional information, such:

The Code is as follows:


o0pd3jqHaN7b0tVPDFJPzJEkSCLw
wxXXX06XX2cXXX88XX
1
1400814743
lqxwMsiY9EXRDpms
c2dxxxe186116b32b06axxxc1a688b671eexxx5e
sha1

Finally, the corresponding business logic processing will not be detailed.

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.