微信企业付款到用户零钱

 public function tixian()
    {
        //结算
        $data = array(
            'mch_appid' => '#######',//商户账号appid
            'mchid' => '#####',//商户号
            'nonce_str' => '616516516',//随机字符串
            'partner_trade_no' => date("YmdHis") . rand(10000, 90000) . rand(10000, 90000),//商户订单号
            'openid' => $openid,//用户openid
            'check_name' => 'NO_CHECK',//校验用户姓名选项,
            're_user_name' => $username,//收款用户姓名
            'amount' => $money,//金额
            'desc' => '积分提现',//企业付款描述信息
            'spbill_create_ip' => '120.26.79.188',//Ip地址
        );
        $secrect_key = '#####';///这个就是个API密码。32位的。。随便MD5一下就可以了
        $data = array_filter($data);
        ksort($data);
        $str = '';
        foreach ($data as $k => $v) {
            $str .= $k . '=' . $v . '&';
        }
        $str .= 'key=' . $secrect_key;
        $data['sign'] = md5($str);
        $xml = arraytoxml($data);
        // echo $xml;
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        $res = curl($xml, $url);
        $return = xmltoarray($res);
        dump($return)

  

function arraytoxml($data){
    $str='<xml>';
    foreach($data as $k=>$v) {
        $str.='<'.$k.'>'.$v.'</'.$k.'>';
    }
    $str.='</xml>';
    return $str;
}
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;
}

function curl($param="",$url) {

    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();                                      //初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);                 //抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);           // 增加 HTTP Header(头)里的字段
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch,CURLOPT_SSLCERT,ROOT_PATH.'/vendor/WxpayAPI/cert/apiclient_cert.pem'); //这个是证书的位置绝对路径
    curl_setopt($ch,CURLOPT_SSLKEY,ROOT_PATH.'/vendor/WxpayAPI/cert/apiclient_key.pem'); //这个也是证书的位置绝对路径
    $data = curl_exec($ch);                                 //运行curl
    curl_close($ch);
    return $data;
}

猜你喜欢

转载自www.cnblogs.com/king-sxx/p/9178687.html