Installation:
composer require payment/saferpay-bundle:2.*
Register the bundle in config/bundles.php (Symfony 4+):
return [
// ...
Payment\Bundle\SaferpayBundle\PaymentSaferpayBundle::class => ['all' => true],
];
Configuration:
Override defaults in config/packages/payment_saferpay.yaml:
payment_saferpay:
payinitparameter:
data:
accountid: "YOUR_ACCOUNT_ID" # Replace with Saferpay test/live account ID
amount: 10.00
currency: EUR
description: "Test payment"
First Use Case: Inject the service and trigger a payment:
use Payment\Bundle\SaferpayBundle\Service\PaymentService;
class PaymentController extends AbstractController
{
public function initiatePayment(PaymentService $paymentService)
{
$params = $paymentService->getPayInitParameter();
$params->setAmount(25.00);
$params->setDescription("Order #12345");
$response = $paymentService->payInit($params);
return $this->redirect($response->getRedirectUrl());
}
}
Payment Initiation:
Use PaymentService to start transactions:
$paymentService->payInit($params)
->then(function ($response) {
// Handle success (redirect to Saferpay)
})
->otherwise(function ($error) {
// Handle failure (e.g., invalid params)
});
Callback Handling:
Configure a route for Saferpay callbacks (e.g., /saferpay/callback):
# config/routes.yaml
payment_saferpay_callback:
path: /saferpay/callback
controller: Payment\Bundle\SaferpayBundle\Controller\CallbackController::callbackAction
Refunds/Notifications:
Use NotificationService for async events:
$notificationService->processNotification($rawData);
PaymentEvents (e.g., payment.saferpay.pre_init) for custom logic.PaymentService and HttpClient interfaces for unit tests:
$this->container->set('payment.saferpay.httpclient.buzz', $mockClient);
Deprecated Bundle:
symfony/http-client).Configuration Overrides:
logger and httpclient services may not exist. Define them in DI:
services:
payment.saferpay.httpclient.buzz:
class: Symfony\Contracts\HttpClient\HttpClientInterface
factory: ['Symfony\Contracts\HttpClient\HttpClient', 'create']
Callback Security:
hash_hmac() to verify notification.signature:
$calculatedSig = hash_hmac('sha256', $rawData, 'YOUR_SECRET_KEY');
if (!hash_equals($notification->getSignature(), $calculatedSig)) {
throw new \RuntimeException('Invalid signature');
}
monolog to debug HTTP requests/responses:
payment_saferpay:
logger:
serviceid: monolog.logger.payment
99867-94913159) to avoid live transactions during development.Custom Parameters:
Extend PayInitParameter to add fields:
class CustomPayInitParameter extends \Payment\Bundle\SaferpayBundle\Model\PayInitParameter
{
private $customField;
public function setCustomField($value) { $this->customField = $value; }
public function getCustomField() { return $this->customField; }
}
Register as a service:
payment_saferpay.payinitparameter.default:
class: App\Model\CustomPayInitParameter
API Extensions:
Override HttpClient to add headers or retry logic:
class CustomHttpClient implements HttpClientInterface
{
public function request(string $method, string $url, array $options = []): ResponseInterface
{
$options['headers']['X-Custom-Header'] = 'value';
return $this->client->request($method, $url, $options);
}
}
How can I help you explore Laravel packages today?