PayflowNvpTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 PayflowNvpTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var PayflowNvp
  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(PayflowNvp::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_WPP_PE_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 and discount are present in the request.
  69. *
  70. * @magentoDataFixture Magento/Paypal/_files/quote_payflowpro.php
  71. * @magentoDbIsolation disabled
  72. */
  73. public function testRequestLineItems()
  74. {
  75. $quote = $this->getQuote('100000015');
  76. /** @var CartFactory $cartFactory */
  77. $cartFactory = $this->objectManager->get(CartFactory::class);
  78. $cart = $cartFactory->create(['salesModel' => $quote]);
  79. $request = 'TENDER=P&AMT=52.14&FREIGHTAMT=0.00&TAXAMT=0.00&'
  80. . 'L_NAME0=Simple 1&L_QTY0=1&L_COST0=7.69&'
  81. . 'L_NAME1=Simple 2&L_QTY1=2&L_COST1=9.69&'
  82. . 'L_NAME2=Simple 3&L_QTY2=3&L_COST2=11.69&'
  83. . 'L_NAME3=Discount&L_QTY3=1&L_COST3=-10.00&'
  84. . 'TRXTYPE=A&ACTION=S&BUTTONSOURCE=Magento_Cart_';
  85. $this->httpClient->method('write')
  86. ->with(
  87. 'POST',
  88. 'https://payflowpro.paypal.com/transaction',
  89. '1.1',
  90. ['PAYPAL-NVP: Y'],
  91. self::equalTo($request)
  92. );
  93. $this->httpClient->method('read')
  94. ->willReturn("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nRESULT=0&RESPMSG=Approved");
  95. $this->nvpApi->setAmount($quote->getBaseGrandTotal());
  96. $this->nvpApi->setPaypalCart($cart);
  97. $this->nvpApi->setQuote($quote);
  98. $this->nvpApi->setIsLineItemsEnabled(true);
  99. $this->nvpApi->callSetExpressCheckout();
  100. }
  101. /**
  102. * Gets quote by reserved order id.
  103. *
  104. * @param string $reservedOrderId
  105. * @return Quote
  106. */
  107. private function getQuote($reservedOrderId)
  108. {
  109. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  110. $searchCriteriaBuilder = $this->objectManager->create(SearchCriteriaBuilder::class);
  111. $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
  112. ->create();
  113. /** @var QuoteRepository $quoteRepository */
  114. $quoteRepository = $this->objectManager->get(QuoteRepository::class);
  115. $items = $quoteRepository->getList($searchCriteria)
  116. ->getItems();
  117. return array_pop($items);
  118. }
  119. }