微信支付包與自制方法

微信官方包代碼修改

獲取openid州刽,type為openid獲取方式

public function GetOpenid($type)
{
    //通過(guò)code獲得openid
    if (!isset($_GET['code'])){
        //觸發(fā)微信返回code碼
        $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
        $url = $this->_CreateOauthUrlForCode($baseUrl,$type);
        Header("Location: $url");
        exit();
    } else {
        //獲取code碼空执,以獲取openid
        $code = $_GET['code'];
        $openid = $this->getOpenidFromMp($code);
        return $openid;
    }
}

這里傳入type,區(qū)分授權(quán)方式

private function _CreateOauthUrlForCode($redirectUrl,$type)
{
    $config = new WxPayConfig();
    $urlObj["appid"] = $config->GetAppId();
    $urlObj["redirect_uri"] = "$redirectUrl";
    $urlObj["response_type"] = "code";
    $urlObj["scope"] = $type;

    $urlObj["state"] = "STATE"."#wechat_redirect";
    $bizString = $this->ToUrlParams($urlObj);
    return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
}


public function GetOpenidFromMp($code)
{
    $url = $this->__CreateOauthUrlForOpenid($code);

    //初始化curl
    $ch = curl_init();
    $curlVersion = curl_version();
    $config = new WxPayConfig();
    $ua = "WXPaySDK/3.0.9 (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
    .$config->GetMerchantId();

    //設(shè)置超時(shí)
    //curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $proxyHost = "0.0.0.0";
    $proxyPort = 0;
    $config->GetProxy($proxyHost, $proxyPort);
    if($proxyHost != "0.0.0.0" && $proxyPort != 0){
        curl_setopt($ch,CURLOPT_PROXY, $proxyHost);
        curl_setopt($ch,CURLOPT_PROXYPORT, $proxyPort);
    }
    //運(yùn)行curl穗椅,結(jié)果以jason形式返回
    $res = curl_exec($ch);
    curl_close($ch);
    //取出openid
    $data = json_decode($res,true);
    $this->data = $data;
    $openid = $data;
    return $openid;
}

獲取openid

public function getOpenids()
{
    $obj = new \JsApiPay();
    $data = $obj->GetOpenid("snsapi_userinfo");
    return $data;
}

public function getOpenid()
{
      session_start();
      $obj = new \JsApiPay();
      $data = $obj->GetOpenid("snsapi_base");
      $_SESSION['openid'] = $data['openid'];
}

curlGet

public static function curlGet($url = '', $options = array())
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    if (!empty($options)) {
        curl_setopt_array($ch, $options);
    }
    //https請(qǐng)求 不驗(yàn)證證書和host
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

獲取access_token并保存至數(shù)據(jù)庫(kù)維護(hù)

public function getWxAccessToken()
{
    $access_token = db('access_token')->where("id>0")->find();
    if ($access_token) {
        $endtime = $access_token['inserttime'] + 7000;
        if ($access_token['access_token'] && $endtime > time()) {
            //如果access_token在session沒有過(guò)期
            return $access_token['access_token'];
        } else {
            //如果access_token比存在或者已經(jīng)過(guò)期脆烟,重新取access_token
            //1 請(qǐng)求url地址
            $config = new \WxPayConfig();
            $AppId = $config->GetAppId();
            $AppSecret = $config->GetAppSecret();
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $AppId . "&secret=" . $AppSecret;
            $res = $this->http_curl($url, 'get', 'json');
            $res = json_decode($res, true);

            $access_token = $res['access_token'];
            //將重新獲取到的aceess_token存到session
            $update_data = [
                'inserttime' => time(),
                'access_token' => $access_token
            ];
            $update_data = db('access_token')->where("id>0")->update($update_data);
            return $access_token;
        }
    } else {
        $config = new \WxPayConfig();
        $AppId = $config->GetAppId();
        $AppSecret = $config->GetAppSecret();
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $AppId . "&secret=" . $AppSecret;
        $res = $this->http_curl($url, 'get', 'json');
        $res = json_decode($res, true);

        $access_token = $res['access_token'];
        //將重新獲取到的aceess_token存到session
        $update_data = [
            'inserttime' => time(),
            'access_token' => $access_token
        ];
        $update_data = db('access_token')->insert($update_data);
        return $access_token;
    }

}

