DenyPaymentTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Plugin;
  7. use Magento\Framework\Api\SearchCriteriaBuilder;
  8. use Magento\Framework\Registry;
  9. use Magento\Payment\Model\Info as PaymentInfo;
  10. use Magento\Paypal\Model\Api\Nvp;
  11. use Magento\Paypal\Model\Config;
  12. use Magento\Paypal\Model\Express;
  13. use Magento\Paypal\Model\Info;
  14. use Magento\Paypal\Model\Pro;
  15. use Magento\Paypal\Model\ProFactory;
  16. use Magento\Sales\Api\Data\OrderInterface;
  17. use Magento\Sales\Api\OrderRepositoryInterface;
  18. use Magento\Signifyd\Api\CaseRepositoryInterface;
  19. use Magento\Signifyd\Api\Data\CaseInterface;
  20. use Magento\Signifyd\Model\SignifydGateway\ApiClient;
  21. use Magento\TestFramework\Helper\Bootstrap;
  22. use Magento\TestFramework\ObjectManager;
  23. use PHPUnit\Framework\MockObject_MockObject as MockObject;
  24. /**
  25. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  26. */
  27. class DenyPaymentTest extends \PHPUnit\Framework\TestCase
  28. {
  29. /**
  30. * @var int
  31. */
  32. private static $caseId = 123;
  33. /**
  34. * @var ObjectManager
  35. */
  36. private $objectManager;
  37. /**
  38. * @var ApiClient|MockObject
  39. */
  40. private $apiClient;
  41. /**
  42. * @var Registry
  43. */
  44. private $registry;
  45. /**
  46. * @inheritdoc
  47. */
  48. protected function setUp()
  49. {
  50. $this->objectManager = Bootstrap::getObjectManager();
  51. $this->apiClient = $this->getMockBuilder(ApiClient::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods(['makeApiCall'])
  54. ->getMock();
  55. $this->registry = $this->objectManager->get(Registry::class);
  56. $this->objectManager->addSharedInstance($this->apiClient, ApiClient::class);
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. protected function tearDown()
  62. {
  63. $this->objectManager->removeSharedInstance(ApiClient::class);
  64. }
  65. /**
  66. * Checks a test case, when payment has been denied
  67. * and calls plugin to cancel Signifyd case guarantee.
  68. *
  69. * @covers \Magento\Signifyd\Plugin\PaymentPlugin::afterDenyPayment
  70. * @magentoDataFixture Magento/Signifyd/_files/approved_case.php
  71. * @magentoConfigFixture current_store fraud_protection/signifyd/active 1
  72. */
  73. public function testAfterDenyPayment()
  74. {
  75. $order = $this->getOrder();
  76. $this->registry->register('current_order', $order);
  77. $this->apiClient->expects(self::once())
  78. ->method('makeApiCall')
  79. ->with(
  80. self::equalTo('/cases/' . self::$caseId . '/guarantee'),
  81. 'PUT',
  82. [
  83. 'guaranteeDisposition' => CaseInterface::GUARANTEE_CANCELED
  84. ]
  85. )
  86. ->willReturn([
  87. 'disposition' => CaseInterface::GUARANTEE_CANCELED
  88. ]);
  89. /** @var \Magento\Sales\Model\Order\Payment $payment */
  90. $payment = $order->getPayment();
  91. $payment->setData('method_instance', $this->getMethodInstance());
  92. $payment->deny();
  93. /** @var CaseRepositoryInterface $caseRepository */
  94. $caseRepository = $this->objectManager->get(CaseRepositoryInterface::class);
  95. $case = $caseRepository->getByCaseId(self::$caseId);
  96. self::assertEquals(CaseInterface::GUARANTEE_CANCELED, $case->getGuaranteeDisposition());
  97. }
  98. /**
  99. * Get stored order.
  100. *
  101. * @return OrderInterface
  102. */
  103. private function getOrder()
  104. {
  105. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  106. $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
  107. $searchCriteria = $searchCriteriaBuilder->addFilter(OrderInterface::INCREMENT_ID, '100000001')
  108. ->create();
  109. $orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
  110. $orders = $orderRepository->getList($searchCriteria)
  111. ->getItems();
  112. /** @var OrderInterface $order */
  113. return array_pop($orders);
  114. }
  115. /**
  116. * Gets payment method instance.
  117. *
  118. * @return Express
  119. */
  120. private function getMethodInstance()
  121. {
  122. /** @var PaymentInfo $infoInstance */
  123. $infoInstance = $this->objectManager->get(PaymentInfo::class);
  124. $infoInstance->setAdditionalInformation(
  125. Info::PAYMENT_STATUS_GLOBAL,
  126. Info::PAYMENTSTATUS_PENDING
  127. );
  128. $infoInstance->setAdditionalInformation(
  129. Info::PENDING_REASON_GLOBAL,
  130. Info::PAYMENTSTATUS_PENDING
  131. );
  132. /** @var Express $methodInstance */
  133. $methodInstance = $this->objectManager->create(
  134. Express::class,
  135. ['proFactory' => $this->getProFactory()]
  136. );
  137. $methodInstance->setData('info_instance', $infoInstance);
  138. return $methodInstance;
  139. }
  140. /**
  141. * Gets Pro factory mock.
  142. *
  143. * @return ProFactory|MockObject
  144. */
  145. protected function getProFactory()
  146. {
  147. $pro = $this->getMockBuilder(Pro::class)
  148. ->disableOriginalConstructor()
  149. ->setMethods(['getApi', 'setMethod', 'getConfig', '__wakeup', 'reviewPayment'])
  150. ->getMock();
  151. $nvpClient = $this->getMockBuilder(Nvp::class)
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. $pro->method('getConfig')
  155. ->willReturn($this->getConfig());
  156. $pro->method('getApi')
  157. ->willReturn($nvpClient);
  158. $pro->method('reviewPayment')
  159. ->willReturn(true);
  160. $proFactory = $this->getMockBuilder(ProFactory::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $proFactory->method('create')
  164. ->willReturn($pro);
  165. return $proFactory;
  166. }
  167. /**
  168. * Gets config mock.
  169. *
  170. * @return Config|MockObject
  171. */
  172. protected function getConfig()
  173. {
  174. $config = $this->getMockBuilder(Config::class)
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. $config->method('getValue')
  178. ->with('payment_action')
  179. ->willReturn(Config::PAYMENT_ACTION_AUTH);
  180. return $config;
  181. }
  182. }