diff --git a/src/Action/OffsiteCaptureAction.php b/src/Action/OffsiteCaptureAction.php index d67e60b..ea35ece 100644 --- a/src/Action/OffsiteCaptureAction.php +++ b/src/Action/OffsiteCaptureAction.php @@ -114,7 +114,13 @@ public function execute($request) } } - $details->replace($response->getData()); + $data = $response->getData(); + if (is_array($data)) { + $details->replace($data); + } else { + $details['_data'] = $data; + } + $details['_reference'] = $response->getTransactionReference(); $details['_status'] = $response->isSuccessful() ? 'captured' : 'failed'; $details['_status_code'] = $response->getCode(); diff --git a/tests/Action/OffsiteCaptureActionTest.php b/tests/Action/OffsiteCaptureActionTest.php index b3bc1c6..dce3df0 100644 --- a/tests/Action/OffsiteCaptureActionTest.php +++ b/tests/Action/OffsiteCaptureActionTest.php @@ -442,6 +442,132 @@ public function shouldSetNotifyUrlIfTokenFactoryAndCaptureTokenPresent() $this->assertEquals('theNotifyUrl', $details['notifyUrl']); } + /** + * @test + */ + public function shouldMergeResponseArrayDataWithDetails() + { + $details = new \ArrayObject([ + 'card' => array('cvv' => 123), + 'clientIp' => '', + ]); + + $responseMock = $this->getMock(OmnipayAbstractResponse::class, [], [], '', false); + $responseMock + ->expects($this->any()) + ->method('getData') + ->willReturn([ + 'foo' => 'fooVal', + ]) + ; + + $requestMock = $this->getMock(OmnipayRequestInterface::class); + $requestMock + ->expects($this->any()) + ->method('send') + ->will($this->returnValue($responseMock)) + ; + + $omnipayGateway = $this->getMock(OffsiteGateway::class); + $omnipayGateway + ->expects($this->once()) + ->method('purchase') + ->willReturn($requestMock) + ; + + $captureToken = new Token(); + $captureToken->setTargetUrl('theCaptureUrl'); + $captureToken->setDetails($identity = new Identity('theId', new \stdClass())); + $captureToken->setGatewayName('theGatewayName'); + + $notifyToken = new Token(); + $notifyToken->setTargetUrl('theNotifyUrl'); + + $tokenFactoryMock = $this->getMock(GenericTokenFactoryInterface::class); + $tokenFactoryMock + ->expects($this->once()) + ->method('createNotifyToken') + ->with('theGatewayName', $this->identicalTo($identity)) + ->willReturn($notifyToken) + ; + + $request = new Capture($captureToken); + $request->setModel($details); + + $action = new OffsiteCaptureAction; + $action->setApi($omnipayGateway); + $action->setGateway($this->createGatewayMock()); + $action->setGenericTokenFactory($tokenFactoryMock); + + $action->execute($request); + + $details = (array) $details; + $this->assertArrayHasKey('foo', $details); + $this->assertEquals('fooVal', $details['foo']); + } + + /** + * @test + */ + public function shouldSetResponseStringDataToDetails() + { + $details = new \ArrayObject([ + 'card' => array('cvv' => 123), + 'clientIp' => '', + ]); + + $responseMock = $this->getMock(OmnipayAbstractResponse::class, [], [], '', false); + $responseMock + ->expects($this->any()) + ->method('getData') + ->willReturn('someData') + ; + + $requestMock = $this->getMock(OmnipayRequestInterface::class); + $requestMock + ->expects($this->any()) + ->method('send') + ->will($this->returnValue($responseMock)) + ; + + $omnipayGateway = $this->getMock(OffsiteGateway::class); + $omnipayGateway + ->expects($this->once()) + ->method('purchase') + ->willReturn($requestMock) + ; + + $captureToken = new Token(); + $captureToken->setTargetUrl('theCaptureUrl'); + $captureToken->setDetails($identity = new Identity('theId', new \stdClass())); + $captureToken->setGatewayName('theGatewayName'); + + $notifyToken = new Token(); + $notifyToken->setTargetUrl('theNotifyUrl'); + + $tokenFactoryMock = $this->getMock(GenericTokenFactoryInterface::class); + $tokenFactoryMock + ->expects($this->once()) + ->method('createNotifyToken') + ->with('theGatewayName', $this->identicalTo($identity)) + ->willReturn($notifyToken) + ; + + $request = new Capture($captureToken); + $request->setModel($details); + + $action = new OffsiteCaptureAction; + $action->setApi($omnipayGateway); + $action->setGateway($this->createGatewayMock()); + $action->setGenericTokenFactory($tokenFactoryMock); + + $action->execute($request); + + $details = (array) $details; + $this->assertArrayHasKey('_data', $details); + $this->assertEquals('someData', $details['_data']); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|GatewayInterface */