Installation Clone the repository and install dependencies via Composer:
composer require milestone/rayacom
Publish the package configuration (if available) and run migrations:
php artisan vendor:publish --provider="Milestone\RayaCom\RayaComServiceProvider"
php artisan migrate
Initial Setup
config/rayacom.php file for API keys, endpoints, and default settings.routes/rayacom.php file (if included) for predefined routes or register them manually in routes/web.php:
Route::prefix('api/rayacom')->group(function () {
Route::middleware('auth:sanctum')->group(base_path('vendor/milestone/rayacom/routes/api.php'));
});
First Use Case: Product Fetching Use the package’s facade or service class to fetch products from Rayaheen’s API:
use Milestone\RayaCom\Facades\RayaCom;
$products = RayaCom::products()->fetch();
return response()->json($products);
API Integration
RayaCom::endpoint()->method() for simplicity (e.g., RayaCom::orders()->create()).RayaComService into controllers:
public function __construct(private RayaComService $rayacom) {}
class CustomRayaCom extends RayaComService {
public function customEndpoint() {
return $this->callApi('custom/endpoint');
}
}
Data Transformation
map or transform to adapt Rayaheen’s response to your models:
$products = RayaCom::products()->fetch()->map(function ($item) {
return new Product([
'name' => $item->title,
'price' => $item->price->amount,
]);
});
Resource classes for API responses:
public function toArray($request) {
return [
'id' => $this->id,
'name' => $this->name,
'rayacom_price' => $this->price->amount,
];
}
Webhook Handling
public function handleWebhook(Request $request) {
$payload = $request->json()->all();
// Validate payload (e.g., signature, event type)
event(new RayaComWebhookReceived($payload));
}
Authentication
.env (e.g., RAYACOM_API_KEY) and use Laravel’s config('rayacom.api_key').dispatch(new SyncProductsJob())).Cache::remember:
$products = Cache::remember('rayacom_products', now()->addHours(1), function () {
return RayaCom::products()->fetch();
});
RayaCom::products()->fetch()->each(function ($product) {
\Log::debug('Fetched product:', ['data' => $product]);
});
Deprecated/Undocumented Features
API Rate Limits
try {
$response = RayaCom::orders()->fetch();
} catch (\GuzzleHttp\Exception\RequestException $e) {
if ($e->getCode() === 429) {
sleep(5); // Retry after 5 seconds
retry();
}
}
Missing Error Handling
throw new \Milestone\RayaCom\Exceptions\InvalidProductException('Product not found');
Configuration Overrides
config/rayacom.php is missing, manually define defaults in config/app.php or override via service provider:
$this->app->singleton(RayaComService::class, function ($app) {
return new RayaComService([
'api_key' => env('RAYACOM_API_KEY', 'default_key'),
'base_url' => env('RAYACOM_BASE_URL', 'https://api.rayaheen.com'),
]);
});
Enable Guzzle Middleware to inspect requests/responses:
RayaCom::withMiddleware()->fetch(); // If supported
Or use Laravel’s tap:
RayaCom::products()->fetch()->tap(function ($response) {
\Log::info('Raw response:', $response->getBody());
});
Test with Postman/cURL to verify Rayaheen’s API behavior before integrating.
Custom Endpoints
Add new methods to the RayaComService class:
public function getInventory($productId) {
return $this->callApi("products/{$productId}/inventory");
}
Model Observers Sync local models with Rayaheen’s data:
class ProductObserver {
public function saved(Product $product) {
RayaCom::products()->update($product->id, $product->toRayaComArray());
}
}
Service Provider Binding
Override the package’s service binding in AppServiceProvider:
$this->app->bind(RayaComService::class, function ($app) {
return new CustomRayaComService($app['config']['rayacom']);
});
Webhook Validation Validate Rayaheen’s webhook signatures (if supported):
$signature = hash_hmac('sha256', $request->getContent(), config('rayacom.webhook_secret'));
if (!hash_equals($request->header('X-RayaCom-Signature'), $signature)) {
abort(403);
}
How can I help you explore Laravel packages today?