<commit_before><?php
/**
 *
 * Adyen Payment Module
 *
 * Copyright (c) 2022 Adyen N.V.
 * This file is open source and available under the MIT license.
 * See the LICENSE file for more info.
 *
 * Author: Adyen <<EMAIL>>
 */

namespace Adyen\Payment\Helper\Webhook;


use Adyen\Payment\Helper\AdyenOrderPayment;
use Adyen\Payment\Helper\CaseManagement;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\Invoice;
use Adyen\Payment\Helper\Order as OrderHelper;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\Notification;
use Adyen\Webhook\PaymentStates;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Sales\Model\Order;

class AuthorisationWebhookHandler implements WebhookHandlerInterface
{
    /** @var AdyenOrderPayment */
    private $adyenOrderPaymentHelper;

    /** @var OrderHelper */
    private $orderHelper;

    /** @var CaseManagement */
    private $caseManagementHelper;

    /** @var SerializerInterface */
    private $serializer;

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

    /** @var ChargedCurrency */
    private $chargedCurrency;

    /** @var Config */
    private $configHelper;

    /** @var Invoice */
    private $invoiceHelper;

    /** @var PaymentMethods */
    private $paymentMethodsHelper;

    public function __construct(
        AdyenOrderPayment $adyenOrderPayment,
        OrderHelper $orderHelper,
        CaseManagement $caseManagementHelper,
        SerializerInterface $serializer,
        AdyenLogger $adyenLogger,
        ChargedCurrency $chargedCurrency,
        Config $configHelper,
        Invoice $invoiceHelper,
        PaymentMethods $paymentMethodsHelper
    )
    {
        $this->adyenOrderPaymentHelper = $adyenOrderPayment;
        $this->orderHelper = $orderHelper;
        $this->caseManagementHelper = $caseManagementHelper;
        $this->serializer = $serializer;
        $this->adyenLogger = $adyenLogger;
        $this->chargedCurrency = $chargedCurrency;
        $this->configHelper = $configHelper;
        $this->invoiceHelper = $invoiceHelper;
        $this->paymentMethodsHelper = $paymentMethodsHelper;
    }

    /**
     * @param Order $order
     * @param Notification $notification
     * @param string $transitionState
     * @return Order
     * @throws LocalizedException
     */
    public function handleWebhook(Order $order, Notification $notification, string $transitionState): Order
    {
        if ($transitionState === PaymentStates::STATE_PAID) {
            $order = $this->handleSuccessfulAuthorisation($order, $notification);
        } elseif ($transitionState === PaymentStates::STATE_FAILED) {
            $order = $this->handleFailedAuthorisation($order, $notification);
        }

        return $order;
    }

