@internal

@param \Psr\Http\Message\ResponseInterface $response
@return null|string notification_key
326914
private function getNotificationToken(ResponseInterface $response)
    {
        if (! $this->isValidResponse($response)) {
            return null;
        }

        $json = json_decode($response->getBody()->getContents(), true);

        return $json['notification_key'];
    }
379207
protected function stringifyBody(ResponseInterface $response): string
    {
        $body = $response->getBody();
        if ($body === null) {
            return '';
        }

        return json_encode($body, JSON_PARTIAL_OUTPUT_ON_ERROR);
    }
1322969
protected function _httpAuthenticate(Varien_Object $response)
    {
        $result = array(
            'authentication' => false,
            'message' => ''
        );

        $this->fixCgiHttpAuthentication(); //add cgi support
        $internalMerchantAccount = $this->_getConfigData('merchantAccount');
        $username = $this->_getConfigData('notification_username');
        $password = Mage::helper('core')->decrypt($this->_getConfigData('notification_password'));
        $submitedMerchantAccount = $response->getData('merchantAccountCode');

        if (empty($submitedMerchantAccount) && empty($internalMerchantAccount)) {
            if (strtolower(
                substr(
                    $response->getData('pspReference'), 0,
                    17
                )
            ) == "testnotification_" || strtolower(
                substr(
                    $response->getData('pspReference'), 0,
                    5
                )
            ) == "test_") {
                Mage::log(
                    'Notification test failed: merchantAccountCode is empty in magento settings', Zend_Log::DEBUG,
                    "adyen_notification.log", true
                );
                $result['message'] = 'merchantAccountCode is empty in magento settings';
            }

            return $result;
        }

        // validate username and password
        if ((!isset($_SERVER['PHP_AUTH_USER']) && !isset($_SERVER['PHP_AUTH_PW']))) {
            if (strtolower(
                substr(
                    $response->getData('pspReference'), 0,
                    17
                )
            ) == "testnotification_" || strtolower(
                substr(
                    $response->getData('pspReference'), 0,
                    5
                )
            ) == "test_") {
                Mage::log(
                    'Authentication failed: PHP_AUTH_USER and PHP_AUTH_PW are empty. See Adyen Magento manual CGI mode',
                    Zend_Log::DEBUG, "adyen_notification.log", true
                );
                $result['message'] = 'Authentication failed: PHP_AUTH_USER and PHP_AUTH_PW are empty. See Adyen Magento manual CGI mode';
            }

            return $result;
        }

        $accountCmp = !$this->_getConfigData('multiple_merchants')
            ? strcmp($submitedMerchantAccount, $internalMerchantAccount)
            : 0;
        $usernameCmp = strcmp($_SERVER['PHP_AUTH_USER'], $username);
        $passwordCmp = strcmp($_SERVER['PHP_AUTH_PW'], $password);
        if ($accountCmp === 0 && $usernameCmp === 0 && $passwordCmp === 0) {
            $result['authentication'] = true;
            return $result;
        }

        // If notification is test check if fields are correct if not return error
        if (strtolower(
            substr(
                $response->getData('pspReference'), 0,
                17
            )
        ) == "testnotification_" || strtolower(
            substr(
                $response->getData('pspReference'), 0,
                5
            )
        ) == "test_") {
            if ($accountCmp != 0) {
                $result['message'] = "MerchantAccount in notification is not the same as in Magento settings and Allow multiple merchants option is not enabled";
            } elseif ($usernameCmp != 0 || $passwordCmp != 0) {
                $result['message'] = "username (PHP_AUTH_USER) and/or password (PHP_AUTH_PW) are not the same as Magento settings";
            }

            Mage::log($result['message'], Zend_Log::DEBUG, "adyen_notification.log", true);
        }

        return $result;
    }
1337846
public function processResponse(ResponseInterface $response)
    {

        $body = $response->getBody();

        $object = json_decode($body);
        if (json_last_error() === JSON_ERROR_NONE) {
            // JSON is valid
            if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) {
                $return = $object->_embedded->items;
            } elseif (property_exists($object, 'error')) {
                if (is_array($object->message)) {
                    foreach ($object->message as $message) {
                        $output .= $message;
                    }
                    throw new \Exception($output);
                } else {
                    throw new \Exception($object->message);
                }
            } else {
                $return = $object;
            }
        } else {
            $return = $body;
        }

        return $return;
    }
1267278
public static function getSigningPublicKeyFromResponse(string $response) : string
    {
        $version = static::getVersion($response);
        if ($version === 2) {
            if (\strlen($response) < 236) {
                throw new InvalidArgumentException;
            }

            return \substr($response, -160, 32);
        }

        throw new InvalidArgumentException('The response provided is not suitable for public key extraction.');
    }
