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.');
}