    /**
     * @param Order $order
     * @param Notification $notification
     * @return Order
     * @throws LocalizedException
     */
    private function handleSuccessfulAuthorisation(Order $order, Notification $notification): Order
    {
        $isAutoCapture = $this->paymentMethodsHelper->isAutoCapture($order, $notification->getPaymentMethod());

        // Set adyen_notification_payment_captured to true so that we ignore a possible OFFER_CLOSED
        if ($notification->isSuccessful() && $isAutoCapture) {
            $order->setData('adyen_notification_payment_captured', 1);
        }

        $this->adyenOrderPaymentHelper->createAdyenOrderPayment($order, $notification, $isAutoCapture);
        $isFullAmountAuthorized = $this->adyenOrderPaymentHelper->isFullAmountAuthorized($order);

        if ($isFullAmountAuthorized) {
            $order = $this->orderHelper->setPrePaymentAuthorized($order);
            $this->orderHelper->updatePaymentDetails($order, $notification);

            $additionalData = !empty($notification->getAdditionalData()) ? $this->serializer->unserialize($notification->getAdditionalData()) : "";
            $requireFraudManualReview = $this->caseManagementHelper->requiresManualReview($additionalData);

            if ($isAutoCapture) {
                $order = $this->handleAutoCapture($order, $notification, $requireFraudManualReview);
            } else {
                $order = $this->handleManualCapture($order, $notification, $requireFraudManualReview);
            }

            // For Boleto confirmation mail is sent on order creation
            // Send order confirmation mail after invoice creation so merchant can add invoicePDF to this mail
            if ($notification->getPaymentMethod() != "adyen_boleto" && !$order->getEmailSent()) {
                $this->orderHelper->sendOrderMail($order);
            }
        } else {
            $this->orderHelper->addWebhookStatusHistoryComment($order, $notification);
        }

        // Set authorized amount in sales_order_payment
        $orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($order, false);
        $orderAmount = $orderAmountCurrency->getAmount();
        $order->getPayment()->setAmountAuthorized($orderAmount);

        if ($notification->getPaymentMethod() == "c_cash" &&
            $this->configHelper->getConfigData('create_shipment', 'adyen_cash', $order->getStoreId())
        ) {<commit_msg>Fix typing issue as requiresManualReview expects an array (#1635)<commit_after><?php
/**
 *
 * Adyen Payment Module
 *
 * Copyright (c) 2022 <NAME>.
 * This file is open source and available under the MIT license.
 * See the LICENSE file for more info.
 *
 * Author: Adyen <<EMAIL>>
 */

namespace Adyen\Payment\Helper\Webhook;


use Adyen\Payment\Helper\AdyenOrderPayment;
use Adyen\Payment\Helper\CaseManagement;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\Invoice;
use Adyen\Payment\Helper\Order as OrderHelper;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\Notification;
use Adyen\Webhook\PaymentStates;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Sales\Model\Order;

class AuthorisationWebhookHandler implements WebhookHandlerInterface
{
    /** @var AdyenOrderPayment */
    private $adyenOrderPaymentHelper;

    /** @var OrderHelper */
    private $orderHelper;

    /** @var CaseManagement */
    private $caseManagementHelper;

    /** @var SerializerInterface */
    private $serializer;

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

    /** @var ChargedCurrency */
    private $chargedCurrency;

    /** @var Config */
    private $configHelper;

    /** @var Invoice */
    private $invoiceHelper;

    /** @var PaymentMethods */
    private $paymentMethodsHelper;

    public function __construct(
        AdyenOrderPayment $adyenOrderPayment,
        OrderHelper $orderHelper,
        CaseManagement $caseManagementHelper,
        SerializerInterface $serializer,
        AdyenLogger $adyenLogger,
        ChargedCurrency $chargedCurrency,
        Config $configHelper,
        Invoice $invoiceHelper,
        PaymentMethods $paymentMethodsHelper
    )
    {
        $this->adyenOrderPaymentHelper = $adyenOrderPayment;
        $this->orderHelper = $orderHelper;
        $this->caseManagementHelper = $caseManagementHelper;
        $this->serializer = $serializer;
        $this->adyenLogger = $adyenLogger;
        $this->chargedCurrency = $chargedCurrency;
        $this->configHelper = $configHelper;
        $this->invoiceHelper = $invoiceHelper;
        $this->paymentMethodsHelper = $paymentMethodsHelper;
    }

    /**
     * @param Order $order
     * @param Notification $notification
     * @param string $transitionState
     * @return Order
     * @throws LocalizedException
     */
    public function handleWebhook(Order $order, Notification $notification, string $transitionState): Order
    {
        if ($transitionState === PaymentStates::STATE_PAID) {
            $order = $this->handleSuccessfulAuthorisation($order, $notification);
        } elseif ($transitionState === PaymentStates::STATE_FAILED) {
            $order = $this->handleFailedAuthorisation($order, $notification);
        }

        return $order;
    }

    /**
     * @param Order $order
     * @param Notification $notification
     * @return Order
     * @throws LocalizedException
     */
    private function handleSuccessfulAuthorisation(Order $order, Notification $notification): Order
    {
        $isAutoCapture = $this->paymentMethodsHelper->isAutoCapture($order, $notification->getPaymentMethod());

        // Set adyen_notification_payment_captured to true so that we ignore a possible OFFER_CLOSED
        if ($notification->isSuccessful() && $isAutoCapture) {
            $order->setData('adyen_notification_payment_captured', 1);
        }

        $this->adyenOrderPaymentHelper->createAdyenOrderPayment($order, $notification, $isAutoCapture);
        $isFullAmountAuthorized = $this->adyenOrderPaymentHelper->isFullAmountAuthorized($order);

        if ($isFullAmountAuthorized) {
            $order = $this->orderHelper->setPrePaymentAuthorized($order);
            $this->orderHelper->updatePaymentDetails($order, $notification);

            $additionalData = !empty($notification->getAdditionalData()) ? $this->serializer->unserialize($notification->getAdditionalData()) : [];
            $requireFraudManualReview = $this->caseManagementHelper->requiresManualReview($additionalData);

            if ($isAutoCapture) {
                $order = $this->handleAutoCapture($order, $notification, $requireFraudManualReview);
            } else {
                $order = $this->handleManualCapture($order, $notification, $requireFraudManualReview);
            }

            // For Boleto confirmation mail is sent on order creation
            // Send order confirmation mail after invoice creation so merchant can add invoicePDF to this mail
            if ($notification->getPaymentMethod() != "adyen_boleto" && !$order->getEmailSent()) {
                $this->orderHelper->sendOrderMail($order);
            }
        } else {
            $this->orderHelper->addWebhookStatusHistoryComment($order, $notification);
        }

        // Set authorized amount in sales_order_payment
        $orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($order, false);
        $orderAmount = $orderAmountCurrency->getAmount();
        $order->getPayment()->setAmountAuthorized($orderAmount);

        if ($notification->getPaymentMethod() == "c_cash" &&
            $this->configHelper->getConfigData('create_shipment', 'adyen_cash', $order->getStoreId())
        ) {
{'fraction_non_alphanumeric': 0.1120312645389411, 'fraction_numerical': 0.0013026891225458267, 'mean_word_length': 3.2871958516154764, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2265146', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2835, 'n_words': 581}