<?php
/**
 * Credorax Payments For WooCommerce
 * https://www.credorax.com/
 *
 * @category Credorax
 * @package  WC_Credorax
 * @author   Girit-Interactive (https://www.girit-tech.com/)
 */

 if (! defined('ABSPATH')) {
     exit; // Exit if accessed directly.
 }

/**
 * Credorax Credorax Auth payment response model.
 */
class WC_Credorax_Model_Response_Payment_Auth extends WC_Credorax_Model_Response_AbstractPayment implements WC_Credorax_Model_ResponseInterface
{
    /**
     * @var string
     */
    protected $_authCode;

    /**
     * @return Dynamic3D
     */
    protected function processResponseData()
    {
        parent::processResponseData();

        $body = $this->getBody();
        $this->_authCode = isset($body['z4']) ? $body['z4'] : null;

        return $this;
    }

    /**
     * @return WC_Credorax_Model_Response_Payment_Auth
     * @throws \Exception
     * @throws \Exception
     */
    protected function updateTransaction()
    {
        parent::updateTransaction();

        if ($this->_authCode) {
            $this->_order->add_meta_data(
                WC_Credorax_Gateway::KEY_CREDORAX_AUTH_CODE,
                $this->_authCode,
                true
            );
        }

        $this->_order->add_meta_data(
            WC_Credorax_Gateway::KEY_CREDORAX_PAYMENT_STATUS,
            WC_Credorax_Gateway::CREDORAX_PAYMENT_STATUS_PENDNIG,
            true
        );

        return $this;
    }

    /**
     * @return string
     */
    public function getAuthCode()
    {
        return $this->_authCode;
    }

    /**
     * @return array
     */
    protected function getRequiredResponseDataKeys()
    {
        $params = array_merge_recursive(
            parent::getRequiredResponseDataKeys(),
            [
                'K',
                'O',
                'z1',
            ]
        );
        $body = $this->getBody();
        if (!(isset($body['3ds_acsurl']) && $body['3ds_acsurl']) || isset($_GET['credorax_challenge_callback'])) {
            $params[] = 'z4';
        }
        return $params;
    }
}
