public functionpay()
{$result = $this->weChatPay();return $result;
}//請求微信統一下單接口
private functionweChatPay()
{$param = array('appid' => $this->APPID,//小程序id
'mch_id'=> $this->MCHID,//商戶id
'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],//終端ip
'notify_url'=>$this->APPURL, //回調通知地址
'nonce_str'=> $this->createNoncestr(),//隨機字符串
'out_trade_no'=>$this->outTradeNo,//商戶訂單編號
'total_fee'=>floatval($this->totalFee), //總金額
'openid'=>$this->openid,//用戶openid
'trade_type'=>$this->TRADETYPE,//交易類型
'body' =>$this->BODY, //商品信息
);//通過簽名算法計算得出的簽名值,詳見簽名生成算法
$param['sign'] = $this->getSign($param);//將數組內容轉為xml格式,向微信發出請求
$xmlData = $this->arrayToXml($parameters);$xml_result = $this->postXmlCurl($xmlData,'https://api.mch.weixin.qq.com/pay/unifiedorder',60);$result = $this->xmlToArray($xml_result);return $result;//var_dump($result);
/** array(9) {
["return_code"]=>
string(7) "SUCCESS"
["return_msg"]=>
string(2) "OK"
["appid"]=>
string(18) "wx2d4fefcfe1a39c9b"
["mch_id"]=>
string(10) "1420537902"
["nonce_str"]=>
string(16) "JASf0yXVuPknKm2J"
["sign"]=>
string(32) "BE3CF30459D01660BB9AB2DE0AD023CE"
["result_code"]=>
string(7) "SUCCESS"
["prepay_id"]=>
string(36) "wx29154208585459f31c6875691178935371"
["trade_type"]=>
string(5) "JSAPI"
}
}
/*
* 對要發送到微信統一下單接口的數據進行簽名*/
protected function getSign($Obj){foreach ($Obj as $k => $v){$param[$k] = $v;
}//簽名步驟一:按字典序排序參數
ksort($param);$String = $this->formatBizQueryParaMap($param, false);//簽名步驟二:在string后加入KEY
$String = $String."&key=".$this->KEY;//簽名步驟三:MD5加密
$String = md5($String);//簽名步驟四:所有字符轉為大寫
$result_ = strtoupper($String);return $result_;
}/**排序並格式化參數方法,簽名時需要使用*/
protected function formatBizQueryParaMap($paraMap, $urlencode)
{$buff = "";ksort($paraMap);foreach ($paraMap as $k => $v)
{if($urlencode)
{$v = urlencode($v);
}//$buff .= strtolower($k) . "=" . $v . "&";
$buff .= $k . "=" . $v . "&";
}$reqPar = "";if (strlen($buff) > 0)
{$reqPar = substr($buff, 0, strlen($buff)-1);
}return $reqPar;
}/** 生成隨機字符串方法*/
protected function createNoncestr($length = 32){$chars = "abcdefghijklmnopqrstuvwxyz0123456789";$str ="";for ( $i = 0; $i < $length; $i++) {$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}return $str;
}//數組轉字符串方法
protected function arrayToXml($arr){$xml = "";foreach ($arr as $key=>$val)
{if (is_numeric($val)){$xml.="".$val."".$key.">";
}else{$xml.="".$key.">";
}
}$xml.="";return $xml;
}//將xml字符串轉換為數組
protected static function xmlToArray($xml){$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);return $array_data;
}//發送xml請求方法
private static function postXmlCurl($xml, $url, $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, FALSE); //嚴格校驗
//設置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);//要求結果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);set_time_limit(0);//運行curl
$data = curl_exec($ch);//返回結果
if ($data) {
curl_close($ch);return $data;
}else{$error = curl_errno($ch);
curl_close($ch);throw new WxPayException("curl出錯,錯誤碼:$error");
}
}