php微信支付php程式碼
阿新 • • 發佈:2018-12-23
//配置調起支付前的配置引數 public $config=array( 'appid'=>'',// 開放平臺appid 'mch_id'=>'',//商戶id 'device_info'=>'web', 'api_key'=>'',//商戶密匙 'nonce_str'=>'設定的隨機字串',//隨機字串 'sign_type'=>'MD5', 'body'=>'app支付訂單', 'out_trade_no'=>'',//訂單號 'total_fee'=>'',//總金額 'spbill_create_ip'=>'',//使用者端ip 'notify_url'=>'',//回撥地址 'trade_type'=>'APP' ); public function pay(){ //填寫基本資訊 //獲取當前伺服器ip function get_client_ip() { if ($_SERVER['REMOTE_ADDR']) { $cip = $_SERVER['REMOTE_ADDR']; } elseif (getenv("REMOTE_ADDR")) { $cip = getenv("REMOTE_ADDR"); } elseif (getenv("HTTP_CLIENT_IP")) { $cip = getenv("HTTP_CLIENT_IP"); } else { $cip = "unknown"; } return $cip; } $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST']:''); $out_trade_no=$_POST['osn'];//訂單號 $total_fee = $_POST['total_fee'];//價格 //除錯 // $out_trade_no=$_GET['osn'];//訂單號 // $total_fee = $_GET['total_fee'];//價格 //獲取指定長度的隨機字串 function getRandChar($length){ $str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $max = strlen($strPol)-1; for($i=0;$i<$length;$i++){ $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介於min和max兩個數之間的一個隨機整數 } return $str; } // echo getRandChar(32);exit; $data['appid']=$this->config['appid']; $data['mch_id']=$this->config['mch_id']; $data['device_info']=$this->config['device_info']; $data['nonce_str']=getRandChar(32); //隨機字串 // $data['nonce_str']=$this->config['nonce_str']; //隨機字串測試 $data['body']=$this->config['body']; $data['out_trade_no']=$out_trade_no; $data['total_fee']=$total_fee*100; $data['spbill_create_ip ']=$_SERVER['REMOTE_ADDR'];//使用者ip $data['trade_type']='APP'; $data['notify_url']=$this->config['notify_url']; if($out_trade_no=='' or $total_fee==''){ $sever=array(); $sever['code']='4001'; $sever['message']='無引數或引數錯誤'; $sever['info']=''; echo json_encode($sever); exit; } //生成第一次簽名陣列字典排序 ksort($data); // echo $data; //字串拼接 foreach ($data as $key => $value) { $stringA.=$key.'='.$value.'&'; } //第二次拼接得到簽名 $stringSignTemp=$stringA."key=".$this->config['api_key']; // echo $stringSignTemp; $stringSignTemp=preg_replace('# #','',$stringSignTemp); // echo $stringSignTemp; $data['sign']=strtoupper(MD5($stringSignTemp));// // echo $data['sign']; // var_dump($data);exit; //陣列轉xml $xml='<xml>'; foreach ($data as $key => $value) { $xml=$xml.'<'.$key.'><![CDATA['.$value.']]></'.$key.'>'; } $xml=$xml.'</xml>'; // echo $xml; // exit; // echo json_encode($data); //準備要傳送的請求 $payurl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //微信統一下單介面地址 //傳送xml的方法 function postXmlCurl($xml, $url, $useCert = false, $second = 30){ $ch = curl_init(); //設定超時 curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2); //設定header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求結果為字串且輸出到螢幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if($useCert == true){ //設定證書 //使用證書:cert 與 key 分別屬於兩個.pem檔案 curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); //curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH); curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); //curl_setopt($ch,CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH); } //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //執行curl $data = curl_exec($ch); //返回結果 if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); return false; } } $Wxinxml= postXmlCurl($xml,$payurl); $parser = xml_parser_create(); //建立解析器 xml_parse_into_struct($parser, $Wxinxml, $values, $index); //解析到陣列 xml_parser_free($parser); //釋放資源 //顯示陣列結構 // print_r($values); // var_dump($values);exit; $Arlist=array(); for($i=0;$i<count($values);$i++){ if($values[$i]['tag']!='XML'){ $Arlist[$values[$i]['tag']]=$values[$i]['value']; } } if($Arlist['RETURN_CODE']!='SUCCESS'){ $sever=array(); $sever['code']='4005'; $sever['message']='訂單生成失敗'; $sever['info']=''; echo json_encode($sever); exit; } // var_dump($Arlist);exit; //時間戳 $time=time(); $warr=array( 'appid'=>$Arlist['APPID'], 'partnerid'=>$Arlist['MCH_ID'], // 'device_info'=>$Arlist['DEVICE_INFO'], 'noncestr'=>$Arlist['NONCE_STR'], 'prepayid'=>$Arlist['PREPAY_ID'], 'package'=>"Sign=WXPay", // 'trade_type'=>$Arlist['TRADE_TYPE'], ); $warr['timestamp']=$time; //二次簽名 ksort($warr); $stringA=''; foreach ($warr as $key => $value) { $stringA.=$key.'='.$value.'&'; } $stringSignTemp=$stringA."key=".$this->config['api_key']; $stringSignTemp=preg_replace('# #','',$stringSignTemp); // echo $stringSignTemp; $warr['sign']=strtoupper(MD5($stringSignTemp)); $sever=array(); $sever['code']='200'; $sever['message']='統一下單'; $sever['info']=$warr; echo json_encode($sever); exit; }