caponica/amazon-mws-complete
Unified PHP client for Amazon MWS APIs. Use service-specific clients directly or simplify setup with MwsClientPool and per-seller/marketplace ClientPacks that prefill common parameters. Includes helpers that convert raw MWS responses into easier-to-use objects.
Installation:
composer require caponica/amazon-mws-complete:dev-master
(Note: Use dev-master as the latest stable release is outdated.)
Basic Configuration:
use CaponicaAmazonMwsComplete\ClientPool\MwsClientPool;
$clientPool = new MwsClientPool(new \Your\Logger\Implementation());
$clientPool->setConfig([
'amazon_site' => MwsClientPoolConfig::SITE_US,
'access_key' => env('AWS_ACCESS_KEY'),
'secret_key' => env('AWS_SECRET_KEY'),
'application_name' => 'YourAppName',
'application_version' => '1.0',
'seller_id' => env('AWS_SELLER_ID'),
]);
First API Call:
$productClient = $clientPool->getProductClientPack();
$response = $productClient->callGetCompetitivePricingForASIN('B08XYZ123');
Centralized Configuration:
$clientPool = new MwsClientPool($logger);
$clientPool->setConfig([
'amazon_site' => MwsClientPoolConfig::SITE_DE, // Germany
'auth_token' => env('AWS_MWS_TOKEN'), // Optional
// ... other keys
]);
getProductClientPack(), getOrdersClientPack()).Service-Specific Calls:
// Products
$pricing = $productClient->retrieveCompetitivePricingForASIN('B08XYZ123');
// Orders
$orders = $ordersClient->retrieveOrders(['CreatedAfter' => '2023-01-01']);
// Reports
$report = $reportClient->getReport($reportType, $reportId);
Helper Methods:
retrieveXyz() methods for parsed objects (e.g., MwsCompetitivePricing[]).callXyz() for raw API responses.Environment Variables:
Store credentials in .env and load via Laravel’s config() helper:
$clientPool->setConfig([
'access_key' => config('services.amazon.access_key'),
]);
Service Providers:
Bind the MwsClientPool to Laravel’s container:
$this->app->singleton(MwsClientPool::class, function ($app) {
return new MwsClientPool($app->make(\Psr\Log\LoggerInterface::class));
});
Jobs/Queues: Offload long-running tasks (e.g., report processing) to Laravel queues:
ProcessAmazonReport::dispatch($reportFilePath, $reportType);
API Rate Limiting: Implement middleware to throttle requests (Amazon’s limits vary by endpoint).
Deprecated APIs:
MWSCartService and MWSCustomerService are deprecated. Avoid using them.ClientLibraryVersions in the package for active APIs.Auth Token Handling:
setConfig(). For dynamic tokens:
$clientPack = $clientPool->getProductClientPack();
$clientPack->setAuthToken($newToken); // If supported by the underlying client.
MWSAuthToken is correctly passed to the underlying MwsClient.Report Parsing:
BaseMwsReport and BaseMwsReportRecord.class CustomReport extends BaseMwsReport {
public static function getReportType() { return 'Custom_Report_Type'; }
}
validateHeaderRowForReportType().Logging:
$logger = \Log::getMonolog();
$clientPool = new MwsClientPool($logger);
Error Handling:
try-catch for MwsException or InvalidReportException.try {
$response = $client->callGetOrder(['AmazonOrderId' => '123']);
} catch (MwsException $e) {
\Log::error('MWS Error: ' . $e->getMessage());
throw new \RuntimeException('Failed to fetch order', 0, $e);
}
Enable Verbose Logging:
$logger = new \Monolog\Logger('amazon', [
new \Monolog\Handler\StreamHandler(storage_path('logs/amazon.log'), \Monolog\Logger::DEBUG)
]);
Check Raw Responses:
Use callXyz() to inspect unparsed responses for debugging:
$rawResponse = $client->callGetCompetitivePricingForASIN('B08XYZ123');
\Log::debug('Raw API Response:', ['response' => $rawResponse]);
Validate Credentials:
Test with a simple call (e.g., getMarketplaceInfo) to ensure auth works:
$marketplaceInfo = $clientPool->getSellersClientPack()->callGetMarketplaceInfo();
Custom Clients:
Extend BaseMwsClient to add domain-specific methods:
class CustomProductClient extends BaseMwsClient {
public function getLowStockAlerts($threshold = 5) {
// Custom logic using underlying MWS API
}
}
Report Extensions: Add support for new report types by implementing:
ReportXyz (header validation).ReportXyzRecord (row parsing).Middleware: Create Laravel middleware to:
class TransformAmazonResponse implements \Illuminate\Pipeline\Pipeline {
public function handle($request, \Closure $next) {
$response = $next($request);
return $this->transform($response);
}
}
Testing:
Mock the MwsClientPool in tests:
$mockPool = Mockery::mock(MwsClientPool::class);
$mockPool->shouldReceive('getProductClientPack')
->andReturn($mockClientPack);
How can I help you explore Laravel packages today?