DirectpostTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorizenet\Test\Unit\Model;
  7. use Magento\Sales\Api\PaymentFailuresInterface;
  8. use Magento\Framework\Simplexml\Element;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. use Magento\Authorizenet\Model\Directpost;
  11. use Magento\Authorizenet\Model\TransactionService;
  12. use Magento\Authorizenet\Model\Request;
  13. use Magento\Authorizenet\Model\Directpost\Request\Factory;
  14. use Magento\Sales\Model\Order;
  15. use Magento\Sales\Model\Order\Payment\Transaction\Repository as TransactionRepository;
  16. /**
  17. * Class DirectpostTest
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class DirectpostTest extends \PHPUnit\Framework\TestCase
  21. {
  22. const TOTAL_AMOUNT = 100.02;
  23. const INVOICE_NUM = '00000001';
  24. const TRANSACTION_ID = '41a23x34fd124';
  25. /**
  26. * @var \Magento\Authorizenet\Model\Directpost
  27. */
  28. protected $directpost;
  29. /**
  30. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $scopeConfigMock;
  33. /**
  34. * @var \Magento\Payment\Model\InfoInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $paymentMock;
  37. /**
  38. * @var \Magento\Authorizenet\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $dataHelperMock;
  41. /**
  42. * @var \Magento\Authorizenet\Model\Directpost\Response\Factory|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $responseFactoryMock;
  45. /**
  46. * @var TransactionRepository|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $transactionRepositoryMock;
  49. /**
  50. * @var \Magento\Authorizenet\Model\Directpost\Response|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $responseMock;
  53. /**
  54. * @var TransactionService|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. protected $transactionServiceMock;
  57. /**
  58. * @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $httpClientMock;
  61. /**
  62. * @var \Magento\Authorizenet\Model\Directpost\Request\Factory|\PHPUnit_Framework_MockObject_MockObject
  63. */
  64. protected $requestFactory;
  65. /**
  66. * @var PaymentFailuresInterface|\PHPUnit_Framework_MockObject_MockObject
  67. */
  68. private $paymentFailures;
  69. /**
  70. * @inheritdoc
  71. */
  72. protected function setUp()
  73. {
  74. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  75. ->getMock();
  76. $this->paymentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
  77. ->disableOriginalConstructor()
  78. ->setMethods([
  79. 'getOrder', 'getId', 'setAdditionalInformation', 'getAdditionalInformation',
  80. 'setIsTransactionDenied', 'setIsTransactionClosed', 'decrypt', 'getCcLast4',
  81. 'getParentTransactionId', 'getPoNumber'
  82. ])
  83. ->getMock();
  84. $this->dataHelperMock = $this->getMockBuilder(\Magento\Authorizenet\Helper\Data::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $this->initResponseFactoryMock();
  88. $this->transactionRepositoryMock = $this->getMockBuilder(
  89. \Magento\Sales\Model\Order\Payment\Transaction\Repository::class
  90. )
  91. ->disableOriginalConstructor()
  92. ->setMethods(['getByTransactionId'])
  93. ->getMock();
  94. $this->transactionServiceMock = $this->getMockBuilder(\Magento\Authorizenet\Model\TransactionService::class)
  95. ->disableOriginalConstructor()
  96. ->setMethods(['getTransactionDetails'])
  97. ->getMock();
  98. $this->paymentFailures = $this->getMockBuilder(
  99. PaymentFailuresInterface::class
  100. )
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $this->requestFactory = $this->getRequestFactoryMock();
  104. $httpClientFactoryMock = $this->getHttpClientFactoryMock();
  105. $helper = new ObjectManagerHelper($this);
  106. $this->directpost = $helper->getObject(
  107. \Magento\Authorizenet\Model\Directpost::class,
  108. [
  109. 'scopeConfig' => $this->scopeConfigMock,
  110. 'dataHelper' => $this->dataHelperMock,
  111. 'requestFactory' => $this->requestFactory,
  112. 'responseFactory' => $this->responseFactoryMock,
  113. 'transactionRepository' => $this->transactionRepositoryMock,
  114. 'transactionService' => $this->transactionServiceMock,
  115. 'httpClientFactory' => $httpClientFactoryMock,
  116. 'paymentFailures' => $this->paymentFailures,
  117. ]
  118. );
  119. }
  120. public function testGetConfigInterface()
  121. {
  122. $this->assertInstanceOf(
  123. \Magento\Payment\Model\Method\ConfigInterface::class,
  124. $this->directpost->getConfigInterface()
  125. );
  126. }
  127. public function testGetConfigValue()
  128. {
  129. $field = 'some_field';
  130. $returnValue = 'expected';
  131. $this->scopeConfigMock->expects($this->once())
  132. ->method('getValue')
  133. ->with('payment/authorizenet_directpost/' . $field)
  134. ->willReturn($returnValue);
  135. $this->assertEquals($returnValue, $this->directpost->getValue($field));
  136. }
  137. public function testSetDataHelper()
  138. {
  139. $storeId = 'store-id';
  140. $expectedResult = 'relay-url';
  141. $helperDataMock = $this->getMockBuilder(\Magento\Authorizenet\Helper\Backend\Data::class)
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. $helperDataMock->expects($this->once())
  145. ->method('getRelayUrl')
  146. ->with($storeId)
  147. ->willReturn($expectedResult);
  148. $this->directpost->setDataHelper($helperDataMock);
  149. $this->assertEquals($expectedResult, $this->directpost->getRelayUrl($storeId));
  150. }
  151. public function testAuthorize()
  152. {
  153. $paymentAction = 'some_action';
  154. $this->scopeConfigMock->expects($this->any())
  155. ->method('getValue')
  156. ->with('payment/authorizenet_directpost/payment_action', 'store', null)
  157. ->willReturn($paymentAction);
  158. $this->paymentMock->expects($this->once())
  159. ->method('setAdditionalInformation')
  160. ->with('payment_type', $paymentAction);
  161. $this->directpost->authorize($this->paymentMock, 10);
  162. }
  163. public function testGetCgiUrl()
  164. {
  165. $url = 'cgi/url';
  166. $this->scopeConfigMock->expects($this->any())
  167. ->method('getValue')
  168. ->with('payment/authorizenet_directpost/cgi_url', 'store', null)
  169. ->willReturn($url);
  170. $this->assertEquals($url, $this->directpost->getCgiUrl());
  171. }
  172. public function testGetCgiUrlWithEmptyConfigValue()
  173. {
  174. $this->scopeConfigMock->expects($this->any())
  175. ->method('getValue')
  176. ->with('payment/authorizenet_directpost/cgi_url', 'store', null)
  177. ->willReturn(null);
  178. $this->assertEquals(Directpost::CGI_URL, $this->directpost->getCgiUrl());
  179. }
  180. public function testGetRelayUrl()
  181. {
  182. $storeId = 100;
  183. $url = 'relay/url';
  184. $this->directpost->setData('store', $storeId);
  185. $this->dataHelperMock->expects($this->any())
  186. ->method('getRelayUrl')
  187. ->with($storeId)
  188. ->willReturn($url);
  189. $this->assertEquals($url, $this->directpost->getRelayUrl());
  190. $this->assertEquals($url, $this->directpost->getRelayUrl($storeId));
  191. }
  192. public function testGetResponse()
  193. {
  194. $this->assertSame($this->responseMock, $this->directpost->getResponse());
  195. }
  196. public function testSetResponseData()
  197. {
  198. $data = [
  199. 'key' => 'value'
  200. ];
  201. $this->responseMock->expects($this->once())
  202. ->method('setData')
  203. ->with($data)
  204. ->willReturnSelf();
  205. $this->assertSame($this->directpost, $this->directpost->setResponseData($data));
  206. }
  207. public function testValidateResponseSuccess()
  208. {
  209. $this->prepareTestValidateResponse('some_md5', 'login', true);
  210. $this->assertEquals(true, $this->directpost->validateResponse());
  211. }
  212. /**
  213. * @expectedException \Magento\Framework\Exception\LocalizedException
  214. */
  215. public function testValidateResponseFailure()
  216. {
  217. $this->prepareTestValidateResponse('some_md5', 'login', false);
  218. $this->directpost->validateResponse();
  219. }
  220. /**
  221. * @param string $transMd5
  222. * @param string $login
  223. * @param bool $isValidHash
  224. */
  225. protected function prepareTestValidateResponse($transMd5, $login, $isValidHash)
  226. {
  227. $this->scopeConfigMock->expects($this->any())
  228. ->method('getValue')
  229. ->willReturnMap(
  230. [
  231. ['payment/authorizenet_directpost/trans_md5', 'store', null, $transMd5],
  232. ['payment/authorizenet_directpost/login', 'store', null, $login]
  233. ]
  234. );
  235. $this->responseMock->expects($this->any())
  236. ->method('isValidHash')
  237. ->with($transMd5, $login)
  238. ->willReturn($isValidHash);
  239. }
  240. public function testCheckTransIdSuccess()
  241. {
  242. $this->responseMock->expects($this->once())
  243. ->method('getXTransId')
  244. ->willReturn('111');
  245. $this->assertEquals(true, $this->directpost->checkTransId());
  246. }
  247. /**
  248. * @expectedException \Magento\Framework\Exception\LocalizedException
  249. */
  250. public function testCheckTransIdFailure()
  251. {
  252. $this->responseMock->expects($this->once())
  253. ->method('getXTransId')
  254. ->willReturn(null);
  255. $this->directpost->checkTransId();
  256. }
  257. /**
  258. * @param bool $responseCode
  259. *
  260. * @dataProvider checkResponseCodeSuccessDataProvider
  261. */
  262. public function testCheckResponseCodeSuccess($responseCode)
  263. {
  264. $this->responseMock->expects($this->once())
  265. ->method('getXResponseCode')
  266. ->willReturn($responseCode);
  267. $this->assertEquals(true, $this->directpost->checkResponseCode());
  268. }
  269. /**
  270. * @return array
  271. */
  272. public function checkResponseCodeSuccessDataProvider()
  273. {
  274. return [
  275. ['responseCode' => Directpost::RESPONSE_CODE_APPROVED],
  276. ['responseCode' => Directpost::RESPONSE_CODE_HELD]
  277. ];
  278. }
  279. /**
  280. * Checks response failures behaviour.
  281. *
  282. * @param int $responseCode
  283. * @param int $failuresHandlerCalls
  284. * @return void
  285. *
  286. * @expectedException \Magento\Framework\Exception\LocalizedException
  287. * @dataProvider checkResponseCodeFailureDataProvider
  288. */
  289. public function testCheckResponseCodeFailure(int $responseCode, int $failuresHandlerCalls): void
  290. {
  291. $reasonText = 'reason text';
  292. $this->responseMock->expects($this->once())
  293. ->method('getXResponseCode')
  294. ->willReturn($responseCode);
  295. $this->responseMock->expects($this->any())
  296. ->method('getXResponseReasonText')
  297. ->willReturn($reasonText);
  298. $this->dataHelperMock->expects($this->any())
  299. ->method('wrapGatewayError')
  300. ->with($reasonText)
  301. ->willReturn(__('Gateway error: %1', $reasonText));
  302. $orderMock = $this->getMockBuilder(Order::class)
  303. ->disableOriginalConstructor()
  304. ->getMock();
  305. $orderMock->expects($this->exactly($failuresHandlerCalls))
  306. ->method('getQuoteId')
  307. ->willReturn(1);
  308. $this->paymentFailures->expects($this->exactly($failuresHandlerCalls))
  309. ->method('handle')
  310. ->with(1);
  311. $reflection = new \ReflectionClass($this->directpost);
  312. $order = $reflection->getProperty('order');
  313. $order->setAccessible(true);
  314. $order->setValue($this->directpost, $orderMock);
  315. $this->directpost->checkResponseCode();
  316. }
  317. /**
  318. * @return array
  319. */
  320. public function checkResponseCodeFailureDataProvider(): array
  321. {
  322. return [
  323. ['responseCode' => Directpost::RESPONSE_CODE_DECLINED, 1],
  324. ['responseCode' => Directpost::RESPONSE_CODE_ERROR, 1],
  325. ['responseCode' => 999999, 0],
  326. ];
  327. }
  328. /**
  329. * @param bool $isInitializeNeeded
  330. *
  331. * @dataProvider setIsInitializeNeededDataProvider
  332. */
  333. public function testSetIsInitializeNeeded($isInitializeNeeded)
  334. {
  335. $this->directpost->setIsInitializeNeeded($isInitializeNeeded);
  336. $this->assertEquals($isInitializeNeeded, $this->directpost->isInitializeNeeded());
  337. }
  338. /**
  339. * @return array
  340. */
  341. public function setIsInitializeNeededDataProvider()
  342. {
  343. return [
  344. ['isInitializationNeeded' => true],
  345. ['isInitializationNeeded' => false]
  346. ];
  347. }
  348. /**
  349. * @param bool $isGatewayActionsLocked
  350. * @param bool $canCapture
  351. *
  352. * @dataProvider canCaptureDataProvider
  353. */
  354. public function testCanCapture($isGatewayActionsLocked, $canCapture)
  355. {
  356. $this->directpost->setData('info_instance', $this->paymentMock);
  357. $this->paymentMock->expects($this->any())
  358. ->method('getAdditionalInformation')
  359. ->with(Directpost::GATEWAY_ACTIONS_LOCKED_STATE_KEY)
  360. ->willReturn($isGatewayActionsLocked);
  361. $this->assertEquals($canCapture, $this->directpost->canCapture());
  362. }
  363. /**
  364. * @return array
  365. */
  366. public function canCaptureDataProvider()
  367. {
  368. return [
  369. ['isGatewayActionsLocked' => false, 'canCapture' => true],
  370. ['isGatewayActionsLocked' => true, 'canCapture' => false]
  371. ];
  372. }
  373. /**
  374. * @covers \Magento\Authorizenet\Model\Directpost::fetchTransactionInfo
  375. *
  376. * @param $transactionId
  377. * @param $resultStatus
  378. * @param $responseStatus
  379. * @param $responseCode
  380. * @return void
  381. *
  382. * @dataProvider dataProviderTransaction
  383. */
  384. public function testFetchVoidedTransactionInfo($transactionId, $resultStatus, $responseStatus, $responseCode)
  385. {
  386. $paymentId = 36;
  387. $orderId = 36;
  388. $this->paymentMock->expects(static::once())
  389. ->method('getId')
  390. ->willReturn($paymentId);
  391. $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  392. ->disableOriginalConstructor()
  393. ->setMethods(['getId', '__wakeup'])
  394. ->getMock();
  395. $orderMock->expects(static::once())
  396. ->method('getId')
  397. ->willReturn($orderId);
  398. $this->paymentMock->expects(static::once())
  399. ->method('getOrder')
  400. ->willReturn($orderMock);
  401. $transactionMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment\Transaction::class)
  402. ->disableOriginalConstructor()
  403. ->getMock();
  404. $this->transactionRepositoryMock->expects(static::once())
  405. ->method('getByTransactionId')
  406. ->with($transactionId, $paymentId, $orderId)
  407. ->willReturn($transactionMock);
  408. $document = $this->getTransactionXmlDocument(
  409. $transactionId,
  410. TransactionService::PAYMENT_UPDATE_STATUS_CODE_SUCCESS,
  411. $resultStatus,
  412. $responseStatus,
  413. $responseCode
  414. );
  415. $this->transactionServiceMock->expects(static::once())
  416. ->method('getTransactionDetails')
  417. ->with($this->directpost, $transactionId)
  418. ->willReturn($document);
  419. // transaction should be closed
  420. $this->paymentMock->expects(static::once())
  421. ->method('setIsTransactionDenied')
  422. ->with(true);
  423. $this->paymentMock->expects(static::once())
  424. ->method('setIsTransactionClosed')
  425. ->with(true);
  426. $transactionMock->expects(static::once())
  427. ->method('close');
  428. $this->directpost->fetchTransactionInfo($this->paymentMock, $transactionId);
  429. }
  430. /**
  431. * @covers \Magento\Authorizenet\Model\Directpost::refund()
  432. * @return void
  433. */
  434. public function testSuccessRefund()
  435. {
  436. $card = 1111;
  437. $this->paymentMock->expects(static::exactly(2))
  438. ->method('getCcLast4')
  439. ->willReturn($card);
  440. $this->paymentMock->expects(static::once())
  441. ->method('decrypt')
  442. ->willReturn($card);
  443. $this->paymentMock->expects(static::exactly(3))
  444. ->method('getParentTransactionId')
  445. ->willReturn(self::TRANSACTION_ID . '-capture');
  446. $this->paymentMock->expects(static::once())
  447. ->method('getPoNumber')
  448. ->willReturn(self::INVOICE_NUM);
  449. $this->paymentMock->expects(static::once())
  450. ->method('setIsTransactionClosed')
  451. ->with(true)
  452. ->willReturnSelf();
  453. $orderMock = $this->getOrderMock();
  454. $this->paymentMock->expects(static::exactly(2))
  455. ->method('getOrder')
  456. ->willReturn($orderMock);
  457. $transactionMock = $this->getMockBuilder(Order\Payment\Transaction::class)
  458. ->disableOriginalConstructor()
  459. ->setMethods(['getAdditionalInformation'])
  460. ->getMock();
  461. $transactionMock->expects(static::once())
  462. ->method('getAdditionalInformation')
  463. ->with(Directpost::REAL_TRANSACTION_ID_KEY)
  464. ->willReturn(self::TRANSACTION_ID);
  465. $this->transactionRepositoryMock->expects(static::once())
  466. ->method('getByTransactionId')
  467. ->willReturn($transactionMock);
  468. $response = $this->getRefundResponseBody(
  469. Directpost::RESPONSE_CODE_APPROVED,
  470. Directpost::RESPONSE_REASON_CODE_APPROVED,
  471. 'Successful'
  472. );
  473. $this->httpClientMock->expects(static::once())
  474. ->method('getBody')
  475. ->willReturn($response);
  476. $this->responseMock->expects(static::once())
  477. ->method('getXResponseCode')
  478. ->willReturn(Directpost::RESPONSE_CODE_APPROVED);
  479. $this->responseMock->expects(static::once())
  480. ->method('getXResponseReasonCode')
  481. ->willReturn(Directpost::RESPONSE_REASON_CODE_APPROVED);
  482. $this->dataHelperMock->expects(static::never())
  483. ->method('wrapGatewayError');
  484. $this->directpost->refund($this->paymentMock, self::TOTAL_AMOUNT);
  485. }
  486. /**
  487. * Get data for tests
  488. * @return array
  489. */
  490. public function dataProviderTransaction()
  491. {
  492. return [
  493. [
  494. 'transactionId' => '9941997799',
  495. 'resultStatus' => 'Successful.',
  496. 'responseStatus' => 'voided',
  497. 'responseCode' => 1
  498. ]
  499. ];
  500. }
  501. /**
  502. * Create mock for response factory
  503. * @return void
  504. */
  505. private function initResponseFactoryMock()
  506. {
  507. $this->responseFactoryMock = $this->getMockBuilder(
  508. \Magento\Authorizenet\Model\Directpost\Response\Factory::class
  509. )->disableOriginalConstructor()->getMock();
  510. $this->responseMock = $this->getMockBuilder(\Magento\Authorizenet\Model\Directpost\Response::class)
  511. ->setMethods(
  512. [
  513. 'isValidHash',
  514. 'getXTransId', 'getXResponseCode', 'getXResponseReasonCode', 'getXResponseReasonText', 'getXAmount',
  515. 'setXResponseCode', 'setXResponseReasonCode', 'setXAvsCode', 'setXResponseReasonText',
  516. 'setXTransId', 'setXInvoiceNum', 'setXAmount', 'setXMethod', 'setXType', 'setData',
  517. 'setXAccountNumber',
  518. '__wakeup'
  519. ]
  520. )
  521. ->disableOriginalConstructor()
  522. ->getMock();
  523. $this->responseMock->expects(static::any())
  524. ->method('setXResponseCode')
  525. ->willReturnSelf();
  526. $this->responseMock->expects(static::any())
  527. ->method('setXResponseReasonCode')
  528. ->willReturnSelf();
  529. $this->responseMock->expects(static::any())
  530. ->method('setXResponseReasonText')
  531. ->willReturnSelf();
  532. $this->responseMock->expects(static::any())
  533. ->method('setXAvsCode')
  534. ->willReturnSelf();
  535. $this->responseMock->expects(static::any())
  536. ->method('setXTransId')
  537. ->willReturnSelf();
  538. $this->responseMock->expects(static::any())
  539. ->method('setXInvoiceNum')
  540. ->willReturnSelf();
  541. $this->responseMock->expects(static::any())
  542. ->method('setXAmount')
  543. ->willReturnSelf();
  544. $this->responseMock->expects(static::any())
  545. ->method('setXMethod')
  546. ->willReturnSelf();
  547. $this->responseMock->expects(static::any())
  548. ->method('setXType')
  549. ->willReturnSelf();
  550. $this->responseMock->expects(static::any())
  551. ->method('setData')
  552. ->willReturnSelf();
  553. $this->responseFactoryMock->expects($this->any())
  554. ->method('create')
  555. ->willReturn($this->responseMock);
  556. }
  557. /**
  558. * Get transaction data
  559. * @param $transactionId
  560. * @param $resultCode
  561. * @param $resultStatus
  562. * @param $responseStatus
  563. * @param $responseCode
  564. * @return Element
  565. */
  566. private function getTransactionXmlDocument(
  567. $transactionId,
  568. $resultCode,
  569. $resultStatus,
  570. $responseStatus,
  571. $responseCode
  572. ) {
  573. $body = sprintf(
  574. '<?xml version="1.0" encoding="utf-8"?>
  575. <getTransactionDetailsResponse
  576. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  577. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  578. xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  579. <messages>
  580. <resultCode>%s</resultCode>
  581. <message>
  582. <code>I00001</code>
  583. <text>%s</text>
  584. </message>
  585. </messages>
  586. <transaction>
  587. <transId>%s</transId>
  588. <transactionType>authOnlyTransaction</transactionType>
  589. <transactionStatus>%s</transactionStatus>
  590. <responseCode>%s</responseCode>
  591. <responseReasonCode>%s</responseReasonCode>
  592. </transaction>
  593. </getTransactionDetailsResponse>',
  594. $resultCode,
  595. $resultStatus,
  596. $transactionId,
  597. $responseStatus,
  598. $responseCode,
  599. $responseCode
  600. );
  601. libxml_use_internal_errors(true);
  602. $document = new Element($body);
  603. libxml_use_internal_errors(false);
  604. return $document;
  605. }
  606. /**
  607. * Get mock for authorize.net request factory
  608. * @return \PHPUnit\Framework\MockObject_MockBuilder
  609. */
  610. private function getRequestFactoryMock()
  611. {
  612. $requestFactory = $this->getMockBuilder(Factory::class)
  613. ->disableOriginalConstructor()
  614. ->setMethods(['create'])
  615. ->getMock();
  616. $request = $this->getMockBuilder(Request::class)
  617. ->disableOriginalConstructor()
  618. ->setMethods(['__wakeup'])
  619. ->getMock();
  620. $requestFactory->expects(static::any())
  621. ->method('create')
  622. ->willReturn($request);
  623. return $requestFactory;
  624. }
  625. /**
  626. * Get mock for order
  627. * @return \PHPUnit_Framework_MockObject_MockObject
  628. */
  629. private function getOrderMock()
  630. {
  631. $orderMock = $this->getMockBuilder(Order::class)
  632. ->disableOriginalConstructor()
  633. ->setMethods([
  634. 'getId', 'getIncrementId', 'getStoreId', 'getBillingAddress', 'getShippingAddress',
  635. 'getBaseCurrencyCode', 'getBaseTaxAmount', '__wakeup'
  636. ])
  637. ->getMock();
  638. $orderMock->expects(static::once())
  639. ->method('getId')
  640. ->willReturn(1);
  641. $orderMock->expects(static::exactly(2))
  642. ->method('getIncrementId')
  643. ->willReturn(self::INVOICE_NUM);
  644. $orderMock->expects(static::once())
  645. ->method('getStoreId')
  646. ->willReturn(1);
  647. $orderMock->expects(static::once())
  648. ->method('getBaseCurrencyCode')
  649. ->willReturn('USD');
  650. return $orderMock;
  651. }
  652. /**
  653. * Create and return mock for http client factory
  654. * @return \PHPUnit_Framework_MockObject_MockObject
  655. */
  656. private function getHttpClientFactoryMock()
  657. {
  658. $this->httpClientMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClient::class)
  659. ->disableOriginalConstructor()
  660. ->setMethods(['request', 'getBody', '__wakeup'])
  661. ->getMock();
  662. $this->httpClientMock->expects(static::any())
  663. ->method('request')
  664. ->willReturnSelf();
  665. $httpClientFactoryMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClientFactory::class)
  666. ->disableOriginalConstructor()
  667. ->setMethods(['create'])
  668. ->getMock();
  669. $httpClientFactoryMock->expects(static::any())
  670. ->method('create')
  671. ->willReturn($this->httpClientMock);
  672. return $httpClientFactoryMock;
  673. }
  674. /**
  675. * Get mocked response for refund transaction
  676. * @param $code
  677. * @param $reasonCode
  678. * @param $reasonText
  679. * @return string
  680. */
  681. private function getRefundResponseBody($code, $reasonCode, $reasonText)
  682. {
  683. $result = array_fill(0, 50, '');
  684. $result[0] = $code; // XResponseCode
  685. $result[2] = $reasonCode; // XResponseReasonCode
  686. $result[3] = $reasonText; // XResponseReasonText
  687. $result[6] = self::TRANSACTION_ID; // XTransId
  688. $result[7] = self::INVOICE_NUM; // XInvoiceNum
  689. $result[9] = self::TOTAL_AMOUNT; // XAmount
  690. $result[10] = Directpost::REQUEST_METHOD_CC; // XMethod
  691. $result[11] = Directpost::REQUEST_TYPE_CREDIT; // XType
  692. $result[37] = md5(self::TRANSACTION_ID); // x_MD5_Hash
  693. $result[50] = '48329483921'; // setXAccountNumber
  694. return implode(Directpost::RESPONSE_DELIM_CHAR, $result);
  695. }
  696. }