Installation Add the bundle via Composer:
composer require clubmaster/economic
Register the bundle in config/bundles.php:
return [
// ...
Clubmaster\EconomicBundle\ClubAccountEconomicBundle::class => ['all' => true],
];
Configuration
Configure the bundle in config/packages/clubmaster_economic.yaml:
clubmaster_economic:
gateway: 'your_gateway_name' # e.g., 'stripe', 'paypal', or custom
api_key: '%env(ECONOMIC_API_KEY)%'
# Other gateway-specific settings
First Use Case: Payment Processing
Inject the EconomicGateway service and use it to create a payment:
use Clubmaster\EconomicBundle\Service\EconomicGateway;
class PaymentController extends AbstractController
{
public function __construct(private EconomicGateway $economicGateway) {}
public function processPayment(Request $request)
{
$payment = $this->economicGateway->createPayment(
amount: 1000, // 10.00 USD (in cents)
currency: 'USD',
description: 'Membership fee',
metadata: ['user_id' => 123]
);
return new JsonResponse($payment);
}
}
Where to Look First
src/Service/EconomicGateway.php for available methods.src/DependencyInjection/Configuration.php or src/Resources/config/services.yaml for supported gateways.src/Event/ for dispatchable events (e.g., PaymentCreatedEvent).Payment Processing
$payment = $economicGateway->createPayment($amount, $currency, $description, $metadata);
$economicGateway->capturePayment($paymentId);
$economicGateway->refundPayment($paymentId, $amount);
Webhook Handling Subscribe to events in your controller or event listener:
use Clubmaster\EconomicBundle\Event\PaymentSucceededEvent;
public function onPaymentSucceeded(PaymentSucceededEvent $event)
{
// Update your database or trigger follow-up actions
}
Register the listener in config/services.yaml:
services:
App\EventListener\PaymentListener:
tags:
- { name: 'kernel.event_listener', event: 'clubmaster.economic.payment.succeeded', method: 'onPaymentSucceeded' }
Gateway-Specific Logic Extend or override default gateways by creating a custom service:
# config/services.yaml
services:
App\Service\CustomEconomicGateway:
decorates: 'clubmaster_economic.gateway'
arguments: ['@App\Service\CustomEconomicGateway.inner']
Testing
Use the TestGateway for unit tests:
$testGateway = new \Clubmaster\EconomicBundle\Service\TestGateway();
$testGateway->setExpectedResponse(['success' => true]);
$this->container->set('clubmaster_economic.gateway', $testGateway);
$builder->add('amount', MoneyType::class, [
'currency' => 'USD',
'scale' => 2,
]);
/**
* @ORM\Entity
*/
class Payment
{
// ...
#[ORM\Column(type: 'json')]
private array $economicData;
}
HttpClient for direct API calls if needed:
$client = $economicGateway->getHttpClient();
$response = $client->request('GET', '/payments');
Gateway Configuration Mismatch
gateway in clubmaster_economic.yaml or providing invalid API keys.CompilerPass or use Symfony’s ParameterBag validation:
if (empty($this->getParameter('clubmaster_economic.api_key'))) {
throw new \RuntimeException('API key is required.');
}
Currency/Amount Handling
10.00) instead of integers (e.g., 1000 for cents).Money component or enforce integer amounts in the gateway:
$amount = (int) round($request->request->get('amount') * 100);
Webhook Verification
HttpFoundation utilities:
$expectedSignature = hash_hmac('sha256', $payload, $this->apiKey);
if (!hash_equals($expectedSignature, $request->headers->get('X-Signature'))) {
throw new \RuntimeException('Invalid signature');
}
Idempotency
idempotency_key parameter in createPayment():
$payment = $economicGateway->createPayment(..., ['idempotency_key' => uniqid()]);
debug: true in clubmaster_economic.yaml to log raw API responses:
clubmaster_economic:
debug: true
EventDispatcher debug tool to inspect dispatched events:
bin/console debug:event-dispatcher
log() method in a custom gateway to write to Monolog:
public function log(string $message, array $context = [])
{
$this->logger->info($message, $context);
}
Custom Gateways
Implement the GatewayInterface to add support for new payment providers:
use Clubmaster\EconomicBundle\Service\GatewayInterface;
class CustomGateway implements GatewayInterface
{
public function createPayment(array $data): array
{
// Custom logic for your gateway
}
// Implement other required methods
}
Register it in config/services.yaml:
services:
App\Service\CustomGateway:
tags:
- { name: 'clubmaster_economic.gateway', alias: 'custom' }
Event Subscribers
Extend functionality by subscribing to events (e.g., PaymentFailedEvent):
use Clubmaster\EconomicBundle\Event\PaymentFailedEvent;
public static function getSubscribedEvents()
{
return [
PaymentFailedEvent::class => 'onPaymentFailed',
];
}
public function onPaymentFailed(PaymentFailedEvent $event)
{
// Send a notification or retry logic
}
HTTP Client Overrides
Replace the default HttpClient for custom retries or middleware:
services:
App\Client\CustomHttpClient:
decorates: 'clubmaster_economic.http_client'
arguments: ['@App\Client\CustomHttpClient.inner']
Configuration Overrides Dynamically override configuration per environment:
# config/packages/dev/clubmaster_economic.yaml
clubmaster_economic:
debug: true
api_key: '%env(ECONOMIC_API_KEY_DEV)%'
How can I help you explore Laravel packages today?