litvinjuan/laravel-shopify

将您的Laravel应用程序连接到Shopify API

1.0.6 2021-09-17 15:30 UTC

This package is auto-updated.

Last update: 2024-09-17 22:04:19 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

这里应该是您的描述。限制在一两段话以内。考虑添加一个小示例。

安装

您可以通过Composer安装此包

composer require litvinjuan/laravel-shopify

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="laravel-shopify-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="laravel-shopify-config"

设置

该包附带一个默认的Shop模型,您可以使用它。但是,如果您想添加额外的属性或关系,您需要创建自己的Shop模型,该模型实现ShopContract接口并使用ShopTrait特征。

class Shop extends Model implements ShopContract
{
    use ShopTrait;

    // Your relationships, properties, etc.
}

注意:记住在运行迁移之前修改迁移以添加您可能需要的任何额外列

然后,您必须将您的商店模型添加到包配置中。如果您还没有发布配置(使用“安装”部分中的命令),请发布配置并将shop-model值更改为您的模型

// laravel-shopify.php
// ...

'shop-model' => App\Models\Shop::class // or your namespace 

// ...

下一步是设置我们的商店所有者,这是一个与您的商店相关的模型。目前,我们只支持用户作为所有者,所以进入您的用户模型并添加以下接口

class User extends Authenticatable implements ShopifyOwner
{
    // ...
}

现在,有两种方式可以将您的用户和商店相关联。
如果您想每个用户只有一个商店,那么您应该使用HasShop特征。如果您想每个用户拥有多个商店,即一个用户可以访问多个商店,则使用HasShops特征。

class User extends Authenticatable implements ShopifyOwner
{
    use HasShop; // one shop per user
    use HasShops; // multiple shops per user
}

注意:您的模型中只包含一个特征

一旦您的用户模型设置完成,请再次进入配置文件并更改user-model值为您自己的模型

// laravel-shopify.php
// ...

'user-model' => App\Models\User::class // or your namespace 

// ...

最后一步是实现ShopLoader。这是一个类,将告诉Shopify在请求期间如何确定使用哪个商店。以下是一个为只有一个商店的用户工作的ShopLoader示例,并简单地告诉Shopify使用当前认证用户的商店

class UserShopLoader implements ShopLoader
{
    public function load(): ?ShopContract
    {
        if (! Auth::check()) {
            return null;
        }

        return Auth::user()->shop;
    }
}

一旦您实现了自己的ShopLoader或使用了上面提供的ShopLoader,您需要将其添加到Shopify配置文件中

// laravel-shopify.php
// ...

'shop-loader-class' => App\Loaders\MyCustomShopLoader::class // or your namespace 

// ...

最后,您需要在Shopify开发者账户中创建一个应用程序,并在.env文件中设置以下变量

SHOPIFY_API_KEY= 这是您的应用程序的API密钥。您可以在应用程序仪表板中找到它
SHOPIFY_API_SECRET= 这是您的应用程序的API密钥。您可以在应用程序仪表板中找到它
SHOPIFY_API_SCOPES= 如果在认证请求期间未提供作用域,Shopify将使用这些作用域。
SHOPIFY_API_CALLBACK_URL= 用户在Shopify上安装应用程序后的回调URL。请记住在应用程序仪表板中将此URL列入白名单。

用法

此包向您的Laravel应用程序添加了一些内容。

首先,您有一个新的中间件signed.shopify,它验证请求是否来自Shopify并且具有有效的签名。将此中间件包含在您希望验证请求来自Shopify的任何webhook路由或其他请求中。

Route::post('/shopify/webhook', 'MyController@webhook')->middleware(['signed.shopify']);

还有一个新的Shopify身份验证守卫,允许您在用户在Shopify管理面板中查看您的应用程序时进行身份验证。您应该使用此身份验证守卫为用户可能从Shopify管理面板访问的任何路由,而不是通过您的网站。

Route::get('/shopify/app', 'ShopifyController@app')->middleware(['auth:shopify']);

身份验证

要在Shopify中身份验证您的用户,您需要添加两个路由:一个用于将用户重定向到Shopify的Oauth屏幕,另一个用于从该屏幕返回的回调

Route::get('/redirect', 'ShopifyController@redirect');
Route::get('/callback', 'ShopifyController@callback');

在您的重定向路由中,您需要接收一个商店域名参数,以连接到用户想要连接的Shopify商店。然后,只需使用以下方式将他们重定向:

public function redirect(Request $request)
{
    $shopDomain = $request->input('shop');
    return Shopify::redirect($user, $shopDomain);
}

用户接受权限范围并在他们的Shopify商店中安装应用程序后,将被重定向回您的回调URL。最后一步是配置回调以生成必要的访问令牌。

public function callback(Request $request)
{
    $shopDomain = $request->input('shop');
    return Shopify::redirect($user, $shopDomain);
}

您还可以将不同的回调URL或权限范围作为重定向方法的第三个和第四个参数传递。

public function callback(Request $request)
{
    $shopDomain = $request->input('shop');
    return Shopify::redirect($user, $shopDomain, 'https://mysite.com/custom-callback', ['scope1', 'another-cool-scope']);
}

要与Shopify API交互,只需调用您想要使用的商店中的api()方法,并访问您想要使用的端点。

$response = $shop->api()->get("/admin/api/2020-07/products.json");
$response = $shop->api()->post("/admin/api/2020-07/webhooks.json", [ /* ... payload ... */ ]);
$response = $shop->api()->put(...);
$response = $shop->api()->delete(...);
// ...

Shopify外观

该包提供了一种Shopify外观,其中包含一些有用的方法。

Shopify::redirect               // First step of the Shopify authentication
Shopify::callback               // Last step of the Shopify authentication
Shopify::getShop                // Get the current shop. For routes viewed from the Shopigy Admin panel, this will be the current Shop. For routes within your own domain, this will be the one inferred from the ShopLoader you set in your config file
Shopify::setShop                // Manually set the current shop
Shopify::forget                 // Forget the current shop
Shopify::hasShop                // Returns true if there is a current shop set
Shopify::isValidHmac            // Validates the Shopify hmac signature for the current request
Shopify::assertShopExists       // Throws an exception if the Shopify store domain isn't found in the database

变更日志

请参阅变更日志获取有关最近更改的更多信息。

贡献

请参阅贡献指南获取详细信息。

安全漏洞

请参阅我们的安全策略了解如何报告安全漏洞。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件