pereval-dating / pereval-symfony
Symfony services and webhook integration for the Pereval PHP SDK
Requires
- php: ^7.4 || ^8.0
- pereval-dating/pereval-php: ^1.0
- psr/event-dispatcher: ^1.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- symfony/dependency-injection: ^5.4 || ^6.4 || ^7.4 || ^8.0
- symfony/http-client-contracts: ^2.5 || ^3.0
- symfony/http-foundation: ^5.4 || ^6.4 || ^7.4 || ^8.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.5
- monolog/monolog: ^2.9 || ^3.0
- symfony/event-dispatcher: ^5.4 || ^6.4 || ^7.4 || ^8.0
- symfony/http-client: ^5.4 || ^6.4 || ^7.4 || ^8.0
- symfony/messenger: ^5.4 || ^6.4 || ^7.4 || ^8.0
Suggests
- guzzlehttp/guzzle: PSR-18 transport with native Guzzle middleware
- monolog/monolog: PSR-3 logging with channels and processors
- symfony/event-dispatcher: Synchronous webhook delivery
- symfony/http-client: Symfony HTTP transport for the SDK
- symfony/messenger: Dispatch verified events to a message bus
Provides
None
Conflicts
None
Replaces
None
README
API documentation ยท API reference
A standalone Symfony integration for the Pereval PHP SDK. No bundle, Flex recipe, automatic routes or global listeners. Use the DI shortcuts or construct just the objects your application needs. Supports PHP 7.4+ subject to Symfony requirements.
Install
composer require pereval-dating/pereval-symfony
Composer installs pereval-php
automatically. For the default HTTP and synchronous webhook configuration,
install symfony/http-client and symfony/event-dispatcher if your app does
not already provide them. Messenger, Guzzle and Monolog are optional.
The component supports Symfony 5.4, 6.4, 7.4 and 8.x with the corresponding
PHP requirements. Use maintained versions in production.
HTTP client
Install symfony/http-client for the default transport. In a PHP service configuration file imported by your application:
use Pereval\Symfony\PerevalServices; use Symfony\Component\DependencyInjection\ContainerBuilder; return static function (ContainerBuilder $container): void { PerevalServices::client($container, '%env(PEREVAL_API_KEY)%'); };
Inject Pereval\Client into your own services and call e.g.
$client->capabilities->get(). All SDK groups are available. The optional
arguments after the key are HTTPS origin, webhook secret and commerce key.
Keep credentials in Symfony secrets or environment variables.
Replace services independently
Register your choices before calling client() or webhooks().
All references below are Symfony service IDs. The helpers use one default client.
For multiple partners define named instances of ClientFactory and WebhookController
with separate credentials, transports and handlers.
| Extension point | How to supply it |
|---|---|
| Symfony HttpClient | PerevalServices::symfonyHttp($container, 'my.scoped_client') |
| PSR-18 + PSR-17 | PerevalServices::psr18($container, 'my.guzzle', 'my.requests', 'my.streams') |
| Custom HTTP transport | Implement Pereval\Transport and alias that interface to your service |
| Monolog or any PSR-3 logger | PerevalServices::psr3($container, 'monolog.logger.pereval') |
| Custom logger | Alias Pereval\Logger to your implementation. Logging is off by default |
| Client construction | Implement and alias Pereval\Symfony\ClientFactoryInterface to own the entire SDK construction |
| Webhook verification | Implement and alias Pereval\Symfony\WebhookDecoderInterface |
| Delivery | Implement and alias Pereval\Symfony\WebhookSinkInterface |
For Guzzle 7 install guzzlehttp/guzzle and register the following services. A configured Guzzle HandlerStack (including native middleware) works unchanged.
$container->register('my.guzzle', GuzzleHttp\Client::class); $container->register('my.factory', GuzzleHttp\Psr7\HttpFactory::class); PerevalServices::psr18($container, 'my.guzzle', 'my.factory', 'my.factory');
Middleware
Tag any number of services implementing Pereval\Middleware. The highest
priority wraps the others. SDK middleware works across transports, including
custom ones. Use Pereval\CallableMiddleware for a function accepting
($next, $method, $url, $headers, $body) and returning the transport tuple.
Avoid recording credentials, request bodies or identifying URLs. Do not blindly
retry non-idempotent operations.
services: App\Pereval\Metrics: tags: - { name: pereval.middleware, priority: 100 } App\Pereval\Tracing: tags: - { name: pereval.middleware, priority: 50 }
Signed webhooks
See service configuration. Register
PerevalServices::webhooks($container, '%env(PEREVAL_WEBHOOK_SECRET)%')
and configure a route explicitly:
pereval_webhook: path: /webhooks/pereval controller: Pereval\Symfony\WebhookController methods: [POST]
With Symfony EventDispatcher, register a listener for
Pereval\Symfony\WebhookEvent (PSR-14). Read the full verified envelope from
$event->payload. Supply a different dispatcher ID as the third argument
of webhooks() if needed. A dispatcher without listeners silently drops events.
To use Messenger instead, install symfony/messenger, call
PerevalServices::messenger($container, 'messenger.default_bus') before
webhooks(), and register a handler for WebhookEvent. For asynchronous handling
route that class to your configured durable transport:
framework: messenger: routing: 'Pereval\Symfony\WebhookEvent': async
The controller verifies the original bytes and X-Network-Signature via the SDK. Invalid signatures get 401, malformed signed events 400 and accepted events 204. Handler and queue failures propagate to Symfony. For async delivery the worker owns retries after enqueue. Deduplicate by partner and event ID in persistent storage in the same transaction as your business changes. Delivery is at least once. Custom decoders must authenticate the unmodified request before parsing it.
Development
From a source checkout run composer install, composer test and
composer test:example. The offline example uses
a mock HTTP client and local fixtures. No partner account is needed.
For changes spanning both projects, run
sh tests/install-local.sh ../pereval-php with a sibling SDK checkout.
MIT licensed. See LICENSE.