Skip to content

Instantly share code, notes, and snippets.

@binarious
Created May 8, 2014 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binarious/08fda89ec326e37a9254 to your computer and use it in GitHub Desktop.
Save binarious/08fda89ec326e37a9254 to your computer and use it in GitHub Desktop.
Basic NotifyAction integration for Payum.
<?php
namespace XXX\PaymentBundle\Action;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Request\NotifyRequest;
use Payum\Core\Request\SecuredNotifyRequest;
use Payum\Core\Action\PaymentAwareAction;
use Payum\Core\Exception\RequestNotSupportedException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Payum\Paypal\Ipn\Api as IpnApi;
/**
*
* WIP!
*
* @Service("XXX_payment.action.store_notification")
*/
class StoreNotificationAction extends PaymentAwareAction
{
protected $em;
protected $buzzClient;
/**
* @InjectParams({
* "em" = @Inject("doctrine.orm.entity_manager"),
* "buzzClient" = @Inject("payum.buzz.client")
* })
*/
public function __construct($em, $buzzClient)
{
$this->em = $em;
$this->buzzClient = $buzzClient;
}
/**
* {@inheritdoc}
*/
public function execute($request)
{
/** @var $request NotifyRequest */
if (false == $this->supports($request)) {
throw RequestNotSupportedException::createActionNotSupported($this, $request);
}
// TODO: read sandbox attribute from config
$api = new IpnApi($this->buzzClient, array(
'sandbox' => true
));
// Verify the IPN via PayPal
if (IpnApi::NOTIFY_VERIFIED !== $api->notifyValidate($request->getNotification())) {
$this->logger->warn(
'Invalid IPN received: ' . print_r($request->getNotification(), true)
);
throw new NotFoundHttpException('Invalid IPN');
}
$notification = $request->getNotification();
$model = $request->getModel();
$previousState = $model['PAYMENTREQUEST_0_PAYMENTSTATUS'];
$currentState = $notification['payment_status'];
// Additional Checks
if (!$this->checkEquality(
array(
'payer_id' => 'PAYERID',
'mc_gross' => 'PAYMENTREQUEST_0_AMT',
// maybe more
),
$notification,
$model
)) {
throw new NotFoundHttpException('Malformed IPN');
}
if ($previousState !== $currentState) {
// ... do something with that state change
$model['PAYMENTREQUEST_0_PAYMENTSTATUS'] = $currentState;
$this->em->persist($model);
$this->em->flush();
} else {
// no state change. Maybe no need to do something.
}
}
protected function checkEquality($array, $notification, $model)
{
foreach ($array as $key => $value) {
if ($notification[$key] !== $model[$value]) {
return false;
}
}
return true;
}
/**
* {@inheritdoc}
*/
public function supports($request)
{
return
$request instanceof NotifyRequest &&
$request->getModel() instanceof \ArrayAccess
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment