class WeiXinPay{
protected $APPID = appid;
protected $APPSECRET = secret;
protected $MCHID = '11111111';
protected $KEY = '192006250b4c09247ec02edce69f6a2d';
protected $APPURL = 'https://smart.afei.com/receivesuc';
protected $TRADETYPE = 'JSAPI';
protected $BODY = 'wx/book';
function __construct($openid,$outTradeNo,$totalFee){
$this->openid = $openid;
$this->outTradeNo = $outTradeNo;
$this->totalFee = $totalFee;
}
public function pay(){
$result = $this->weixinapp();
return $result;
}
private function weixinapp(){
$unifiedorder=$this->unifiedorder();
$parameters=array(
'appId'=>$this->APPID,
'timeStamp'=>''.time().'',
'nonceStr'=>$this->createNoncestr(),
'package'=>'prepay_id='.$unifiedorder['prepay_id'],
'signType'=>'MD5'
);
$parameters['paySign']=$this->getSign($parameters);
return $parameters;
}
private function unifiedorder(){
$parameters = array(
'appid' => $this->APPID,
'mch_id'=> $this->MCHID,
'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],
'notify_url'=>$this->APPURL,
'nonce_str'=> $this->createNoncestr(),
'out_trade_no'=>$this->outTradeNo,
'total_fee'=>floatval($this->totalFee),
'open_id'=>$this->openid,
'trade_type'=>$this->TRADETYPE,
'body' =>$this->BODY,
);
$parameters['sign'] = $this->getSign($parameters);
$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;
}
protected function arrayToXml($arr){
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
protected function xmlToArray($xml){
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
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);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
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);
$data = curl_exec($ch);
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw new WxPayException("curl出错,错误码:$error");
}
}
protected function getSign($Obj){
foreach ($Obj as $k => $v){
$Parameters[$k] = $v;
}
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
$String = $String."&key=".$this->KEY;
$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 .= $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;
}
}