<?php
require_once "WxTransfers.Config.php";
/**
* 微信企业转账工具类
*/
class WxTransfers
{
// 企业转账请求地址
const TRANSFERS_URL = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
//获取转账信息地址
const GETINFO_URL='https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo';
// 转账需要的配置 'wxappid','mch_id','key'
private $_keys;
// 转账需要的证书文件 'api_cert', 'api_key', 'rootca',请传入绝对路径!!!
private $_cert;
protected $log_file;
public $error;
// 相关配置必备参数
protected $_parameters = array();
// 最后一次生产的订单号
protected $_lastPartnerTradeNo;
// 记录最后一次发送请求的结果对象
protected $_lastResult;
// 最后一次随机数
protected $_lastRandNum;
public function __construct($config)
{
$keys = array(
'wxappid',
'mch_id',
'key'
);
$files = array(
'api_cert',
'api_key',
'rootca'
);
foreach ($keys as $key) {
try {
$this->_keys[$key] = $config[$key];
} catch (Exception $e) {
throw new Exception('参数缺失:' . $key);
}
}
foreach ($files as $file) {
try {
$cret_file = $config[$file];
if (is_file($cret_file)) {
$this->_cert[$file] = $cret_file;
}
} catch (Exception $e) {
throw new Exception('证书错误');
}
}
}
public function transfers($parameters){
$this->log($parameters, 'SEND_PARAM');
$this->setParameter('mchid', $this->_keys['mch_id']);
$this->setParameter('mch_appid', $this->_keys['wxappid']);
$must = array(
'openid',
'check_name',
're_user_name',
'amount',
'desc',
'spbill_create_ip',
);
foreach ($must as $key) {
if (isset($parameters[$key]) && $parameters[$key]) {
$this->setParameter($key, $parameters[$key]);
} else
if (! isset($this->_parameters[$key]) || ! $this->_parameters[$key]) {
$this->error = '参数缺损:' . $key;
return false;
}
}
if (! isset($parameters['partner_trade_no'])) {
$parameters['partner_trade_no'] = $this->getPartnerTradeNo();
}
$this->setParameter('partner_trade_no', $parameters['partner_trade_no']);
$this->setParameter('nonce_str', $this->getRand(30, 3));
$postXml = $this->_createXml();
if (! $postXml) {
return false;
}
$this->log($postXml, 'SEND_XML');
$result = $this->curl_post_ssl(self::TRANSFERS_URL, $postXml);
$this->log($result, 'RESULT_XML');
if (! $result) {
return false;
}
$resultObj = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->_lastResult = $resultObj;
if ($resultObj->return_code == 'SUCCESS') { // 成功标识
if ($resultObj->result_code == 'SUCCESS') {
return $resultObj->send_listid;
}
if ($resultObj->return_msg) {
$this->error = (string) $resultObj->return_msg;
return false;
}
$this->error = (string) $resultObj->err_code_des;
return false;
}
if ($resultObj->return_code != 'FAIL') {
$this->error = '返回信息格式异常';
return false;
}
$this->error = (string) $resultObj->return_msg;
return false;
}
/**
* 获取转账信息
* @param unknown $partner_trade_no
* @return boolean|SimpleXMLElement
*/
public function getInfo($partner_trade_no){
$param = array(
'nonce_str' => $this->getRand(30, 3),
'partner_trade_no'=> $partner_trade_no ,
'mch_id' => $this->_keys['mch_id'],
'appid' => $this->_keys['wxappid'],
);
ksort($param);
$unSignParaString = $this->_formatQueryParaMap($param, false);
$param['sign'] = $this->_sign($unSignParaString, $this->_keys['key']);
$xml = $this->arrayToXml($param);
$this->log($xml, 'GETINFO_XML');
$result = $this->curl_post_ssl(self::GETINFO_URL, $xml);
if(!$result){
return false ;
}
$this->log($result, 'RESULT_XML');
$resultObj = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->_lastResult = $resultObj ;
if($resultObj->return_code == 'SUCCESS'){//成功标识
if($resultObj->result_code == 'SUCCESS'){
return $resultObj ;
}
if($resultObj->return_msg){
$this->error = $resultObj->return_msg ;
return false ;
}
$this->error = $resultObj->err_code_des ;
return false ;
}
if($resultObj->return_code != 'FAIL'){
$this->error = '返回信息格式异常';
return false ;
}
$this->error = $resultObj->return_msg ;
return false ;
}
/**
* 设置所需要的参数
* @param $parameter 键值数组/键
* @param $value 值
* @return WxBonusApi
*/
public function setParameter($parameter, $value = null)
{
if (! is_array($parameter)) {
return $this->setParameter(array(
$parameter => $value
));
}
foreach ($parameter as $key => $value) {
$key = trim($key);
$value = trim($value);
$this->_parameters[$key] = $value;
}
return $this;
}
/**
* 获取参数值
* @param $parameter 键名
* @return multitype:
*/
public function getParameter($parameter)
{
return $this->_parameters[$parameter];
}
/**
* 获取随机数
* @param number $len 随机数的位数
* @param number $type 取值范围 1表示数字 2小写字母 4大写字母
* @return string
*/
public function getRand($len = 30, $type = 0)
{
$str = '';
$max = - 1;
if (! $type) {
$type = 3;
}
if ($type & 1) {
$str .= '1234567890';
$max += 10;
}
if ($type & 2) {
$str .= 'abcdefghijklmnopqrstuvwxyz';
$max += 26;
}
if ($type & 4) {
$str .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$max += 26;
}
$rand = '';
for ($i = 0; $i < $len; $i ++) {
$rand .= $str[rand(0, $max)];
}
return $rand;
}
/**
* 生成商户的订单号
* @return string
*/
public function getPartnerTradeNo()
{
$this->_lastPartnerTradeNo = $this->_parameters['mch_id'] . date('YmdHis') . $this->getRand(4, 1); // $this->getRandNum();
return $this->_lastPartnerTradeNo;
}
/**
* 获取最后一次创建生成的订单号
* @return string
*/
public function getLastPartnerTradeNo()
{
return $this->_lastPartnerTradeNo;
}
/**
* 创建XML的方法
* @param number $retcode
* @param string $reterrmsg
* @return boolean|string
*/
private function _createXml()
{
try {
$sign = $this->_getSign();
if (! $sign) {
return false;
}
$this->setParameter('sign', $sign);
return $this->arrayToXml($this->_parameters);
} catch (Exception $e) {
$this->error = $e->getMessage();
return false;
}
}
/**
* 参数转换成XML
* @param array $arr 参数数组
* @return string
*/
public 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;
}
/**
* 获得签名结果
* @return boolean|Ambigous <string, boolean>
*/
protected function _getSign()
{
try {
if ($this->_checkSign() == false) { // 检查生成签名参数
$this->error = '生成签名参数缺失!';
$this->log(json_encode($this->_parameters, JSON_UNESCAPED_UNICODE), 'ERROR_Sign_XML');
return false;
}
ksort($this->_parameters);
$unSignParaString = $this->_formatQueryParaMap($this->_parameters, false);
return $this->_sign($unSignParaString, $this->_keys['key']);
} catch (Exception $e) {
$this->error = $e->getMessage();
return false;
}
}
/