NvpTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Api;
  7. use Magento\Framework\Api\SearchCriteriaBuilder;
  8. use Magento\Framework\App\ProductMetadataInterface;
  9. use Magento\Framework\HTTP\Adapter\Curl;
  10. use Magento\Framework\HTTP\Adapter\CurlFactory;
  11. use Magento\Framework\ObjectManagerInterface;
  12. use Magento\Paypal\Model\CartFactory;
  13. use Magento\Paypal\Model\Config;
  14. use Magento\Quote\Model\Quote;
  15. use Magento\Quote\Model\QuoteRepository;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class NvpTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var Nvp
  25. */
  26. private $nvpApi;
  27. /**
  28. * @var ObjectManagerInterface
  29. */
  30. private $objectManager;
  31. /**
  32. * @var Curl|MockObject
  33. */
  34. private $httpClient;
  35. /**
  36. * @inheritdoc
  37. */
  38. protected function setUp()
  39. {
  40. $this->objectManager = Bootstrap::getObjectManager();
  41. /** @var CurlFactory|MockObject $httpFactory */
  42. $httpFactory = $this->getMockBuilder(CurlFactory::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->httpClient = $this->getMockBuilder(Curl::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $httpFactory->method('create')
  49. ->willReturn($this->httpClient);
  50. $this->nvpApi = $this->objectManager->create(Nvp::class, [
  51. 'curlFactory' => $httpFactory
  52. ]);
  53. /** @var ProductMetadataInterface|MockObject $productMetadata */
  54. $productMetadata = $this->getMockBuilder(ProductMetadataInterface::class)
  55. ->getMock();
  56. $productMetadata->method('getEdition')
  57. ->willReturn('');
  58. /** @var Config $config */
  59. $config = $this->objectManager->get(Config::class);
  60. $config->setMethodCode(Config::METHOD_EXPRESS);
  61. $refObject = new \ReflectionObject($config);
  62. $refProperty = $refObject->getProperty('productMetadata');
  63. $refProperty->setAccessible(true);
  64. $refProperty->setValue($config, $productMetadata);
  65. $this->nvpApi->setConfigObject($config);
  66. }
  67. /**
  68. * Checks a case when items with FPT (Fixed Product Tax) are present in the request.
  69. *
  70. * @magentoConfigFixture current_store tax/weee/enable 1
  71. * @magentoConfigFixture current_store tax/weee/include_in_subtotal 0
  72. * @magentoDataFixture Magento/Paypal/_files/quote_with_fpt.php
  73. */
  74. public function testRequestTotalsAndLineItemsWithFPT()
  75. {
  76. $quote = $this->getQuote('100000016');
  77. /** @var CartFactory $cartFactory */
  78. $cartFactory = $this->objectManager->get(CartFactory::class);
  79. $cart = $cartFactory->create(['salesModel' => $quote]);
  80. $request = 'PAYMENTACTION=Authorization&AMT=112.70'
  81. . '&SHIPPINGAMT=0.00&ITEMAMT=112.70&TAXAMT=0.00'
  82. . '&L_NAME0=Simple+Product+FPT&L_QTY0=1&L_AMT0=100.00'
  83. . '&L_NAME1=FPT&L_QTY1=1&L_AMT1=12.70'
  84. . '&METHOD=SetExpressCheckout&VERSION=72.0&BUTTONSOURCE=Magento_Cart_';
  85. $this->httpClient->method('write')
  86. ->with(
  87. 'POST',
  88. 'https://api-3t.paypal.com/nvp',
  89. '1.1',
  90. [],
  91. $this->equalTo($request)
  92. );
  93. $this->httpClient->method('read')
  94. ->willReturn(
  95. "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nRESULT=0&RESPMSG=Approved"
  96. );
  97. $this->nvpApi->setAmount($quote->getBaseGrandTotal());
  98. $this->nvpApi->setPaypalCart($cart);
  99. $this->nvpApi->setQuote($quote);
  100. $this->nvpApi->setIsLineItemsEnabled(true);
  101. $this->nvpApi->callSetExpressCheckout();
  102. }
  103. /**
  104. * Test that the refund request to Paypal sends the correct data
  105. *
  106. * @magentoDataFixture Magento/Paypal/_files/order_express_with_tax.php
  107. */
  108. public function testCallRefundTransaction()
  109. {
  110. /** @var \Magento\Sales\Model\Order $order */
  111. $order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
  112. $order->loadByIncrementId('100000001');
  113. /** @var \Magento\Sales\Model\Order\Payment $payment */
  114. $payment = $order->getPayment();
  115. $this->nvpApi->setPayment(
  116. $payment
  117. )->setTransactionId(
  118. 'fooTransactionId'
  119. )->setAmount(
  120. $payment->formatAmount($order->getBaseGrandTotal())
  121. )->setCurrencyCode(
  122. $order->getBaseCurrencyCode()
  123. )->setRefundType(
  124. Config::REFUND_TYPE_PARTIAL
  125. );
  126. $httpQuery = 'TRANSACTIONID=fooTransactionId&REFUNDTYPE=Partial'
  127. .'&CURRENCYCODE=USD&AMT=145.98&METHOD=RefundTransaction'
  128. .'&VERSION=72.0&BUTTONSOURCE=Magento_Cart_';
  129. $this->httpClient->expects($this->once())->method('write')
  130. ->with(
  131. 'POST',
  132. 'https://api-3t.paypal.com/nvp',
  133. '1.1',
  134. [],
  135. $httpQuery
  136. );
  137. $this->nvpApi->callRefundTransaction();
  138. }
  139. /**
  140. * Gets quote by reserved order id.
  141. *
  142. * @param string $reservedOrderId
  143. * @return Quote
  144. */
  145. private function getQuote($reservedOrderId)
  146. {
  147. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  148. $searchCriteriaBuilder = $this->objectManager->create(SearchCriteriaBuilder::class);
  149. $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
  150. ->create();
  151. /** @var QuoteRepository $quoteRepository */
  152. $quoteRepository = $this->objectManager->get(QuoteRepository::class);
  153. $items = $quoteRepository->getList($searchCriteria)
  154. ->getItems();
  155. return array_pop($items);
  156. }
  157. }