it-bens/shopware-sdk

Shopware 6 平台的 PHP SDK (基于 vin-sw/shopware-sdk 分支)

dev-main 2024-10-01 16:20 UTC

This package is auto-updated.

Last update: 2024-10-01 19:34:02 UTC


README

php

"Buy Me A Coffee"

GitHub Release Latest Version on Packagist Software License

Shopware PHP SDK 是 Shopware 6 API 的简单 SDK 实现。它帮助以面向对象的方式访问 API。

如果你熟悉 Shopware 6 DAL 语法以及如何获取它,你可能觉得这个示例是可预测且直接的

image

或者从外部服务器发送通知 image

你可以使用 1.x 连接到 sw 6.5,但为了获取最新的模式和新的 6.5 功能,你应该使用 2.x 版本。

安装

使用 Composer 安装

composer require vin-sw/shopware-sdk

SDK 主要功能

  • Admin API

    • CRUD API
    • 同步 API
    • 用户配置 API
    • 通知 API
    • Admin 搜索 API
    • 其他服务
    • ... (待办事项)
  • Webhook 辅助工具

    • Webhook 注册
    • Webhook 认证
    • Webhook 接收器
  • 如果你使用 Laravel 8,请考虑查看此 Laravel Shopware SDK 适配器

使用方法

更多示例在 examples 文件夹中,对于将 SDK 集成到应用程序中,请参阅此 存储库

管理员身份验证

  • 支持 3 种授权类型,你可以为身份验证创建这 3 种授权类型之一

使用密码授权类型

$grantType = new PasswordGrantType($username, $password);

使用客户端凭据授权类型

$grantType = new ClientCredentialsGrantType($clientId, $clientSecret);

使用刷新令牌授权类型

$grantType = new RefreshTokenGrantType($refreshToken);

或者通过动态方式

$grantType = GrantType::createFromConfig($config);

请参阅 身份验证 示例进行参考。

在获得 GrantType 对象后,您可以使用 AdminAuthenticator 获取管理员的访问令牌

$adminClient = new AdminAuthenticator($grantType, $shopUrl);
$accessToken = $adminClient->fetchAccessToken();
$context = new Context($shopUrl, $accessToken);

注意:您可能希望将访问令牌对象存储到数据库中,这样您可以在每次进行 Admin API 请求时创建对象而不必请求另一个访问令牌。

使用标准和使用存储库

当你使用 Shopware 的核心或 SW 管理中的存储库工作时,它与您所期望的非常相似。

以下是一个获取具有免费送货的产品示例

// Create the repository for the entity
$productRepository = RepositoryFactory::create(ProductDefinition::ENTITY_NAME);

// Create the criteria
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('shippingFree', true));

// Using this criteria and the context object that you create from authentication step, you can retrieving the result
$products = $productRepository->search($criteria, $context);

每个 Shopware 实体都映射到 Entity 和 Collection 类中,这些类可以在 Data/Entity 中找到,因此您可以在从 Admin API 获取数据时轻松访问它们的属性。

支持方法 getsearchsearchIdscreateupdatedeletesyncDeletedcreateVersionmergeVersiondeleteVersioncloneschema。每个方法都需要一个 Context 对象

请参阅 examples/entity-repository.php 以获取一些有用的参考。

使用应用程序

AppRegistration 示例

此示例从 Laravel 路由操作中提取,但也可以在其他框架中使用相同的做法

public function register(ShopRepository $repository): RegistrationResponse
{
    $authenticator = new WebhookAuthenticator();

    $app = new App(config('sas_app.app_name'), config('sas_app.app_secret'));

    $response = $authenticator->register($app);

    // Save the response the database...
    $repository->createShop($response->getShop());

    $confirmationUrl = route('sas.app.auth.confirmation');

    return new RegistrationResponse($response, $confirmationUrl);
}

public function confirm(Request $request, ShopRepository $shopRepository): Response
{
    $shopId = $request->request->get('shopId');

    $shopSecret = $shopRepository->getSecretByShopId($shopId);

    if (!WebhookAuthenticator::authenticatePostRequest($shopSecret)) {
        return new Response(null, 401);
    }

    $shopRepository->updateAccessKeysForShop(
        $shopId,
        $request->request->get('apiKey'),
        $request->request->get('secretKey')
    );

    return new Response();
}

动作按钮响应示例

当你收到来自动作按钮的 POST 请求时,你可以返回以下之一 ActionResponse(PSR-7 Response)类,这些类已从 Shopware 的核心移植

namespace Vin\ShopwareSdk\Data\Response;
/**
* @see Shopware\Core\Framework\App\ActionButton
 */
new EmptyResponse();
new ReloadDataResponse($shopSecret);
new OpenNewTabResponse($shopSecret, 'http://shopware.test');
new NotificationResponse($shopSecret, 'Success!', NotificationResponse::SUCCESS);
new NotificationResponse($shopSecret, 'Error!', NotificationResponse::ERROR);
new OpenModalResponse($shopSecret, $iframeUrl, OpenModalResponse::LARGE_SIZE, true);

使用 Admin API 服务

Api服务需要一个上下文对象作为其第一个参数。查看examples/sync-service.phpexamples/info-service.php以获取一些参考。

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

与Webhook一起工作

贡献

欢迎在GitHub issues页面创建问题或直接通过levienthuong@gmail.com联系我。

安全性

如果您发现任何安全问题,请通过电子邮件levienthuong@gmail.com联系,而不是使用问题跟踪器。

要求

  • ext-curl
  • PHP >=7.4
  • SW >= 6.4

此SDK主要针对Shopware 6.4及更高版本,较早的SW应用程序可能仍可使用,但未经测试。

致谢

许可

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