微信支付企业打款,获取用户信息

function weixin_qr_action(){
        //$wapUrl = $this->config['sy_weburl'].'/index.php?m=media&c=weixin_change' ;
        include(APP_PATH.'api/weixin/lib/WxPay.Config.php');
        //注意url要转码urlencode
        $wapUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.WxPayConfig::APPID.'&redirect_uri='.urlencode($this->config['sy_weburl'].'/index.php?m=media&c=weixin_change').'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
        include_once LIB_PATH."yunqrcode.class.php";
        YunQrcode::generatePng2($wapUrl,4);
    }
    
    
    function weixin_change_action(){
        include(APP_PATH.'api/weixin/weixin.php');
        
        $code = $_GET['code'] ;
        
        $weixin_obj = new weixin();
        $res = $weixin_obj->get_user_info($code);
        
      
        $data = array();
        $data['openid'] = $res['openid'];
        $data['amount'] = 150;
        $data['re_user_name'] = $res['nickname'];
        $data['partner_trade_no'] = 'HAG00888610';
         
        
       $re = $weixin_obj->pay_for_customer($data);
       
       
       
       if($re['result_code'] == 'SUCCESS'){
           echo '<script>alert("取现成功,已经成功打入你的微信零钱")</script>';
       }else{
           echo '<script>alert("'.$re['err_code_des'].'")</script>';
       }
        
    }
   



/**

     * 获取用户信息
     * @author zcb
     * @copyright 2016-03-18
     **/
    
    function get_user_info($code){
       
        //获取access_token
        $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WxPayConfig::APPID.'&secret='.WxPayConfig::APPSECRET.'&code='.$code.'&grant_type=authorization_code';
        $res = $this->https_request( $token_url ) ;
        
        $token = json_decode( $res);
        
        if(isset($token->errcode)){
            return array('msg'=>'获取access_token失败','data'=>$token->errcode);
        }
        
        //获取用户信息
        $access_token_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token->access_token.'&openid='.$token->openid;
        
        $user_info = $this->https_request($access_token_url);
        
        return json_decode($user_info,true);
        
        
    }
    
    /**
     * 获取公众号所有的人的openid
     * @author zcb
     * @copyright 2016-03-18
     **/
        
    function get_user_openid(){
        
        $res = $this->get_user_access_token();
        if( $res ){
            $jsoninfo = json_decode($res, true);
            $access_token = $jsoninfo["access_token"];
            $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token";
            $result = $this->https_request($url);
            $jsoninfo = json_decode($result,true);  // 默认false,为Object,若是True,为Array
            
            $data = $jsoninfo->data;
            $arr = $data->openid;      // 获得所有用户的Openid
        }
        
        return $jsoninfo ;
    }
    
    /**
     * 获取用户的access_token
     * @author zcb
     * @copyright 2016-03-18
     **/
    function get_user_access_token(){
        
        $appsecret = WxPayConfig::APPSECRET ;
        $appid = WxPayConfig::APPID ;
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
        
        $res = $this->https_request($url) ;
        
        return $res ;
        
    }
    
    
    function https_request($url){
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
        curl_close($curl);
        return $data;
    }
    
    
    /**
     * 企业打款
     * @author zcb
     * @copyright 2016-03-18
     *
     **/
    
    function pay_for_customer($data){
        
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        $data['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
        $data['check_name'] = 'NO_CHECK';
        $data['desc'] = '客户提现';
        $data['mch_appid'] = WxPayConfig::APPID ;
        $data['mchid'] = WxPayConfig::MCHID ;
        $data['nonce_str'] = $this->create_rand_string();
        $stringA = $this->ToUrlParams($data,false);
        $stringSingTemp = $stringA.'&key='.WxPayConfig::KEY ;
        $sign = strtoupper(md5($stringSingTemp));
        $data['sign'] = $sign ;
       
        $postXml = $this->ToXml($data) ;
       
        $res = $this->postXmlCurl($postXml, $url,true);
        
        $data = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        file_put_contents('weixin.txt',var_export($res,true));
        return $data;
        
    }
    
    
    /**
     * 随机字符串
     * @author zcb
     * @copyright 2016-03-18
     **/
    
    function create_rand_string( $length = 32 ){
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $str = '' ;
        for( $i = 0 ; $i < $length ; $i++){
            $str .=substr($chars, mt_rand(0, strlen($chars)-1),1);
        }
        return $str ;
    }
    
    /**
     * 格式化参数格式化成url参数
     */
    public function ToUrlParams($paramap,$urlencode)
    {
        $buff = "";
        
        ksort($paramap);
        foreach ($paramap as $k => $v)
        {
            if($k != "sign" && $v != null && $v !='null'){
                if($urlencode){
                    $v = urlencode($v);
                }
                $buff .= $k . "=" . $v . "&";
            }
        }
         
        $reqpar = '' ;
        if(strlen($buff) > 0 ){
            $reqpar = substr($buff, 0,strlen($buff)-1);
        }
        
        return $reqpar;
    }
    
    
    /**
     * 把数据组装成xml
     * @author zcb
     * @copyright
     **/
    public function ToXml( $data )
    {
        $xml = "<xml>";
        foreach ( $data as $key => $val )
        {
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
       
        return $xml;
    }
    
    
    /**
     * 以post方式提交xml到对应的接口url
     *
     * @param string $xml  需要post的xml数据
     * @param string $url  url
     * @param bool $useCert 是否需要证书,默认不需要
     * @param int $second   url执行超时时间,默认30s
     * @throws WxPayException
     */
    function postXmlCurl($xml, $url, $useCert = false, $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_RETURNTRANSFER, 1);
    
        //如果有配置代理这里就设置代理
//         if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
//             && WxPayConfig::CURL_PROXY_PORT != 0){
//             curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
//             curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
//         }
        
        
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, false);
        
    
        if($useCert == true){
            //设置证书
            //使用证书:cert 与 key 分别属于两个.pem文件
          // curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
            curl_setopt($ch,CURLOPT_CAINFO, PATH.'/cert'.DIRECTORY_SEPARATOR.'rootca.pem');
           curl_setopt($ch,CURLOPT_SSLCERT, PATH.'/cert'.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
          // curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
           curl_setopt($ch,CURLOPT_SSLKEY,  PATH.'/cert'.DIRECTORY_SEPARATOR.'apiclient_key.pem');
          
        }
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //运行curl
        
        $data = curl_exec($ch);
   
        //返回结果
        if($data){
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            curl_close($ch);
          
            throw new WxPayException("curl出错,错误码:$error");
        }
    }

}

