Clover OAuth2 提供程序,用于 Laravel Socialite

4.1.0 2024-03-13 00:08 UTC

This package is auto-updated.

Last update: 2024-08-26 21:28:06 UTC


README

composer require socialiteproviders/clover

安装与基本用法

请参阅基本安装指南,然后按照以下特定提供程序的说明操作。

确保应用有读取员工的权限。

config/services.php中添加配置

'clover' => [
  'client_id' => env('CLOVER_CLIENT_ID'),
  'client_secret' => env('CLOVER_CLIENT_SECRET'),
  'redirect' => env('CLOVER_REDIRECT_URI')
  'environment' => env('CLOVER_ENVIRONMENT', 'north_america'), // one of the following: 'sandbox', 'north_america' (for US/Canada), 'europe', or 'latin_america'
],

添加提供程序事件监听器

Laravel 11+

在 Laravel 11 中,默认的 EventServiceProvider 提供程序已被移除。取而代之的是,在您的 AppServiceProviderboot 方法中使用 Event 门面上的 listen 方法添加监听器。

  • 注意:除非您使用自己的提供程序覆盖它们,否则您不需要为内置的 Socialite 提供程序添加任何内容。
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
    $event->extendSocialite('Clover', \SocialiteProviders\Clover\Provider::class);
});
Laravel 10 或以下配置包的监听器以监听 `SocialiteWasCalled` 事件。

app/Providers/EventServiceProvider 中的 listen[] 数组中添加事件。有关详细说明,请参阅基本安装指南

protected $listen = [
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [
        // ... other providers
        \SocialiteProviders\Clover\CloverExtendSocialite::class.'@handle',
    ],
];

用法

现在您应该能够像通常使用 Socialite 一样使用此提供程序(假设您已安装门面)

return Socialite::driver('clover')->redirect();

假设您使用此 OAuth 提供程序是为了检索调用其他 API 端点的 API 令牌。

用户包含一个 token 属性,您可以使用以下方式检索 API 令牌:

Route::get('clover/auth/callback', function () {
    $user = Socialite::driver('clover')->user();

    // Save these tokens somewhere for use with the API.
    $token = $user->accessTokenResponseBody;

    // Here’s what it looks like:
    // [
    //     'access_token' => 'JWT',
    //     'access_token_expiration' => 1709563149,
    //     'refresh_token' => 'clvroar-6e49ffe9b5122f137aa39d8f7f930558',
    //     'refresh_token_expiration' => 1741097349,
    // ]

    // You may also want to store the merchant ID somewhere.
    $merchantId = request()->input('merchant_id');

    // Here’s what it looks like:
    // 'ABC123DEF4567'
});