微信网页授权及企业付款到零钱

页面授权

private $appid = '基础设置中的appid';
    private $appsecret = '基础设置中的开发者密码';
   /**
    * 1、获取微信用户信息,判断有没有code,有使用code换取access_token,没有去获取code。
    * @return array 微信用户信息数组
    */
    public function index(){
        if (!isset($_GET['code'])){//没有code,去微信接口获取code码
            $callback = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];//微信服务器回调url,这里是本页url
            $this->get_code($callback);
        } else {//获取code后跳转回来到这里了
            $code = $_GET['code'];
            //在这我只需要OpenId
            $data = $this->get_access_token($code);//获取网页授权access_token和用户openid
            // $data_all = $this->get_user_info($data['access_token'],$data['openid']);//获取微信用户信息   
            //将获取到的openid保存到session中
            $_SESSION['openid']=$data['openid'];

        }
    }
   /**
    * 2、用户授权并获取code
    * @param string $callback 微信服务器回调链接url
    */
    private function get_code($callback){
        $appid = $this->appid;
        $scope = 'snsapi_userinfo';
        $state = md5(uniqid(rand(), TRUE));//唯一ID标识符绝对不会重复
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . urlencode($callback) .  '&response_type=code&scope=' . $scope . '&state=' . $state . '&connect_redirect=1#wechat_redirect';
        header("Location:$url");
    }    
   /**
    * 3、使用code换取access_token
    * @param string 用于换取access_token的code,微信提供
    * @return array access_token和用户openid数组
    */
    private function get_access_token($code){
        $appid = $this->appid;
        $appsecret = $this->appsecret;    
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
        $user = json_decode(file_get_contents($url));
        if (isset($user->errcode)) {
            echo 'error:' . $user->errcode.'
msg :'
. $user->errmsg;exit; } $data = json_decode(json_encode($user),true);//返回的json数组转换成array数组 return $data; } /** * 4、使用access_token获取用户信息 * @param string access_token * @param string 用户的openid * @return array 用户信息数组 */ private function get_user_info($access_token,$openid){ $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN'; $user = json_decode(file_get_contents($url)); if (isset($user->errcode)) { echo 'error:' . $user->errcode.'
msg :'
. $user->errmsg;exit; } $data = json_decode(json_encode($user),true);//返回的json数组转换成array数组 return $data; } public function __construct() { parent::__construct(); }

企业付款

   /**
     *  array转xml
     */
public function arrayToXml($arr){
    $xml = "";
    foreach ($arr as $key => $val) {
        if (is_numeric($val)) {
            $xml.="<".$key.">".$val.".$key.">";
        }else{
            $xml.="<".$key.">.$val."]]>.$key.">";
        }
    }
    $xml.="";
    return $xml;
}
//使用证书,以post方式提交xml到对应的接口url

/**
     *   作用:使用证书,以post方式提交xml到对应的接口url
     */
function curl_post_ssl($url,$vars,$second=30){
    $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, "证书/必须是绝对路径");
    curl_setopt($ch, CURLOPT_SSLKEY, "证书/必须是绝对路径");


    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 1;
    }
}

//企业向个人付款
public function payToUser($openid,$desc,$amount)
{
    //微信付款到个人的接口
    $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
    $params["mch_appid"]        = '****';   //公众账号appid
    $params["mchid"]            = '****';   //商户号 微信支付平台账号
    $params["nonce_str"]        = 'zhongyaojixie11'.mt_rand(100,999);   //随机字符串
    $params["partner_trade_no"] = mt_rand(10000000,99999999);           //商户订单号
    $params["amount"]           = $amount;          //金额
    $params["desc"]             = $desc;            //企业付款描述
    $params["openid"]           = $openid;          //用户openid
    $params["check_name"]       = 'NO_CHECK';       //不检验用户姓名
    $params['spbill_create_ip'] = '47.93.216.156';   //获取IP

    //生成签名(签名算法后面详细介绍)
    $str = 'amount='.$params["amount"].'&check_name='.$params["check_name"].'&desc='.$params["desc"].'&mch_appid='.$params["mch_appid"].'&mchid='.$params["mchid"].'&nonce_str='.$params["nonce_str"].'&openid='.$params["openid"].'&partner_trade_no='.$params["partner_trade_no"].'&spbill_create_ip='.$params['spbill_create_ip'].'&key=iBbQvUdIif9Yhj64Q4vJ7D90qvU2nS9r';
    //md5加密 转换成大写
    $sign = strtoupper(md5($str));
    $params["sign"] = $sign;//签名
    $xml = $this->arrayToXml($params);
    return $this->curl_post_ssl($url, $xml);
}

你可能感兴趣的:(微信网页授权及企业付款到零钱)