composer require andchir/omnipay-bundle
config/packages/omnipay.yaml):
omnipay:
success_url: '/success'
fail_url: '/fail'
return_url: '/return'
notify_url: '/notify'
cancel_url: '/cancel'
gateways:
PayPal_Express:
parameters:
username: '%env(PAYPAL_USERNAME)%'
password: '%env(PAYPAL_PASSWORD)%'
signature: '%env(PAYPAL_SIGNATURE)%'
Payment entity (e.g., via Doctrine) with required fields (userId, email, orderId, amount, currency).$omnipayService = $this->get('omnipay');
$payment = (new Payment())->setAmount(100)->setCurrency('USD')->setEmail('user@example.com');
$omnipayService->initialize($payment);
$response = $omnipayService->sendPurchase($payment);
config/packages/omnipay.yaml: Central configuration for gateways and URLs.src/Service/OmnipayService.php: Core service for payment operations.src/Controller/DefaultController.php: Example controller for handling return/notify routes.Payment Creation:
Payment entity (e.g., Doctrine).OmnipayService::initialize() to map entity fields to Omnipay parameters.$payment->setOptions([
'gatewayName' => 'YandexMoney',
'dataKeys' => ['customerEmail' => 'customerNumber']
]);
$omnipayService->initialize($payment);
Processing Payments:
$response = $omnipayService->sendPurchase($payment);
return $response->redirect();
/omnipay_return):
public function returnAction(Request $request, OmnipayService $omnipayService) {
$payment = $omnipayService->completePurchase($request);
// Update payment status, log transaction, etc.
}
Webhook Handling:
notify_url to validate IPN/POST requests:
public function notifyAction(Request $request, OmnipayService $omnipayService) {
$payment = $omnipayService->handleNotify($request);
if ($payment->isValid()) {
// Process successful payment
}
}
Gateway-Specific Logic:
prefersAuthorize for gateways like Sberbank (e.g., Sberbank: prefersAuthorize: true).purchase vs. complete):
gateways:
RoboKassa:
purchase:
testMode: true
complete:
testMode: false
Data Mapping:
data_keys to map custom fields (e.g., customerEmail: ['customerNumber', 'Email']).OmnipayService to add custom mappers for complex logic.Testing:
testMode: true in gateway configs and use sandbox environments (e.g., PayPal sandbox).Deprecated Dependencies:
omnipay/sberbank@^3.2). Ensure compatibility with your Omnipay version.composer.json or update to newer Omnipay packages manually.URL Configuration:
/omnipay_return) may conflict with your routing.config/routes.yaml or extend the DefaultController.Sberbank Gateway:
omnipay-sberbank but removed it in v1.0.18. Ensure you install it separately:
composer require andrewnovikof/omnipay-sberbank
Doctrine Mismatch:
Symfony 5+ Compatibility:
OmnipayService to log raw responses for debugging:
public function sendPurchase(Payment $payment) {
$response = parent::sendPurchase($payment);
$this->logger->debug('Omnipay Response:', ['data' => $response->getData()]);
return $response;
}
OmnipayService::getGateway() to inspect configured gateways:
$gateway = $omnipayService->getGateway('PayPal_Express');
$this->logger->debug('Gateway Config:', ['config' => $gateway->getParameters()]);
try {
$response = $omnipayService->sendPurchase($payment);
} catch (\Omnipay\Common\Exception\InvalidRequestException $e) {
$this->logger->error('Invalid Request: ' . $e->getMessage());
}
Custom Gateways:
OmnipayService to add support for unsupported gateways:
public function addCustomGateway(string $name, array $config) {
$this->gateways[$name] = Omnipay::create($name, $config);
}
config/packages/omnipay.yaml:
gateways:
Custom_Gateway:
parameters: { /* ... */ }
Pre/Post-Processing:
initialize() or completePurchase() to add custom logic:
public function completePurchase(Request $request) {
$payment = parent::completePurchase($request);
// Add custom validation or business logic
return $payment;
}
Event Listeners:
// In OmnipayService
$eventDispatcher->dispatch(new PaymentEvent($payment, 'pre.purchase'));
$response = $this->getGateway()->purchase($parameters);
$eventDispatcher->dispatch(new PaymentEvent($payment, 'post.purchase'));
$eventDispatcher->addListener('pre.purchase', function (PaymentEvent $event) {
// Pre-purchase logic
});
Testing Utilities:
public function createMockResponse(array $data) {
$response = $this->getMockBuilder('Omnipay\Common\Message\AbstractResponse')
->disableOriginalConstructor()
->getMock();
$response->method('getData')->willReturn($data);
return $response;
}
How can I help you explore Laravel packages today?