php 微信支付md5签名,微信支付回调验证签名处理
微信支付回調(diào)驗(yàn)證簽名:一定要驗(yàn)證簽名,可能不造成偽造數(shù)據(jù),或者數(shù)據(jù)庫(kù)造到灌水;
/**
* 微信支付回調(diào)類
* @name callbackaction.class.php
* @author yangzl
* @date(20180820)
*/
class callbackaction extends action{
/**
* @param 獲取微信支付回調(diào)接口
* @return [type] [descripti
* @date(20180820)
* @author yangzl
*/
public function getpaymentcallback(){
$curl_request = $_server['request_method']; //獲取請(qǐng)求方式
if($curl_request == 'post'){
$xmldata=file_get_contents("php://input");
libxml_disable_entity_loader(true);
//把微信支付回調(diào)結(jié)果寫入日志
$this->writelogs(runtime_path.'logs/','getpaymentcallback',"\r\n-------------------".date('y-m-d h:i:s')."微信支付回調(diào)結(jié)果---------\r\n---響應(yīng)數(shù)據(jù):".json_encode(simplexml_load_string($xmldata, 'simplexmlelement', libxml_nocdata))."\r\n------------\r\n");
//處理微信支付返回的xml數(shù)據(jù)
$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數(shù)據(jù)
$this->returnxml();
if($sign == $sign_return){
//把數(shù)據(jù)提交給訂單處理方法
$this->proorders($data);
}
}
}
/*
* 格式化參數(shù)格式化成url參數(shù) 生成簽名sign
*/
public function appgetsign($data){
require_once web_lib."wxpay.config.php";
$config = new wxpayconfig();
$appwxpay_key = $config->getkey();
//簽名步驟一:按字典序排序參數(shù)
ksort($data);
$string = $this->callbacktourlparams($data);
//簽名步驟二:在string后加入key
if($appwxpay_key){
$string = $string."&key=".$appwxpay_key;
}
//簽名步驟三:md5加密
$string = md5($string);
//簽名步驟四:所有字符轉(zhuǎn)為大寫
$result_ = strtoupper($string);
return $result_;
}
/**
* 格式化參數(shù)格式化成url參數(shù)
*/
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數(shù)據(jù)返回
* @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 支付回調(diào)程序處理
* @author yangzl
* @date(20180820)
*/
public function proorders($data){
if (!$data) {
$date = date("y-m-d h:i:s",time());
log::write( "proorders方法錯(cuò)誤".$date);
}
//處理則返回?cái)?shù)據(jù)入庫(kù) 分表
$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){ //不存在
//存數(shù)據(jù)
$table_id = json_decode($orders_info['attach'],true)['table_id'];
//根據(jù)tableid查詢桌臺(tái)信息
$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'];
}
//回調(diào)支付信息
$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, //是否關(guān)注公眾賬號(hào)
'sub_mch_id' => $orders_info['sub_mch_id'],
'total_fee' =>sprintf("%.2f",$orders_info['total_fee']/100),
'bank_type' => $orders_info['bank_type'],
);
//存數(shù)據(jù)
$add_data = $orders_model->add_pay_info($branch_id,$pay_info);
if(!$add_data){
log::write( "支付數(shù)據(jù)存儲(chǔ)失敗".$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---響應(yīng)數(shù)據(jù):".json_encode($order_data)."\r\n------------\r\n");
//數(shù)據(jù)處理
$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')."微信支付數(shù)據(jù)處理結(jié)果---------\r\n---響應(yīng)數(shù)據(jù):".json_encode($data_handle)."\r\n------------\r\n");
//支付方式入庫(kù)
$pay_data = array(
'orders_id' => $data_handle['orders_id'], //訂單編號(hào)
'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, // 桌臺(tái)id
);
//添加副表
$pay_sn = $orders_model->add_orders_pay_sn($pay_data);
$pay_state = $data_handle['state'];
if($pay_state == 1){ //完成訂單
//完成訂單后,沒(méi)有確認(rèn)的訂單也全部清空 add yangzl
$del_redis_orders = $orders_model->del_redis_orders_p($branch_id, $table_id);
if (!$del_redis_orders){
log::write( "現(xiàn)金訂單完成后收尾".$table_id);
}
//設(shè)置狀態(tài)
$table_model = new tablesmodel();
$state = $table_model->set_table_state($table_id, $branch_id, 4);
}
// 服務(wù)員下單一對(duì)一推送
$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( "支付訂單號(hào)數(shù)據(jù)支付失敗::支付訂單號(hào)".$orders_info['transaction_id']);
exit();
}
}else{
log::write( "支付訂單號(hào)數(shù)據(jù)已處理".$orders_info['transaction_id']);
$this->returnxml();
exit();
}
}
/**********寫入日志方法***********/
/**
* 日志記錄
* @param $path string 日志文件目錄
* @param $file string 日志文件名,不包含后綴
* @param $content string 記錄內(nèi)容
* @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+");
}
/**
* 寫內(nèi)容
* @param $filename string 日志文件名
* @param $data string 記錄內(nèi)容
* @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;
}
}
}
?>
如您對(duì)本文有疑問(wèn)或者有任何想說(shuō)的,請(qǐng)點(diǎn)擊進(jìn)行留言回復(fù),萬(wàn)千網(wǎng)友為您解惑!
總結(jié)
以上是生活随笔為你收集整理的php 微信支付md5签名,微信支付回调验证签名处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android cpu负载 工具,计算A
- 下一篇: 500 内部服务器错误php,如何解决p