Source of file PaymentServiceTest.php
Size: 10,255 Bytes - Last Modified: 2021-12-24T06:34:53+00:00
/var/www/docs.ssmods.com/process/src/tests/PaymentServiceTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 | <?php namespace SilverStripe\Omnipay\Tests; use Omnipay\Common\AbstractGateway; use SilverStripe\Omnipay\Service\ServiceFactory; use SilverStripe\Omnipay\Service\ServiceResponse; use SilverStripe\Omnipay\Service\PaymentService; use Omnipay\Common\Message\NotificationInterface; use SilverStripe\Core\Config\Config; use SilverStripe\Omnipay\GatewayInfo; use SilverStripe\Control\HTTPResponse; use SilverStripe\Omnipay\Tests\Extensions\TestNotifyResponseExtension; class PaymentServiceTest extends PaymentTest { /** @var \SilverStripe\Omnipay\Service\PurchaseService */ protected $service; public function setUp() { parent::setUp(); $this->service = $this->factory->getService($this->payment, ServiceFactory::INTENT_PURCHASE); } public function testCancel() { $response = $this->service->cancel(); $this->assertEquals('Void', $this->payment->Status); $this->assertTrue($response->isCancelled()); } public function testGateway() { Config::modify()->merge(GatewayInfo::class, 'PaymentExpress_PxPay', array( // set some invalid params 'parameters' => array( 'DummyParameter' => 'DummyValue' ) )); $gateway = $this->service->oGateway(); $this->assertEquals($gateway->getShortName(), 'Dummy'); // change the payment gateway $this->payment->Gateway = 'PaymentExpress_PxPay'; $gateway = $this->service->oGateway(); $this->assertEquals($gateway->getShortName(), 'PaymentExpress_PxPay'); $expectedParams = array( 'username' => 'EXAMPLEUSER', 'password' => '235llgwxle4tol23l' ); $this->assertEquals( // the gateway might return more parameters, but it should at least contain the expected params array_intersect_assoc($gateway->getParameters(), $expectedParams), $expectedParams ); // The dummy parameter should not be in there $this->assertNotContains('DummyParameter', array_keys($gateway->getParameters())); } // Test a successful notification public function testHandleNotificationSuccess() { $service = $this->buildNotificationService(NotificationInterface::STATUS_COMPLETED); $serviceResponse = $service->handleNotification(); // notification should be handled fine $this->assertFalse($serviceResponse->isError()); // response should be flagged as notification $this->assertTrue($serviceResponse->isNotification()); // response should have an instance of the notification attached $this->assertNotNull($serviceResponse->getOmnipayResponse()); $this->assertInstanceOf( '\Omnipay\Common\Message\NotificationInterface', $serviceResponse->getOmnipayResponse() ); $httpResponse = $serviceResponse->redirectOrRespond(); $this->assertInstanceOf(HTTPResponse::class, $httpResponse); $this->assertEquals(200, $httpResponse->getStatusCode()); $this->assertEquals('OK', $httpResponse->getBody()); } // Test notification response modified by extension public function testHandleModifiedNotification() { PaymentService::add_extension(TestNotifyResponseExtension::class); $service = $this->buildNotificationService(NotificationInterface::STATUS_COMPLETED); $this->payment->setGateway('FantasyGateway'); $serviceResponse = $service->handleNotification(); // notification should be handled fine $this->assertFalse($serviceResponse->isError()); // response should be flagged as notification $this->assertTrue($serviceResponse->isNotification()); // response should have an instance of the notification attached $this->assertNotNull($serviceResponse->getOmnipayResponse()); $this->assertInstanceOf( '\Omnipay\Common\Message\NotificationInterface', $serviceResponse->getOmnipayResponse() ); $httpResponse = $serviceResponse->redirectOrRespond(); $this->assertInstanceOf(HTTPResponse::class, $httpResponse); $this->assertEquals(200, $httpResponse->getStatusCode()); $this->assertEquals('OK', $httpResponse->getBody()); $this->assertEquals('apikey12345', $httpResponse->getHeader('X-FantasyGateway-Api')); // change to default gateway $service = $this->buildNotificationService(NotificationInterface::STATUS_COMPLETED); $serviceResponse = $service->handleNotification(); // notification should be handled fine $this->assertFalse($serviceResponse->isError()); // response should be flagged as notification $this->assertTrue($serviceResponse->isNotification()); // response should have an instance of the notification attached $this->assertNotNull($serviceResponse->getOmnipayResponse()); $this->assertInstanceOf( '\Omnipay\Common\Message\NotificationInterface', $serviceResponse->getOmnipayResponse() ); $httpResponse = $serviceResponse->redirectOrRespond(); $this->assertInstanceOf(HTTPResponse::class, $httpResponse); $this->assertEquals(200, $httpResponse->getStatusCode()); // body will be SUCCESS instead of OK $this->assertEquals('SUCCESS', $httpResponse->getBody()); PaymentService::remove_extension(TestNotifyResponseExtension::class); } // Test an error notification public function testHandleNotificationError() { $service = $this->buildNotificationService(NotificationInterface::STATUS_FAILED); $serviceResponse = $service->handleNotification(); // notification should error $this->assertTrue($serviceResponse->isError()); // response should be flagged as notification $this->assertTrue($serviceResponse->isNotification()); // response should have an instance of the notification attached $this->assertNotNull($serviceResponse->getOmnipayResponse()); $this->assertInstanceOf( '\Omnipay\Common\Message\NotificationInterface', $serviceResponse->getOmnipayResponse() ); } // Test a pending notification public function testHandleNotificationPending() { $service = $this->buildNotificationService(NotificationInterface::STATUS_PENDING); $serviceResponse = $service->handleNotification(); // notification should not error $this->assertFalse($serviceResponse->isError()); // response should be flagged as notification $this->assertTrue($serviceResponse->isNotification()); // response should be flagged as pending $this->assertTrue($serviceResponse->isAwaitingNotification()); // response should have an instance of the notification attached $this->assertNotNull($serviceResponse->getOmnipayResponse()); $this->assertInstanceOf( '\Omnipay\Common\Message\NotificationInterface', $serviceResponse->getOmnipayResponse() ); } // Test a gateway that doesn't return an instance of NotificationInterface public function testHandleNotificationInvalid() { // build a notification that returns an AbstractResponse instead of the expected NotificationInterface $service = $this->buildNotificationService( NotificationInterface::STATUS_PENDING, 'Omnipay\Common\Message\AbstractResponse' ); $serviceResponse = $service->handleNotification(); // notification should error $this->assertTrue($serviceResponse->isError()); // response should be flagged as notification $this->assertTrue($serviceResponse->isNotification()); // response should NOT have an instance of the response attached (since it's invalid) $this->assertNull($serviceResponse->getOmnipayResponse()); } /** * Test with a gateway that doesn't implement `acceptNotification`. * @expectedException \SilverStripe\Omnipay\Exception\InvalidConfigurationException */ public function testHandleNotificationWithIncompatibleGateway() { $payment = $this->payment->setGateway('PaymentExpress_PxPay'); $service = $this->factory->getService($payment, ServiceFactory::INTENT_PURCHASE); // build a gateway that doesn't have the `acceptNotification` method $stubGateway = $this->getMockBuilder('Omnipay\Common\AbstractGateway') ->setMethods(array('getName')) ->getMock(); $service->setGatewayFactory($this->stubGatewayFactory($stubGateway)); // this should throw an exception $service->handleNotification(); } protected function buildNotificationService( $returnState, $contract = NotificationInterface::class ) { $payment = $this->payment->setGateway('PaymentExpress_PxPay'); $service = $this->factory->getService($payment, ServiceFactory::INTENT_PURCHASE); //-------------------------------------------------------------------------------------------------------------- // Notification response $notificationResponse = $this->getMockBuilder($contract) ->disableOriginalConstructor() ->setMethods(['getTransactionStatus', 'getTransactionReference', 'isSuccessful', 'getMessage', 'getData']) ->getMock(); $notificationResponse->expects($this->any()) ->method('getTransactionStatus')->will($this->returnValue($returnState)); //-------------------------------------------------------------------------------------------------------------- // Build the gateway $stubGateway = $this->getMockBuilder(AbstractGateway::class) ->setMethods(['acceptNotification', 'getName']) ->getMock(); $stubGateway->expects($this->once()) ->method('acceptNotification') ->will($this->returnValue($notificationResponse)); $service->setGatewayFactory($this->stubGatewayFactory($stubGateway)); return $service; } } |