Installation
composer require chargily/epay-symfony
Add to config/bundles.php:
\Chargily\SymfonyBundle\ChargilySymfonyBundle::class => ['all' => true],
Configure API Keys
Add to config/services.yaml:
parameters:
chargily.api_key: "%env(CHARGILY_API_KEY)%"
chargily.secret_key: "%env(CHARGILY_SECRET_KEY)%"
First Use Case: Redirect to Payment Page
Inject ChargilyClient service and call createPayment():
use Chargily\SymfonyBundle\Service\ChargilyClient;
public function paymentAction(Request $request, ChargilyClient $chargily)
{
$payload = [
'client' => 'user123',
'client_email' => 'user@example.com',
'invoice_number' => 'INV-1001',
'amount' => 100.00,
'mode' => 'CIB', // or 'CARD'
'back_url' => $request->getSchemeAndHttpHost() . '/payment/success',
'webhook_url' => $request->getSchemeAndHttpHost() . '/webhook/epay',
];
$response = $chargily->createPayment($payload);
return new RedirectResponse($response['payment_url']);
}
Payment Initiation
Use ChargilyClient::createPayment() to generate a payment link. Always include:
back_url: Redirect after payment (success/failure).webhook_url: Endpoint to handle asynchronous updates (e.g., POST /webhook/epay).Webhook Handling Implement a Symfony controller to process webhook payloads:
public function webhookAction(Request $request, ChargilyClient $chargily)
{
$payload = json_decode($request->getContent(), true);
$signature = $request->headers->get('X-Chargily-Signature');
if ($chargily->validateWebhook($payload, $signature)) {
// Process payment status (e.g., update database)
$this->handlePaymentUpdate($payload);
return new Response('OK', 200);
}
return new Response('Invalid', 403);
}
Refunds and Cancellations
Use ChargilyClient::refund() or ChargilyClient::cancel():
$chargily->refund('INV-1001', 50.00); // Partial refund
$chargily->cancel('INV-1001'); // Full cancellation
api_key and secret_key in .env (e.g., CHARGILY_API_KEY=your_key).$builder->add('amount', MoneyType::class, ['currency' => 'EGP']);
$builder->add('mode', ChoiceType::class, ['choices' => ['CIB' => 'CIB', 'CARD' => 'Card']]);
$logger->info('Payment response', ['data' => $response]);
Webhook Validation
ChargilyClient::validateWebhook():
if (!$chargily->validateWebhook($payload, $signature)) {
throw new \RuntimeException('Invalid webhook signature');
}
URL Encoding
back_url/webhook_url must be absolute and URL-encoded.UrlGenerator:
$backUrl = $this->generateUrl('payment_success', [], UrlGeneratorInterface::ABSOLUTE_URL);
Mode Selection
mode (e.g., 'CIB') may break if Chargily changes supported modes.# config/services.yaml
parameters:
chargily.supported_modes: ['CIB', 'CARD', 'MADA']
Idempotency
webhook_signature as a unique key).debug: true in config to log raw API responses:
chargily:
debug: true
mode: 'TEST' in payload) before going live.Custom Payloads Extend the payload with Chargily’s additional fields:
$payload['custom_data'] = json_encode(['user_id' => 123]);
Event Dispatching Trigger Symfony events after payment/webhook processing:
$event = new PaymentProcessedEvent($payload);
$this->eventDispatcher->dispatch($event, PaymentEvents::POST_PROCESS);
Monetary Precision
Ensure amount uses PHP’s bcmath for precision (e.g., 100.00 instead of 100).
How can I help you explore Laravel packages today?