微信支付 小程序支付 網頁授權


項目示例:qumi

網頁授權:

按照easywechat教程走一遍,示例代碼:

//自測網頁授權(發起授權)
public function oauthAccess()
{
$config = [
'oauth' => [
// 'scopes' => ['snsapi_userinfo'],
'scopes' => ['snsapi_base'],
'callback' => 'hospital/oauthAccessBack',
],
'app_id' => 'wx9df006157b24d4e4', // AppID
'secret' => '0ac224d488c858f41e136221e69129cd', // AppSecret
'token' => 'weixin', // Token
'aes_key' => 'pG9BO18oH5uJ9keTRtQ3n6k0yDP0JXapCRCucdEM5gC',
];
$app = new Application($config);
$oauth = $app->oauth;

// $oauth = $this->wechat->oauth;
// 未登錄
if (empty($_SESSION['wechat_user'])) {
$_SESSION['target_url'] = 'oauthAccess';
// 這里不一定是return,如果你的框架action不是返回內容的話你就得使用
return $oauth->redirect();
}
// 已經登錄過
$user = $_SESSION['wechat_user'];
// return $oauth;
$response = $oauth->scopes(['snsapi_userinfo'])
->redirect();
return $response;
}
public function oauthAccessBack()
{
$config = [
'oauth' => [
// 'scopes' => ['snsapi_userinfo'],
'scopes' => ['snsapi_base'],
'callback' => 'hospital/oauthAccessBack',
],
'app_id' => 'appid', // AppID
'secret' => 'appsecret', // AppSecret
'token' => 'weixin', // Token
'aes_key' => 'pG9BO18oH5uJ9keTRtQ3n6k0yDP0JXapCRCucdEM5gC',
];
$app = new Application($config);
$oauth = $app->oauth;
// $oauth = $this->wechat->oauth;
// 獲取 OAuth 授權結果用戶信息
$user = $oauth->user();
$_SESSION['wechat_user'] = $user->toArray();
return $user->toArray();
// return $this->return_url();
}
//頁面調用
public function return_url () {
// return redirect("http://qumi.dxtzy.com/admin");
return redirect("http://sport.danxigu.com/jiudian.html");
}
以上使用的是測試賬號
如果配置時出現redirect_uri 頁面錯誤,是頁面授權地址有誤,注意修改
微信和微信小程序支付 easywechat 3.0
先配置config/wechat.php 的對應信息,其中cert下的證書,登錄相應的商戶平台申請,過期的話更改后下載即可
商戶平台登錄網址:
https://pay.weixin.qq.com/index.php/core/home/login
配置完成后,按照easywechat教程走
二:頁面授權獲取用戶信息
1、在對應公眾號 接口權限中修改頁面回調授權域名
2、使用以下代碼獲取json用戶信息
public function ccspUserOpenInfo(Request $request)
{
$data = $request->all();
$code = $data['code'];
$appid = "wx23725****";
$secret = "c68ee7****";
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = $this->getJson($oauth2Url);
//第二步:根據全局access_token和openid查詢用戶信息
$access_token = $oauth2["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = $this->sendGetCurl($get_user_info_url);
return $userinfo;
}
public function getJson ($url){
$ch = curl_init();
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);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
public function sendGetCurl ($url){
$ch = curl_init();
//設置抓取的url
curl_setopt($ch, CURLOPT_URL, $url);
//設置頭文件的信息作為數據流輸出
curl_setopt($ch, CURLOPT_HEADER, 0);
//設置獲取的信息以文件流的形式返回,而不是直接輸出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//執行命令
$data = curl_exec($ch);
//關閉URL請求
curl_close($ch);
//顯示獲得的數據
// $output_array = json_decode($data);
return $data;
// return $output_array;
}

注:laravel中使用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM