TransparentTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Model\Payflow;
  7. use Magento\Paypal\Block\Payment\Info;
  8. use Magento\Paypal\Model\Payflowpro;
  9. use Magento\Paypal\Model\Payflow\Transparent;
  10. use Magento\Vault\Api\Data\PaymentTokenInterface;
  11. use Magento\Vault\Model\CreditCardTokenFactory;
  12. /**
  13. * Class TransparentTest
  14. *
  15. * Test class for \Magento\Paypal\Model\Payflow\Transparent
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class TransparentTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /** @var Transparent|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $object;
  22. /** @var \Magento\Paypal\Model\Payflow\Service\Gateway|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $gatewayMock;
  24. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $storeManagerMock;
  26. /** @var \Magento\Payment\Model\Method\ConfigInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $configFactoryMock;
  28. /** @var \Magento\Payment\Model\Method\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $configMock;
  30. /** @var \Magento\Framework\DataObject */
  31. protected $responseMock;
  32. /** @var \Magento\Sales\Model\Order\Payment\Info|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $paymentMock;
  34. /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $orderMock;
  36. /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $addressBillingMock;
  38. /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $addressShippingMock;
  40. /**
  41. * @var CreditCardTokenFactory|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $paymentTokenFactory;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject|
  46. * \Magento\Paypal\Model\Payflow\Service\Response\Validator\ResponseValidator
  47. */
  48. protected $responseValidator;
  49. protected function setUp()
  50. {
  51. $this->paymentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
  52. ->setMethods([])
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->paymentTokenFactory = $this->getMockBuilder(CreditCardTokenFactory::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods(['create'])
  58. ->getMock();
  59. $this->gatewayMock = $this->getMockBuilder(\Magento\Paypal\Model\Payflow\Service\Gateway::class)
  60. ->setMethods(['postRequest'])
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  64. ->setMethods(['getStore', 'getId'])
  65. ->disableOriginalConstructor()
  66. ->getMockForAbstractClass();
  67. $this->storeManagerMock->method('getStore')
  68. ->willReturnSelf();
  69. $this->configMock = $this->getMockBuilder(\Magento\Paypal\Model\PayflowConfig::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->configFactoryMock = $this->getMockBuilder(\Magento\Payment\Model\Method\ConfigInterfaceFactory::class)
  73. ->setMethods(['create'])
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->configFactoryMock->method('create')
  77. ->willReturn($this->configMock);
  78. $this->responseMock = new \Magento\Framework\DataObject();
  79. $this->responseValidator = $this->getMockBuilder(
  80. \Magento\Paypal\Model\Payflow\Service\Response\Validator\ResponseValidator::class
  81. )->disableOriginalConstructor()
  82. ->setMethods(['validate'])
  83. ->getMock();
  84. $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  85. $this->object = $objectHelper->getObject(
  86. \Magento\Paypal\Model\Payflow\Transparent::class,
  87. [
  88. 'gateway' => $this->gatewayMock,
  89. 'storeManager' => $this->storeManagerMock,
  90. 'configFactory' => $this->configFactoryMock,
  91. 'responseValidator' => $this->responseValidator,
  92. 'paymentTokenFactory' => $this->paymentTokenFactory
  93. ]
  94. );
  95. }
  96. /**
  97. * Initializing a collection Mock for Authorize method
  98. *
  99. * @return void
  100. */
  101. protected function initializationAuthorizeMock()
  102. {
  103. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  104. ->setMethods([
  105. 'getCustomerId', 'getBillingAddress', 'getShippingAddress', 'getCustomerEmail',
  106. 'getId', 'getIncrementId', 'getBaseCurrencyCode'
  107. ])
  108. ->disableOriginalConstructor()
  109. ->getMock();
  110. $this->addressBillingMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  111. ->setMethods(
  112. [
  113. 'getFirstname',
  114. 'getLastname',
  115. 'getStreet',
  116. 'getCity',
  117. 'getRegionCode',
  118. 'getPostcode',
  119. 'getCountryId'
  120. ]
  121. )->disableOriginalConstructor()
  122. ->getMock();
  123. $this->addressShippingMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  124. ->setMethods(
  125. [
  126. 'getFirstname',
  127. 'getLastname',
  128. 'getStreet',
  129. 'getCity',
  130. 'getRegionCode',
  131. 'getPostcode',
  132. 'getCountryId'
  133. ]
  134. )->disableOriginalConstructor()
  135. ->getMock();
  136. }
  137. /**
  138. * Build data for request for operation Authorize
  139. *
  140. * @return void
  141. */
  142. protected function buildRequestData()
  143. {
  144. $this->paymentMock->expects($this->once())
  145. ->method('getOrder')
  146. ->willReturn($this->orderMock);
  147. $this->orderMock->expects($this->once())
  148. ->method('getBaseCurrencyCode')
  149. ->willReturn('USD');
  150. $this->orderMock->expects($this->once())
  151. ->method('getBillingAddress')
  152. ->willReturn($this->addressBillingMock);
  153. $this->orderMock->expects(static::once())
  154. ->method('getId')
  155. ->willReturn(1);
  156. $this->orderMock->expects(static::once())
  157. ->method('getIncrementId')
  158. ->willReturn('0000001');
  159. $this->orderMock->expects($this->once())
  160. ->method('getShippingAddress')
  161. ->willReturn($this->addressShippingMock);
  162. $this->addressBillingMock->expects($this->once())
  163. ->method('getFirstname')
  164. ->willReturn('Firstname');
  165. $this->addressBillingMock->expects($this->once())
  166. ->method('getLastname')
  167. ->willReturn('Lastname');
  168. $this->addressBillingMock->expects($this->once())
  169. ->method('getStreet')
  170. ->willReturn(['street-1', 'street-2']);
  171. $this->addressBillingMock->expects($this->once())
  172. ->method('getCity')
  173. ->willReturn('City');
  174. $this->addressBillingMock->expects($this->once())
  175. ->method('getRegionCode')
  176. ->willReturn('RegionCode');
  177. $this->addressBillingMock->expects($this->once())
  178. ->method('getPostcode')
  179. ->willReturn('Postcode');
  180. $this->addressBillingMock->expects($this->once())
  181. ->method('getCountryId')
  182. ->willReturn('CountryId');
  183. $this->orderMock->expects($this->once())
  184. ->method('getCustomerEmail')
  185. ->willReturn('customer@email.com');
  186. $this->addressShippingMock->expects($this->once())
  187. ->method('getFirstname')
  188. ->willReturn('Firstname');
  189. $this->addressShippingMock->expects($this->once())
  190. ->method('getLastname')
  191. ->willReturn('Lastname');
  192. $this->addressShippingMock->expects($this->once())
  193. ->method('getStreet')
  194. ->willReturn(['street-1', 'street-2']);
  195. $this->addressShippingMock->expects($this->once())
  196. ->method('getCity')
  197. ->willReturn('City');
  198. $this->addressShippingMock->expects($this->once())
  199. ->method('getRegionCode')
  200. ->willReturn('RegionCode');
  201. $this->addressShippingMock->expects($this->once())
  202. ->method('getPostcode')
  203. ->willReturn('Postcode');
  204. $this->addressShippingMock->expects($this->once())
  205. ->method('getCountryId')
  206. ->willReturn('CountryId');
  207. }
  208. /**
  209. * @return \Magento\Framework\DataObject
  210. */
  211. protected function crateVoidResponseMock()
  212. {
  213. $voidResponseMock = new \Magento\Framework\DataObject(
  214. [
  215. 'result_code' => Transparent::RESPONSE_CODE_APPROVED,
  216. 'pnref' => 'test-pnref'
  217. ]
  218. );
  219. $this->responseMock->setData(Transparent::PNREF, 'test-pnref');
  220. $this->paymentMock->expects($this->once())
  221. ->method('setParentTransactionId')
  222. ->with('test-pnref');
  223. $this->paymentMock->expects($this->once())
  224. ->method('getParentTransactionId')
  225. ->willReturn('test-pnref');
  226. $this->paymentMock->expects($this->once())
  227. ->method('setTransactionId')
  228. ->with('test-pnref')
  229. ->willReturnSelf();
  230. $this->paymentMock->expects($this->once())
  231. ->method('setIsTransactionClosed')
  232. ->with(1)
  233. ->willReturnSelf();
  234. $this->paymentMock->expects($this->once())
  235. ->method('setShouldCloseParentTransaction')
  236. ->with(1);
  237. return $voidResponseMock;
  238. }
  239. /**
  240. * @expectedException \Exception
  241. */
  242. public function testAuthorizeException()
  243. {
  244. $this->initializationAuthorizeMock();
  245. $this->buildRequestData();
  246. $this->gatewayMock->expects($this->once())
  247. ->method('postRequest')
  248. ->willThrowException(new \Exception());
  249. $this->object->authorize($this->paymentMock, 33);
  250. }
  251. /**
  252. * @expectedException \Magento\Framework\Exception\LocalizedException
  253. * @expectedExceptionMessage The payment couldn't be processed at this time. Please try again later.
  254. */
  255. public function testAuthorizeValidationException()
  256. {
  257. $this->initializationAuthorizeMock();
  258. $this->buildRequestData();
  259. $voidResponseMock = $this->crateVoidResponseMock();
  260. $this->gatewayMock->expects($this->at(0))
  261. ->method('postRequest')
  262. ->willReturn($this->responseMock);
  263. $this->responseValidator->expects($this->once())
  264. ->method('validate')
  265. ->with($this->responseMock)
  266. ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('Error')));
  267. $this->gatewayMock->expects($this->at(1))
  268. ->method('postRequest')
  269. ->willReturn($voidResponseMock);
  270. $this->paymentMock->expects($this->once())
  271. ->method('getAdditionalInformation')
  272. ->with(Payflowpro::PNREF)
  273. ->willReturn('test-pnref');
  274. $this->responseMock->setData('result_code', Payflowpro::RESPONSE_CODE_FRAUDSERVICE_FILTER);
  275. $this->object->authorize($this->paymentMock, 33);
  276. }
  277. /**
  278. * @param int $resultCode
  279. * @param int $origResult
  280. *
  281. * @expectedException \Magento\Framework\Exception\LocalizedException
  282. * @dataProvider authorizeLocalizedExceptionDataProvider
  283. */
  284. public function testAuthorizeLocalizedException(
  285. $resultCode,
  286. $origResult
  287. ) {
  288. $this->initializationAuthorizeMock();
  289. $this->buildRequestData();
  290. $this->responseMock->setData('result_code', $resultCode);
  291. $this->responseMock->setData('origresult', $origResult);
  292. $this->gatewayMock->expects($this->exactly(1))
  293. ->method('postRequest')
  294. ->willReturn($this->responseMock);
  295. $this->object->authorize($this->paymentMock, 33);
  296. }
  297. /**
  298. * @return array
  299. */
  300. public function authorizeLocalizedExceptionDataProvider()
  301. {
  302. return [
  303. [
  304. 'origResult' => Payflowpro::RESPONSE_CODE_APPROVED,
  305. 'resultCode' => Payflowpro::RESPONSE_CODE_DECLINED_BY_FILTER
  306. ],
  307. [
  308. 'origResult' => Payflowpro::RESPONSE_CODE_DECLINED_BY_FILTER,
  309. 'resultCode' => Payflowpro::RESPONSE_CODE_FRAUDSERVICE_FILTER
  310. ],
  311. [
  312. 'origResult' => Payflowpro::RESPONSE_CODE_DECLINED,
  313. 'resultCode' => 1111111111
  314. ],
  315. [
  316. 'origResult' => 3432432423,
  317. 'resultCode' => 23233432423
  318. ],
  319. ];
  320. }
  321. /**
  322. * Test method
  323. * with resultCode = RESPONSE_CODE_APPROVED and Origresult != RESPONSE_CODE_FRAUDSERVICE_FILTER
  324. */
  325. public function testAuthorize()
  326. {
  327. $this->initializationAuthorizeMock();
  328. $this->buildRequestData();
  329. $paymentTokenMock = $this->createMock(PaymentTokenInterface::class);
  330. $extensionAttributes = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderPaymentExtensionInterface::class)
  331. ->disableOriginalConstructor()
  332. ->setMethods(['setVaultPaymentToken'])
  333. ->getMockForAbstractClass();
  334. $ccDetails = [
  335. 'cc_type' => 'VI',
  336. 'cc_number' => '1111'
  337. ];
  338. $this->responseMock->setData('result_code', Payflowpro::RESPONSE_CODE_APPROVED);
  339. $this->responseMock->setData('origresult', 0);
  340. $this->responseMock->setData('pnref', 'test-pnref');
  341. $this->gatewayMock->expects($this->once())->method('postRequest')->willReturn($this->responseMock);
  342. $this->responseValidator->expects($this->once())
  343. ->method('validate')
  344. ->with($this->responseMock);
  345. $this->paymentMock->expects($this->once())
  346. ->method('setTransactionId')
  347. ->with('test-pnref')
  348. ->willReturnSelf();
  349. $this->paymentMock->expects($this->once())
  350. ->method('setIsTransactionClosed')
  351. ->with(0);
  352. $this->paymentMock->expects($this->once())
  353. ->method('getCcExpYear')
  354. ->willReturn('2017');
  355. $this->paymentMock->expects($this->once())
  356. ->method('getCcExpMonth')
  357. ->willReturn('12');
  358. $this->paymentMock->expects(static::any())
  359. ->method('getAdditionalInformation')
  360. ->willReturnMap(
  361. [
  362. [Transparent::CC_DETAILS, $ccDetails],
  363. [Transparent::PNREF, 'test-pnref']
  364. ]
  365. );
  366. $this->paymentTokenFactory->expects(static::once())
  367. ->method('create')
  368. ->willReturn($paymentTokenMock);
  369. $paymentTokenMock->expects(static::once())
  370. ->method('setGatewayToken')
  371. ->with('test-pnref');
  372. $paymentTokenMock->expects(static::once())
  373. ->method('setTokenDetails')
  374. ->with(json_encode($ccDetails));
  375. $paymentTokenMock->expects(static::once())
  376. ->method('setExpiresAt')
  377. ->with('2018-01-01 00:00:00');
  378. $this->paymentMock->expects(static::once())
  379. ->method('getExtensionAttributes')
  380. ->willReturn($extensionAttributes);
  381. $extensionAttributes->expects(static::once())
  382. ->method('setVaultPaymentToken')
  383. ->with($paymentTokenMock);
  384. $this->paymentMock->expects($this->at(8))
  385. ->method('unsAdditionalInformation')
  386. ->with(Transparent::CC_DETAILS);
  387. $this->paymentMock->expects($this->at(9))
  388. ->method('unsAdditionalInformation')
  389. ->with(Transparent::PNREF);
  390. $this->assertSame($this->object, $this->object->authorize($this->paymentMock, 33));
  391. }
  392. /**
  393. * @covers \Magento\Paypal\Model\Payflow\Transparent::getInfoBlockType()
  394. */
  395. public function testGetInfoBlockType()
  396. {
  397. static::assertEquals(Info::class, $this->object->getInfoBlockType());
  398. }
  399. }