mohamed-haroun / laravel-zid
Laravel client for the Zid (زد) store API: OAuth, products, orders, customers and webhooks.
Requires
- php: ^8.2
- illuminate/http: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^11.0|^12.0|^13.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A Laravel client for the Zid (زد) store API. It handles the OAuth flow, sends the headers Zid expects, and wraps the products, orders, customers and webhooks endpoints.
Everything here comes from Zid's public API documentation at https://docs.zid.sa. Where the docs do not say something, the package does not guess. See Limitations.
Requirements
- PHP 8.2+
- Laravel 12 or 13
- A Zid Partner account and an app (client ID and secret) to get merchant tokens
Install
composer require mohamed-haroun/laravel-zid
php artisan vendor:publish --tag=zid-config # optional
.env:
ZID_CLIENT_ID= ZID_CLIENT_SECRET= ZID_REDIRECT_URI=https://your-app.example.com/zid/callback ZID_APP_ID= # your app's ID, used as original_id for webhooks ZID_STORE_ID= # optional, see Limitations ZID_LOCALE=en # en or ar ZID_WEBHOOK_USERNAME= # optional, for verifying webhooks ZID_WEBHOOK_PASSWORD=
Connect a store (OAuth)
Zid uses the authorization code grant for confidential clients, with a state check and no PKCE.
use MohamedHaroun\Zid\Facades\Zid; Route::get('/zid/redirect', function () { $state = Zid::oauth()->newState(); session(['zid_state' => $state]); return redirect(Zid::oauth()->authorizationUrl($state)); }); Route::get('/zid/callback', function (Request $request) { abort_unless($request->input('state') === session('zid_state'), 403); // The code works once and expires after 10 minutes. Exchange it right here. $tokens = Zid::oauth()->exchangeCode($request->input('code')); // Store these per merchant, encrypted: // $tokens->authorization, $tokens->managerToken, $tokens->refreshToken, $tokens->expiresAt });
Refresh with Zid::oauth()->refresh($refreshToken). A refresh returns a new refresh token and a new manager token and revokes the old refresh token, so save the whole set. Zid's docs say the refresh token lives one year while the access token lives three, and recommend refreshing on a schedule (about every 10 months), not after a 401.
Use the API
$zid = Zid::connect($authorizationToken, $managerToken); // or: Zid::connectWith($tokens); $zid->account()->store(); $zid->account()->me(); // Products $zid->products()->list(['search' => 'bag', 'page' => 1, 'page_size' => 20]); foreach ($zid->products()->each(['is_published' => true]) as $product) { /* ... */ } $zid->products()->find($id); $zid->products()->create([...]); // payload as in Zid's Create Product page $zid->products()->update($id, [...]); // PATCH $zid->products()->delete($id); // Orders $zid->orders()->list(['payload_type' => 'default', 'order_status' => 'new']); $zid->orders()->find($orderId); $zid->orders()->changeStatus($orderId, 'ready', ['inventory_address_id' => $uuid]); // Customers $zid->customers()->list(['search_term' => 'john']); foreach ($zid->customers()->each() as $customer) { /* cursor pagination */ } $zid->customers()->find($id); $zid->customers()->create([...]); $zid->customers()->update($id, [...]); // PUT $zid->customers()->delete($id); // Webhooks $zid->webhooks()->create('order.create', 'https://your-app.example.com/zid/webhook', conditions: ['status' => 'new'], username: 'user', password: 'secret'); $zid->webhooks()->list(); $zid->webhooks()->delete();
Methods return Zid's decoded JSON as arrays. Request bodies for create and update calls are sent as you give them, so the field names are the ones in Zid's docs.
Each request sends Authorization: Bearer <authorization>, X-Manager-Token, and Accept-Language (en or ar, as Zid requires). Product requests also send Access-Token (same value as the manager token), Role: Manager, and Store-Id when configured, because Zid's product pages list those headers.
Errors
Non-2xx responses throw MohamedHaroun\Zid\Exceptions\ZidException with status, the decoded body, and errorCode() (from message.code, which Zid says is stable; the descriptions are localised). A 429 throws RateLimitException with retryAfter from the Retry-After header. Zid documents a limit of 60 requests per minute per app per store.
Webhooks
Create a webhook with a username and password. Zid then sends them back as a Basic Authentication header. Protect your endpoint with the bundled middleware:
Route::post('/zid/webhook', ZidWebhookController::class)->middleware('zid.webhook');
It compares the header to ZID_WEBHOOK_USERNAME and ZID_WEBHOOK_PASSWORD and returns 401 otherwise. Exclude the route from CSRF protection as you would for any webhook, for example in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) { $middleware->validateCsrfTokens(except: ['zid/webhook']); })
Without that, Laravel answers 419 before the request reaches the middleware. The events Zid lists are in Webhooks::EVENTS. Payload shapes are in Zid's webhook docs. This package does not parse them.
Testing your code
The client uses Laravel's HTTP client, so Http::fake() works:
Http::fake(['api.zid.sa/*' => Http::response(['results' => []])]);
Limitations
- Not tested against a live Zid store. The test suite fakes HTTP using request and response shapes from Zid's docs. Nothing here has been run against Zid's servers with real merchant tokens.
- Endpoints covered: store and user profile, products (list, view, create, update, delete), orders (list, view, change status), customers (list, view, add, update, delete), webhooks (list, create, delete by
original_id). The rest of Zid's API (categories, variants, stock, coupons, reverse orders, abandoned carts, blogs and so on) is not wrapped yet.ZidClientexposesget,post,put,patchanddeleteif you need them. - Store-Id on product calls. Zid's product pages mark a
Store-Idheader as required, but the docs do not say where to get the value or whether it is enforced when a manager token is sent. SetZID_STORE_IDif your calls need it. - Webhook
original_id. Zid describes it as an identifier for your app and shows a numeric app ID in examples. SetZID_APP_IDto your app's ID. - Scopes. Your app needs the scopes for what you call (for example
products.read,orders.read,orders.read_write,third_customers_read,third_customers_write,third_webhook_read,third_webhook_write). The scope names are copied from each endpoint's docs page. A missing scope returns a 401 from Zid, and refreshing will not fix it. - Orders list pagination.
pageandper_pageare required by Zid and default to 1 and 15. There is no auto-paging helper for orders because the list response shape was not readable in the docs. - Webhook payloads are not parsed into objects.
- Laravel 11 is not listed: at the time of writing every 11.x release is flagged by Packagist security advisories, so Composer will not install it by default.
Tests
composer install
composer test
License
MIT. Not affiliated with or endorsed by Zid.