mergetech/payment-gateway-logger

Logging capabilities for Omnipay gateways

dev-master 2019-06-16 20:44 UTC

README

Logging capabilities for Omnipay gateways via an EventSubscriberInterface subscriber for Omnipay payment gateway-specific events. These events are dispatched via the HTTP client's EventDispatcherInterface.

  • The omnipay gateway needs to be updated to emit any of the RequestEvent, ResponseEvent or ErrorEvent objects. For example in the gateway's sendData() methods we can do:

    $event_dispatcher = $this->httpClient->getEventDispatcher();
    $event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_BEFORE_SEND, new RequestEvent($request));

    Logging Errors and Responses events can be emitted like so

    $event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_ERROR new ErrorEvent($exception, $request));
    $event_dispatcher->dispatch(Constants::OMNIPAY_RESPONSE_SUCCESS, new ResponseEvent($response));

OmnipayGatewayRequestSubscriber.php takes in a logger of type LoggerInterface which will listen to and log these events.

The subscriber can be set up to listen to these events when instantiating the HTTP client for the gateway like so:

$httpClient = new GuzzleHttp\Client();
$gateway = Omnipay::create('Vindicia', $httpClient);
$eventDispatcher = $httpClient->getEventDispatcher();
$eventDispatcher->addSubscriber(new OmnipayGatewayRequestSubscriber($gateway_name, new LoggerClassThatImplementsPSRInterface()));