akotsepatrice/moneyfusion-laravel
Intégration plug-and-play de MoneyFusion pour Laravel : pay-in, payout, liste des transactions, webhooks, vérification de statut, commande Artisan. Publication en une commande (config, contrôleurs, vues, migration) et liaison aux utilisateurs via Trait.
Installation
composer require akotsepatrice/moneyfusion-laravel
Publish the config file:
php artisan vendor:publish --provider="MoneyFusion\MoneyFusionServiceProvider" --tag="config"
Configure .env with your MoneyFusion API credentials:
MONEYFUSION_API_KEY=your_api_key_here
MONEYFUSION_API_SECRET=your_api_secret_here
First Use Case: Creating a Transfer
Use the MoneyFusion facade to initiate a transfer:
use MoneyFusion\Facades\MoneyFusion;
$transfer = MoneyFusion::transfer()
->setSender('sender@example.com')
->setRecipient('recipient@example.com')
->setAmount(100.00)
->setCurrency('USD')
->execute();
Where to Look First
config/moneyfusion.php (API keys, endpoints, default settings).app/Facades/MoneyFusion.php (main entry point for API interactions).app/Services/MoneyFusionService.php (core logic for API calls).app/Exceptions/MoneyFusionException.php (handle API errors gracefully).Initiating Transfers Chain methods for clarity:
$transfer = MoneyFusion::transfer()
->setSender($user->email)
->setRecipient($recipient->email)
->setAmount($amount)
->setCurrency('EUR')
->setReference('INV-'.$orderId)
->execute();
Handling Webhooks
Use the webhook facade method to validate incoming MoneyFusion webhooks:
use MoneyFusion\Facades\MoneyFusion;
$payload = request()->all();
$signature = request()->header('X-Signature');
if (MoneyFusion::webhook()->verify($payload, $signature)) {
// Process the webhook (e.g., update order status)
MoneyFusion::webhook()->handle($payload);
}
Retrieving Transfer Status Fetch transfer details by ID:
$transfer = MoneyFusion::transfer()->find($transferId);
if ($transfer->status === 'completed') {
// Update your database
}
Batch Processing Loop through orders and create transfers in bulk:
foreach ($orders as $order) {
MoneyFusion::transfer()
->setSender($order->user->email)
->setRecipient($order->customer->email)
->setAmount($order->amount)
->execute();
}
TransferJob::dispatch($transferData)->delay(now()->addMinutes(5));
MoneyFusion::setLogger(app(\Monolog\Logger::class));
MoneyFusionService in tests:
$this->mock(MoneyFusionService::class)->shouldReceive('transfer')->andReturn($mockTransfer);
API Rate Limits MoneyFusion may throttle requests. Implement exponential backoff in your retries:
try {
$transfer = MoneyFusion::transfer()->execute();
} catch (RateLimitException $e) {
sleep($e->retryAfter);
retry();
}
Webhook Verification Always verify webhook signatures to avoid spoofing:
if (!MoneyFusion::webhook()->verify($payload, $signature)) {
abort(403, 'Invalid webhook signature');
}
Currency Mismatches
Ensure the setCurrency() method matches the recipient’s supported currencies. Check MoneyFusion’s currency list for validation.
Sandbox vs. Live Mode Test thoroughly in sandbox before switching to live mode. Use:
MONEYFUSION_ENVIRONMENT=sandbox # or 'live'
MONEYFUSION_DEBUG=true in .env to log raw API responses.InvalidAmountException, RecipientNotFoundException).MoneyFusion::validateTransferData($data) to catch issues before API calls.Custom Webhook Handlers
Extend the WebhookHandler class to add logic for specific events:
class CustomWebhookHandler extends \MoneyFusion\WebhookHandler
{
protected function handleTransferCompleted(array $payload): void
{
// Custom logic for completed transfers
}
}
Register it in config/moneyfusion.php:
'webhook_handler' => \App\Services\CustomWebhookHandler::class,
Middleware for API Calls Add middleware to log or modify requests/responses:
MoneyFusion::setMiddleware(function ($request, $next) {
// Pre-process request
$response = $next($request);
// Post-process response
return $response;
});
Custom Exceptions
Extend MoneyFusionException for domain-specific errors:
class InsufficientFundsException extends MoneyFusionException {}
Throw it in your service layer when needed.
MONEYFUSION_TIMEOUT (default: 30 seconds) for slow connections.MONEYFUSION_RETRY_ATTEMPTS (default: 3) and MONEYFUSION_RETRY_DELAY (default: 1000ms).MONEYFUSION_DEFAULT_CURRENCY to avoid repeated setCurrency() calls.How can I help you explore Laravel packages today?