<?php /** * 微信支付服務端相關業(yè)務 * @author 魏鵬 */ class WxapiAction extends Action{ const WXURL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; const NOIFYURL = "";//微信異步回掉地址 const APPID = ''; const MCHID = '';//商家號 const KEY = ''; const APPSECRET = ''; private $wxDate = null; /** * 請求微信API進行微信統(tǒng)一下單 * URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder */ public function getWxPrepayid(){ header("Content-type: text/html; charset=utf-8"); $order = $this -> chargepay_api(); //配置參數(shù)檢測 $this -> checkCofigParam(); $noceStr = md5(rand(100,1000).time());//獲取隨機字符串 $time = time(); $body = $_REQUEST['c_Money1']*100; $paramarr = array( "appid" => WxapiAction::APPID, "body" => "",//說明內(nèi)容 "mch_id" => WxapiAction::MCHID, "nonce_str" => $noceStr, "notify_url" => WxapiAction::NOIFYURL, "out_trade_no"=> $order, "total_fee" => "1", "trade_type" => "APP" ); $sign = $this -> sign($paramarr);//生成簽名 $paramarr['sign'] = $sign; $paramXml = "<xml>"; foreach($paramarr as $k => $v){ $paramXml .= "<" . $k . ">" . $v . "</" . $k . ">"; } $paramXml .= "</xml>"; file_put_contents("./test.txt", ""); file_put_contents("./test.txt", $paramXml); $ch = curl_init (); @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查 @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 從證書中檢查SSL加密算法是否存在 @curl_setopt($ch, CURLOPT_URL, "https://api.mch.weixin.qq.com/pay/unifiedorder"); @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @curl_setopt($ch, CURLOPT_POST, 1); @curl_setopt($ch, CURLOPT_POSTFIELDS, $paramXml); @$resultXmlStr = curl_exec($ch); if(curl_errno($ch)){ print curl_error($ch); } curl_close($ch); file_put_contents("./test2.txt", ""); file_put_contents("./test2.txt", $resultXmlStr); $result = $this -> xmlToArray($resultXmlStr); $time2 = time(); $prepayid = $result['prepay_id']; $sign = ""; $noceStr = md5(rand(100,1000).time());//獲取隨機字符串 $paramarr2 = array( "appid" => WxapiAction::APPID, "noncestr" => $noceStr, "package" => "Sign=WXPay", "partnerid" => WxapiAction::MCHID, "prepayid" => $prepayid, "timestamp" => $time2 ); $paramarr2["sign"] = $this -> sign($paramarr2);//生成簽名 echo json_encode($paramarr2); } /** * 檢測配置信息是否完整 */ public function checkCofigParam(){ if(WxapiAction::APPID == ""){ echo "微信APPID未配置"; exit; }elseif(WxapiAction::MCHID == ""){ echo "微信商戶號MCHID未配置"; exit; }elseif(WxapiAction::KEY == ""){ echo "微信API密鑰KEY未配置"; exit; } } /** * sign拼裝獲取 */ private function sign($param){ $sign = ""; foreach($param as $k => $v){ $sign .= $k."=".$v."&"; } $sign .= "key=".WxapiAction::KEY; $sign = strtoupper(md5($sign)); return $sign; } /** * xml轉為數(shù)組 */ private function xmlToArray($xmlStr){ $msg = array(); $postStr = $xmlStr; $msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); return $msg; } //微信異步回調認證 public function noify_url(){ $xmlInfo = $GLOBALS['HTTP_RAW_POST_DATA']; $this -> logecho($xmlInfo);//log打印保存 //解析xml $arrayInfo = $this -> xmlToArray($xmlInfo); $this -> wxDate = $arrayInfo; /* 測試數(shù)據(jù)打印log數(shù)組轉字符串=============== */ $test = ""; foreach($arrayInfo as $k => $v){ $test .= $k.":".$v."\t\r\n"; } /*======================================== */ $this -> logecho("數(shù)據(jù)打印測試:".$test);//log打印保存 if($arrayInfo['return_code'] == "SUCCESS"){ if($return_msg != null){ echo $this -> returnInfo("FAIL","簽名失敗"); $this -> logecho("簽名失敗:".$sign);//log打印保存 }else{ $wxSign = $arrayInfo['sign']; unset($arrayInfo['sign']); $arrayInfo['appid'] = WxapiAction::APPID; $arrayInfo['mch_id'] = WxapiAction::MCHID; ksort($arrayInfo);//按照字典排序參數(shù)數(shù)組 $sign = $this -> sign($arrayInfo);//生成簽名 $this -> logecho("數(shù)據(jù)打印測試簽名signmy:".$sign."微信sign:".$wxSign);//log打印保存 if($this -> checkSign($wxSign,$sign)){ echo $this -> returnInfo("SUCCESS","OK"); $this -> logecho("簽名驗證結果成功:".$sign);//log打印保存 $this -> orderServer();//訂單處理業(yè)務邏輯 }else{ echo $this -> returnInfo("FAIL","簽名失敗"); $this -> logecho("簽名驗證結果失敗:".$sign);//log打印保存 } } }else{ echo $this -> returnInfo("FAIL","簽名失敗"); } } private function returnInfo($type,$msg){ if($type == "SUCCESS"){ return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code></xml>"; }else{ return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code><return_msg><![CDATA[{$msg}]]></return_msg></xml>"; } } //打印log private function logecho($msg){ $str = file_get_contents("./logwx.txt"); file_put_contents("./logwx.txt",$str."\t\r\n".date("Y:m:d H:i:s")."\t\r\n".$msg); } //簽名驗證 private function checkSign($sign1,$sign2){ return trim($sign1) == trim($sign2); } /*fms訂單查詢加值業(yè)務處理 * @param orderNum 訂單號 */ private function orderServer(){ //業(yè)務邏輯處理 } //生成訂單號碼 private function chargepay_api(){ } }
|