/laravel-magento-client

A client to easily communicate with Magento from your Laravel application

Primary LanguagePHPMIT LicenseMIT

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'];
        }
    }
}

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

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
Identity link URL: https://example.com/magento/oauth/identity

When creating the integration, Magento will send multiple tokens and secrets to your application via the callback-endpoint. This information will be saved in a JSON file, as configured in magento.php. 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

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 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')
        ->paginate(1, 50)
        ->get();

You can view more examples in Magento's documentation.

Pre defined requests (deprecated)

Requests are deprecated as the lazy functionality has been made available in the client itself.

This package comes bundled with a few predefined request so that you do not have to reinvent the wheel.

More Examples

You can view the tests for more examples.

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.