<?php
/**
 * 微信企业付款操作类
 * Author  :  Max.wen
 * DateTime: <15/9/16 11:00>
 */
class WxPay {
    function __construct() {
    }
	
	function pay($url, $obj) {
		$obj["nonce_str"] = $this->create_noncestr();
		$stringA = $this->formatQueryParaMap($obj, false);
		
		$stringSignTemp = $stringA . "&key=" . WECHAT_PAY_KEY_58;
		
		
		$sign = strtoupper(md5($stringSignTemp));
		$obj["sign"] = $sign;
		$postXml = $this->arrayToXml($obj);
		$responseXml = $this->curl_post_ssl($postXml, $url);	
		$responseArray = json_decode(json_encode($responseXml), true);       		
		return $responseArray;
	}
	
	function create_noncestr($length = 32) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		$str = "";
		for($i=0; $i<$length; $i++) {
			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		}
		return $str;
	}
	
	function formatQueryParaMap($paraMap, $urlencode) {
		$buff = "";
		ksort($paraMap);
		foreach($paraMap as $k=>$v) {
			if(null !=$v && "null" != $v && "sign" != $k) {
				if($urlencode) {
					$v = urlencode($v);
				}
				$buff .= $k . "=" . $v . "&";
			}
		}
		$reqPar;
		if(strlen($buff) > 0) {
			$reqPar = substr($buff, 0, strlen($buff) - 1);
		}
		
		return $reqPar;
	}
	
	//数组转Xml
    /**
     * 生成请求xml数据
     * @return string
     */
    public function arrayToXml($arr){
		$xml = "<xml>";
		foreach($arr as $key=>$val) {
			$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
			/**
			if(is_numeric($val)) {
				$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
			} else {
				$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
			}
			**/
		}
		$xml .= "</xml>";
		return $xml;
    }


    /**
     *     作用：使用证书，以post方式提交xml到对应的接口url
    */
    function curl_post_ssl($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);
        //设置证书
        curl_setopt($ch,CURLOPT_CAINFO, WxPayConfig::SSLROOTCA_PATH);
        //使用证书：cert 与 key 分别属于两个.pem文件
        //默认格式为PEM，可以注释
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH);
        //默认格式为PEM，可以注释
        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);
        $data = curl_exec($ch);
        //返回结果
        if($data){
            curl_close($ch);
			return simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
        } else {
            $error = curl_errno($ch);
			echo "url:" . $url . "<br>";
            echo "curl:[" . $error . "]<br>";
            echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>Reason!</a></br>";
            curl_close($ch);
            return false;
        }
    }
}