composer 安装easyWechat  地址:https://packagist.org/packages/overtrue/wechat

执行命令:composer require overtrue/wechat

配置config/wechat.php

    <?php    
    /*
     * This file is part of the overtrue/laravel-wechat.
     *
     * (c) overtrue <i@overtrue.me>
     *
     * This source file is subject to the MIT license that is bundled
     * with this source code in the file LICENSE.
     */
    return [
        /*
         * 默认配置,将会合并到各模块中
         */
        'defaults' => [
            /*
             * 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
             */
            'response_type' => 'array',
            /*
             * 使用 Laravel 的缓存系统
             */
            'use_laravel_cache' => true,
            /*
             * 日志配置
             *
             * level: 日志级别,可选为:
             *                 debug/info/notice/warning/error/critical/alert/emergency
             * file:日志文件位置(绝对路径!!!),要求可写权限
             */
            'log' => [
                'level' => env('WECHAT_LOG_LEVEL', 'debug'),
                'file' => env('WECHAT_LOG_FILE', storage_path('logs/wechat.log')),
            ],
        ],
        /*
         * 路由配置
         */
        'route' => [
            /*
             * 开放平台第三方平台路由配置
             */
            // 'open_platform' => [
            //     'uri' => 'serve',
            //     'action' => Overtrue\LaravelWeChat\Controllers\OpenPlatformController::class,
            //     'attributes' => [
            //         'prefix' => 'open-platform',
            //         'middleware' => null,
            //     ],
            // ],
        ],
        /*
         * 公众号
         */
        'official_account' => [
            'default' => [
                'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID', 'wxfab3581c80794b90'),         // AppID
                'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET', 'e947eb255d2ca4483d590402292bccf0'),    // AppSecret
                'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN', 'bschem2020'),           // Token
                'aes_key' => env('WECHAT_OFFICIAL_ACCOUNT_AES_KEY', 'V5DLLG5Z9VebIKCmpVXPHOoWkC84YTQFhOuROtzgo50'),                 // EncodingAESKey
                /*
                 * OAuth 配置
                 *
                 * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
                 * callback:OAuth授权完成后的回调页地址(如果使用中间件,则随便填写。。。)
                 */
                 'oauth' => [
                     'scopes'   => array_map('trim', explode(',', env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_SCOPES', 'snsapi_userinfo'))),
                     'callback' => env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_CALLBACK', '/wechat/callback'),
                 ],
            ],
        ],
        /*
         * 开放平台第三方平台
         */
        // 'open_platform' => [
        //     'default' => [
        //         'app_id'  => env('WECHAT_OPEN_PLATFORM_APPID', ''),
        //         'secret'  => env('WECHAT_OPEN_PLATFORM_SECRET', ''),
        //         'token'   => env('WECHAT_OPEN_PLATFORM_TOKEN', ''),
        //         'aes_key' => env('WECHAT_OPEN_PLATFORM_AES_KEY', ''),
        //     ],
        // ],
        /*
         * 小程序
         */
        // 'mini_program' => [
        //     'default' => [
        //         'app_id'  => env('WECHAT_MINI_PROGRAM_APPID', ''),
        //         'secret'  => env('WECHAT_MINI_PROGRAM_SECRET', ''),
        //         'token'   => env('WECHAT_MINI_PROGRAM_TOKEN', ''),
        //         'aes_key' => env('WECHAT_MINI_PROGRAM_AES_KEY', ''),
        //     ],
        // ],
        /*
         * 微信支付
         */
         'payment' => [
             'default' => [
                 'sandbox'            => env('WECHAT_PAYMENT_SANDBOX', false),
                 'app_id'             => env('WECHAT_PAYMENT_APPID', 'wxfab3581c80794b90'),
                 'mch_id'             => env('WECHAT_PAYMENT_MCH_ID', '1588059240'),
                 'key'                => env('WECHAT_PAYMENT_KEY', '79e5c9037480b960b826e236b072b0d0'),
                 'cert_path'          => env('WECHAT_PAYMENT_CERT_PATH', public_path('cert/apiclient_cert.pem')),    // XXX: 绝对路径!!!!
                 'key_path'           => env('WECHAT_PAYMENT_KEY_PATH',  public_path('cert/apiclient_key.pem')),      // XXX: 绝对路径!!!!
                 'notify_url'         => 'http://m.bychem.com/payments/wechat-notify',                           // 默认支付结果通知地址
             ],
             // ...
             'labgle' => [
                   'sandbox'            => env('WECHAT_PAYMENT_SANDBOX', false),
                   'app_id'             => env('WECHAT_PAYMENT_APPID', 'wx1131d7b4dc9e0fc0'),
                   'mch_id'             => env('WECHAT_PAYMENT_MCH_ID', '1494237800'),
                   'key'                => env('WECHAT_PAYMENT_KEY', 'bd0932aae78d3bb5f3757ed07f3de4zf'),
                   'cert_path'          => '',    // XXX: 绝对路径!!!!
                   'key_path'           => '',      // XXX: 绝对路径!!!!
                   //'cert_path'          => env('WECHAT_PAYMENT_CERT_PATH', public_path('cert/apiclient_cert.pem')),    // XXX: 绝对路径!!!!
                   //'key_path'           => env('WECHAT_PAYMENT_KEY_PATH',  public_path('cert/apiclient_key.pem')),      // XXX: 绝对路径!!!!
                   'notify_url'         => 'http://m.bychem.com/payments/wechat-notify',                           // 默认支付结果通知地址
              ],
         ],
         /*
          * 企业微信
          */
        // 'work' => [
        //     'default' => [
        //         'corp_id' => 'xxxxxxxxxxxxxxxxx',
        //         'agent_id' => 100020,
        //         'secret'   => env('WECHAT_WORK_AGENT_CONTACTS_SECRET', ''),
        //          //...
        //      ],
        // ],
    ];

