<commit_before><?php

/**
 * Checkout.com
 * Authorized and regulated as an electronic money institution
 * by the UK Financial Conduct Authority (FCA) under number 900816.
 *
 * PHP version 7
 *
 * @category  Magento2
 * @package   Checkout.com
 * @author    Platforms Development Team <<EMAIL>>
 * @copyright 2010-present Checkout.com
 * @license   https://opensource.org/licenses/mit-license.html MIT License
 * @link      https://docs.checkout.com/
 */

declare(strict_types=1);

namespace CheckoutCom\Magento2\Controller\Payment;

use Checkout\CheckoutApi;
use Checkout\Library\Exceptions\CheckoutHttpException;
use CheckoutCom\Magento2\Gateway\Config\Config;
use CheckoutCom\Magento2\Helper\Logger;
use CheckoutCom\Magento2\Model\Service\ApiHandlerService;
use CheckoutCom\Magento2\Model\Service\OrderHandlerService;
use CheckoutCom\Magento2\Model\Service\OrderStatusHandlerService;
use CheckoutCom\Magento2\Model\Service\PaymentErrorHandlerService;
use CheckoutCom\Magento2\Model\Service\QuoteHandlerService;
use CheckoutCom\Magento2\Model\Service\TransactionHandlerService;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Class Fail
 */
class Fail extends Action
{
    /**
     * $messageManager field
     *
     * @var ManagerInterface $messageManager
     */
    protected $messageManager;
    /**
     * $transactionHandler field
     *
     * @var TransactionHandlerService $transactionHandler
     */
    private $transactionHandler;
    /**
     * $storeManager field
     *
     * @var StoreManagerInterface $storeManager
     */
    private $storeManager;
    /**
     * $apiHandler field
     *
     * @var CheckoutApi $apiHandler
     */
    private $apiHandler;
    /**
     * $orderHandler field
     *
     * @var OrderHandlerService $orderHandler
     */
    private $orderHandler;
    /**
     * $orderStatusHandler field
     *
     * @var OrderStatusHandlerService $orderStatusHandler
     */
    private $orderStatusHandler;
    /**
     * $logger field
     *
     * @var Logger $logger
     */
    private $logger;
    /**
     * $paymentErrorHandlerService field
     *
     * @var PaymentErrorHandlerService $paymentErrorHandlerService
     */
    private $paymentErrorHandlerService;
    /**
     * $config field
     *
     * @var Config $config
     */
    private $config;
    /**
     * $session field
     *
     * @var Session $session
     */
    private $session;

    /**
     * Fail constructor
     *
     * @param Context                    $context
     * @param ManagerInterface           $messageManager
     * @param TransactionHandlerService  $transactionHandler
     * @param StoreManagerInterface      $storeManager
     * @param ApiHandlerService          $apiHandler
     * @param OrderHandlerService        $orderHandler
     * @param OrderStatusHandlerService  $orderStatusHandler
     * @param Logger                     $logger
     * @param PaymentErrorHandlerService $paymentErrorHandlerService
     * @param Config                     $config
     * @param Session                    $session
     */
    public function __construct(
        Context $context,
        ManagerInterface $messageManager,
        TransactionHandlerService $transactionHandler,
        StoreManagerInterface $storeManager,
        ApiHandlerService $apiHandler,
        OrderHandlerService $orderHandler,
        OrderStatusHandlerService $orderStatusHandler,
        Logger $logger,
        PaymentErrorHandlerService $paymentErrorHandlerService,
        Config $config,
        Session $session
    ) {
        parent::__construct($context);

        $this->messageManager             = $messageManager;
        $this->storeManager               = $storeManager;
        $this->apiHandler                 = $apiHandler;
        $this->orderHandler               = $orderHandler;
        $this->orderStatusHandler         = $orderStatusHandler;
        $this->logger                     = $logger;
        $this->paymentErrorHandlerService = $paymentErrorHandlerService;
        $this->config                     = $config;
        $this->session                    = $session;
        $this->transactionHandler         = $transactionHandler;
    }

