Home > Backend Development > PHP Tutorial > PHP code for processing WeChat payment callbacks to change order payment status

PHP code for processing WeChat payment callbacks to change order payment status

不言
Release: 2023-04-03 15:48:02
Original
6984 people have browsed it

The content this article brings to you is about the PHP server APP WeChat payment callback verification signature to change the order payment status. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<?php

       //处理微信支付回调
    	
    	$time = time();

        $testxml  = file_get_contents("php://input");  //接收微信发送的支付成功信息

        //file_put_contents(&#39;D:\webroot\xianyijie\JK\weixin.txt&#39;,$testxml);

       	$result = XMLDataParse($testxml);

       	file_put_contents("D:\webroot\xianyijie\JK\weixin.txt", json_encode($result)."\r\n", FILE_APPEND); //将信息写入txt文件做支付备份

        $sign_return = $result[&#39;sign&#39;];

    		//如果成功返回了
    		
            if($result[&#39;return_code&#39;] == &#39;SUCCESS&#39; && $result[&#39;result_code&#39;] == &#39;SUCCESS&#39;){

            		$key = "商户密钥key";

            		unset($result[&#39;sign&#39;]);

            		$sign = appgetSign($result,$key);

				

				   if($sign == $sign_return)    //验证签名
				   {
 					  
 					  //这里是验签成功后的sql操作
                    
				   
                        //如果操作成功
						return "<xml><return_code><![CDATA[SUCCESS]]></return_code>
				                      <return_msg><![CDATA[OK]]></return_msg>
				                 </xml>";
                
						//如果操作失败
                        return "fail";	

				   }
				
            }


/*
 * 格式化参数格式化成url参数  生成签名sign
*/
 function appgetSign($Obj,$appwxpay_key)

  {

    foreach ($Obj as $k => $v)

    {

      $Parameters[$k] = $v;

    }

    //签名步骤一:按字典序排序参数

    ksort($Parameters);

    $String = formatBizQueryParaMap($Parameters, false);

    //echo &#39;【string1】&#39;.$String.&#39;</br>&#39;;

    //签名步骤二:在string后加入KEY
        if($appwxpay_key){
            $String = $String."&key=".$appwxpay_key;
        }
        
    //echo "【string2】".$String."</br>";

    //签名步骤三:MD5加密

    $String = md5($String);

    //echo "【string3】 ".$String."</br>";

    //签名步骤四:所有字符转为大写

    $result_ = strtoupper($String);

    //echo "【result】 ".$result_."</br>";

    return $result_;

  }



//xml格式数据解析函数
 function XMLDataParse($data){
  $xml = simplexml_load_string($data,NULL,LIBXML_NOCDATA);
  $array=json_decode(json_encode($xml),true);
  return $array;
 }


?>
Copy after login

Recommended related articles:

POST and GET request codes in curl of php

About PHP intermediate keys Analysis

The above is the detailed content of PHP code for processing WeChat payment callbacks to change order payment status. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template