业务逻辑代码

    <?php    
    namespace App\Console\Commands;
    use Alipay\EasySDK\Kernel\Config;
    use Alipay\EasySDK\Kernel\Factory;
    use Illuminate\Console\Command;
    use Illuminate\Support\Facades\Log;
    use PHPUnit\Exception;
    class Sync_Trade_Query extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'SyncTradeQuery';
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Sync Trade Query';
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
        /**
          * https://pay.weixin.qq.com/wiki/doc/api/jsapi_sl.php?chapter=9_6
         * https://www.easywechat.com/docs
         * https://packagist.org/packages/overtrue/wechat
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            $billDate = '20200603';
            //微信api
            #$this->downloadWechatBill($billDate);
        }   
        private function downloadWechatBill($bill_date, $bill_type ='All'){
            Log::info('downloadWechatBill', ['downloadWechatBill 20200703']);
            try{
                #$app = app('wechat.official_account');
                $app = app('wechat.payment.labgle');
                $bill = $app->bill->get('20200603');
                Log::info('bill', [$bill]);
                $bill->saveAs('public', 'file20200603.csv');
                Log::info('down end', ['end ri']);
            }catch (Exception $e){
                Log::info('downloadWechatBill Exception', [$e->getMessage()]);
            }
        }
    }

查看 easyWechat api readme

    # laravel-wechat    
    微信 SDK for Laravel 5 / Lumen, 基于 [overtrue/wechat](https://github.com/overtrue/wechat)
    > 交流QQ群:319502940
    ## 框架要求
    Laravel/Lumen >= 5.1
    ## 安装
    ```shell
    # Laravel < 5.8
    composer require "overtrue/laravel-wechat:~4.0"
    # Laravel >= 5.8
    composer require "overtrue/laravel-wechat:~5.0"
    ```
    ## 配置
    ### Laravel 应用
    1. 在 `config/app.php` 注册 ServiceProvider 和 Facade (Laravel 5.5 + 无需手动注册)
    ```php
    'providers' => [
        // ...
        Overtrue\LaravelWeChat\ServiceProvider::class,
    ],
    'aliases' => [
        // ...
        'EasyWeChat' => Overtrue\LaravelWeChat\Facade::class,
    ],
    ```
    2. 创建配置文件:
    ```shell
    php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider"
    ```
    3. 修改应用根目录下的 `config/wechat.php` 中对应的参数即可。
    4. 每个模块基本都支持多账号,默认为 `default`。
    ### Lumen 应用
    1. 在 `bootstrap/app.php` 中 82 行左右:
    ```php
    $app->register(Overtrue\LaravelWeChat\ServiceProvider::class);
    ```
    2. 如果你习惯使用 `config/wechat.php` 来配置的话,将 `vendor/overtrue/laravel-wechat/src/config.php` 拷贝到`项目根目录/config`目录下,并将文件名改成`wechat.php`。
    ## 使用
    :rotating_light: 在中间件 `App\Http\Middleware\VerifyCsrfToken` 排除微信相关的路由,如:
    ```php
    protected $except = [
        // ...
        'wechat',
    ];
    ```
    下面以接收普通消息为例写一个例子:
    > 假设您的域名为 `overtrue.me` 那么请登录微信公众平台 “开发者中心” 修改 “URL(服务器配置)” 为: `http://overtrue.me/wechat`。
    路由:
    ```php
    Route::any('/wechat', 'WeChatController@serve');
    ```
    > 注意:一定是 `Route::any`, 因为微信服务端认证的时候是 `GET`, 接收用户消息时是 `POST` !
    然后创建控制器 `WeChatController`:
    ```php
    <?php
    namespace App\Http\Controllers;
    use Log;
    class WeChatController extends Controller
    {
        /**
         * 处理微信的请求消息
         *
         * @return string
         */
        public function serve()
        {
            Log::info('request arrived.'); # 注意:Log 为 Laravel 组件,所以它记的日志去 Laravel 日志看,而不是 EasyWeChat 日志
            $app = app('wechat.official_account');
            $app->server->push(function($message){
                return "欢迎关注 overtrue!";
            });
            return $app->server->serve();
        }
    }
    ```
    > 上面例子里的 Log 是 Laravel 组件,所以它的日志不会写到 EasyWeChat 里的,建议把 wechat 的日志配置到 Laravel 同一个日志文件,便于调试。
    ### 我们有以下方式获取 SDK 的服务实例
    ##### 使用外观
    ```php
      $officialAccount = \EasyWeChat::officialAccount(); // 公众号
      $work = \EasyWeChat::work(); // 企业微信
      $payment = \EasyWeChat::payment(); // 微信支付
      $openPlatform = \EasyWeChat::openPlatform(); // 开放平台
      $miniProgram = \EasyWeChat::miniProgram(); // 小程序
      
      // 均支持传入配置账号名称
      \EasyWeChat::officialAccount('foo'); // `foo` 为配置文件中的名称,默认为 `default`
      //...
    ```
    ## OAuth 中间件
    使用中间件的情况下 `app/config/wechat.php` 中的 `oauth.callback` 就随便填写吧(因为用不着了 :smile:)。
    1. 在 `app/Http/Kernel.php` 中添加路由中间件:
    ```php
    protected $routeMiddleware = [
        // ...
        'wechat.oauth' => \Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate::class,
    ];
    ```
    2. 在路由中添加中间件:
    ```php
    //...
    Route::group(['middleware' => ['web', 'wechat.oauth']], function () {
        Route::get('/user', function () {
            $user = session('wechat.oauth_user.default'); // 拿到授权用户资料
            dd($user);
        });
    });
    ```
    中间件支持指定配置名称:`'wechat.oauth:default'`,当然,你也可以在中间件参数指定当前的 `scopes`:
    ```php
    Route::group(['middleware' => ['wechat.oauth:snsapi_userinfo']], function () {
      // ...
    });
    // 或者指定账户的同时指定 scopes:
    Route::group(['middleware' => ['wechat.oauth:default,snsapi_userinfo']], function () {
      // ...
    });
    ```
    上面的路由定义了 `/user` 是需要微信授权的,那么在这条路由的**回调 或 控制器对应的方法里**, 你就可以从 `session('wechat.oauth_user.default')` 拿到已经授权的用户信息了。
    ## 模拟授权
    有时候我们希望在本地开发完成后线上才真实的走微信授权流程,这将减少我们的开发成本,那么你需要做以下两步:
    1. 准备假资料:
    > 以下字段在 scope 为 `snsapi_userinfo` 时尽可能配置齐全哦,当然,如果你的模式只是 `snsapi_base` 的话只需要 `openid` 就好了。
    ```php
    use Illuminate\Support\Arr;
    use Overtrue\Socialite\User as SocialiteUser;
    $user = new SocialiteUser([
                    'id' => Arr::get($user, 'openid'),
                    'name' => Arr::get($user, 'nickname'),
                    'nickname' => Arr::get($user, 'nickname'),
                    'avatar' => Arr::get($user, 'headimgurl'),
                    'email' => null,
                    'original' => [],
                    'provider' => 'WeChat',
                ]);
    ```
    2. 将资料写入 session:
    > 注意:一定要在 OAuth 中间件之前写入,比如你可以创建一个全局中间件来完成这件事儿,当然了,只在开发环境启用即可。
    ```php
    session(['wechat.oauth_user.default' => $user]); // 同理,`default` 可以更换为您对应的其它配置名
    ```
    ## 事件
    > 你可以监听相应的事件,并对事件发生后执行相应的操作。
    - OAuth 网页授权:`Overtrue\LaravelWeChat\Events\WeChatUserAuthorized`
    ```php
    // 该事件有以下属性
    $event->user; // 同 session('wechat.oauth_user.default') 一样
    $event->isNewSession; // 是不是新的会话(第一次创建 session 时为 true)
    $event->account; // 当前中间件所使用的账号,对应在配置文件中的配置项名称
    ```
    ## 开放平台路由支持
    在配置文件 `route` 处取消注释即可启用。
    ```php
    'open_platform' => [
        'uri' => 'serve',
        'action' => Overtrue\LaravelWeChat\Controllers\OpenPlatformController::class,
        'attributes' => [
            'prefix' => 'open-platform',
            'middleware' => null,
        ],
    ],
    ```
    Tips: 默认的控制器会根据微信开放平台的推送内容触发如下事件,你可以监听相应的事件并进行处理:
    - 授权方成功授权:`Overtrue\LaravelWeChat\Events\OpenPlatform\Authorized`
    - 授权方更新授权:`Overtrue\LaravelWeChat\Events\OpenPlatform\UpdateAuthorized`
    - 授权方取消授权:`Overtrue\LaravelWeChat\Events\OpenPlatform\Unauthorized`
    - 开放平台推送 VerifyTicket:`Overtrue\LaravelWeChat\Events\OpenPlatform\VerifyTicketRefreshed`
    ```php
    // 事件有如下属性
    $message = $event->payload; // 开放平台事件通知内容
    ```
    配置后 `http://example.com/open-platform/serve` 则为开放平台第三方应用设置的授权事件接收 URL。
    更多 SDK 的具体使用请参考:https://easywechat.com
    ## PHP 扩展包开发
    > 想知道如何从零开始构建 PHP 扩展包?
    >
    > 请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)
    ## License
    MIT