php 微信支付md5签名,微信支付回调验证签名处理
微信支付回調驗證簽名:一定要驗證簽名,可能不造成偽造數據,或者數據庫造到灌水;
/**
* 微信支付回調類
* @name callbackaction.class.php
* @author yangzl
* @date(20180820)
*/
class callbackaction extends action{
/**
* @param 獲取微信支付回調接口
* @return [type] [descripti
* @date(20180820)
* @author yangzl
*/
public function getpaymentcallback(){
$curl_request = $_server['request_method']; //獲取請求方式
if($curl_request == 'post'){
$xmldata=file_get_contents("php://input");
libxml_disable_entity_loader(true);
//把微信支付回調結果寫入日志
$this->writelogs(runtime_path.'logs/','getpaymentcallback',"\r\n-------------------".date('y-m-d h:i:s')."微信支付回調結果---------\r\n---響應數據:".json_encode(simplexml_load_string($xmldata, 'simplexmlelement', libxml_nocdata))."\r\n------------\r\n");
//處理微信支付返回的xml數據
$data = json_encode(simplexml_load_string($xmldata, 'simplexmlelement', libxml_nocdata));
$sign_return = json_decode($data,true)['sign'];
$sign = $this->appgetsign(json_decode($data,true));
//給微信返回接收成功通知,生成xml數據
$this->returnxml();
if($sign == $sign_return){
//把數據提交給訂單處理方法
$this->proorders($data);
}
}
}
/*
* 格式化參數格式化成url參數 生成簽名sign
*/
public function appgetsign($data){
require_once web_lib."wxpay.config.php";
$config = new wxpayconfig();
$appwxpay_key = $config->getkey();
//簽名步驟一:按字典序排序參數
ksort($data);
$string = $this->callbacktourlparams($data);
//簽名步驟二:在string后加入key
if($appwxpay_key){
$string = $string."&key=".$appwxpay_key;
}
//簽名步驟三:md5加密
$string = md5($string);
//簽名步驟四:所有字符轉為大寫
$result_ = strtoupper($string);
return $result_;
}
/**
* 格式化參數格式化成url參數
*/
public function callbacktourlparams($parameters){
$buff = "";
foreach ($parameters as $k => $v){
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
/**
* @param 拼裝xml數據返回
* @author yangzl ]>
*/
public function returnxml(){
header("content-type:text/xml;");
$xml = "<?xml version='1.0' encoding='utf-8'?>\n";
$xml .= "\n";
$xml .= "success\n";
$xml .= "ok\n";
$xml .= "\n";
echo $xml;
}
/**
* @param 支付回調程序處理
* @author yangzl
* @date(20180820)
*/
public function proorders($data){
if (!$data) {
$date = date("y-m-d h:i:s",time());
log::write( "proorders方法錯誤".$date);
}
//處理則返回數據入庫 分表
$orders_info = json_decode($data,true);
$orders_model = new ordersmodel();
$branch_id = json_decode($orders_info['attach'],true)['branch_id'];
//查詢排重
$result_pay_data = $orders_model->get_pay_data($branch_id,$orders_info['transaction_id']);
if(!$result_pay_data){ //不存在
//存數據
$table_id = json_decode($orders_info['attach'],true)['table_id'];
//根據tableid查詢桌臺信息
$tables_model = new tablesmodel();
$table_info = $tables_model->get_table_by_id( $table_id, $branch_id);
if($table_info['is_delete'] == '0'){
$title = $table_info['title'];
}
//回調支付信息
$pay_info = array(
'branch_id' => $branch_id,
'transaction_id' => $orders_info['transaction_id'],
'cash_fee' => sprintf("%.2f",$orders_info['cash_fee']/100),
'pay_type' => 1,
'mch_id' =>$orders_info['mch_id'],
'result_code' => $orders_info['result_code'] == 'success' ? 1 : 0,
'orders_id' =>$orders_info['out_trade_no'],
'time_end' => $orders_info['time_end'],
'title' => $title,
'openid'=> $orders_info['openid'],
'pay_source' => 1,
'is_subscribe' => $orders_info['is_subscribe'] == 'y' ? 1 : 0, //是否關注公眾賬號
'sub_mch_id' => $orders_info['sub_mch_id'],
'total_fee' =>sprintf("%.2f",$orders_info['total_fee']/100),
'bank_type' => $orders_info['bank_type'],
);
//存數據
$add_data = $orders_model->add_pay_info($branch_id,$pay_info);
if(!$add_data){
log::write( "支付數據存儲失敗".$orders_info['transaction_id']);
return false;
}
if($orders_info['result_code'] == 'success'){
//查詢訂單信息
// $order_data = $orders_model->get_orders_data($table_id, $branch_id);
$order_data = $orders_model->get_orders_tem($table_id, $branch_id);
if(!$order_data){
log::write( "查詢訂單信息失敗".time());
return false;
}
$this->writelogs(runtime_path.'logs/','proorders',"\r\n-------------------".date('y-m-d h:i:s')."查詢訂單信息---------\r\n---響應數據:".json_encode($order_data)."\r\n------------\r\n");
//數據處理
$data_handle = $orders_model->data_handle($order_data,$table_id,$branch_id,$orders_info['cash_fee']/100,$orders_info['transaction_id']);
$this->writelogs(runtime_path.'logs/','proorders',"\r\n-------------------".date('y-m-d h:i:s')."微信支付數據處理結果---------\r\n---響應數據:".json_encode($data_handle)."\r\n------------\r\n");
//支付方式入庫
$pay_data = array(
'orders_id' => $data_handle['orders_id'], //訂單編號
'branch_id' => (int)$branch_id, // 店鋪 id
'pay_sn' => $orders_info['transaction_id'], // 支付 sn
'pay_total' => sprintf("%.2f",$orders_info['cash_fee']/100), // 支付金額
'pay_type' => 1, // 支付類型
'table_id' => $table_id, // 桌臺id
);
//添加副表
$pay_sn = $orders_model->add_orders_pay_sn($pay_data);
$pay_state = $data_handle['state'];
if($pay_state == 1){ //完成訂單
//完成訂單后,沒有確認的訂單也全部清空 add yangzl
$del_redis_orders = $orders_model->del_redis_orders_p($branch_id, $table_id);
if (!$del_redis_orders){
log::write( "現金訂單完成后收尾".$table_id);
}
//設置狀態
$table_model = new tablesmodel();
$state = $table_model->set_table_state($table_id, $branch_id, 4);
}
// 服務員下單一對一推送
$table_base = $table_model->get_table_by_id($table_id,$branch_id);
$table_title = $table_base['title'];
push::app_push_waiter_checkout($table_id, $table_title, '1');
exit();
}else{ //支付失敗
log::write( "支付訂單號數據支付失敗::支付訂單號".$orders_info['transaction_id']);
exit();
}
}else{
log::write( "支付訂單號數據已處理".$orders_info['transaction_id']);
$this->returnxml();
exit();
}
}
/**********寫入日志方法***********/
/**
* 日志記錄
* @param $path string 日志文件目錄
* @param $file string 日志文件名,不包含后綴
* @param $content string 記錄內容
* @param @author yangzl
* @return void
**/
public function writelogs($path,$file,$content,$more=true){
$newpath = '';
if (!file_exists($path)) {
mkdir ($path);
@chmod ($path, 0777 );
}
if($more){
$newpath .= $path.$file.@date('y-m-d').".log";
}else{
$newpath .= $path.$file.".log";
}
$content .="\r\n"."----------------------------------------------------------------------------------------------------------------"."\r\n";
$this->write_file($newpath,$content,"a+");
}
/**
* 寫內容
* @param $filename string 日志文件名
* @param $data string 記錄內容
* @param $method
* @author yanzl
**/
private function write_file($filename,$data,$method="rb+",$iflock=1){
@touch($filename);
$handle=@fopen($filename,$method);
if($iflock){
@flock($handle,lock_ex);
}
@fputs($handle,$data);
if($method=="rb+") @ftruncate($handle,strlen($data));
@fclose($handle);
@chmod($filename,0777);
if( is_writable($filename) ){
return 1;
}else{
return 0;
}
}
}
?>
如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!
總結
以上是生活随笔為你收集整理的php 微信支付md5签名,微信支付回调验证签名处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android cpu负载 工具,计算A
- 下一篇: 尝试cython和openmp