    /**
     * Handles the controller method
     *
     * @return ResultInterface|ResponseInterface
     * @throws NoSuchEntityException|LocalizedException
     */
    public function execute(): ResponseInterface
    {
        try {
            // Get the session id
            $sessionId = $this->getRequest()->getParam('cko-session-id', null);
            if ($sessionId) {
                // Get the store code
                $storeCode = $this->storeManager->getStore()->getCode();

                // Initialize the API handler
                $api = $this->apiHandler->init($storeCode,ScopeInterface::SCOPE_STORE);

                // Get the payment details
                $response = $api->getPaymentDetails($sessionId);

                // Logging
                $this->logger->display($response);

                // Don't restore quote if saved card request
                if ($response->amount !== 0 && $response->amount !== 100) {
                    // Find the order from increment id
                    $order = $this->orderHandler->getOrder([
                        'increment_id' => $response->reference,
                    ]);

                    $storeCode = $this->storeManager->getStore()->getCode();
                    $action    = $this->config->getValue('order_action_failed_payment', null, $storeCode);
                    $status    = $action === 'cancel' ? 'canceled' : false;

                    // Log the payment error
                    $this->paymentErrorHandlerService->logPaymentError(
                        $response,
                        $order,
                        $status
                    );

                    // Restore the quote
                    $this->session->restoreQuote();

                    // Handle the failed order
                    $this->orderStatusHandler->handleFailedPayment($order);

                    $errorMessage = null;
                    if (isset($response->actions[0]['response_code'])) {
                        $errorMessage = $this->paymentErrorHandlerService->getErrorMessage(
                            $response->actions[0]['response_code']
                        );
                    }

                    if ($response->source['type'] === 'knet') {
                        $amount = $this->transactionHandler->amountFromGateway(<commit_msg>feature/CHECMAG2003-11-41 : Fix error on failed payment<commit_after><?php

/**
 * Checkout.com
 * Authorized and regulated as an electronic money institution
 * by the UK Financial Conduct Authority (FCA) under number 900816.
 *
 * PHP version 7
 *
 * @category  Magento2
 * @package   Checkout.com
 * @author    Platforms Development Team <<EMAIL>>
 * @copyright 2010-present Checkout.com
 * @license   https://opensource.org/licenses/mit-license.html MIT License
 * @link      https://docs.checkout.com/
 */

declare(strict_types=1);

namespace CheckoutCom\Magento2\Controller\Payment;

use Checkout\CheckoutApi;
use Checkout\Library\Exceptions\CheckoutHttpException;
use CheckoutCom\Magento2\Gateway\Config\Config;
use CheckoutCom\Magento2\Helper\Logger;
use CheckoutCom\Magento2\Model\Service\ApiHandlerService;
use CheckoutCom\Magento2\Model\Service\OrderHandlerService;
use CheckoutCom\Magento2\Model\Service\OrderStatusHandlerService;
use CheckoutCom\Magento2\Model\Service\PaymentErrorHandlerService;
use CheckoutCom\Magento2\Model\Service\QuoteHandlerService;
use CheckoutCom\Magento2\Model\Service\TransactionHandlerService;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Class Fail
 */
class Fail extends Action
{
    /**
     * $messageManager field
     *
     * @var ManagerInterface $messageManager
     */
    protected $messageManager;
    /**
     * $transactionHandler field
     *
     * @var TransactionHandlerService $transactionHandler
     */
    private $transactionHandler;
    /**
     * $storeManager field
     *
     * @var StoreManagerInterface $storeManager
     */
    private $storeManager;
    /**
     * $apiHandler field
     *
     * @var CheckoutApi $apiHandler
     */
    private $apiHandler;
    /**
     * $orderHandler field
     *
     * @var OrderHandlerService $orderHandler
     */
    private $orderHandler;
    /**
     * $orderStatusHandler field
     *
     * @var OrderStatusHandlerService $orderStatusHandler
     */
    private $orderStatusHandler;
    /**
     * $logger field
     *
     * @var Logger $logger
     */
    private $logger;
    /**
     * $paymentErrorHandlerService field
     *
     * @var PaymentErrorHandlerService $paymentErrorHandlerService
     */
    private $paymentErrorHandlerService;
    /**
     * $config field
     *
     * @var Config $config
     */
    private $config;
    /**
     * $session field
     *
     * @var Session $session
     */
    private $session;

    /**
     * Fail constructor
     *
     * @param Context                    $context
     * @param ManagerInterface           $messageManager
     * @param TransactionHandlerService  $transactionHandler
     * @param StoreManagerInterface      $storeManager
     * @param ApiHandlerService          $apiHandler
     * @param OrderHandlerService        $orderHandler
     * @param OrderStatusHandlerService  $orderStatusHandler
     * @param Logger                     $logger
     * @param PaymentErrorHandlerService $paymentErrorHandlerService
     * @param Config                     $config
     * @param Session                    $session
     */
    public function __construct(
        Context $context,
        ManagerInterface $messageManager,
        TransactionHandlerService $transactionHandler,
        StoreManagerInterface $storeManager,
        ApiHandlerService $apiHandler,
        OrderHandlerService $orderHandler,
        OrderStatusHandlerService $orderStatusHandler,
        Logger $logger,
        PaymentErrorHandlerService $paymentErrorHandlerService,
        Config $config,
        Session $session
    ) {
        parent::__construct($context);

        $this->messageManager             = $messageManager;
        $this->storeManager               = $storeManager;
        $this->apiHandler                 = $apiHandler;
        $this->orderHandler               = $orderHandler;
        $this->orderStatusHandler         = $orderStatusHandler;
        $this->logger                     = $logger;
        $this->paymentErrorHandlerService = $paymentErrorHandlerService;
        $this->config                     = $config;
        $this->session                    = $session;
        $this->transactionHandler         = $transactionHandler;
    }

    /**
     * Handles the controller method
     *
     * @return ResultInterface|ResponseInterface
     * @throws NoSuchEntityException|LocalizedException
     */
    public function execute(): ResponseInterface
    {
        try {
            // Get the session id
            $sessionId = $this->getRequest()->getParam('cko-session-id', null);
            if ($sessionId) {
                // Get the store code
                $storeCode = $this->storeManager->getStore()->getCode();

                // Initialize the API handler
                $api = $this->apiHandler->init($storeCode,ScopeInterface::SCOPE_STORE);

                // Get the payment details
                $response = $api->getPaymentDetails($sessionId);

                // Logging
                $this->logger->display($response);

                // Don't restore quote if saved card request
                if ($response->amount !== 0 && $response->amount !== 100) {
                    // Find the order from increment id
                    $order = $this->orderHandler->getOrder([
                        'increment_id' => $response->reference,
                    ]);

                    $storeCode = $this->storeManager->getStore()->getCode();
                    $action    = $this->config->getValue('order_action_failed_payment', null, $storeCode);
                    $status    = $action === 'cancel' ? 'canceled' : 'false';

                    // Log the payment error
                    $this->paymentErrorHandlerService->logPaymentError(
                        $response,
                        $order,
                        $status
                    );

                    // Restore the quote
                    $this->session->restoreQuote();

                    // Handle the failed order
                    $this->orderStatusHandler->handleFailedPayment($order);

                    $errorMessage = null;
                    if (isset($response->actions[0]['response_code'])) {
                        $errorMessage = $this->paymentErrorHandlerService->getErrorMessage(
                            $response->actions[0]['response_code']
                        );
                    }

                    if ($response->source['type'] === 'knet') {
                        $amount = $this->transactionHandler->amountFromGateway(
{'fraction_non_alphanumeric': 0.09939845316528216, 'fraction_numerical': 0.004583213978802635, 'mean_word_length': 2.0584756898817345, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 95, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1046241', 'n_tokens_mistral': 3519, 'n_tokens_neox': 3397, 'n_words': 694}