From 15e756956f1837dff58ebeb3bb56c01399142e15 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 9 Sep 2022 11:43:06 +0800 Subject: [PATCH 1/8] f --- app/controller/Wechat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index 8d05f2e..1add6a2 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -29,7 +29,7 @@ class Wechat public function url() { $this->initWechat(); - redirect($this->app->oauth->scopes(['snsapi_userinfo'])->redirect()); + return redirect($this->app->oauth->scopes(['snsapi_userinfo'])->redirect()); } /** From 87aa9237b53e0164226295c85a1bd7c3f5960033 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 9 Sep 2022 11:55:54 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Wechat.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index 1add6a2..ec506a3 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -28,8 +28,10 @@ class Wechat */ public function url() { + $post = $GLOBALS['data']['data']['url']; + $this->initWechat(); - return redirect($this->app->oauth->scopes(['snsapi_userinfo'])->redirect()); + return $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($post); } /** From d06ac577fe5f57dd3040318692b81ba2678d7716 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 9 Sep 2022 11:58:34 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Wechat.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index ec506a3..3ae8a2b 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -4,6 +4,7 @@ namespace app\controller; use EasyWeChat\Factory; use EasyWeChat\OfficialAccount\Application; +use think\facade\Config; class Wechat { @@ -17,7 +18,7 @@ class Wechat */ private function initWechat() { - $this->app = Factory::officialAccount(config('wechat')); + $this->app = Factory::officialAccount(Config::get('wechat')); } /** From 13c6ceb3ca829ea0b622ed02e632f9be98fa111a Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 9 Sep 2022 12:02:45 +0800 Subject: [PATCH 4/8] =?UTF-8?q?code=E5=85=91=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Wechat.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index 3ae8a2b..633daae 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -41,10 +41,11 @@ class Wechat * @Date : 2022/9/9 11:29 * @Author : */ - public function callback() + public function code() { + $code = $GLOBALS['data']['data']['code']; $this->initWechat(); - $user = $this->app->oauth->user(); + $user = $this->app->oauth->userFromCode($code); // $user 可以用的方法: // $user->getId(); // 对应微信的 OPENID // $user->getNickname(); // 对应微信的 nickname From 9d9409acc405f5c1806eee1703d66f46fcdcf552 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 9 Sep 2022 13:25:21 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Wechat.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index 633daae..9e5d6fb 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -5,6 +5,7 @@ namespace app\controller; use EasyWeChat\Factory; use EasyWeChat\OfficialAccount\Application; use think\facade\Config; +use think\facade\Route; class Wechat { @@ -27,12 +28,13 @@ class Wechat * @Date : 2022/9/9 11:29 * @Author : */ - public function url() + public function url(): string { - $post = $GLOBALS['data']['data']['url']; - + $url = $GLOBALS['data']['data']['url']; + $redirect = Route::buildUrl('wechat/code', ['callback' => $url]) + ->domain(true); $this->initWechat(); - return $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($post); + return $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect); } /** @@ -43,8 +45,9 @@ class Wechat */ public function code() { - $code = $GLOBALS['data']['data']['code']; $this->initWechat(); + $user = $this->app->oauth->user(); + $code = $GLOBALS['data']['data']['code']; $user = $this->app->oauth->userFromCode($code); // $user 可以用的方法: // $user->getId(); // 对应微信的 OPENID From 4d387d1c3737107dae4ed7f46df154f9022486fa Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 9 Sep 2022 13:26:33 +0800 Subject: [PATCH 6/8] update --- app/controller/Wechat.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index 9e5d6fb..ded2a20 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -32,6 +32,7 @@ class Wechat { $url = $GLOBALS['data']['data']['url']; $redirect = Route::buildUrl('wechat/code', ['callback' => $url]) + ->suffix(false) ->domain(true); $this->initWechat(); return $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect); From d219da7cc8b9b45bb9b0024e5d3d11f16d12584e Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 9 Sep 2022 13:31:08 +0800 Subject: [PATCH 7/8] view --- app/controller/Wechat.php | 3 ++- {view => app/view}/README.md | 0 app/view/wechat/payment.html | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) rename {view => app/view}/README.md (100%) create mode 100644 app/view/wechat/payment.html diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index ded2a20..c7eb318 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -6,6 +6,7 @@ use EasyWeChat\Factory; use EasyWeChat\OfficialAccount\Application; use think\facade\Config; use think\facade\Route; +use think\facade\View; class Wechat { @@ -67,7 +68,7 @@ class Wechat */ public function payment() { - + return View::fetch(); } /** diff --git a/view/README.md b/app/view/README.md similarity index 100% rename from view/README.md rename to app/view/README.md diff --git a/app/view/wechat/payment.html b/app/view/wechat/payment.html new file mode 100644 index 0000000..520f874 --- /dev/null +++ b/app/view/wechat/payment.html @@ -0,0 +1,10 @@ + + + + + Title + + +

支付页面

+ + \ No newline at end of file From b76aa4de0ee4299fe8216fab41be7343059d3d37 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 9 Sep 2022 13:40:45 +0800 Subject: [PATCH 8/8] update --- app/controller/Wechat.php | 16 ++++++- app/view/wechat/payment.html | 82 +++++++++++++++++++++++++++++++++--- config/wechat.php | 40 ++++++++++++------ 3 files changed, 117 insertions(+), 21 deletions(-) diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index c7eb318..24f869a 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -66,9 +66,21 @@ class Wechat * @Date : 2022/9/9 11:32 * @Author : */ - public function payment() + public function payment(): string { - return View::fetch(); + $config = Config::get('wechat.payment'); + $payment = Factory::payment($config); + $unify = $payment->order->unify([ + 'body' => '商品订单', + 'out_trade_no' => time(), + 'total_fee' => 100, + 'notify_url' => '', + 'trade_type' => 'JSAPI', + 'openid' => '$openid', + ]); + $prepayId = $unify->prepay_id; + $jssdk = $payment->jssdk->bridgeConfig($prepayId); + return View::fetch('', ['jssdk' => $jssdk]); } /** diff --git a/app/view/wechat/payment.html b/app/view/wechat/payment.html index 520f874..9bdb54a 100644 --- a/app/view/wechat/payment.html +++ b/app/view/wechat/payment.html @@ -1,10 +1,82 @@ - + - - Title + + + + 微信支付 + -

支付页面

+
+
+ + - \ No newline at end of file + diff --git a/config/wechat.php b/config/wechat.php index 02c6644..f478a0e 100644 --- a/config/wechat.php +++ b/config/wechat.php @@ -4,10 +4,11 @@ return [ /** * 账号基本信息,请从微信公众平台/开放平台获取 */ - 'app_id' => 'wx3793a03c18984e4f', // AppID - 'secret' => 'f1c242f4f28f735d4687abb469072xxx', // AppSecret - 'token' => 'iQoSaqys8cQawIusDZn6H5LPYTMIOWVg', // Token - 'aes_key' => 'fmkgnwli1JaabwxlcZdkI6bZ2lwxiVGnxg5o0mSHsuB', // EncodingAESKey,兼容与安全模式下请一定要填写!!! + 'app_id' => 'wx3793a03c18984e4f', // AppID + 'secret' => 'f1c242f4f28f735d4687abb469072xxx', // AppSecret + 'token' => 'iQoSaqys8cQawIusDZn6H5LPYTMIOWVg', // Token + 'aes_key' => 'fmkgnwli1JaabwxlcZdkI6bZ2lwxiVGnxg5o0mSHsuB', + // EncodingAESKey,兼容与安全模式下请一定要填写!!! /** * 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名 @@ -22,20 +23,20 @@ return [ * debug/info/notice/warning/error/critical/alert/emergency * path:日志文件位置(绝对路径!!!),要求可写权限 */ - 'log' => [ - 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod + 'log' => [ + 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod 'channels' => [ // 测试环境 - 'dev' => [ + 'dev' => [ 'driver' => 'single', - 'path' => '/tmp/easywechat.log', - 'level' => 'debug', + 'path' => '/tmp/easywechat.log', + 'level' => 'debug', ], // 生产环境 'prod' => [ 'driver' => 'daily', - 'path' => '/tmp/easywechat.log', - 'level' => 'info', + 'path' => '/tmp/easywechat.log', + 'level' => 'info', ], ], ], @@ -48,10 +49,10 @@ return [ * - retry_delay: 重试延迟间隔(单位:ms),默认 500 * - log_template: 指定 HTTP 日志模板,请参考:https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php */ - 'http' => [ + 'http' => [ 'max_retries' => 1, 'retry_delay' => 500, - 'timeout' => 5.0, + 'timeout' => 5.0, // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri ], @@ -61,8 +62,19 @@ return [ * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login * callback:OAuth授权完成后的回调页地址 */ - 'oauth' => [ + 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => '/wechat/callback', ], + + 'payment' => [ + // 必要配置 + 'app_id' => 'wx3793a03c18984e4f', + 'mch_id' => '1620663318', + 'key' => 'iQoSaqys8cQawIusDZn6H5LPYTMIOWVg', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥) + // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书) + 'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!! + 'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!! + 'notify_url' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它 + ], ];