justbetter/laravel-magento-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

justbetter/laravel-magento-client

Composer 安装命令:

composer require justbetter/laravel-magento-client

包简介

A client to interact with Magento

README 文档

README

Banner

Laravel Magento Client

Tests Coverage Analysis Total downloads

A client to communicate with Magento from your Laravel application.

<?php

class Example
{
    public function __construct(
        protected \JustBetter\MagentoClient\Client\Magento $magento,
    ) {
    }

    public function retrieveProduct()
    {
        $response = $this->magento->get('products/1');
    }

    public function retrieveOrdersLazily()
    {
        $retrievedOrders = [];

        $searchCriteria = \JustBetter\MagentoClient\Query\SearchCriteria::make()
            ->where('state', 'processing');

        foreach ($this->magento->lazy('orders', $searchCriteria->get()) as $order) {
            $retrievedOrders[] = $order['increment_id'];
        }
    }

    public function multipleConnections()
    {
        $this->magento->connection('connection_one')->get('products/1');
        $this->magento->connection('connection_two')->get('products/1');
    }
}

Looking to synchronize prices or stock to Magento?

Installation

Are you coming from grayloon/laravel-magento-api? We have written a migration guide!

Require this package:

composer require justbetter/laravel-magento-client

Configuration

Add the following to your .env:

MAGENTO_BASE_URL=
MAGENTO_ACCESS_TOKEN=

Optionally, publish the configuration file of this package.

php artisan vendor:publish --provider="JustBetter\MagentoClient\ServiceProvider" --tag=config

Multiple connections

This package supports connecting to multiple Magento instances. In the configuration file you will find an array with the connections where you can configure each connection.

    'connection' => 'default',
    'connections' => [
        'default' => [
            /* Base URL of Magento, for example: https://magento.test */
            'base_url' => env('MAGENTO_BASE_URL'),

           // Other settings
        ],
        'another_connection' => [
            'base_url' => env('ANOTHER_MAGENTO_BASE_URL'),

            // Other settings
        ],
    ],

The connection setting sets which connection is used by default. You can switch connections by using the connection method on the client.

/** @var \JustBetter\MagentoClient\Client\Magento $client */
$client = app(\JustBetter\MagentoClient\Client\Magento::class);

$client->connection('connection_one')->get('products');

Authentication

By default, this packages uses Bearer tokens to authenticate to Magento. Since Magento 2.4.4, this method of authentication requires additional configuration in Magento as described here.

It is recommended to authenticate to Magento using OAuth 1.0 which will also prevent the additional configuration requirements, besides setting up the integration itself.

Setting up OAuth 1.0

Start by adding the following variable to your .env-file.

MAGENTO_AUTH_METHOD=oauth

Note that you can also remove MAGENTO_ACCESS_TOKEN at this point, as it will be saved in a file instead.

Next, open Magento and create a new integration. When configuring, supply a Callback URL and Identity link URL. If you have not made any changes to your configuration, these are the URLs:

Callback URL:      https://example.com/magento/oauth/callback/{connection}
Identity link URL: https://example.com/magento/oauth/identity/{connection}

connection is the key in your connections array in the configuration file.

When creating the integration, Magento will send multiple tokens and secrets to your application via the callback-endpoint. This information will be saved in the database, as configured in magento.php. You may adjust this to save the key in a JSON file or create your own implementation Magento will redirect you to the identity-endpoint in order to activate the integration.

For more information about OAuth 1.0 in Magento, please consult the documentation.

Identity endpoint

Caution

Be sure to add your authentication middleware

Note that the identity-endpoint does not have any authentication or authorization middleware by default - you should add this in the configuration yourself. If you do not have any form of protection, anyone could change the tokens in your secret file.

It is recommended that only administrators of your application are allowed to access the identity endpoint.

Usage

You can get an instance of the client by injecting it or by resolving it:

<?php

public function __construct(
    protected \JustBetter\MagentoClient\Client\Magento $magento
) {

}

// OR

/** @var \JustBetter\MagentoClient\Client\Magento $client */
$client = app(\JustBetter\MagentoClient\Client\Magento::class);

After you got an instance you can use the graphql, get, post, put and patch methods to use the Magento API.

SearchCriteria / Filtering

To easily create search criteria you can use the \JustBetter\MagentoClient\Query\SearchCriteria class. For example:

<?php

$search = \JustBetter\MagentoClient\Query\SearchCriteria::make()
        ->select('items[sku,name]')
        ->where('sku', '!=', '123')
        ->orWhere('price', '>', 10),
        ->whereIn('sku', ['123', '456'])
        ->orWhereNull('name')
        ->orderBy('sku')
        ->paginate(1, 50)
        ->get();

You can view more examples in Magento's documentation.

Connections

You can connect to other connections using the connection method on the client.

/** @var \JustBetter\MagentoClient\Client\Magento $client */
$client = app(\JustBetter\MagentoClient\Client\Magento::class);

$client->connection('connection_one')->get('products');
$client->connection('connection_two')->get('products');

GraphQL

You can run authenticated GraphQL queries using the graphql method on the client.

/** @var \JustBetter\MagentoClient\Client\Magento $client */
$client = app(\JustBetter\MagentoClient\Client\Magento::class);

$client->graphql(
    'query searchProducts($search: String) {
        products(
            search: $search
        ) {
            items {
            sku
            }
        }
    }',
    [
        'search' => 'test'
    ]
);

More Examples

You can view the tests for more examples.

Checking if Magento is available

This client keeps track whether Magento is available by storing the count of any >=502 and <=504 response code in cache. You may use the available() method on the client in order to check if Magento is available.

The threshold, timespan and status codes can be configured in the configuration file per connection.

Jobs

This package provides a job middleware that releases jobs back onto the queue when Magento is not available. The middleware is located at \JustBetter\MagentoClient\Jobs\Middleware\AvailableMiddleware.

Testing

This package uses Laravel's HTTP client so that you can fake the requests.

<?php

Http::fake([
    '*/rest/all/V1/products*' => Http::response([
        'items' => [['sku' => '::some_sku::']],
        'total_count' => 1,
    ]),
]);

Quality

To ensure the quality of this package, run the following command:

composer quality

This will execute three tasks:

  1. Makes sure all tests are passed
  2. Checks for any issues using static code analysis
  3. Checks if the code is correctly formatted

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

JustBetter logo

justbetter/laravel-magento-client 适用场景与选型建议

justbetter/laravel-magento-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 111.93k 次下载、GitHub Stars 达 49, 最近一次更新时间为 2022 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 justbetter/laravel-magento-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 justbetter/laravel-magento-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 justbetter/laravel-magento-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 111.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 49
  • 点击次数: 9
  • 依赖项目数: 8
  • 推荐数: 1

GitHub 信息

  • Stars: 49
  • Watchers: 5
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-20