php processing WeChat bill

Payment reconciliation is to be done recently, that is, to check whether the third-party payment corresponds to the bill in the database one-to-one, which involves the processing of WeChat billing statements. The WeChat billing interface returns a string similar to the following results:


The function that the program needs to implement is to extract the valid information from 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(
                'wechat_order_no' => $reponse[$base_index + 6],
                'order_count' => $reponse[$base_index + 13],
                'order_discount' => $reponse[$base_index + 23]
            );
        }
        return $result;
    }

 

The main idea is that the result format returned by the WeChat bill is fixed, and the string can be divided by '`', and then each 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. Only the fields I need are taken in the code. If other fields are needed, they can be added according to this format.

The code still has the following points to be improved:

1. If the string is particularly large, the memory allocated by the php process may be exhausted. For ordinary merchant orders, it is sufficient if the daily transaction volume is not particularly large.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839515&siteId=291194637