vin-sw/shopware-sdk

Shopware 6 平台的 PHP SDK

2.0.0 2023-05-30 17:33 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 主要功能

  • 管理员 API

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

    • Webhook 注册
    • Webhook 身份验证
    • Webhook 接收器
  • 如果你使用 Laravel 8,可以考虑查看这个Laravel Shopware SDK 适配器

用法

更多示例在示例文件夹中,对于在 App 中集成 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);

注意:你可能希望将访问令牌对象存储到数据库中,这样你就可以在每次管理员 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 中找到,因此你可以轻松访问它们的属性,当从管理员 API 获取数据时。

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

查看 示例 以获取一些有用的参考。

使用 App

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);

使用管理员 API 服务

ApiService需要一个Context对象作为它的第一个参数。查看examples/sync-service.phpexamples/info-service.php以获取一些参考。

变更日志

请参阅CHANGELOG了解最近发生了哪些变化。

使用Webhook

贡献

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

安全

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

要求

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

此SDK主要针对Shopware 6.4及更高版本,较早的SW应用程序在没有测试的情况下可能仍然可使用

致谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。