Install the Bundle (if using the forked version):
composer require troopers/mangopay-bundle
(Note: The original appventus/mangopay-bundle is archived; use the Troopers fork instead.)
Enable the Bundle in config/bundles.php:
Troopers\MangopayBundle\TroopersMangopayBundle::class => ['all' => true],
Configure Mangopay in config/packages/troopers_mangopay.yaml:
mangopay:
client_id: "%env(MANGOPAY_CLIENT_ID)%"
client_password: "%env(MANGOPAY_CLIENT_PASSWORD)%"
base_url: "%env(MANGOPAY_BASE_URL)%" # e.g., "https://api.sandbox.mangopay.com/v2"
First Use Case: Fetch a User’s Wallet Balance
Inject the MangoPayClient service (auto-registered) into a controller/service:
use Troopers\MangopayBundle\Service\MangoPayClient;
public function getUserBalance(MangoPayClient $mangoPay, User $user)
{
$wallet = $mangoPay->getWallet($user->mangopayWalletId);
return $wallet->getBalance();
}
Service Integration:
MangoPayClient to interact with Mangopay’s API.$payment = $mangoPay->createPayment([
'AuthorId' => $user->mangopayUserId,
'DebitedFunds' => ['WalletId' => $user->walletId, 'Amount' => 1000, 'Currency' => 'EUR'],
'CreditedFunds' => [['WalletId' => $merchantWalletId, 'Amount' => 950, 'Currency' => 'EUR']],
'Fees' => [['Amount' => 50, 'Currency' => 'EUR']],
]);
Event-Driven Extensions:
PaymentSucceeded) via Symfony’s event system.use Troopers\MangopayBundle\Event\MangoPayEvent;
public function onPaymentSucceeded(MangoPayEvent $event)
{
$payment = $event->getPayment();
// Update your database or trigger logic.
}
services.yaml:
services:
App\EventListener\MangoPayListener:
tags:
- { name: 'kernel.event_listener', event: 'mangopay.payment.succeeded', method: 'onPaymentSucceeded' }
Model Mapping:
User, Wallet, Payment) to your Eloquent models.User model:
use Troopers\MangopayBundle\Traits\MangoPayUserTrait;
class User extends Model
{
use MangoPayUserTrait;
}
Batch Operations:
createPayIns) for bulk operations:
$payIns = $mangoPay->createPayIns([
['WalletId' => $walletId1, 'Amount' => 500, 'Currency' => 'EUR'],
['WalletId' => $walletId2, 'Amount' => 300, 'Currency' => 'EUR'],
]);
Deprecated Bundle:
appventus/mangopay-bundle is archived. Use the Troopers fork for updates.Environment Variables:
MANGOPAY_CLIENT_ID, MANGOPAY_CLIENT_PASSWORD, and MANGOPAY_BASE_URL are set in .env.https://api.sandbox.mangopay.com/v2) before going live.Error Handling:
try {
$mangoPay->createPayment($data);
} catch (\Mangopay\Exception\MangopayException $e) {
Log::error("Mangopay Error: " . $e->getMessage());
throw new \RuntimeException("Payment failed: " . $e->getCode());
}
Idempotency:
IdempotencyKey for idempotent requests (e.g., payments). Generate a unique key per request:
$idempotencyKey = Str::uuid()->toString();
$mangoPay->createPayment($data + ['IdempotencyKey' => $idempotencyKey]);
Webhook Verification:
MangoPayWebhookValidator service:
use Troopers\MangopayBundle\Service\MangoPayWebhookValidator;
public function verifyWebhook(MangoPayWebhookValidator $validator, Request $request)
{
if (!$validator->validate($request)) {
abort(403, 'Invalid webhook signature');
}
// Process webhook.
}
Logging:
config/packages/monolog.yaml:
handlers:
mangopay:
type: stream
path: "%kernel.logs_dir%/mangopay.log"
level: debug
channels: ["mangopay"]
MangoPayClient configuration.Testing:
MangoPayClient in PHPUnit:
$this->mock(MangoPayClient::class)->shouldReceive('createPayment')->andReturn($mockPayment);
Custom Entities:
User, Wallet) to add custom fields:
class CustomUser extends \Mangopay\User
{
public function getCustomField()
{
return $this->customField;
}
}
Performance:
$cache = $this->container->get('cache.app');
$balance = $cache->get("wallet_{$walletId}_balance", function() use ($mangoPay, $walletId) {
return $mangoPay->getWallet($walletId)->getBalance();
});
Documentation:
How can I help you explore Laravel packages today?