Sonata\Component\Payment\Paypal::handleError PHP Method

handleError() public method

public handleError ( Sonata\Component\Payment\TransactionInterface $transaction )
$transaction Sonata\Component\Payment\TransactionInterface
    public function handleError(TransactionInterface $transaction)
    {
        $order = $transaction->getOrder();
        switch ($transaction->getStatusCode()) {
            case TransactionInterface::STATUS_ORDER_UNKNOWN:
                if ($this->getLogger()) {
                    $this->getLogger()->emergency('[Paypal:handlerError] ERROR_ORDER_UNKNOWN');
                }
                break;
            case TransactionInterface::STATUS_ERROR_VALIDATION:
                if ($this->getLogger()) {
                    $this->getLogger()->emergency(sprintf('[Paypal:handlerError] STATUS_ERROR_VALIDATION - Order %s - Paypal reject the postback validation', $order->getReference()));
                }
                break;
            case TransactionInterface::STATUS_CANCELLED:
                // cancelled
                $order->setStatus(OrderInterface::STATUS_CANCELLED);
                if ($this->getLogger()) {
                    $this->getLogger()->emergency(sprintf('[Paypal:handlerError] STATUS_CANCELLED - Order %s - The Order has been cancelled, see callback dump for more information', $order->getReference()));
                }
                break;
            case TransactionInterface::STATUS_PENDING:
                // pending
                $order->setStatus(OrderInterface::STATUS_PENDING);
                if ($this->getLogger()) {
                    $reasons = self::getPendingReasonsList();
                    $this->getLogger()->emergency(sprintf('[Paypal:handlerError] STATUS_PENDING - Order %s - reason code : %s - reason : %s', $order->getReference(), $reasons[$transaction->get('pending_reason')], $transaction->get('pending_reason')));
                }
                break;
            default:
                if ($this->getLogger()) {
                    $this->getLogger()->emergency(sprintf('[Paypal:handlerError] STATUS_PENDING - uncaught error'));
                }
        }
        $transaction->setState(TransactionInterface::STATE_KO);
        if ($order->getStatus() === null) {
            $order->setStatus(OrderInterface::STATUS_CANCELLED);
        }
        if ($transaction->getStatusCode() == null) {
            $transaction->setStatusCode(TransactionInterface::STATUS_UNKNOWN);
        }
    }

Usage Example

Example #1
0
 public function testHandleError()
 {
     $router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
     $translator = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
     $paypal = new Paypal($router, $translator);
     $paypal->setLogger($this->getMock('Psr\\Log\\LoggerInterface'));
     $order = $this->getMock('Sonata\\Component\\Order\\OrderInterface');
     $order->expects($this->any())->method('getCreatedAt')->will($this->returnValue(new \DateTime()));
     $order->expects($this->any())->method('isValidated')->will($this->returnValue(true));
     $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface');
     $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order));
     $paypal->handleError($transaction);
     $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface');
     $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order));
     $transaction->expects($this->any())->method('getStatusCode')->will($this->returnValue(TransactionInterface::STATUS_ORDER_UNKNOWN));
     $paypal->handleError($transaction);
     $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface');
     $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order));
     $transaction->expects($this->any())->method('getStatusCode')->will($this->returnValue(TransactionInterface::STATUS_ERROR_VALIDATION));
     $paypal->handleError($transaction);
     $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface');
     $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order));
     $transaction->expects($this->any())->method('getStatusCode')->will($this->returnValue(TransactionInterface::STATUS_CANCELLED));
     $paypal->handleError($transaction);
     $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface');
     $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order));
     $transaction->expects($this->any())->method('getStatusCode')->will($this->returnValue(TransactionInterface::STATUS_PENDING));
     $transaction->expects($this->any())->method('get')->will($this->returnValue(Paypal::PENDING_REASON_ADDRESS));
     $paypal->handleError($transaction);
 }