ghanem/reloadly
Laravel package providing a simple facade to the Reloadly API for airtime top-ups. Fetch countries and operators, auto-detect operator by phone number, check balances, create recharge transactions, and list or retrieve transactions by ID.
Installation:
composer require ghanem/reloadly
php artisan vendor:publish --provider="Ghanem\Reloadly\ReloadlyServiceProvider" --tag="config"
Configure .env with your Reloadly API credentials:
RELOADLY_API_KEY=your_api_key
RELOADLY_API_SECRET=your_api_secret
First Use Case: Fetch countries to validate a user's input (e.g., in a form):
use Ghanem\Reloadly\Facades\Reloadly;
$countries = Reloadly::countries(); // Returns array of country data
countries(), countryByIsoCode()).config/reloadly.php—adjust API endpoints or defaults if needed.ReloadlyServiceProvider.Data Validation:
Use Reloadly::countryByIsoCode() to validate country codes in registration forms:
$country = Reloadly::countryByIsoCode(request('country_code'));
if (!$country) abort(422, 'Invalid country code');
Operator Lookup: Fetch mobile operators for a country to pre-populate dropdowns:
$operators = Reloadly::operators('eg'); // Operators for Egypt
Integration with Eloquent: Attach country/operator data to models via accessors:
// In User model
public function getCountryNameAttribute() {
return Reloadly::countryByIsoCode($this->country_code)?->name ?? null;
}
API Rate Limiting: Cache responses for static data (e.g., countries) to avoid hitting Reloadly’s API limits:
$countries = Cache::remember('reloadly_countries', now()->addHours(1), function() {
return Reloadly::countries();
});
class CountryService {
public function getValidCountries() {
return Reloadly::countries()->map(fn($c) => ['value' => $c->iso_code, 'label' => $c->name]);
}
}
try-catch to handle API failures gracefully:
try {
$data = Reloadly::someMethod();
} catch (\Ghanem\Reloadly\Exceptions\ReloadlyException $e) {
Log::error($e->getMessage());
abort(503, 'Service unavailable');
}
$this->mock(Reloadly::class)->shouldReceive('countries')->andReturn([...]);
Deprecated Methods:
The package is outdated (last release 2021). Some Reloadly API endpoints may have changed. Verify endpoints in config/reloadly.php match Reloadly’s current docs.
Authentication Issues:
RELOADLY_API_KEY and RELOADLY_API_SECRET are correct.X-API-KEY) are forwarded.Rate Limits: Reloadly’s API has strict rate limits. Cache responses aggressively for static data (e.g., countries).
GIFs Feature: The README mentions "includes Gifs," but this isn’t documented. Avoid relying on undocumented features.
Enable API Logging:
Add to config/reloadly.php:
'debug' => env('RELOADLY_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Check HTTP Status Codes:
Wrap calls in try-catch to log Reloadly’s response status:
try {
$response = Reloadly::someMethod();
} catch (\Ghanem\Reloadly\Exceptions\HttpException $e) {
Log::error('Reloadly API Error: ' . $e->getResponse()->status());
}
Custom Endpoints:
Override the base URL in config/reloadly.php:
'api_url' => env('RELOADLY_API_URL', 'https://api.reloadly.com/v1'),
Add New Methods: Extend the facade by binding a custom service:
// In a service provider
$this->app->bind('reloadly.custom', function() {
return new \App\Services\ReloadlyCustomService();
});
Then use it via the facade:
Reloadly::customMethod();
Webhook Handling:
The package doesn’t support webhooks. For real-time updates, use Laravel’s queue:work with Reloadly’s webhook URLs and validate signatures manually.
reloadly:clear-cache Artisan Command:
If the package includes one, clear cached API responses during deployment:
php artisan reloadly:clear-cache
composer.json or fork the package./countries endpoint returns ISO codes").How can I help you explore Laravel packages today?