獲取用戶信息

public function getUserdata($openid,$access_token)
{
    $response = self::curlGet('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
    return json_decode($response,true);
}

將用戶信息存入session并返回

public function getuser()
{
    session_start();
    $data = $this->getOpenids();
    $openid = $data['openid'];
    $access_token = $data['access_token'];
    $data = $this->getUserdata($openid,$access_token);
    $_SESSION['user'] = $data;
    $this->redirect('調(diào)回路由');
}

獲取公眾號(hào)微信config

public function getwxconfig()
{
    //session_start();
    $token = $this->getWxAccessToken();
    $urls = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
    //https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
    $jsapi = file_get_contents($urls);
    $arr = json_decode($jsapi, true);

    //獲取jsapi_ticket
    $jsapi_ticket = $arr['ticket'];
    //隨機(jī)字符串
    $noncestr = $this->getRandChar(16);
    //獲取時(shí)間戳
    $timestamp = strtotime(date("Y-m-d H:i:s"));
    //獲取url
    $config = new \WxPayConfig();
    $AppId = $config->GetAppId();

    $url = input('url');
    //$data = array("jsapi_ticket"=>$jsapi_ticket,"noncestr"=>$noncestr,"timestamp"=>$timestamp,"url"=>$url);
    $sha1 = sha1("jsapi_ticket=$jsapi_ticket&noncestr=$noncestr&timestamp=$timestamp&url=$url");
    $arr = array('appid' => $AppId, 'noncestr' => $noncestr, 'timestamp' => $timestamp, 'sha1' => $sha1);
    return json($arr);
}

微信字符串

public function getRandChar($length)
{
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max兩個(gè)數(shù)之間的一個(gè)隨機(jī)整數(shù)
    }
    return $str;
}

初始化設(shè)置

public function configall()
{
    $this->getOpenid();//靜默獲取openid
    $this->getwxconfig();//獲取wxconfig參數(shù)
    $this->redirect("跳轉(zhuǎn)路由");
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市房待,隨后出現(xiàn)的幾起案子邢羔,更是在濱河造成了極大的恐慌,老刑警劉巖桑孩,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拜鹤,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡流椒,警方通過(guò)查閱死者的電腦和手機(jī)敏簿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人惯裕,你說(shuō)我怎么就攤上這事温数。” “怎么了蜻势?”我有些...
    開封第一講書人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵撑刺,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我握玛,道長(zhǎng)够傍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任挠铲,我火速辦了婚禮冕屯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拂苹。我一直安慰自己安聘,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開白布瓢棒。 她就那樣靜靜地躺著浴韭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪音羞。 梳的紋絲不亂的頭發(fā)上囱桨,一...
    開封第一講書人閱讀 51,554評(píng)論 1 305
  • 那天仓犬,我揣著相機(jī)與錄音嗅绰,去河邊找鬼。 笑死搀继,一個(gè)胖子當(dāng)著我的面吹牛窘面,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播叽躯,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼财边,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了点骑?” 一聲冷哼從身側(cè)響起酣难,我...
    開封第一講書人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎黑滴,沒想到半個(gè)月后憨募,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡袁辈,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年菜谣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡尾膊,死狀恐怖媳危,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情冈敛,我是刑警寧澤待笑,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站莺债,受9級(jí)特大地震影響滋觉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜齐邦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一椎侠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧措拇,春花似錦我纪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至券犁,卻和暖如春术健,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背粘衬。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工荞估, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人稚新。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓勘伺,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親褂删。 傳聞我的和親對(duì)象是個(gè)殘疾皇子飞醉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容