Installation:
composer require osiset/laravel-shopify
Publish the config file:
php artisan vendor:publish --provider="Osiset\Shopify\ShopifyServiceProvider"
Add the middleware to your app/Http/Kernel.php:
'shopify' => \Osiset\Shopify\Middleware\ShopifyMiddleware::class,
Configuration:
Update .env with your Shopify API credentials:
SHOPIFY_API_KEY=your_api_key
SHOPIFY_API_SECRET=your_api_secret
SHOPIFY_API_SCOPES=read_products,write_products
SHOPIFY_API_VERSION=2023-10
First Use Case: Fetch a Shopify shop’s products in a controller:
use Osiset\Shopify\Facades\Shopify;
public function index()
{
$products = Shopify::get('products');
return response()->json($products);
}
Shopify facade for API interactions (e.g., Shopify::get(), Shopify::post()).Authentication & Session Handling:
// Redirect to Shopify for OAuth
return Shopify::redirectToShopify();
// Handle callback after OAuth
public function handleCallback()
{
return Shopify::handleCallback();
}
config/shopify.php).API Interactions:
$products = Shopify::get('products', ['limit' => 10]);
$product = Shopify::post('products', ['title' => 'New Product']);
Shopify::subscribe('products/create', 'https://your-app.com/webhooks/shopify');
Billable Metrics (for Paid Apps):
Shopify::billable():
Shopify::billable()->increment('api_calls', 1);
Middleware Integration:
ShopifyMiddleware to ensure valid sessions:
Route::middleware(['shopify'])->group(function () {
Route::get('/dashboard', 'DashboardController@index');
});
$this->mockShopifyResponse('products', ['data' => []]);
app/Shopify/Handlers.Deprecated Package:
Session Management:
SHOPIFY_SESSION_DRIVER in .env matches your Laravel session driver (e.g., database).Shopify::session()->forget();
API Rate Limits:
Shopify::billable() to avoid hitting Shopify’s limits.$products = Cache::remember('shopify_products', now()->addHours(1), function () {
return Shopify::get('products');
});
Webhook Verification:
if (!Shopify::verifyWebhook(request()->header('X-Shopify-Hmac-Sha256'))) {
abort(403);
}
Enable Debug Mode:
SHOPIFY_DEBUG=true
Logs will appear in storage/logs/shopify.log.
Common Errors:
Invalid OAuth token: Regenerate API credentials in Shopify Partners Dashboard.404 Not Found: Ensure SHOPIFY_API_VERSION matches Shopify’s current API version.SHOPIFY_WEBHOOK_SECRET in .env matches the Shopify app setting.Custom Handlers: Override default behavior by publishing and extending:
php artisan vendor:publish --tag=shopify-views
Modify resources/views/vendor/shopify/ templates.
Event Listeners:
Listen to Shopify events (e.g., Shopify\Events\WebhookReceived):
public function handle(WebhookReceived $event)
{
// Process webhook data
}
API Extensions:
Add custom API methods to the Shopify facade by binding a service provider:
Shopify::extend(function ($app) {
$app->singleton('shopify.custom', function () {
return new CustomShopifyService();
});
});
How can I help you explore Laravel packages today?