PHPackages                             mrthito/laravel-google-merchant-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mrthito/laravel-google-merchant-api

ActiveLibrary[API Development](/categories/api)

mrthito/laravel-google-merchant-api
===================================

Laravel Google Merchant Products API for Google Shopping.

1.0(6mo ago)02MITPHPPHP ^8.2|^8.3|^8.4

Since Oct 18Pushed 6mo agoCompare

[ Source](https://github.com/mrthito/laravel-google-merchant-api)[ Packagist](https://packagist.org/packages/mrthito/laravel-google-merchant-api)[ Docs](https://github.com/mrthito/laravel-google-merchant-api)[ RSS](/packages/mrthito-laravel-google-merchant-api/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Laravel Google API
==================

[](#laravel-google-api)

A sweet package for managing Google Merchant Center feeds for Google Shopping. This package is prepared to implement the advanced [Content API](https://developers.google.com/shopping-content/v2.1/quickstart) for Merchants.

Example usage:

```
use MrThito\GoogleMerchantApi\Facades\ProductApi;
use MrThito\GoogleMerchantApi\Facades\OrderApi;

...

ProductApi::insert(function($product){
    $product->offerId(1)
        	->title('Purple Shoes')
        	->description('What are thooose!!')
        	->price(10)
        	->custom('purchase_quantity_limit', 1000)
            ->availabilityDate( today()->addDays(7) );
})->then(function($response){
    echo 'Product inserted';
})->otherwise(function($response){
    echo 'Insert failed';
})->catch(function($e){
    echo($e->getResponse()->getBody()->getContents());
});

OrderApi::list()->then(function($response){
    //
})->otherwise(function($response){
    echo 'List failed';
})->catch(function($e){
    echo($e->getResponse()->getBody()->getContents());
});

OrderApi::scout(); // Scout and fire event
```

Features
--------

[](#features)

- **Products API**
    - Implements the `insert`, `get`, `delete` and `list` API calls
    - Uses defined schema interface for directly working with eloquent models (a product model)
    - Event listeners to respond to changes made to eloquent models, and `insert` them automatically
- **Orders API**
    - Implements the `acknowledge`, `cancel`, `cancelLineItem`, `rejectReturnLineItem`, `returnRefundLineItem`, `get` and `list` API calls.
    - Internal schedule scouts un-acknowledged orders and fires an event. This means new orders on your Google Shopping can be automatically acknowledged and registered.
    - Includes sandbox functions for `testOrders`.
- Multiple Merchants (1.1.0)

Updating to 1.1.x
-----------------

[](#updating-to-11x)

Although backwards compatible, be sure to update your config to be able to use multiple merchants.

Installation
------------

[](#installation)

Via composer:

```
composer require mrthito/laravel-google-merchant-api
```

Install the service provider (skip for Laravel&gt;=5.5);

```
// config/app.php
'providers' => [
    ...
    MrThito\GoogleMerchantApi\GoogleMerchantApiServiceProvider::class,
],

```

Publish the config

```
php artisan vendor:publish --tag="google-merchant-api-config"
```

Setup &amp; Authorisation
-------------------------

[](#setup--authorisation)

- Follow the instructions [here](https://developers.google.com/shopping-content/v2/quickstart) and create a service account key. Create `storage/app/google-merchant-api/service-account-credentials.json` in your app root and store the downloaded json contents
- Obtain your numeric Merchant ID
- Add your Merchant ID and the path to your service account credentials to the config
- In the config, setup the attributes section in product content if you need to use arrays or models

Usage
-----

[](#usage)

### Multiple Merchants

[](#multiple-merchants)

From `1.1.0` we can now define multiple merchants and switch between them by simply calling the `merchant` method from either the Order or Product API class.

```
ProductApi::merchant('my-pet-store')->insert($product);
```

```
// config/laravel-google-merchant-api.php
...
    'merchants' => [
        'my-pet-store' => [
			'app_name' => config('app.name'),
			'merchant_id' => '000000000',
			'client_credentials_path' => storage_path('app/my-pet-store-credentials.json'),
		]
    ],
...
```

Or

```
ProductApi::merchant([
    'app_name' => 'My Pet Store',
    'merchant_id' => '000000000',
    'client_credentials_path' => storage_path('app/my-pet-store-credentials.json')
])->insert($product);
```

### Product API

[](#product-api)

The Google Merchant contents can be queried via the `insert`, `get`, `delete`, and `list` methods. The product content is contained and handled via the `Product` class. An instance of this class can be passed directly or resolved in a Closure callback. An instance can be population by

- Directly accessing underlying attributes. See [special functions](doc/prodcut-conent-special-methods.md).
- Passing an eloquent model, or by
- Passing a raw array

To pass an array or a model, the attributes relationships must be defined in the config.

#### Insert

[](#insert)

The insert method creates a new content, as well as updates an old content if the `channel`, `contentLanguage`, `targetCountry` and `offerId` are the same.

```
$attributes = [
    'id' => 1, // maps to offerId (if set in config)
    'name' => 'Product 1', // likewise maps to title
];
ProductApi::insert(function($product) use($attributes){
    $product->with($attributes)
        	->link('https://mrthito.com/mg001')
        	->price(60, 'USD');
})->then(function($data){
    echo 'Product inserted';
})->otherwise(function(){
    echo 'Insert failed';
})->catch(function($e){
    dump($e);
});
```

**With arrays**:

```
use MrThito\GoogleMerchantApi\Contents\Product\Product as GMProduct;

...
$attributes = [
    'id' => 1,
    'name' => 'Product 1',
];
$product = (new GMProduct)->with($attributes);
```

The `attributes` values must be defined as per the attributes map in the config.

**With Eloquent Models**:

```
use App\Models\Product;
use MrThito\GoogleMerchantApi\Contents\Product\Product as GMProduct;

...
$model = Product::find(1);
$product = (new GMProduct)->with($model);

ProductApi::insert($product)->catch(function($e){
    // always catch exceptions
});
```

The model `attributes` values must be defined as per the attributes map in the config. For accessing undefined models attributes, use Accessors and custom Model attributes:

```
protected $appends = [
    'availability',
    'gm_price',
];

...

public function getAvailabilityAttribute(){
    return 'in stock'; // calculate
}
public function getGmPriceAttribute(){
    return [
        'value' => $this->price,
        'currency' => $this->currency->code,
    ];
}
```

For setting custom Product contents (`customAttributes`), you're probably better off using the `custom()` method. Likewise for `availabilityDate` use the `availabilityUntil()` method.

**With Events &amp; Listeners**:

The provided event and listener can be setup such that when your application creates or updates a model, the product content is automatically inserted.

To set this up, add the following snippet to your eloquent mode. The `product` variable can be a model or an array.

```
use MrThito\GoogleMerchantApi\Events\ProductCreatedOrUpdatedEvent;

...

/**
 * The "booting" method of the model.
 *
 * @return void
 */
protected static function boot() {
    parent::boot();

    // when a product is created
    static::created(function(Product $product){
        // perhaps a logic to ignore drafts and private products
        if($product->is_active && (config('app.env') === 'production')){
        	event(new ProductCreatedOrUpdatedEvent($product));
        }
    });

    // when a product is updated
    static::updated(function(Product $product){
        // perhaps a logic to ignore drafts and private products
        if($product->is_active && (config('app.env') === 'production')){
        	event(new ProductCreatedOrUpdatedEvent(function($gm_product) use ($product){
                $gm_product->with($product)
                    	   ->preorder()
                    	   ->availabilityDate($product->preorder_date);
            }));
        }
    });
}
```

Next, define the events relationship in `EventServiceProvider.php`.

```
use MrThito\GoogleMerchantApi\Listeners\ProductCreatedOrUpdatedListener;

...

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    ...,
    /**
     * Product events
     */
    ProductCreatedOrUpdatedEvent::class => [
        ProductCreatedOrUpdatedListener::class,
    ],

];
```

#### Get &amp; List

[](#get--list)

```
ProductApi::get($product)->then(function($data){
    //
})->catch(function($e){
    // always catch exceptions
});
```

The `list` method calls the `get` method without any parameters;

```
ProductApi::list()->then(function($data){
    //
});
```

So the following should likewise retrieve the product list:

```
ProductApi::get()->then(function($data){
    //
});
```

#### Delete

[](#delete)

```
ProductApi::delete($product)->then(function($data){
    //
});
```

To set up with the event listener, add the following to your eloquent model:

```
use MrThito\GoogleMerchantApi\Events\ProductDeletedEvent;

...

protected static function boot() {
    parent::boot();

    ...

    // when a product is deleted
    static::deleted(function(Product $product){
        if(config('app.env') === 'production'){
        	event(new ProductDeletedEvent($product));
        }
    });
}
```

Then define the relationship in `EventServiceProvider.php`:

```
use MrThito\GoogleMerchantApi\Listeners\ProductDeletedListener;

...

protected $listen = [
    ...,
    ProductDeletedEvent::class => [
        ProductDeletedListener::class,
    ],
];
```

Order API
=========

[](#order-api)

***Please note that these implementations have not been properly tested.***

#### Using the API methods

[](#using-the-api-methods)

The `acknowledge`, `cancel`, `cancelLineItem`, `rejectReturnLineItem`, `returnRefundLineItem`, `get`, `list` methods are currently implemented for interacting with your Google Merchant.

The format for using these methods are standard across the entire package. For example, an order can be acknowledged by

```
OrderApi::acknowledge(function($order){
    $order->id('TEST-1953-43-0514');
});
```

or by

```
$order = (new Order)->with([
    'id' => 'TEST-1953-43-0514',
]);
OrderApi::acknowledge($order);
```

Additionally the `listAcknowledged` method is provided so one can list acknowledged orders if needed.

#### Scheduled Scouts

[](#scheduled-scouts)

If `schedule_orders_check` is set as true in the config, the package will regularly scout un-acknowledged orders and will fire a `\MrThito\GoogleMerchantApi\Events\NewOrdersScoutedEvent` event. This event includes an **array** of orders of class `\MrThito\GoogleMerchantApi\Contents\Order`. The orders are structured as per the [Order Resource](https://developers.google.com/shopping-content/v2/reference/v2.1/orders#resource).

Example handle in your listener:

```
use MrThito\GoogleMerchantApi\Events\NewOrdersScoutedEvent;
use MrThito\GoogleMerchantApi\Facades\OrderApi;

...
public function handle(NewOrdersScoutedEvent $event)
{
    $merchant = $event->merchant; // array key as defined in config
    $merchant_id = $event->merchant_id;

    foreach($event->orders as $gm_order){
        OrderApi::acknowledge($gm_order);

        $gm_order = $gm_order->all(); // get all attributes, including mutated attributes
        foreach($gm_order['lineItems'] as $line_item){
            $model = $line_item['model']; // retrieves model
            $quantity = $line_item['quantityOrdered'];
            $shipping = $line_item['shippingDetails'];
            $delivery_date = $shipping['deliverByDate']->diffForHumans();

            // register new order item
        }

        // register new order
    }
}
```

**Notes**:

- Accessing the `lineItems` will automatically resolve and attach the corresponding model to each item. Of course this assumes your inserted products' `offerId` correspond to the model's ID &amp; primary key.
- If you haven't already started Laravel scheduler, you'll need to add the following Cron entry to your server. `* * * * * php artisan schedule:run >> /dev/null 2>&1`.
- It's important you test that the scheduler is set up correctly. For this reason, the `MrThito\GoogleMerchantApi\Events\OrderContentScoutedEvent` event is provided. If `debug_scout` is set to true in the config, this event is fired whenever the scheduler fires.

#### Sandboxing

[](#sandboxing)

The OrderApi class provide a way of calling some of the sandbox operations. Example:

```
OrderApi::sandbox()->create(function($order){
    $order->shippingCost(30)
          ->shippingOption('economy')
          ->predefinedEmail('pog.dwight.schrute@gmail.com')
          ->predefinedDeliveryAddress('dwight');
})
```

You may use

```
OrderApi::sandbox()->testCreate();
```

to use a preset example.

Implemented sandbox actions:

FunctionSandbox Action`create`createtestorder`advance`advancetestorder`cancel`createtestorder`createReturn`createtestreturnCommands
--------

[](#commands)

This package provides an artisan command for scouting orders.

```
php artisan gm-orders:scout
```

Handling Errors
---------------

[](#handling-errors)

Methods that throw exceptions

- `MrThito\GoogleMerchantApi\Contents\Product::with()`

    throws `MrThito\GoogleMerchantApi\Exceptions\ProductContentAttributesUndefined` if the supplied attributes is not a Model or array.
- The `insert`, `get`, `delete`, `list`, `listAcknowledged` and `scout` methods in the API classes will throw `GuzzleHttp\Exception\ClientException` if the client request is corrupted, fails, not defined or not authorised.
- The `MrThito\GoogleMerchantApi\Exceptions\Invalid**Input` exceptions are thrown if an unresolvable entity is passed as a content attribute.
- The `merchant` method throws `MrThito\GoogleMerchantApi\Exceptions\InvalidMechantDetails` if unable to resolve a merchant ID or credentials path.

Exceptions should be handled using the `catch` function. If making synchronous calls, use the try-catch block. You'd be well advised to always catch requests (and notify your business logic), seeing that Google has a million reasons to deny any request.

Design Notes
------------

[](#design-notes)

- Insert, List, Get, Delete methods will always return a clone of the original instance if using the default asynchronous feature. This allows the then, otherwise, and catch callbacks of multiple requests to not override. These methods return a Guzzle response if set to synchronous mode.
- If the delete method is called and the resolved content ID is invalid, it returns without making any requests or throwing any errors. If the get method, it returns a list of products or orders.
- A valid product content ID follows the pattern *online:en:AU:1* i.e. `channel:contentLanguage:targetCountry:offerId`. This ID is of course auto generated; and the attributes, except for `offerId`, have default values.
- Requests can take up to 2 hours before they reflect on your Google Merchant Center. Patience!
- Unlike the ProductApi or OrderApi classes, the events constructor may take a Model, array or callback.
- Calling the `all` method on a `Product`, `Order` or any content class will resolve all mutated attributes. e.g. `$order['lineItems'][0]['shippingDetails']['deliverByDate']` returns a `Carbon`.

#### Synchronous Calls

[](#synchronous-calls)

All the above are by default asynchronous. To make synchronous calls, use the `sync` method:

```
try{
    $response = ProductApi::sync()->insert(function($product){
        $product->offerId(1)
            	->country('AU')
            	->inStock(false);
    });
}catch(\GuzzleHttp\Exception\ClientException $e){
    //
}
```

**Note**: In this case, methods such as `insert`, `get`, `delete`, and `list`, etc, returns a Guzzle response when called asynchronously (rather than an instance of `ProductApi` or `OrderApi`. This means your exception blocks should be wrapped around requests.

Contributing
------------

[](#contributing)

This package is intended to provide a Laravel solution for the Google Shopping API for Google Merchant. Currently, only the Product Content has been adequately implemented and tested. For orders, refunds, etc., ideas and pull-requests are welcome.

Credits
-------

[](#credits)

- [Augustus Okoye](https://github.com/augustusnaz)
- [All Contributors](https://github.com/augustusnaz/laravel-google-merchant-api/graphs/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance66

Regular maintenance activity

Popularity3

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 78.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

206d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/73e18bf44d32db8cbecb0b5d40a21d946592b207fd3a3d17db57887c8b2148e0?d=identicon)[mrthito](/maintainers/mrthito)

---

Top Contributors

[![augustusnaz](https://avatars.githubusercontent.com/u/51074349?v=4)](https://github.com/augustusnaz "augustusnaz (30 commits)")[![mrthito](https://avatars.githubusercontent.com/u/49310993?v=4)](https://github.com/mrthito "mrthito (2 commits)")[![torro-uk](https://avatars.githubusercontent.com/u/68008600?v=4)](https://github.com/torro-uk "torro-uk (2 commits)")[![chris-ware](https://avatars.githubusercontent.com/u/19684457?v=4)](https://github.com/chris-ware "chris-ware (1 commits)")[![Paula2001](https://avatars.githubusercontent.com/u/39751126?v=4)](https://github.com/Paula2001 "Paula2001 (1 commits)")[![trippo](https://avatars.githubusercontent.com/u/497169?v=4)](https://github.com/trippo "trippo (1 commits)")[![ianfortier](https://avatars.githubusercontent.com/u/2520657?v=4)](https://github.com/ianfortier "ianfortier (1 commits)")

---

Tags

laravelGoogle Merchant APIGoogle Shopping APImrthito.com

### Embed Badge

![Health badge](/badges/mrthito-laravel-google-merchant-api/health.svg)

```
[![Health](https://phpackages.com/badges/mrthito-laravel-google-merchant-api/health.svg)](https://phpackages.com/packages/mrthito-laravel-google-merchant-api)
```

###  Alternatives

[andreaselia/laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API

1.0k586.2k3](/packages/andreaselia-laravel-api-to-postman)[mollie/laravel-mollie

Mollie API client wrapper for Laravel &amp; Mollie Connect provider for Laravel Socialite

3624.1M28](/packages/mollie-laravel-mollie)[api-ecosystem-for-laravel/dingo-api

A RESTful API package for the Laravel and Lumen frameworks.

3121.5M10](/packages/api-ecosystem-for-laravel-dingo-api)[moirei/laravel-google-merchant-api

Laravel Google Merchant Products API for Google Shopping.

4434.0k](/packages/moirei-laravel-google-merchant-api)[essa/api-tool-kit

set of tools to build an api with laravel

52680.5k](/packages/essa-api-tool-kit)[mll-lab/laravel-graphiql

Easily integrate GraphiQL into your Laravel project

683.2M9](/packages/mll-lab-laravel-graphiql)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
