From ae9349d0579548de1ff24f00e8dbcca4f20c0087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=90=8D?= <邮箱> Date: Tue, 1 Aug 2023 10:05:21 +0800 Subject: [PATCH] update wxpay --- mobile/plugins/payment/wxpay.php | 128 +++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 65 deletions(-) diff --git a/mobile/plugins/payment/wxpay.php b/mobile/plugins/payment/wxpay.php index 155ab46..29c1758 100755 --- a/mobile/plugins/payment/wxpay.php +++ b/mobile/plugins/payment/wxpay.php @@ -106,6 +106,67 @@ class wxpay } /** + * 获取openid + */ + public function get_openid() + { + $appid = 'wx79343915f99167e6'; + $appsecret = 'f2f72c5e0ac29c2373bfaf22cf059c02'; + $redirect_uri = 'https://shop.heavenk.com/mobile/source/apps/flow/views/done.html'; // 回调URL + + // 构造授权链接 + $auth_url = 'https://open.weixin.qq.com/connect/oauth2/authorize' + . '?appid=' . urlencode($appid) + . '&redirect_uri=' . urlencode($redirect_uri) + . '&response_type=code' + . '&scope=snsapi_userinfo' // snsapi_base 只获取openid,snsapi_userinfo 获取用户信息 + . '&state=STATE'; // wechat_redirect + + // 如果用户已经授权,则重定向到回调URL,并获取用户的openid + if (isset($_GET['code'])) { + $code = $_GET['code']; + + // 构造获取access_token的URL + $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token' + . '?appid=' . urlencode($appid) + . '&secret=' . urlencode($appsecret) + . '&code=' . urlencode($code) + . '&grant_type=authorization_code'; + + // 发送HTTP请求获取access_token + + // 创建一个cURL句柄 + $ch = curl_init(); + + // 设置请求的URL和其他选项 + curl_setopt($ch, CURLOPT_URL, $token_url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + // 发送HTTP GET请求并获取响应 + $response = curl_exec($ch); + + // 检查是否有错误发生 + if(curl_errno($ch)) { + echo 'cURL Error: ' . curl_error($ch); + } + + // 关闭cURL句柄 + curl_close($ch); + + // 输出响应 + $response = json_decode($response); + var_dump($response->openid); + return $response->openid; + + } else { + // 如果没有授权,则重定向到授权页面 + header('Location: ' . $auth_url); + var_dump($_GET['code']); + exit(); + } + } + + /** * 生成支付代码 * @param array $order 订单信息 * @param array $payment 支付方式信息 @@ -126,71 +187,8 @@ class wxpay // $_SESSION['openid'] = 'sdf4asgdf2ag1'; // return false; - // 设置应用信息 - $appid = 'wx79343915f99167e6'; - $appsecret = 'f2f72c5e0ac29c2373bfaf22cf059c02'; - $redirect_uri = 'https://shop.heavenk.com/mobile/plugins/payment/wxpay.php'; // 回调URL - - // 构造授权链接 - $auth_url = 'https://open.weixin.qq.com/connect/oauth2/authorize' - . '?appid=' . urlencode($appid) - . '&redirect_uri=' . urlencode($redirect_uri) - . '&response_type=code' - . '&scope=snsapi_userinfo' // snsapi_base 只获取openid,snsapi_userinfo 获取用户信息 - . '&state=STATE'; // wechat_redirect - - // 如果用户已经授权,则重定向到回调URL,并获取用户的openid - if (isset($_GET['code'])) { - $code = $_GET['code']; - - // $code = '100010'; - - // 构造获取access_token的URL - $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token' - . '?appid=' . urlencode($appid) - . '&secret=' . urlencode($appsecret) - . '&code=' . urlencode($code) - . '&grant_type=authorization_code'; - - // 发送HTTP请求获取access_token - - // 创建一个cURL句柄 - $ch = curl_init(); - - // 设置请求的URL和其他选项 - curl_setopt($ch, CURLOPT_URL, $token_url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - - // 发送HTTP GET请求并获取响应 - $response = curl_exec($ch); - - // 检查是否有错误发生 - if(curl_errno($ch)) { - echo 'cURL Error: ' . curl_error($ch); - } - - // 关闭cURL句柄 - curl_close($ch); - - // 输出响应 - $response = json_decode($response); - // var_dump($response->openid); - $_SESSION['openid'] = $response->openid; - - - // $client = new \GuzzleHttp\Client(); - // $response = $client->request('GET', $token_url); - // $result = json_decode($response->getBody(), true); - // $openid = $result['openid']; - - // // 在这里处理获取到的openid - // echo $openid; - } else { - // 如果没有授权,则重定向到授权页面 - header('Location: ' . $auth_url); - var_dump($_GET['code']); - exit(); - } + $openid = $this->get_openid(); + $_SESSION['openid'] = $openid; } // 设置必填参数