IpnTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\Paypal\Model\Ipn
  8. */
  9. namespace Magento\Paypal\Test\Unit\Model;
  10. use Magento\Sales\Model\Order;
  11. class IpnTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Paypal\Model\Ipn
  15. */
  16. protected $_ipn;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_orderMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_paypalInfo;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $configFactory;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $curlFactory;
  33. protected function setUp()
  34. {
  35. $methods = [
  36. 'create',
  37. 'loadByIncrementId',
  38. 'canFetchPaymentReviewUpdate',
  39. 'getId',
  40. 'getPayment',
  41. 'getMethod',
  42. 'getStoreId',
  43. 'update',
  44. 'getAdditionalInformation',
  45. 'getEmailSent',
  46. 'save',
  47. 'getState',
  48. ];
  49. $this->_orderMock = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, $methods);
  50. $this->_orderMock->expects($this->any())->method('create')->will($this->returnSelf());
  51. $this->_orderMock->expects($this->any())->method('loadByIncrementId')->will($this->returnSelf());
  52. $this->_orderMock->expects($this->any())->method('getId')->will($this->returnSelf());
  53. $this->_orderMock->expects($this->any())->method('getMethod')->will($this->returnSelf());
  54. $this->_orderMock->expects($this->any())->method('getStoreId')->will($this->returnSelf());
  55. $this->_orderMock->expects($this->any())->method('getEmailSent')->will($this->returnValue(true));
  56. $this->configFactory = $this->createPartialMock(\Magento\Paypal\Model\ConfigFactory::class, ['create']);
  57. $configMock = $this->getMockBuilder(\Magento\Paypal\Model\Config::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->configFactory->expects($this->any())->method('create')->willReturn($configMock);
  61. $configMock->expects($this->any())->method('isMethodActive')->will($this->returnValue(true));
  62. $configMock->expects($this->any())->method('isMethodAvailable')->will($this->returnValue(true));
  63. $configMock->expects($this->any())->method('getValue')->will($this->returnValue(null));
  64. $configMock->expects($this->any())->method('getPayPalIpnUrl')
  65. ->will($this->returnValue('https://ipnpb_paypal_url'));
  66. $this->curlFactory = $this->createPartialMock(
  67. \Magento\Framework\HTTP\Adapter\CurlFactory::class,
  68. ['create', 'setConfig', 'write', 'read']
  69. );
  70. $this->curlFactory->expects($this->any())->method('create')->will($this->returnSelf());
  71. $this->curlFactory->expects($this->any())->method('setConfig')->will($this->returnSelf());
  72. $this->curlFactory->expects($this->any())->method('write')->will($this->returnSelf());
  73. $this->curlFactory->expects($this->any())->method('read')->will($this->returnValue(
  74. '
  75. VERIFIED'
  76. ));
  77. $this->_paypalInfo = $this->createPartialMock(
  78. \Magento\Paypal\Model\Info::class,
  79. ['importToPayment', 'getMethod', 'getAdditionalInformation']
  80. );
  81. $this->_paypalInfo->expects($this->any())->method('getMethod')->will($this->returnValue('some_method'));
  82. $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  83. $this->_ipn = $objectHelper->getObject(
  84. \Magento\Paypal\Model\Ipn::class,
  85. [
  86. 'configFactory' => $this->configFactory,
  87. 'curlFactory' => $this->curlFactory,
  88. 'orderFactory' => $this->_orderMock,
  89. 'paypalInfo' => $this->_paypalInfo,
  90. 'data' => ['payment_status' => 'Pending', 'pending_reason' => 'authorization']
  91. ]
  92. );
  93. }
  94. public function testLegacyRegisterPaymentAuthorization()
  95. {
  96. $this->_orderMock->expects($this->any())->method('canFetchPaymentReviewUpdate')->will(
  97. $this->returnValue(false)
  98. );
  99. $methods = [
  100. 'setPreparedMessage',
  101. '__wakeup',
  102. 'setTransactionId',
  103. 'setParentTransactionId',
  104. 'setIsTransactionClosed',
  105. 'registerAuthorizationNotification',
  106. ];
  107. $payment = $this->createPartialMock(\Magento\Sales\Model\Order\Payment::class, $methods);
  108. $payment->expects($this->any())->method('setPreparedMessage')->will($this->returnSelf());
  109. $payment->expects($this->any())->method('setTransactionId')->will($this->returnSelf());
  110. $payment->expects($this->any())->method('setParentTransactionId')->will($this->returnSelf());
  111. $payment->expects($this->any())->method('setIsTransactionClosed')->will($this->returnSelf());
  112. $this->_orderMock->expects($this->any())->method('getPayment')->will($this->returnValue($payment));
  113. $this->_orderMock->expects($this->any())->method('getAdditionalInformation')->will($this->returnValue([]));
  114. $this->_paypalInfo->expects($this->once())->method('importToPayment');
  115. $this->_ipn->processIpnRequest();
  116. }
  117. public function testPaymentReviewRegisterPaymentAuthorization()
  118. {
  119. $this->_orderMock->expects($this->any())->method('getPayment')->will($this->returnSelf());
  120. $this->_orderMock->expects($this->any())->method('canFetchPaymentReviewUpdate')->will($this->returnValue(true));
  121. $this->_orderMock->expects($this->once())->method('update')->with(true)->will($this->returnSelf());
  122. $this->_ipn->processIpnRequest();
  123. }
  124. public function testPaymentReviewRegisterPaymentFraud()
  125. {
  126. $paymentMock = $this->createPartialMock(
  127. \Magento\Sales\Model\Order\Payment::class,
  128. ['getAdditionalInformation', '__wakeup', 'registerCaptureNotification']
  129. );
  130. $paymentMock->expects($this->any())
  131. ->method('getAdditionalInformation')
  132. ->will($this->returnValue([]));
  133. $paymentMock->expects($this->any())
  134. ->method('registerCaptureNotification')
  135. ->will($this->returnValue(true));
  136. $this->_orderMock->expects($this->any())->method('getPayment')->will($this->returnValue($paymentMock));
  137. $this->_orderMock->expects($this->any())->method('canFetchPaymentReviewUpdate')->will($this->returnValue(true));
  138. $this->_orderMock->expects($this->once())->method('getState')->will(
  139. $this->returnValue(Order::STATE_PENDING_PAYMENT)
  140. );
  141. $this->_paypalInfo->expects($this->once())
  142. ->method('importToPayment')
  143. ->with(
  144. [
  145. 'payment_status' => 'pending',
  146. 'pending_reason' => 'fraud',
  147. 'collected_fraud_filters' => ['Maximum Transaction Amount'],
  148. ],
  149. $paymentMock
  150. );
  151. $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  152. $this->_ipn = $objectHelper->getObject(
  153. \Magento\Paypal\Model\Ipn::class,
  154. [
  155. 'configFactory' => $this->configFactory,
  156. 'curlFactory' => $this->curlFactory,
  157. 'orderFactory' => $this->_orderMock,
  158. 'paypalInfo' => $this->_paypalInfo,
  159. 'data' => [
  160. 'payment_status' => 'Pending',
  161. 'pending_reason' => 'fraud',
  162. 'fraud_management_pending_filters_1' => 'Maximum Transaction Amount',
  163. ]
  164. ]
  165. );
  166. $this->_ipn->processIpnRequest();
  167. $this->assertEquals('IPN "Pending"', $paymentMock->getPreparedMessage());
  168. }
  169. public function testRegisterPaymentDenial()
  170. {
  171. /** @var \Magento\Sales\Model\Order\Payment $paymentMock */
  172. $paymentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
  173. ->setMethods([
  174. 'getAdditionalInformation',
  175. 'setTransactionId',
  176. 'setNotificationResult',
  177. 'setIsTransactionClosed',
  178. 'deny'
  179. ])
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. $paymentMock->expects($this->exactly(6))->method('getAdditionalInformation')->willReturn([]);
  183. $paymentMock->expects($this->once())->method('setTransactionId')->willReturnSelf();
  184. $paymentMock->expects($this->once())->method('setNotificationResult')->willReturnSelf();
  185. $paymentMock->expects($this->once())->method('setIsTransactionClosed')->willReturnSelf();
  186. $paymentMock->expects($this->once())->method('deny')->with(false)->willReturnSelf();
  187. $this->_orderMock->expects($this->exactly(4))->method('getPayment')->will($this->returnValue($paymentMock));
  188. $this->_paypalInfo->expects($this->once())
  189. ->method('importToPayment')
  190. ->with(['payment_status' => 'denied'], $paymentMock);
  191. $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  192. $this->_ipn = $objectHelper->getObject(
  193. \Magento\Paypal\Model\Ipn::class,
  194. [
  195. 'configFactory' => $this->configFactory,
  196. 'curlFactory' => $this->curlFactory,
  197. 'orderFactory' => $this->_orderMock,
  198. 'paypalInfo' => $this->_paypalInfo,
  199. 'data' => [
  200. 'payment_status' => 'Denied',
  201. ]
  202. ]
  203. );
  204. $this->_ipn->processIpnRequest();
  205. }
  206. }