美文网首页
企业付款到零钱 和 企业现金红包

企业付款到零钱 和 企业现金红包

作者: vins | 来源:发表于2019-10-25 13:03 被阅读0次
企业付款到零钱
<?php
        //企业付款到微信零钱,PHP接口调用方法
define("APPID", "xxx"); // 商户账号appid
define("MCHID", "xxx");         // 商户号
define("SECRECT_KEY", "xxx");  //key为商户平台设置的密钥key


/**
 * @param int $length
 * @return string
 * 生成随机字符串
 */
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 unicode() {
    $str = uniqid(mt_rand(),1);
    $str=sha1($str);
    return md5($str);
}

/**
 * @param $data
 * @return string
 * 数组转换成XML格式
 */
function arraytoxml($data){
    $str='<xml>';
    foreach($data as $k=>$v) {
        $str.='<'.$k.'>'.$v.'</'.$k.'>';
    }
    $str.='</xml>';
    return $str;
}

/**
 * @param $data
 * @return string
 * 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;
}

function curl($param,$url) {

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

/*
$amount 发送的金额(分)目前发送金额不能少于1元
$re_openid, 发送人的 openid
$desc  //  企业付款描述信息 (必填)
$check_name    收款用户姓名 (选填)
*/
function sendMoney($amount,$re_openid,$desc,$check_name,$ip){

    $total_amount = (100) * $amount;
    $data=array(
        'mch_appid'=>APPID,//商户账号appid
        'mchid'=> MCHID,//商户号
        'nonce_str'=>createNoncestr(),//随机字符串
        'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商户订单号
        'openid'=> $re_openid,//用户openid
        'check_name'=>'NO_CHECK',//校验用户姓名选项,
        're_user_name'=> $check_name,//收款用户姓名
        'amount'=>$total_amount,//金额
        'desc'=> $desc,//企业付款描述信息
        'spbill_create_ip'=> $ip,//Ip地址
    );
    $secrect_key=SECRECT_KEY;
    $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);
    $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //调用接口
    $res=curl($xml,$url);
    var_dump($res);exit;
    $return=xmltoarray($res);
    print_r($return);
    //返回来的结果
    // [return_code] => SUCCESS [return_msg] => Array ( ) [mch_appid] => wxd44b890e61f72c63 [mchid] => 1493475512 [nonce_str] => 616615516 [result_code] => SUCCESS [partner_trade_no] => 20186505080216815
    // [payment_no] => 1000018361251805057502564679 [payment_time] => 2018-05-15 15:29:50
    $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
    echo $res= $responseObj->return_code;  //SUCCESS  如果返回来SUCCESS,则发生成功,处理自己的逻辑
    return $res;
?>
企业现金红包
<?php
   public function s(){
        $money = 100; //最低1元,单位分
        $sender = "xxxx";
        $obj2 = array();
        $obj2['wxappid'] = "xxx"; //appid
        $obj2['mch_id'] = "xxx";//商户id
        $obj2['mch_billno'] = date('YmdHis').rand(1000,9999);//组合成28位,根据官方开发文档,可以自行设置
        $obj2['client_ip'] = $_SERVER['REMOTE_ADDR'];
        $obj2['re_openid'] = "xxx";//接收红包openid
        $obj2['total_amount'] = $money;
        $obj2['min_value'] = $money;
        $obj2['max_value'] = $money;
        $obj2['total_num'] = 1;
        $obj2['nick_name'] = $sender;
        $obj2['send_name'] = $sender;
        $obj2['wishing'] = "测试";
        $obj2['act_name'] = $sender."红包";
        $obj2['remark'] = $sender."红包";
        $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
        //$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
        $res = $this->pay($url, $obj2);
        var_dump($res);
    }

    //配置参数信息
    const SHANGHUHAO = "1529607531";//商户号
    const PARTNERKEY = "b4d55f7d4113a6f282987e3f940521c0";  //api,商户后台
    //核心支付函数,参数:请求地址和参数
    function pay($url,$obj) {
        $obj['nonce_str'] = $this->create_noncestr();  //创建随机字符串
        $stringA = $this->create_qianming($obj,false);  //创建签名
        $stringSignTemp = $stringA."&key=b4d55f7d4113a6f282987e3f940521c0";  //签名后加api
        $sign = strtoupper(md5($stringSignTemp));  //签名加密并大写
        $obj['sign'] = $sign;  //将签名传入数组
        $postXml = $this->arrayToXml($obj);  //将参数转为xml格式
        $responseXml = $this->curl_post_ssl($url,$postXml);  //提交请求
        var_dump($responseXml);
        return $responseXml;
    }
    //生成签名,参数:生成签名的参数和是否编码
    function create_qianming($arr,$urlencode) {
        $buff = "";
        ksort($arr); //对传进来的数组参数里面的内容按照字母顺序排序,a在前面,z在最后(字典序)
        foreach ($arr as $k=>$v) {
            if(null!=$v && "null" != $v && "sign" != $k) {  //签名不要转码
                if ($urlencode) {
                    $v = urlencode($v);
                }
                $buff.=$k."=".$v."&";
            }
        }
        if (strlen($buff)>0) {
            $reqPar = substr($buff,0,strlen($buff)-1); //去掉末尾符号“&”
        }
        return $reqPar;
    }
    //生成随机字符串,默认32位
    function create_noncestr($length=32) {
        //创建随机字符
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for($i=0;$i<$length;$i++) {
            $str.=substr($chars, mt_rand(0,strlen($chars)-1),1);
        }
        return $str;
    }
    //数组转xml
    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;
    }
    //post请求网站,需要证书
    function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        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_SSLCERT,public_path().'/WxPay/cert/apiclient_cert.pem');
        curl_setopt($ch,CURLOPT_SSLKEY,public_path().'/WxPay/cert/apiclient_key.pem');
        curl_setopt($ch,CURLOPT_CAINFO,public_path().'/WxPay/cert/apiclient_cert.p12');
        if( count($aHeader) >= 1 ){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
        $data = curl_exec($ch);
        if($data){
            curl_close($ch);
            return $data;
        }
        else {
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }
?>

相关文章

网友评论

      本文标题:企业付款到零钱 和 企业现金红包

      本文链接:https://www.haomeiwen.com/subject/wzssvctx.html