微信支付 小程序支付 网页授权
项目示例: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中使用