Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractIpn.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Paypal\Model;
8 
10 
12 {
16  protected $_config;
17 
23  protected $_ipnRequest;
24 
30  protected $_debugData = [];
31 
35  protected $_configFactory;
36 
40  protected $_curlFactory;
41 
48  public function __construct(
49  \Magento\Paypal\Model\ConfigFactory $configFactory,
50  \Psr\Log\LoggerInterface $logger,
51  \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
52  array $data = []
53  ) {
54  $this->_configFactory = $configFactory;
55  $this->logger = $logger;
56  $this->_curlFactory = $curlFactory;
57  $this->_ipnRequest = $data;
58  }
59 
66  public function getRequestData($key = null)
67  {
68  if (null === $key) {
69  return $this->_ipnRequest;
70  }
71  return isset($this->_ipnRequest[$key]) ? $this->_ipnRequest[$key] : null;
72  }
73 
81  protected function _postBack()
82  {
83  $httpAdapter = $this->_curlFactory->create();
84  $postbackQuery = http_build_query($this->getRequestData()) . '&cmd=_notify-validate';
85  $postbackUrl = $this->_config->getPayPalIpnUrl();
86  $this->_addDebugData('postback_to', $postbackUrl);
87 
88  $httpAdapter->setConfig(['verifypeer' => $this->_config->getValue('verifyPeer')]);
89  $httpAdapter->write(\Zend_Http_Client::POST, $postbackUrl, '1.1', ['Connection: close'], $postbackQuery);
90  try {
91  $postbackResult = $httpAdapter->read();
92  } catch (\Exception $e) {
93  $this->_addDebugData('http_error', ['error' => $e->getMessage(), 'code' => $e->getCode()]);
94  throw $e;
95  }
96 
97  /*
98  * Handle errors on PayPal side.
99  */
100  $responseCode = \Zend_Http_Response::extractCode($postbackResult);
101  if (empty($postbackResult) || in_array($responseCode, ['500', '502', '503'])) {
102  if (empty($postbackResult)) {
103  $reason = 'Empty response.';
104  } else {
105  $reason = 'Response code: ' . $responseCode . '.';
106  }
107  $this->_debugData['exception'] = 'PayPal IPN postback failure. ' . $reason;
108  throw new RemoteServiceUnavailableException(__($reason));
109  }
110 
111  $response = preg_split('/^\r?$/m', $postbackResult, 2);
112  $response = trim($response[1]);
113  if ($response != 'VERIFIED') {
114  $this->_addDebugData('postback', $postbackQuery);
115  $this->_addDebugData('postback_result', $postbackResult);
116  throw new \Exception('PayPal IPN postback failure. See system.log for details.');
117  }
118  }
119 
127  protected function _filterPaymentStatus($ipnPaymentStatus)
128  {
129  switch ($ipnPaymentStatus) {
130  case 'Created':
131  // break is intentionally omitted
132  case 'Completed':
134  case 'Denied':
136  case 'Expired':
138  case 'Failed':
140  case 'Pending':
142  case 'Refunded':
144  case 'Reversed':
146  case 'Canceled_Reversal':
148  case 'Processed':
150  case 'Voided':
152  default:
153  return '';
154  }
155  // documented in NVP, but not documented in IPN:
156  //Info::PAYMENTSTATUS_NONE
157  //Info::PAYMENTSTATUS_INPROGRESS
158  //Info::PAYMENTSTATUS_REFUNDEDPART
159  }
160 
166  protected function _debug()
167  {
168  if ($this->_config && $this->_config->getValue('debug')) {
169  $this->logger->debug(var_export($this->_debugData, true));
170  }
171  }
172 
178  protected function _addDebugData($key, $value)
179  {
180  $this->_debugData[$key] = $value;
181  return $this;
182  }
183 }
const PAYMENTSTATUS_COMPLETED
Definition: Info.php:117
$response
Definition: 404.php:11
const PAYMENTSTATUS_PROCESSED
Definition: Info.php:137
__()
Definition: __.php:13
$logger
_filterPaymentStatus($ipnPaymentStatus)
$value
Definition: gender.phtml:16
const PAYMENTSTATUS_UNREVERSED
Definition: Info.php:135
__construct(\Magento\Paypal\Model\ConfigFactory $configFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory, array $data=[])
Definition: AbstractIpn.php:48
$configFactory
Definition: config_data.php:43
const PAYMENTSTATUS_REFUNDED
Definition: Info.php:129
const PAYMENTSTATUS_REVERSED
Definition: Info.php:133
static extractCode($response_str)
Definition: Response.php:449