public isCallbackValid ( Sonata\Component\Payment\TransactionInterface $transaction ) | ||
$transaction | Sonata\Component\Payment\TransactionInterface |
public function isCallbackValid(TransactionInterface $transaction)
{
$order = $transaction->getOrder();
if (!$this->isRequestValid($transaction)) {
$transaction->setState(TransactionInterface::STATE_KO);
$transaction->setStatusCode(TransactionInterface::STATUS_WRONG_CALLBACK);
return false;
}
if ($order->isValidated()) {
$transaction->setState(TransactionInterface::STATE_KO);
$transaction->setStatusCode(TransactionInterface::STATUS_WRONG_CALLBACK);
return false;
}
if ($transaction->get('payment_status') === 'Pending') {
$transaction->setState(TransactionInterface::STATE_OK);
$transaction->setStatusCode(TransactionInterface::STATUS_PENDING);
return true;
}
if ($transaction->get('payment_status') === 'Completed') {
$transaction->setState(TransactionInterface::STATE_OK);
$transaction->setStatusCode(TransactionInterface::STATUS_VALIDATED);
return true;
}
if ($transaction->get('payment_status') === 'Cancelled') {
$transaction->setState(TransactionInterface::STATE_OK);
$transaction->setStatusCode(TransactionInterface::STATUS_CANCELLED);
return true;
}
$transaction->setState(TransactionInterface::STATE_KO);
$transaction->setStatusCode(TransactionInterface::STATUS_UNKNOWN);
return false;
}
public function testIsCallbackValid() { $router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface'); $translator = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface'); $paypal = new Paypal($router, $translator); $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)); $this->assertFalse($paypal->isCallbackValid($transaction), "Paypal::isCallbackValid false because request invalid"); $check = sha1($order->getReference() . $order->getCreatedAt()->format("m/d/Y:G:i:s") . $order->getId()); $transaction->expects($this->any())->method('get')->will($this->returnValue($check)); $this->assertFalse($paypal->isCallbackValid($transaction), "Paypal::isCallbackValid false because order not validated"); $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(false)); $check = sha1($order->getReference() . $order->getCreatedAt()->format("m/d/Y:G:i:s") . $order->getId()); $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface'); $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order)); $transaction->expects($this->any())->method('get')->will($this->returnValue($check)); $this->assertFalse($paypal->isCallbackValid($transaction), "Paypal::isCallbackValid false because payment_status invalid."); $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface'); $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order)); $transaction->expects($this->any())->method('get')->will($this->returnCallback(function () use($check) { $asked = func_get_arg(0); switch ($asked) { case 'check': return $check; case 'payment_status': return "Pending"; } })); $this->assertTrue($paypal->isCallbackValid($transaction), "Paypal::isCallbackValid true because payment_status pending."); $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface'); $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order)); $transaction->expects($this->any())->method('get')->will($this->returnCallback(function () use($check) { $asked = func_get_arg(0); switch ($asked) { case 'check': return $check; case 'payment_status': return "Completed"; } })); $this->assertTrue($paypal->isCallbackValid($transaction), "Paypal::isCallbackValid true because payment_status completed."); $transaction = $this->getMock('Sonata\\Component\\Payment\\TransactionInterface'); $transaction->expects($this->any())->method('getOrder')->will($this->returnValue($order)); $transaction->expects($this->any())->method('get')->will($this->returnCallback(function () use($check) { $asked = func_get_arg(0); switch ($asked) { case 'check': return $check; case 'payment_status': return "Cancelled"; } })); $this->assertTrue($paypal->isCallbackValid($transaction), "Paypal::isCallbackValid true because payment_status cancelled."); }