diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index 8d05f2e..24f869a 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -4,6 +4,9 @@ namespace app\controller; use EasyWeChat\Factory; use EasyWeChat\OfficialAccount\Application; +use think\facade\Config; +use think\facade\Route; +use think\facade\View; class Wechat { @@ -17,7 +20,7 @@ class Wechat */ private function initWechat() { - $this->app = Factory::officialAccount(config('wechat')); + $this->app = Factory::officialAccount(Config::get('wechat')); } /** @@ -26,10 +29,14 @@ class Wechat * @Date : 2022/9/9 11:29 * @Author : */ - public function url() + public function url(): string { + $url = $GLOBALS['data']['data']['url']; + $redirect = Route::buildUrl('wechat/code', ['callback' => $url]) + ->suffix(false) + ->domain(true); $this->initWechat(); - redirect($this->app->oauth->scopes(['snsapi_userinfo'])->redirect()); + return $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect); } /** @@ -38,10 +45,12 @@ class Wechat * @Date : 2022/9/9 11:29 * @Author : */ - public function callback() + public function code() { $this->initWechat(); $user = $this->app->oauth->user(); + $code = $GLOBALS['data']['data']['code']; + $user = $this->app->oauth->userFromCode($code); // $user 可以用的方法: // $user->getId(); // 对应微信的 OPENID // $user->getNickname(); // 对应微信的 nickname @@ -57,9 +66,21 @@ class Wechat * @Date : 2022/9/9 11:32 * @Author : */ - public function payment() + public function payment(): string { - + $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/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..9bdb54a --- /dev/null +++ b/app/view/wechat/payment.html @@ -0,0 +1,82 @@ + + + + + + + 微信支付 + + + +
+
+ + + + 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' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它 + ], ];