DirectpostTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorizenet\Model;
  7. use Magento\Framework\Api\SearchCriteriaBuilder;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\HTTP\ZendClient;
  10. use Magento\Framework\HTTP\ZendClientFactory;
  11. use Magento\Framework\Simplexml\Element;
  12. use Magento\Sales\Api\Data\OrderInterface;
  13. use Magento\Sales\Api\OrderRepositoryInterface;
  14. use Magento\Sales\Model\Order;
  15. use Magento\Sales\Model\Order\Payment;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  18. /**
  19. * Class contains tests for Direct Post integration
  20. *
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class DirectpostTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. * @var ObjectManager
  27. */
  28. private $objectManager;
  29. /**
  30. * @var ZendClientFactory|MockObject
  31. */
  32. private $httpClientFactory;
  33. /**
  34. * @var Directpost
  35. */
  36. private $directPost;
  37. protected function setUp()
  38. {
  39. $this->objectManager = Bootstrap::getObjectManager();
  40. $this->httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['create'])
  43. ->getMock();
  44. $this->directPost = $this->objectManager->create(Directpost::class, [
  45. 'httpClientFactory' => $this->httpClientFactory
  46. ]);
  47. }
  48. /**
  49. * @covers \Magento\Authorizenet\Model\Directpost::capture
  50. * @magentoDataFixture Magento/Authorizenet/_files/order.php
  51. */
  52. public function testCapture()
  53. {
  54. $amount = 120.15;
  55. /** @var Payment $payment */
  56. $payment = $this->getPayment('100000002');
  57. $transactionId = '106235225';
  58. /** @var ZendClient|MockObject $httpClient */
  59. $httpClient = $this->getMockBuilder(ZendClient::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(['setUri', 'setConfig', 'setParameterPost', 'setMethod', 'request'])
  62. ->getMock();
  63. $this->httpClientFactory->expects(static::once())
  64. ->method('create')
  65. ->willReturn($httpClient);
  66. $response = $this->getMockBuilder('Zend_Http_Response')
  67. ->disableOriginalConstructor()
  68. ->setMethods(['getBody'])
  69. ->getMock();
  70. $response->expects(static::once())
  71. ->method('getBody')
  72. ->willReturn(
  73. "1(~)1(~)1(~)This transaction has been approved.(~)AWZFTG(~)P(~){$transactionId}(~)100000002(~)
  74. (~)120.15(~)CC(~)prior_auth_capture(~)(~)Anthony(~)Nealy(~)(~)Pearl St(~)Los Angeles(~)California
  75. (~)10020(~)US(~)22-333-44(~)(~)customer@example.com(~)John(~)Doe(~)
  76. (~)Bourne St(~)London(~)(~)DW23W(~)UK(~)0.00(~)(~){$amount}(~)(~)
  77. (~)74B5D54ADFE98093A0FF6446(~)(~)(~)(~)(~)(~)(~)(~)(~)(~)(~)(~)(~)XXXX1111(~)Visa(~)(~)(~)(~)(~)
  78. (~)(~)(~)(~)(~)(~)(~)(~)(~)(~)(~)(~)"
  79. );
  80. $httpClient->expects(static::once())
  81. ->method('request')
  82. ->willReturn($response);
  83. $this->directPost->capture($payment, $amount);
  84. static::assertEquals($transactionId, $payment->getTransactionId());
  85. static::assertFalse($payment->getIsTransactionClosed());
  86. static::assertEquals('US', $payment->getOrder()->getBillingAddress()->getCountryId());
  87. static::assertEquals('UK', $payment->getOrder()->getShippingAddress()->getCountryId());
  88. }
  89. /**
  90. * Verifies that order is placed in correct state according the action taken for a transaction that
  91. * triggered one or more of the Advanced Fraud Detection Suite filters.
  92. *
  93. * @param string $filterAction
  94. * @param string $orderId
  95. * @param string $expectedOrderState
  96. *
  97. * @magentoConfigFixture current_store payment/authorizenet_directpost/trans_md5 TestHash
  98. * @magentoConfigFixture current_store payment/authorizenet_directpost/login TestLogin
  99. * @magentoDataFixture Magento/Authorizenet/_files/order.php
  100. * @dataProvider fdsFilterActionDataProvider
  101. */
  102. public function testProcessWithFdsFilterActionReportOnly($filterAction, $orderId, $expectedOrderState)
  103. {
  104. $responseBody = $this->getSuccessResponse($orderId);
  105. $transactionService = $this->getTransactionService($filterAction);
  106. $this->objectManager->addSharedInstance($transactionService, TransactionService::class);
  107. $this->directPost->process($responseBody);
  108. /** @var Payment $payment */
  109. $payment = $this->getPayment($orderId);
  110. $this->objectManager->removeSharedInstance(TransactionService::class);
  111. static::assertEquals($expectedOrderState, $payment->getOrder()->getState());
  112. }
  113. /**
  114. * @return array
  115. */
  116. public function fdsFilterActionDataProvider()
  117. {
  118. return [
  119. [
  120. 'filter_action' => 'authAndHold',
  121. 'order_id' => '100000003',
  122. 'expected_order_state' => Order::STATE_PAYMENT_REVIEW
  123. ],
  124. [
  125. 'filter_action' => 'report',
  126. 'order_id' => '100000004',
  127. 'expected_order_state' => Order::STATE_COMPLETE
  128. ],
  129. ];
  130. }
  131. /**
  132. * @param string $orderId
  133. * @return array
  134. */
  135. private function getSuccessResponse($orderId)
  136. {
  137. return [
  138. 'x_response_code' => '1',
  139. 'x_response_reason_code' => '1',
  140. 'x_response_reason_text' => 'This transaction has been approved.',
  141. 'x_avs_code' => 'Y',
  142. 'x_auth_code' => 'YWO2E2',
  143. 'x_trans_id' => '40004862720',
  144. 'x_method' => 'CC',
  145. 'x_card_type' => 'Visa',
  146. 'x_account_number' => 'XXXX1111',
  147. 'x_first_name' => 'John',
  148. 'x_last_name' => 'Smith',
  149. 'x_company' => 'CompanyName',
  150. 'x_address' => 'Green str, 67',
  151. 'x_city' => 'CityM',
  152. 'x_state' => 'Alabama',
  153. 'x_zip' => '93930',
  154. 'x_country' => 'US',
  155. 'x_phone' => '3468676',
  156. 'x_fax' => '04040404',
  157. 'x_email' => 'user_1@example.com',
  158. 'x_invoice_num' => $orderId,
  159. 'x_description' => '',
  160. 'x_type' => 'auth_only',
  161. 'x_cust_id' => '',
  162. 'x_ship_to_first_name' => 'John',
  163. 'x_ship_to_last_name' => 'Smith',
  164. 'x_ship_to_company' => 'CompanyName',
  165. 'x_ship_to_address' => 'Green str, 67',
  166. 'x_ship_to_city' => 'CityM',
  167. 'x_ship_to_state' => 'Alabama',
  168. 'x_ship_to_zip' => '93930',
  169. 'x_ship_to_country' => 'US',
  170. 'x_amount' => '120.15',
  171. 'x_tax' => '0.00',
  172. 'x_duty' => '0.00',
  173. 'x_freight' => '5.00',
  174. 'x_tax_exempt' => 'FALSE',
  175. 'x_po_num' => '',
  176. 'x_MD5_Hash' => 'C1CC5AB9D6F0481E240AD74DFF624584',
  177. 'x_SHA2_Hash' => '',
  178. 'x_cvv2_resp_code' => 'P',
  179. 'x_cavv_response' => '2',
  180. 'x_test_request' => 'false',
  181. 'controller_action_name' => 'directpost_payment',
  182. 'is_secure' => '1',
  183. ];
  184. }
  185. /**
  186. * Get order payment.
  187. *
  188. * @param string $orderId
  189. * @return Payment
  190. */
  191. private function getPayment($orderId)
  192. {
  193. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  194. $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
  195. $searchCriteria = $searchCriteriaBuilder->addFilter(OrderInterface::INCREMENT_ID, $orderId)
  196. ->create();
  197. $orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
  198. $orders = $orderRepository->getList($searchCriteria)
  199. ->getItems();
  200. /** @var OrderInterface $order */
  201. $order = array_pop($orders);
  202. return $order->getPayment();
  203. }
  204. /**
  205. * Returns TransactionService mocked object with authorize predefined response.
  206. *
  207. * @param string $filterAction
  208. * @return TransactionService|MockObject
  209. */
  210. private function getTransactionService($filterAction)
  211. {
  212. $response = str_replace(
  213. '{filterAction}',
  214. $filterAction,
  215. file_get_contents(__DIR__ . '/../_files/transaction_details.xml')
  216. );
  217. $transactionService = $this->getMockBuilder(TransactionService::class)
  218. ->disableOriginalConstructor()
  219. ->getMock();
  220. $transactionService->method('getTransactionDetails')
  221. ->willReturn(
  222. new Element($response)
  223. );
  224. return $transactionService;
  225. }
  226. }