class WeixinPayToUser
{
public $parameters = [];
public $SSLROOTCA_PATH='';
public $SSLCERT_PATH='';
public $SSLKEY_PATH='';
public $appid='';
public $secret='';
public $mchid='';
public $key='';
public function __construct()
{
$this->url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
$this->curl_timeout = 10;
$this->SSLROOTCA_PATH=dirname(__FILE__).'/weixin/cert/rootca.pem';
$this->SSLCERT_PATH=dirname(__FILE__).'/weixin/cert/apiclient_cert.pem';
$this->SSLKEY_PATH=dirname(__FILE__).'/weixin/cert/apiclient_key.pem';
}
public function setParameter($key,$value){
$this->parameters[$key]=$value;
}
function arrayToXml($arr,$dom=0,$item=0){
if (!$dom){
$dom = new DOMDocument("1.0");
}
if(!$item){
$item = $dom->createElement("xml");
$dom->appendChild($item);
}
foreach ($arr as $key=>$val){
$itemx = $dom->createElement(is_string($key)?$key:"item");
$item->appendChild($itemx);
if (!is_array($val)){
$text = $dom->createTextNode($val);
$itemx->appendChild($text);
}else {
$this->arrayToXml($val,$dom,$itemx);
}
}
$dom->encoding = 'UTF-8';
return $dom->saveXML();
}
public function getSign($paramArr){
ksort($paramArr);
$paramStr = http_build_query($paramArr);
$paramStr=urldecode($paramStr);
$param_temp=$paramStr.'&key='.$this->key;
$signValue=strtoupper(md5($param_temp));
return $signValue;
}
public function createXml()
{
$this->parameters['mch_appid'] = $this->appid;
$this->parameters['mchid'] = $this->mchid;
$this->parameters['nonce_str'] = 'dddfff';
$this->parameters['sign'] = $this->getSign($this->parameters);
$a= $this->arrayToXml($this->parameters);
return $a;
}
public function pay(){
$xml=$this->createXml();
$url=$this->url;
return $this->postXmlSSLCurl($xml,$url,$second=30);
}
function postXmlSSLCurl($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_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $this->SSLCERT_PATH);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $this->SSLKEY_PATH);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "curl出错,错误码:$error"."<br>";
echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
curl_close($ch);
return false;
}
}
}
?>