PHP实现微信企业自动打款

关于企业付款可以阅读微信商户平台的相关介绍
网址:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1
根据官网文档的介绍。用到的微信官方接口是
https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers
而实现这个功能是必须要证书的。(重点)而且需要传递的数据格式为xml类型
/**
* 发送xml
*
* @param string $trade_no
* 订单号
* @param string $open_id
* 微信openid
* @param string $re_user_name
* 真实姓名
* @param string a m o u n t ∗ 金 额 ∗ @ r e t u r n b o o l e a n ∗ / p u b l i c f u n c t i o n s e n d X m l ( amount * 金额 * @return boolean */ public function sendXml( amount@returnboolean/publicfunctionsendXml(trade_no, $open_id, $re_user_name, $amount)
{
$url = “https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers”;
$mch_appid = config(‘weixin.app_id’);
$mch_id = config(‘weixin.mch_id’);
$secrect_key = config(‘weixin.api_secret’);
$data = array(
‘mch_appid’ => $mch_appid, // 商户账号appid
‘mchid’ => $mch_id, // 商户号
‘nonce_str’ => $this->nonce_str(), // 随机字符串
‘partner_trade_no’ => $trade_no, // 商户订单号
‘openid’ => $open_id, // 用户openid
‘check_name’ => ‘NO_CHECK’, // 校验用户姓名选项,
‘re_user_name’ => $re_user_name, // 收款用户姓名
‘amount’ => $amount, // 金额
‘desc’ => “微信提现” // 企业付款描述信息
);

    // 生成签名算法
    $secrect_key = $secrect_key; // /这个就是个API密码。MD5 32位。
    //字典排序
    $data = array_filter($data);
    ksort($data);
    $str = '';
    foreach ($data as $k => $v) {
        $str .= $k . '=' . $v . '&';
    }
    $str .= 'key=' . $secrect_key;

    $data['sign'] = strtoupper(md5($str));
   $xmldate = $this->arraytoxml($data); //把数组转换为xml格式
    $isdir = $_SERVER['DOCUMENT_ROOT'] . "/cert/"; // 证书位置;绝对路径
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置执行最长秒数
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_URL, $url); // 抓取指定网页
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 终止从服务端进行验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //
    curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); // 证书类型
    curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem'); // 证书位置
    curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); // CURLOPT_SSLKEY中规定的私钥的加密类型
    curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem'); // 证书位置
    curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
    curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
    curl_setopt($ch, CURLOPT_POST, 1); // post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldate); // 全部数据使用HTTP协议中的"POST"操作来发送

    $result = curl_exec($ch); // 执行回话
    $result = $this->xmltoarray($result);
    return $result;
}

// 组合xml
public function arraytoxml($data)
{
    $str = '';
    foreach ($data as $k => $v) {
        $str .= '<' . $k . '>' . $v . '';
    }
    $str .= '';
    return $str;
}

// 生成随机字符串
public function nonce_str()
{
    list ($usec, $sec) = explode(" ", microtime());
    $now_date = getdate($sec);

    $now_usec = floor($usec * 1000);
    $tody_time = $now_date['hours'] * 3600 + $now_date['minutes'] * 60 + $now_date['seconds']; // 今天过去了多少秒
    $today_time_str = substr(strval($tody_time + 100000), 1, 5); // 5位长度

    $nonce_str = date('ymd', $sec) . $today_time_str . $now_usec;
    return $nonce_str;
}

// xml转数组
function xmltoarray($xml)
{
    // 禁止引用外部xml实体
    libxml_disable_entity_loader(true);
    $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
    $val = json_decode(json_encode($xmlstring), true);
    return $val;
}

再次需要注意的是实现这个功能需要在商户平台开通这个功能,并且90天内有企业流水才可实现。

你可能感兴趣的:(PHP实现微信企业自动打款)