<commit_before><?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2019 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <<EMAIL>>
 */

namespace Adyen\Payment\Model;

use Adyen\Payment\Api\AdyenThreeDS2ProcessInterface;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Helper\Vault;
use Adyen\Payment\Logger\AdyenLogger;
use Magento\Checkout\Model\Session;
use Magento\Sales\Model\OrderFactory;

class AdyenThreeDS2Process implements AdyenThreeDS2ProcessInterface
{
    /**
     * @var Session
     */
    private $checkoutSession;

    /**
     * @var Data
     */
    private $adyenHelper;

    /**
     * @var OrderFactory
     */
    private $orderFactory;

    /**
     * @var AdyenLogger
     */
    private $adyenLogger;

    /**
     * @var Vault
     */
    private $vaultHelper;

    /**
     * AdyenThreeDS2Process constructor.
     *
     * @param Session $checkoutSession
     * @param Data $adyenHelper
     * @param OrderFactory $orderFactory
     * @param AdyenLogger $adyenLogger
     * @param Vault $vaultHelper
     */
    public function __construct(
        Session $checkoutSession,
        Data $adyenHelper,
        OrderFactory $orderFactory,
        AdyenLogger $adyenLogger,
        Vault $vaultHelper
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->adyenHelper = $adyenHelper;
        $this->orderFactory = $orderFactory;
        $this->adyenLogger = $adyenLogger;
        $this->vaultHelper = $vaultHelper;
    }

    /**
     * @param string $payload
     * @return string
     * @api
     */
    public function initiate($payload)
    {
        // Decode payload from frontend
        $payload = json_decode($payload, true);

        // Validate JSON that has just been parsed if it was in a valid format
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('3D secure 2.0 failed because the request was not a valid JSON')
            );
        }

        if (empty($payload['orderId'])) {
            $order = $this->getOrder();
            // In the next major release remove support for retrieving order from session and throw exception instead
            //throw new \Magento\Framework\Exception\LocalizedException
            //(__('3D secure 2.0 failed because of a missing order id'));
        } else {
            // Create order by order id
            $order = $this->orderFactory->create()->load($payload['orderId']);
            // don't send orderId to adyen. Improve that orderId and state.data are separated in payload
            unset($payload['orderId']);
        }

        $payment = $order->getPayment();

        // Init payments/details request
        $result = [];

        if ($paymentData = $payment->getAdditionalInformation("adyenPaymentData")) {
            // Add payment data into the request object
            $request = [
                "paymentData" => $paymentData
            ];

            // unset payment data from additional information
            $payment->unsAdditionalInformation("adyenPaymentData");
        } else {
            $this->adyenLogger->error("3D secure 2.0 failed, payment data not found");
            throw new \Magento\Framework\Exception\LocalizedException(
                __('3D secure 2.0 failed, payment data not found')
            );
        }

        // Depends on the component's response we send a fingerprint or the challenge result
        if (!empty($payload['details']['threeds2.fingerprint'])) {
            $request['details']['threeds2.fingerprint'] = $payload['details']['threeds2.fingerprint'];
        } elseif (!empty($payload['details']['threeds2.challengeResult'])) {
            $request['details']['threeds2.challengeResult'] = $payload['details']['threeds2.challengeResult'];
        } elseif (!empty($payload)) {
            $request = $payload;
        }


        // Send the request
        try {
            $client = $this->adyenHelper->initializeAdyenClient($order->getStoreId());
            $service = $this->adyenHelper->createAdyenCheckoutService($client);

            $result = $service->paymentsDetails($request);
        } catch (\Adyen\AdyenException $e) {
            $this->adyenLogger->error("3D secure 2.0 failed" . $e->getMessage());
            throw new \Magento\Framework\Exception\LocalizedException(__('3D secure 2.0 failed'));
        }

        // Check if result is challenge shopper, if yes return the token
        if (!empty($result['resultCode']) &&
            $result['resultCode'] === 'ChallengeShopper' &&
            !empty($result['authentication']['threeds2.challengeToken'])
        ) {
            return $this->adyenHelper->buildThreeDS2ProcessResponseJson(
                $result['resultCode'],
                $result['authentication']['threeds2.challengeToken']
            );
        }
        //Fallback for 3DS in case of redirect
        if (!empty($result['resultCode']) &&
            $result['resultCode'] === 'RedirectShopper'
        ) {
            $response['type'] =  $result['resultCode'];
            $response['action']= $result['action'];

            return json_encode($response);
        }

        // Save the payments response because we are going to need it during the place order flow
        $payment->setAdditionalInformation("paymentsResponse", $result);

        $this->vaultHelper->saveRecurringDetails($payment, $result['additionalData']);

        // To actually save the additional info changes into the quote
        $order->save();

        $response = [];

        if ($result['resultCode'] != 'Authorised') {
            $this->checkoutSession->restoreQuote();

            // Always cancel the order if the paymenth has failed
            if (!$order->canCancel()) {
                $order->setState(\Magento\Sales\Model\Order::STATE_NEW);
            }

            $order->cancel()->save();

            $this->adyenLogger->error(
                sprintf("Payment details call failed for action or 3ds2 payment method, resultcode is %s Raw API responds: %s",
                    $result['resultCode'],
                    print_r($result, true)
                ));

            throw new \Magento\Framework\Exception\LocalizedException(__('The payment is REFUSED.'));
        }
