dvelopment/fastbill
Laravel package to work with the FastBill API. Provides a simple PHP client and helpers for integrating FastBill features into your app, handling authentication and common requests with a straightforward interface.
Installation
composer require development/fastbill
Ensure your composer.json includes "minimum-stability": "dev" if the package is in a dev state.
Configuration
Add your FastBill API credentials to .env:
FASTBILL_API_KEY=your_api_key_here
FASTBILL_API_SECRET=your_api_secret_here
First Use Case: Fetching a Client
use Development\FastBill\FastBill;
$fastbill = new FastBill(config('fastbill'));
$client = $fastbill->clients->get(1); // Fetch client with ID 1
Key Files to Review
src/FastBill.php: Main class for API initialization.src/Resources/Clients.php: Client-related methods (e.g., get(), create()).src/Exceptions/FastBillException.php: Error handling.CRUD Operations
Use the resource-specific classes (e.g., Clients, Invoices, Payments) for standard operations:
// Create an invoice
$invoice = $fastbill->invoices->create([
'client_id' => 1,
'amount' => 100.00,
'due_date' => now()->addDays(30)->format('Y-m-d'),
]);
// Update an invoice
$fastbill->invoices->update(123, ['amount' => 150.00]);
// Delete an invoice
$fastbill->invoices->delete(123);
Pagination and Filtering Leverage built-in pagination for lists:
$clients = $fastbill->clients->all(['per_page' => 20, 'page' => 1]);
Webhooks Configure webhooks via the API and handle callbacks in Laravel:
// In routes/web.php
Route::post('/fastbill-webhook', [WebhookController::class, 'handle']);
Validate payloads using FastBill’s signature header:
$signature = request()->header('X-FastBill-Signature');
$payload = request()->getContent();
if (!FastBill::verifyWebhook($signature, $payload)) {
abort(401);
}
Integration with Laravel Jobs Offload API calls to background jobs for performance:
use Development\FastBill\Jobs\SyncInvoices;
SyncInvoices::dispatch()->onQueue('fastbill');
Service Provider Binding
Bind the FastBill client in AppServiceProvider for dependency injection:
public function register()
{
$this->app->singleton(FastBill::class, function ($app) {
return new FastBill(config('fastbill'));
});
}
API Rate Limiting Implement middleware to handle FastBill’s rate limits:
public function handle($request, Closure $next)
{
if (FastBill::isRateLimited()) {
return response()->json(['error' => 'Rate limit exceeded'], 429);
}
return $next($request);
}
Authentication Errors
401 Unauthorized responses may occur if FASTBILL_API_KEY or FASTBILL_API_SECRET are misconfigured..env and ensure the API key has the correct permissions in the FastBill dashboard.Deprecated Endpoints
$response = $fastbill->http->get('/v2/clients', ['param' => 'value']);
Webhook Delays
if (Webhook::where('payload_hash', $payloadHash)->exists()) {
return response()->json(['status' => 'already_processed'], 200);
}
Currency Formatting
// Bad
$fastbill->invoices->create(['amount' => '100.00']);
// Good
$fastbill->invoices->create(['amount' => 100.00]);
Enable Debug Mode
Set FASTBILL_DEBUG=true in .env to log raw API responses:
FASTBILL_DEBUG=true
Logging Exceptions
Catch Development\FastBill\Exceptions\FastBillException and log details:
try {
$fastbill->invoices->get(123);
} catch (FastBillException $e) {
\Log::error('FastBill Error: ' . $e->getMessage(), ['response' => $e->getResponse()]);
}
Custom Endpoints
Extend the FastBill class to add unsupported endpoints:
class CustomFastBill extends FastBill
{
public function customEndpoint($method, $path, $data = [])
{
return $this->http->request($method, $path, $data);
}
}
Model Bindings Use Laravel’s model binding to simplify client/invoice retrieval:
// In AppServiceProvider
Route::bind('client', function ($id) {
return $fastbill->clients->get($id);
});
Then use in routes:
Route::get('/client/{client}', [ClientController::class, 'show']);
Testing Mock the FastBill client in tests using Laravel’s HTTP testing:
$response = $this->actingAs($user)
->post('/invoices', ['client_id' => 1, 'amount' => 100.00])
->assertCreated();
How can I help you explore Laravel packages today?