Home  >  Article  >  WeChat Applet  >  Detailed explanation of WeChat payment development transaction notification example code

Detailed explanation of WeChat payment development transaction notification example code

高洛峰
高洛峰Original
2017-03-27 14:04:162349browse

This article mainly introduces how to get transaction notifications when using JS API to pay, and provides code examples. I hope it will be helpful to partners who develop WeChat payment

1. Transaction notifications

After the user successfully completes the payment, the WeChat backend notifies (POST) the merchant server (notify_url) of the payment result. Merchants can use the notification results of notify_url to display personalized pages.

When interacting with background notifications, if the response received by WeChat from the merchant is not success or times out, WeChat will not fail the notification. WeChat will regularly re-initiate the notification through a certain strategy (such as 8 times in 30 minutes). It may improve the success rate of notifications, but WeChat does not guarantee that notifications will ultimately be successful.

Background notification is performed through notify_url in the request, using the POST mechanism.

At the same time, xml data will also be included in postData.

2. Obtaining and responding to transaction results

According to the official documentation, create notice.php to notify the results.

The program content is as follows:

<?php foreach ($_GET as $key=>$value) 
{
 logger("Key: $key; Value: $value");
}
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
logger($postStr);

if (isset($_GET)){
 echo "success";
}

//日志记录
function logger($log_content)
{
 $max_size = 100000;
 $log_filename = "log.xml";
 if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
 file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
}
?>

The function of the above program is:

Get the notifications posted to the url, they are in the form of GET variables

Get the XML data packet of the post

Return the success message success

Put the full path of notice.php into the JS API Payment notice url.

$wxPayHelper->setParameter("notify_url", http://www.doucube.com/wxpay/notice.php);

In this way, when the transaction is completed, the url will receive a notification and be recorded in the log file. Our test is as follows:

The obtained GET variables and XML are as follows:

Key: bank_billno; Value: 201405273540085997
Key: bank_type; Value: 2011
Key: discount; Value: 0
Key: fee_type; Value: 1
Key: input_charset; Value: GBK
Key: notify_id; Value: Gx8ov6tT6_yaARrtKG6RFZ4KiVtKqVnJzvulFlteJ3dhBg38iRtKs0pTXXfgh8WnH15mIhG6j65ggbzzYguh1mutG3B5oHsK
Key: out_trade_no; Value: JfuKdiBig4zZnE4n
Key: partner; Value: 1234567890
Key: product_fee; Value: 1
Key: sign; Value: 08876C4A9F7A36A9EA972C211C122362
Key: sign_type; Value: MD5
Key: time_end; Value: 20140527194139
Key: total_fee; Value: 1
Key: trade_mode; Value: 1
Key: trade_state; Value: 0
Key: transaction_id; Value: 1218614901201405273313473135
Key: transport_fee; Value: 0

<xml><openid></openid>
<appid></appid>
<issubscribe>1</issubscribe>
<timestamp>1401190899</timestamp>
<noncestr></noncestr>
<appsignature></appsignature>
<signmethod></signmethod>
</xml>

And you will receive an OK pop-up window in the WeChat window

Detailed explanation of WeChat payment development transaction notification example code

The above is the information for developing transaction notifications for WeChat payment Organized, thank you for your support!

The above is the detailed content of Detailed explanation of WeChat payment development transaction notification example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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