You are here

public function IPNHandler::process in Commerce PayPal 8

Processes an incoming IPN request.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

mixed The request data array.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException

Overrides IPNHandlerInterface::process

File

src/IPNHandler.php, line 54

Class

IPNHandler

Namespace

Drupal\commerce_paypal

Code

public function process(Request $request) {

  // Get IPN request data.
  $ipn_data = $this
    ->getRequestDataArray($request
    ->getContent());

  // Exit now if the $_POST was empty.
  if (empty($ipn_data)) {
    $this->logger
      ->warning('IPN URL accessed with no POST data submitted.');
    throw new BadRequestHttpException('IPN URL accessed with no POST data submitted.');
  }

  // Make PayPal request for IPN validation.
  $url = $this
    ->getIpnValidationUrl($ipn_data);
  $validate_ipn = 'cmd=_notify-validate&' . $request
    ->getContent();
  try {
    $request = $this->httpClient
      ->post($url, [
      'body' => $validate_ipn,
    ])
      ->getBody();
    $paypal_response = $this
      ->getRequestDataArray($request
      ->getContents());
  } catch (ClientException $exception) {
  }

  // If the IPN was invalid, log a message and exit.
  if (!isset($paypal_response) || isset($paypal_response['INVALID'])) {
    $this->logger
      ->alert('Invalid IPN received and ignored.');
    throw new BadRequestHttpException('Invalid IPN received and ignored.');
  }
  return $ipn_data;
}