Home >Backend Development >PHP Tutorial >php handles WeChat bills

php handles WeChat bills

不言
不言Original
2018-04-26 09:14:291674browse

The content introduced in this article is about php processing WeChat bills. It has a certain reference value. Now I share it with you. Friends in need can refer to it. It has a certain reference value. Now Share it with everyone, friends in need can refer to

Recently I have to do payment reconciliation, that is, check whether the third-party payment and the bill in the database correspond one-to-one, which involves the processing of WeChat statement, and the WeChat bill interface returns The result is a string similar to the following:


The function that the program needs to implement is to extract the effective information of each order from this string. The reference code is as follows:



##

function deal_wechat_return_result($reponse)
    {        
    $result = array();        
    $reponse = str_replace(","," ",$reponse);        
    $reponse = explode("`",$reponse);        
    $total_order_count =( count($reponse) - 6 ) / 24;        
    for($i = 0; $i< $total_order_count; $i++)
        {            
        $base_index = 24 * $i;            
        $result[$reponse[$base_index + 7]] = array(
                &#39;wechat_order_no&#39; => $reponse[$base_index + 6],
                &#39;order_count&#39; => $reponse[$base_index + 13],
                &#39;order_discount&#39; => $reponse[$base_index + 23]
            );
        }       
         return $result;
    }


## The main idea is that the format of the result returned by WeChat bill is fixed. You can use '`' to split the string. Then every 24 fields are the description information of an order, and the last 6 fields are the summary information of the bill. Therefore, the entire bill can be traversed through the for loop. The code only takes the fields I need. If other fields are needed, I can add them by myself according to this format.

The code still needs to be improved in the following points:

1. It does not consider the case where the string is particularly large, which may cause the memory allocated by the php process to be exhausted. For ordinary merchant orders, every day It is sufficient when the trading volume is not particularly large.

2. By default, the format returned by WeChat is fixed. In fact, dynamic matching can be performed based on the head and tail of the string returned by WeChat.

Related recommendations:

php method for processing URLs with Chinese characters

php method for processing form upload files

PHP processing Excel table instance method

The above is the detailed content of php handles WeChat bills. 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