Payum making inroads into PHP Ecommerce – Notifications: Series I

My friend Maksim is making great inroads in an ecommerce component for abstracting payment processing. The library is called Payum and it is being integrated into popular Ecommerce frameworks such as Vespolina and Sylius. We have successfully integrated it with Sylius and soon will be in Vespolina.

Here is his write up on how to handle notifications with Payum:

Untitled (1)

Managing notifications.

The notification is a callback. A payment can send it back to us to let us know about changes.
It could be Paypal Instant Payment Notification (IPN) or Payex Transaction Callback for example.
Here in this chapter we show you how to store it somewhere and process it later (with a cron script for example).

Preparations

First we have to create a model where we would store all the info:

<?php
 
class Notification extends \ArrayObject
{
    protected $id;
 
    public function getId()
    {
        return $this->id;
    }
}

Then we have to do our notification action which would actually do all the job:

<?php
 
use Payum\Action\ActionInterface;
use Payum\Request\SecuredNotifyRequest;
 
class StoreNotificationAction implements ActionInterface
{
    protected $notificationStorage;
 
    public function __constructor(StorageInterface $notificationStorage)
    {
        $this->notificationStorage = $notificationStorage;
    }
 
    public function execute($request)
    {
        $notification = $this->notificationStorage->createModel();
        foreach ($request->getNotification() as $name => $value) {
            $paymentNotification[$name] => $value;
        }
 
        $this->notificationStorage->updateModel($notification);
    }
 
    public function supports($request)
    {
        return $request instanceof SecuredNotifyRequest;
    }
}

In the code above we created payum custom action.
The main purpose of the action to store notification that with a request.
To do so it requires a storage.
Now we can update `config.php` described in the documentation.

 
<?php
//config.php
 
use Payum\Extension\StorageExtension;
use Payum\Storage\FilesystemStorage;
 
$storeNotificationAction = new StoreNotificationAction(
    new FilesystemStorage('/path/to/storage', 'Notification', 'id')
);
 
$registry->getPayment('paypal')->addAction($storeNotificationAction);

Now we have to implement `notify.php` script which must be accessible from the internet.

<?php
//notify.php
 
use Payum\Request\SecuredNotifyRequest;
 
include 'config.php';
 
$token = $requestVerifier->verify();
$payment = $registry->getPayment($token->getPaymentName());
 
$payment->execute(new SecuredNotifyRequest($_REQUEST, $token));

Setup Paypal IPN.

The code above could be reused by any payment.
Now I want to show changes need to enable Paypal IPN. To do so we have to modify `prepare.php` a bit:

<?php
 
$notifyToken = $tokenStorage->createModel();
$notifyToken->setPaymentName('paypal');
$notifyToken->setDetails($storage->getIdentificator($paymentDetails));
$notifyToken->setTargetUrl('http://'.$_SERVER['HTTP_HOST'].'/notify.php?payum_token='.$doneToken->getHash());
$tokenStorage->updateModel($notifyToken);
 
$paymentDetails['NOTIFYURL'] = $notifyToken->getTargetUrl();
$storage->updateModel($paymentDetails);

Here we created one more token: `notify` and tell paypal to use its target url for notifications.

You can find more on http://github.com/payum and the official site here.

It is comming to you soon in http://pilotci.com too!

Thanks to Maksim’s fine abstractions!

One thought on “Payum making inroads into PHP Ecommerce – Notifications: Series I

  1. $paymentNotification[$name] => $value;

    I think this should be.

    $notification[$name] => $value;

    And github.com/payum is not a link 🙂

Leave a Reply to Sebastiaan Stok Cancel reply

Your email address will not be published. Required fields are marked *