shoplygg / shoply-php
Shoply's API from your own server: every route as a method, safe retries, pages, checking webhooks, and OAuth for apps.
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Shoply's API from your own server. Every route of the API reference is a method, with array shapes your editor and PHPStan read, safe retries, lists a page at a time or all at once, and checking that a webhook came from Shoply.
composer require shoplygg/shoply-php
PHP 8.1 or later, with the curl extension. No other dependencies.
$shoply = new Shoply\Client(['apiKey' => getenv('SHOPLY_API_KEY')]); $order = $shoply->orders->show('ORDER_ID'); foreach ($shoply->customers->list(['search' => 'gmail.com']) as $customer) { echo $customer['emailAddress'], "\n"; } $coupon = $shoply->coupons->create(['code' => 'SPRING10', 'value' => 10]);
Make the key under Developers, API Keys in the dashboard, with the permissions your code needs. Keep it on your server.
A route's method is its id: Get an order is orders.show, so $shoply->orders->show($orderId). The address's IDs come first, in order, then the fields as one array, then options.
Errors
A refusal is a Shoply\ShoplyException, with status, errorCode (not_found, missing_scope, validation_failed...), errors for each field that is wrong, and requestId.
try { $shoply->coupons->create(['code' => 'SPRING10', 'value' => 10]); } catch (Shoply\ShoplyException $e) { if ($e->errorCode === 'validation_failed') { print_r($e->errors); } }
A change is sent with an Idempotency-Key, the same on every try, so a try whose answer was lost is never carried out twice. Pass ['idempotencyKey' => ...] in the options, the last argument, to use your own.
Webhooks
$event = Shoply\Webhooks::verify( (string) file_get_contents('php://input'), getallheaders(), (string) getenv('SHOPLY_WEBHOOK_SECRET'), ); if ($event['event'] === 'order.paid') { // $event['data'] is the order }
The Customer API
Shoply\Customer signs a buyer in with a code emailed to them, and reads their own orders, keys and subscriptions. It holds the buyer's token and no key.
$buyer = new Shoply\Customer(['storeId' => 'YOUR_STORE_ID']); $buyer->login(['email' => $email]); $buyer->verify(['email' => $email, 'code' => $code]); $me = $buyer->show();
OAuth
An app that sellers connect to their own stores gets a token by OAuth, and passes it as apiKey.
use Shoply\OAuth; $pkce = OAuth::pkce(); $url = OAuth::authorizeUrl(['clientId' => $clientId, 'redirectUri' => $redirectUri, 'state' => $state, 'codeChallenge' => $pkce['challenge']]); // Back at your redirect URL: $token = OAuth::exchange(['clientId' => $clientId, 'clientSecret' => $clientSecret, 'code' => $code, 'redirectUri' => $redirectUri, 'codeVerifier' => $pkce['verifier']]); $shoply = new Shoply\Client(['apiKey' => $token['accessToken'], 'storeId' => $token['storeId']]);
How apps work: https://docs.shoply.gg/guides/oauth
The whole guide: https://docs.shoply.gg/api-reference/php