<commit_msg>Check additionalData key in result before using (#833)<commit_after><?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2019 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <<EMAIL>>
 */

namespace Adyen\Payment\Model;

use Adyen\Payment\Api\AdyenThreeDS2ProcessInterface;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Helper\Vault;
use Adyen\Payment\Logger\AdyenLogger;
use Magento\Checkout\Model\Session;
use Magento\Sales\Model\OrderFactory;

class AdyenThreeDS2Process implements AdyenThreeDS2ProcessInterface
{
    /**
     * @var Session
     */
    private $checkoutSession;

    /**
     * @var Data
     */
    private $adyenHelper;

    /**
     * @var OrderFactory
     */
    private $orderFactory;

    /**
     * @var AdyenLogger
     */
    private $adyenLogger;

    /**
     * @var Vault
     */
    private $vaultHelper;

    /**
     * AdyenThreeDS2Process constructor.
     *
     * @param Session $checkoutSession
     * @param Data $adyenHelper
     * @param OrderFactory $orderFactory
     * @param AdyenLogger $adyenLogger
     * @param Vault $vaultHelper
     */
    public function __construct(
        Session $checkoutSession,
        Data $adyenHelper,
        OrderFactory $orderFactory,
        AdyenLogger $adyenLogger,
        Vault $vaultHelper
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->adyenHelper = $adyenHelper;
        $this->orderFactory = $orderFactory;
        $this->adyenLogger = $adyenLogger;
        $this->vaultHelper = $vaultHelper;
    }

    /**
     * @param string $payload
     * @return string
     * @api
     */
    public function initiate($payload)
    {
        // Decode payload from frontend
        $payload = json_decode($payload, true);

        // Validate JSON that has just been parsed if it was in a valid format
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('3D secure 2.0 failed because the request was not a valid JSON')
            );
        }

        if (empty($payload['orderId'])) {
            $order = $this->getOrder();
            // In the next major release remove support for retrieving order from session and throw exception instead
            //throw new \Magento\Framework\Exception\LocalizedException
            //(__('3D secure 2.0 failed because of a missing order id'));
        } else {
            // Create order by order id
            $order = $this->orderFactory->create()->load($payload['orderId']);
            // don't send orderId to adyen. Improve that orderId and state.data are separated in payload
            unset($payload['orderId']);
        }

        $payment = $order->getPayment();

        // Init payments/details request
        $result = [];

        if ($paymentData = $payment->getAdditionalInformation("adyenPaymentData")) {
            // Add payment data into the request object
            $request = [
                "paymentData" => $paymentData
            ];

            // unset payment data from additional information
            $payment->unsAdditionalInformation("adyenPaymentData");
        } else {
            $this->adyenLogger->error("3D secure 2.0 failed, payment data not found");
            throw new \Magento\Framework\Exception\LocalizedException(
                __('3D secure 2.0 failed, payment data not found')
            );
        }

        // Depends on the component's response we send a fingerprint or the challenge result
        if (!empty($payload['details']['threeds2.fingerprint'])) {
            $request['details']['threeds2.fingerprint'] = $payload['details']['threeds2.fingerprint'];
        } elseif (!empty($payload['details']['threeds2.challengeResult'])) {
            $request['details']['threeds2.challengeResult'] = $payload['details']['threeds2.challengeResult'];
        } elseif (!empty($payload)) {
            $request = $payload;
        }


        // Send the request
        try {
            $client = $this->adyenHelper->initializeAdyenClient($order->getStoreId());
            $service = $this->adyenHelper->createAdyenCheckoutService($client);

            $result = $service->paymentsDetails($request);
        } catch (\Adyen\AdyenException $e) {
            $this->adyenLogger->error("3D secure 2.0 failed" . $e->getMessage());
            throw new \Magento\Framework\Exception\LocalizedException(__('3D secure 2.0 failed'));
        }

        // Check if result is challenge shopper, if yes return the token
        if (!empty($result['resultCode']) &&
            $result['resultCode'] === 'ChallengeShopper' &&
            !empty($result['authentication']['threeds2.challengeToken'])
        ) {
            return $this->adyenHelper->buildThreeDS2ProcessResponseJson(
                $result['resultCode'],
                $result['authentication']['threeds2.challengeToken']
            );
        }
        //Fallback for 3DS in case of redirect
        if (!empty($result['resultCode']) &&
            $result['resultCode'] === 'RedirectShopper'
        ) {
            $response['type'] =  $result['resultCode'];
            $response['action']= $result['action'];

            return json_encode($response);
        }

        // Save the payments response because we are going to need it during the place order flow
        $payment->setAdditionalInformation("paymentsResponse", $result);

        if (!empty($result['additionalData'])) {
            $this->vaultHelper->saveRecurringDetails($payment, $result['additionalData']);
        }

        // To actually save the additional info changes into the quote
        $order->save();

        $response = [];

        if ($result['resultCode'] != 'Authorised') {
            $this->checkoutSession->restoreQuote();

            // Always cancel the order if the paymenth has failed
            if (!$order->canCancel()) {
                $order->setState(\Magento\Sales\Model\Order::STATE_NEW);
            }

            $order->cancel()->save();

            $this->adyenLogger->error(
                sprintf("Payment details call failed for action or 3ds2 payment method, resultcode is %s Raw API responds: %s",
                    $result['resultCode'],
                    print_r($result, true)
                ));

            throw new \Magento\Framework\Exception\LocalizedException(__('The payment is REFUSED.'));
        }

{'fraction_non_alphanumeric': 0.17680211522404676, 'fraction_numerical': 0.005496799332034511, 'mean_word_length': 2.1672542970471573, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 83, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '99011', 'n_tokens_mistral': 3876, 'n_tokens_neox': 3744, 'n_words': 827}