HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vm8 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: afleverb (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/drakkar_site/wp-content/plugins/woocommerce-mercadopago/src/Endpoints/CheckoutCustom.php
<?php

namespace MercadoPago\Woocommerce\Endpoints;

use Exception;
use MercadoPago\Woocommerce\Configs\Seller;
use MercadoPago\Woocommerce\Helpers\Requester;
use MercadoPago\Woocommerce\Helpers\Session;
use MercadoPago\Woocommerce\Hooks\Endpoints;
use MercadoPago\Woocommerce\Libraries\Logs\Logs;
use MercadoPago\Woocommerce\Translations\StoreTranslations;

if (!defined('ABSPATH')) {
    exit;
}

class CheckoutCustom
{
    public Endpoints $endpoints;

    public Logs $logs;

    public Requester $requester;

    public Session $session;

    public Seller $seller;

    private array $threeDsTranslations;

    /**
     *
     */
    public function __construct(
        Endpoints $endpoints,
        Logs $logs,
        Requester $requester,
        Session $session,
        Seller $seller,
        StoreTranslations $storeTranslations
    ) {
        $this->endpoints           = $endpoints;
        $this->logs                = $logs;
        $this->requester           = $requester;
        $this->session             = $session;
        $this->seller              = $seller;
        $this->threeDsTranslations = $storeTranslations->threeDsTranslations;

        $this->registerFrontendEndpoints();
    }

    /**
     *
     */
    public function registerFrontendEndpoints()
    {
        $this->registerCustomCheckoutEndpoints();
    }

    /**
     *
     */
    public function registerCustomCheckoutEndpoints()
    {
        $this->endpoints->registerWCAjaxEndpoint('mp_get_3ds_from_session', [$this, 'mercadopagoGet3DSFromSession']);
        $this->endpoints->registerWCAjaxEndpoint('mp_redirect_after_3ds_challenge', [$this, 'mercadopagoRedirectAfter3DSChallenge']);
    }

    /**
     * Get 3DS information from Session
     *
     * @return void
     */
    public function mercadopagoGet3DSFromSession(): void
    {
        try {
            wp_send_json_success([
                'result' => 'success',
                'data'   => [
                    '3ds_url'  => $this->session->getSession('mp_3ds_url'),
                    '3ds_creq' => $this->session->getSession('mp_3ds_creq'),
                ],
            ]);
        } catch (Exception $e) {
            $this->logs->file->error('3DS session error: ' . $e->getMessage(), __CLASS__);
            wp_send_json_error([
                'result' => 'failure',
                'data' => [
                    'error' => 'Couldn\'t find 3DS info on current session',
                ],
            ]);
        }
    }

    /**
     * Get 3DS information from Session
     *
     * @return void
     */
    public function mercadopagoRedirectAfter3DSChallenge(): void
    {
        $orderId   = $this->session->getSession('mp_order_id');
        $paymentId = $this->session->getSession('mp_payment_id');

        $order = wc_get_order($orderId);

        try {
            $this->session->deleteSession('mp_3ds_url');
            $this->session->deleteSession('mp_3ds_creq');
            $this->session->deleteSession('mp_order_id');
            $this->session->deleteSession('mp_payment_id');

            $headers  = ['Authorization: Bearer ' . $this->seller->getCredentialsAccessToken()];
            $payment  = $this->requester->get("/v1/payments/$paymentId", $headers)->getData();

            if ($payment['status'] === 'approved' || $payment['status'] === 'pending') {
                wp_send_json_success([
                    'result'   => 'success',
                    'redirect' => $order->get_checkout_order_received_url(),
                ]);
            } else {
                wp_send_json_error([
                    'result' => 'failure',
                    'redirect' => '',
                    'data' => [
                        'error' => $this->threeDsTranslations['message_3ds_declined'],
                    ],
                ]);
            }
        } catch (Exception $e) {
            $this->logs->file->error('3DS session error: ' . $e->getMessage(), __CLASS__);

            wp_send_json_error([
                'result' => 'failed',
                'redirect' => $order->get_checkout_payment_url(true),
            ]);
        }
    }
}