1217582
public static function getResponseBodySummary(ResponseInterface $response)
    {
        $body = $response->getBody();

        if (!$body->isSeekable() || !$body->isReadable()) {
            return null;
        }

        $size = $body->getSize();

        if ($size === 0) {
            return null;
        }

        $summary = $body->read(120);
        $body->rewind();

        if ($size > 120) {
            $summary .= ' (truncated...)';
        }

        // Matches any printable character, including unicode characters:
        // letters, marks, numbers, punctuation, spacing, and separators.
        if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/', $summary)) {
            return null;
        }

        return $summary;
    }
15532
public function getResponse(array $response)
    {
        if (isset($response['error']['code'])) {
            $this->handleRpcErrors($response['error']);
        }

        return isset($response['result']) ? $response['result'] : null;
    }
560131
private function requiresTfa(ResponseInterface $response)
    {
        return substr($response->getStatusCode(), 0, 1) === '4' && $response->hasHeader(self::TFA_HEADER);
    }
828602
protected function getSamlAssertion($response) {
      $xml = new \DOMDocument();
      $xml->loadXML($response);
      $xpath = new \DOMXPath($xml);

      if ($xpath->query("//*[name()='saml:Assertion']")->length > 0) {
        $nodeToken = $xpath->query("//*[name()='saml:Assertion']");
        if (!empty($nodeToken)) {
          return $nodeToken;
        }
      }
      return NULL;
    }
951228
public function execute(ResponseInterface $response): ResponseInterface
    {
        if ($response->getStatusCode() === self::STATUS_CODE_OK) {
            if (is_null($this->publicKeyServer)) {
                // No installation yet.
                return $response;
            } else {
                $toVerify =
                    $response->getStatusCode() .
                    self::NEWLINE .
                    $this->determineHeaderStringForSignedResponse($response->getHeaders()) .
                    self::NEWLINE . self::NEWLINE .
                    $response->getBody()->getContents();

                $signature = base64_decode($response->getHeaderLine(self::HEADER_SERVER_SIGNATURE));
                $publicKey = $this->publicKeyServer->getKey();

                $signatureResult = openssl_verify($toVerify, $signature, $publicKey, OPENSSL_ALGO_SHA256);

                if ($signatureResult === self::RESULT_SIGNATURE_CORRECT) {
                    return $response;
                } else {
                    throw new SecurityException(self::ERROR_VERIFYING_RESPONSE_FAILED);
                }
            }
        } else {
            return $response;
        }
    }
137337
private function decodeResponse(ResponseInterface $response): ResponseInterface
    {
        $response = $this->decodeOnEncodingHeader('Transfer-Encoding', $response);

        if ($this->useContentEncoding) {
            $response = $this->decodeOnEncodingHeader('Content-Encoding', $response);
        }

        return $response;
    }
1115719
private function parseResponse(ResponseInterface $response = null): array
    {
        $body = $response
            ? (string) $response->getBody()
            : null;

        return $body
            ? guzzle_json_decode($body, true)
            : [];
    }
793822
public function onReceive(RequestInterface $request,
                              ResponseInterface $response)
    {
        $this->coockieJar->extractCookies($request, $response);
        return $response;
    }
1130799
protected function hydrateResponse(ResponseInterface $response, string $class)
    {
        if (null === $this->hydrator) {
            return $response;
        }

        if (200 !== $response->getStatusCode() && 201 !== $response->getStatusCode()) {
            $this->handleErrors($response);
        }

        return $this->hydrator->hydrate($response, $class);
    }
1337715
public static function getMaxAge(ResponseInterface $response)
    {
        $smaxage = Utils::getDirective($response, 's-maxage');
        if (is_numeric($smaxage)) {
            return (int) $smaxage;
        }

        $maxage = Utils::getDirective($response, 'max-age');
        if (is_numeric($maxage)) {
            return (int) $maxage;
        }

        if ($response->hasHeader('Expires')) {
            return strtotime($response->getHeader('Expires')) - time();
        }

        return null;
    }
495605
private function getTtl(ResponseInterface $response)
    {
        $ttl = 0;

        if ($cacheControl = $response->getHeader('Cache-Control')) {
            $maxAge = Utils::getDirective($response, 'max-age');
            if (is_numeric($maxAge)) {
                $ttl += $maxAge;
            }

            // According to RFC5861 stale headers are *in addition* to any
            // max-age values.
            $stale = Utils::getDirective($response, 'stale-if-error');
            if (is_numeric($stale)) {
                $ttl += $stale;
            }
        } elseif ($expires = $response->getHeader('Expires')) {
            $ttl += strtotime($expires) - time();
        }

        return $ttl ?: $this->defaultTtl;
    }
1074326
public function send(RequestInterface $request, MessageInterface $response)
    {
        $cacheKey = $this->generateCacheKeyForRequest($request);

        if ($this->cache->contains($cacheKey)) {
            $this->hits++;

            $cachedResponse = unserialize($this->cache->fetch($cacheKey));
            /* @var $cachedResponse \Buzz\Message\Response */

            $response->setContent($cachedResponse->getContent());
            $response->setHeaders($cachedResponse->getHeaders());

            return;
        }

        $this->misses++;

        $this->client->send($request, $response);

        $this->cache->save($cacheKey, serialize($response), $this->lifetime);
    }