控制器
class Pay extends Controller { const WX_PAY_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; const NOTIFY_URL = "http://xxx.xxx.com/wx_notify"; const WX_APPID = "wx***********"; const WX_MCHID = "1********"; const WX_MCHKEY = "*********"; //key是在商戶平臺API安全裏本身設置的 public function wxPay() { $money = input('post.money/s','','trim'); $nonce_str = randCode(); //調用隨機字符串生成方法獲取隨機字符串 $data['appid'] = self::WX_APPID; //appid $data['mch_id'] = self::WX_MCHID; //商戶號 $data['body'] = "APP支付測試"; $data['spbill_create_ip'] = getIp(); //ip地址 $data['total_fee'] = $money/100; //金額 $data['out_trade_no'] = time().mt_rand(10000,99999); //商戶訂單號,不能重複 $data['nonce_str'] = $nonce_str; //隨機字符串 $data['notify_url'] = self::NOTIFY_URL; //回調地址,用戶接收支付後的通知,必須爲能直接訪問的網址,不能跟參數 $data['trade_type'] = 'APP'; //支付方式 //將參與簽名的數據保存到數組 注意:以上幾個參數是追加到$data中的,$data中應該同時包含開發文檔中要求必填的剔除sign之外的全部數據 $data['sign'] = getSign($data,self::WX_MCHKEY); //獲取簽名 $xml = ToXml($data); //數組轉xml //curl 傳遞給微信方 $url = self::WX_PAY_URL; //運行curl $data = curlHttpRequest($url, $xml); //返回結果 if($data){ //返回成功,將xml數據轉換爲數組. $re = FromXml($data); if($re['return_code'] != 'SUCCESS'){ echo json_encode("code"=>"201","data"=>"","msg"=>'簽名失敗'); } else { //接收微信返回的數據,傳給APP! $arr =array( 'prepayid' =>$re['prepay_id'], 'appid' => self::WX_APPID, 'partnerid' => self::WX_MCHID, 'package' => 'Sign=WXPay', 'noncestr' => $nonce_str, 'timestamp' =>time(), ); //第二次生成簽名 $sign = getSign($arr,self::WX_MCHKEY); $arr['sign'] = $sign; echo json_encode("code"=>"200","data"=>$arr,"msg"=>'簽名成功'); } } else { $error = curl_errno($ch); curl_close($ch); echo json_encode("code"=>"201","data"=>'"","msg"=>'curl出錯,錯誤碼:$error'); } // 微信支付回調 public function wxNotify(){ //接收微信返回的數據數據,返回的xml格式 $xmlData = file_get_contents('php://input'); //將xml格式轉換爲數組 $data = FromXml($xmlData); //用日誌記錄檢查數據是否接受成功,驗證成功一次以後,可刪除。 $file = fopen('./log.txt', 'a+'); fwrite($file,var_export($data,true)); //爲了防止假數據,驗證簽名是否和返回的同樣。 //記錄一下,返回回來的簽名,生成簽名的時候,必須剔除sign字段。 $sign = $data['sign']; unset($data['sign']); if($sign == getSign($data)){ //簽名驗證成功後,判斷返回微信返回的 if ($data['result_code'] == 'SUCCESS') { //根據返回的訂單號作業務邏輯 $arr = array( 'pay_status' => 1, ); $re = M('order')->where(['order_sn'=>$data['out_trade_no']])->save($arr); //處理完成以後,告訴微信成功結果! if($re){ echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';exit(); } } else { //支付失敗,輸出錯誤信息 $file = fopen('./log.txt', 'a+'); fwrite($file,"錯誤信息:".$data['return_msg'].date("Y-m-d H:i:s"),time()."\r\n"); } } else { $file = fopen('./log.txt', 'a+'); fwrite($file,"錯誤信息:簽名驗證失敗".date("Y-m-d H:i:s"),time()."\r\n"); } } } ?>
調用方法 function.php
<?php function ToXml($data=array()) { if(!is_array($data) || count($data) <= 0) { return '數組異常'; } $xml = "<xml>"; foreach ($data as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } function randCode() { $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62個字符 $str = str_shuffle($str); $str = substr($str,0,32); return $str; } function getSign($params,$key) { ksort($params); //將參數數組按照參數名ASCII碼從小到大排序 foreach ($params as $key => $item) { if (!empty($item)) { //剔除參數值爲空的參數 $newArr[] = $key.'='.$item; // 整合新的參數數組 } } $stringA = implode("&", $newArr); //使用 & 符號鏈接參數 $stringSignTemp = $stringA."&key=".$key; //拼接key // key是在商戶平臺API安全裏本身設置的 $stringSignTemp = MD5($stringSignTemp); //將字符串進行MD5加密 $sign = strtoupper($stringSignTemp); //將全部字符轉換爲大寫 return $sign; } function getIp() { if ($_SERVER['REMOTE_ADDR']) { $ip = $_SERVER['REMOTE_ADDR']; } elseif (getenv("REMOTE_ADDR")) { $ip = getenv("REMOTE_ADDR"); } elseif (getenv("HTTP_CLIENT_IP")) { $ip = getenv("HTTP_CLIENT_IP"); } else { $ip = "unknown"; } return $ip; } function curlHttpRequest($url,$xml) { //header("Content-type:text/xml");); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); if(stripos($url,"https://")!==FALSE){ curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); } else { curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗 } //設置header curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求結果爲字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //設置超時 curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_POST, TRUE); //傳輸文件 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //釋放句柄 curl_close($ch); //運行curl $data = curl_exec($ch); //返回結果 return $data; } ?>