//发放现金红包

function cash_money($data){
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
        $money = 150 ;
        $sender = '好招工';
       
        $data['wxappid'] = WxPayConfig::APPID;
        $data['mch_id'] = WxPayConfig::MCHID;
        $data['mch_billno'] = WxPayConfig::MCHID.date('YmdHis').rand(1000,9999);
        $data['client_ip'] = $_SERVER['REMOTE_ADDR'];
        $data['total_amount'] = $money;
        $data['min_value'] = $money;
        $data['max_value'] = $money;
        $data['total_num'] = 1;
        $data['nick_name'] = $sender;
        $data['send_name'] = $sender;
        $data['wishing'] = '感谢关注好找工';
        $data['act_name'] = $sender.'红包';
        $data['remark'] = $sender.'红包';
        //$data['re_openid'] ='';
        $data['nonce_str'] = $this->create_rand_string();
        $stringA = $this->ToUrlParams($data,false);
        $stringSingTemp = $stringA.'&key='.WxPayConfig::KEY ;
        $sign = strtoupper(md5($stringSingTemp));
        $data['sign'] = $sign ;
         
        $postXml = $this->ToXml($data) ;
      
        $res = $this->postXmlCurl($postXml, $url,true);
        
        $data = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        file_put_contents('weixin.txt',var_export($res,true));
        return $data;
        
    }

转载于:https://my.oschina.net/u/588516/blog/643564

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值