文化王者/shopify-app-auth-laravel

适用于Laravel 5.x的Shopify OAuth应用程序

3.4 2017-06-29 04:55 UTC

README

Shopify应用程序的Laravel认证样板

安装

安装

composer require culturekings/shopify-app-auth-laravel

添加到提供者

在config/app.php中添加提供者 CultureKings\ShopifyAuth\ShopifyAuthServiceProvider::class

发布

php artisan vendor:publish

设置视图

安装成功页面

在您的resources/views文件夹中,创建您的文件夹和install-sucess.blade.php文件,然后在shopify-auth.app_name中,将您的view_install_success_path值设置为所需的任何值(例如,在以下配置应用程序中查看)。

认证中间件

在路由上设置中间件 - 确保ShopifyAuthCheck位于路由中。Web也是如此,但在web.php文件中可能是标准操作。此外,请注意,在创建路由时,appname必须是URL路由中的第二个参数,例如:apps/app_name/create。

Route::group(
    [
        'prefix' => 'shopify-apps',
        'namespace' => 'ShopifyApps',
        'middleware' => 'CultureKings\ShopifyAuth\Http\Middleware\ShopifyAuthCheck'
    ],
    function () {
        Route::get('appname', 'AppnameController@getDashboard');
    }
);

在配置中配置应用程序

发布后,设置您的应用程序。

您可以在下面看到所有内容都设置在"appname"下,然后从那时起使用app_name。

在路由中,您可以看到appName作为变量传递给认证URL,因此确保URL与"appname"数组键相同非常重要。

您可以根据需要更改此值,以便通过单个认证流程运行多个应用程序。

'appname' => [
    'name' => 'app_name', // checked in db, so shouldn't change after launch
    'price' => 0.00,
    'redirect_url' => '/shopify-auth/app_name/auth/callback', // relative uri
    'success_url' => '/shopify-auth/app_name/install/success',
    'dashboard_url' => '/apps/app_name/dashboard',
    'scope' => [
        "write_products",
        "write_script_tags"
    ],
    'view_folder_path' => 'shopify-apps.app_name',
    'view_install_success_path' => 'shopify-apps.app_name.install-success',
    'key' => env("SHOPIFY_APPNAME_APIKEY"),
    'secret' => env("SHOPIFY_APPNAME_SECRET"),
],

用法

所有Shopify调用都应通过服务进行,并执行类似以下的调用

// $appName comes from url passed into method as param (look at middleware)
$shopifyAppConfig = config('shopify-auth.'.$appName);

/ call shopify api
$this->shopify
    ->setKey($shopifyAppConfig['key'])
    ->setSecret($shopifyAppConfig['secret'])
    ->setShopUrl($shopUrl)
    ->setAccessToken($accessToken)
    ->post('admin/script_tags.json', $scriptTags);

认证商店应用程序

导航到:http://exampledomain.dev/shopify-auth/app_name/install?shop=shopifydomain.myshopify.com

然后它将重定向您到商店的认证过程以开始

常见控制器设置

我通常使用此构造

public function __construct(ShopifyApi $shopify, Request $request, ShopifyAuthService $shopifyAuthService)
{
     $this->shopify = $shopify;
     $this->shopifySession = $request->session()->get('shopifyapp');
     $this->shopifyAuthService = $shopifyAuthService;
 }

然后设置一个方法

public function getDashboard()
{
    $shopUrl = $this->shopifySession['shop_url'];
    $appName = $this->shopifySession['app_name'];

    // find user, then get countdowns based on that
    $user = ShopifyUser::where([
        'shop_url' => $shopUrl,
        'app_name' => $appName,
    ])->first();

    $appConfig = config('shopify-auth.' . $appName);

    $this->checkExistsAndCreateScriptTag($shopUrl, $user->access_token, $user, $appName);

    return view('app_name.dashboard')->with([
        'app_key' => config($appConfig['key']),
    ]);
}