<?php
class wxPay
{
protected $SSLCERT_PATH = '../cert/apiclient_cert.pem';
protected $SSLKEY_PATH = '../cert/apiclient_key.pem';
protected $APPID = 'wxa32c969fa0fc81b7';
protected $MCHID = '1423340802';
protected $KEY = 'duola90da6f1265e4901ffb80afaa36f';
protected $APPSECRET = '759cd9ed5683c7dbc06a8ab5e95086db';
protected $APPURL = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
/**
* 微信企业付款
* @param [type] $openid 商户appid下,某用户的openid
* @param [type] $username 收款用户真实姓名。
* @param [type] $desc 企业付款操作说明信息。必填。
* @param [type] $money 企业付款金额,单位为分
* @author saso <--415944661@qq.com-->
*/
function pay($openid = '', $username = '', $desc = '', $money = 0)
{
$apiUrl = $this->APPURL $Parameters = array();
$Parameters['amount'] = $money $Parameters['check_name'] = 'OPTION_CHECK' $Parameters['desc'] = $desc $Parameters['mch_appid'] = $this->APPID $Parameters['mchid'] = $this->MCHID $Parameters['nonce_str'] = $this->createNoncestr() $Parameters['openid'] = $openid $Parameters['partner_trade_no'] = 'saso' . time() . rand(10000, 99999) $Parameters['re_user_name'] = $username $Parameters['spbill_create_ip'] = $_SERVER['SERVER_ADDR'] $Parameters['sign'] = $this->getSign($Parameters) $xml = $this->arrayToXml($Parameters);
$res = $this->postXmlSSLCurl($xml, $apiUrl);
$return = $this->xmlToArray($res);
return $return;
}
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;
}
function getSign($Obj)
{
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
</br>';
$String = $String . "&key=849829de4e8a32eca903dc826cf52ea9";
</br>";
$String = md5($String);
</br>";
$result_ = strtoupper($String);
</br>";
return $result_;
}
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;
}
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;
}
function xmlToArray($xml)
{
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
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>";
curl_close($ch);
return false;
}
}
}