PHP code example of dansmaculotte / paypal-ipn-listener

1. Go to this page and download the library: Download dansmaculotte/paypal-ipn-listener library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dansmaculotte / paypal-ipn-listener example snippets


use DansMaCulotte\PayPal\Ipn\ListenerBuilder\Guzzle\InputStreamListenerBuilder as ListenerBuilder;

$listener = (new ListenerBuilder)->build();

use GuzzleHttp\Client;
use DansMaCulotte\PayPal\Ipn\InputStream;
use DansMaCulotte\PayPal\Ipn\Listener;
use DansMaCulotte\PayPal\Ipn\MessageFactory\InputStreamMessageFactory;
use DansMaCulotte\PayPal\Ipn\Service\GuzzleService;
use DansMaCulotte\PayPal\Ipn\Verifier;
use Symfony\Component\EventDispatcher\EventDispatcher;

$service = new GuzzleService(
    new Client(),
    'https://www.sandbox.paypal.com/cgi-bin/webscr'
);

$verifier = new Verifier($service);

$messageFactory = new InputStreamMessageFactory(new InputStream());

$listener = new Listener(
    $messageFactory,
    $verifier,
    new EventDispatcher()
);

use DansMaCulotte\PayPal\Ipn\Event\MessageVerifiedEvent;
use DansMaCulotte\PayPal\Ipn\Event\MessageInvalidEvent;
use DansMaCulotte\PayPal\Ipn\Event\MessageVerificationFailureEvent;

$listener->onVerified(function (MessageVerifiedEvent $event) {
   $ipnMessage = $event->getMessage();
   
   // IPN message was verified, everything is ok! Do your processing logic here...
});

$listener->onInvalid(function (MessageInvalidEvent $event) {
   $ipnMessage = $event->getMessage();
   
   // IPN message was was invalid, something is not right! Do your logging here...
});

$listener->onVerificationFailure(function (MessageVerificationFailureEvent $event) {
    $error = $event->getError();
    
    // Something bad happend when trying to communicate with PayPal! Do your logging here...
});

use DansMaCulotte\PayPal\Ipn\Event\MessageVerifiedEvent;

class IpnProcessor
{
    public function onVerified(MessageVerifiedEvent $event)
    {
        $message = $event->getMessage();
        
        // ...
    }
}

$listener->onVerified([new Processor, 'onVerified']);

use DansMaCulotte\PayPal\Ipn\Event\MessageVerifiedEvent;

class IpnProcessor
{
    public static function onVerified(MessageVerifiedEvent $event)
    {
        $message = $event->getMessage();
        
        // ...
    }
}

$listener->onVerified(['IpnProcessor', 'onVerified']);

$listener->listen();

use DansMaCulotte\PayPal\Ipn\Event\MessageVerifiedEvent;
use DansMaCulotte\PayPal\Ipn\Event\MessageInvalidEvent;
use DansMaCulotte\PayPal\Ipn\Event\MessageVerificationFailureEvent;
use DansMaCulotte\PayPal\Ipn\ListenerBuilder\Guzzle\InputStreamListenerBuilder as ListenerBuilder;

$listener = (new ListenerBuilder)->build();

$listener->onVerified(function (MessageVerifiedEvent $event) {
   $ipnMessage = $event->getMessage();
   
   // IPN message was verified, everything is ok! Do your processing logic here...
});

$listener->onInvalid(function (MessageInvalidEvent $event) {
   $ipnMessage = $event->getMessage();
   
   // IPN message was was invalid, something is not right! Do your logging here...
});

$listener->onVerificationFailure(function (MessageVerificationFailureEvent $event) {
    $error = $event->getError();
    
    // Something bad happend when trying to communicate with PayPal! Do your logging here...
});

$listener->listen();

use DansMaCulotte\PayPal\Ipn\ListenerBuilder\Guzzle\InputStreamListenerBuilder as ListenerBuilder;

$listenerBuilder = new ListenerBuilder();

$listenerBuilder->useSandbox(); // use PayPal sandbox

$listener = $listenerBuilder->build();