<?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 SaleTokenization payment response model.
 */
class WC_Credorax_Model_Response_Payment_SaleTokenization extends WC_Credorax_Model_Response_Payment_Sale implements WC_Credorax_Model_ResponseInterface
{
    /**
     * @return WC_Credorax_Model_Response_Payment_SaleTokenization
     */
    protected function processResponseData()
    {
        parent::processResponseData();
        $body = $this->getBody();

		if ( ! empty( $body['z2'] ) && $body['z2'] !== '06' && empty( $body['g1'] ) ) {
			throw new Exception( __( 'Tokenization failed.', 'credorax' ) );
		}

        $this->_token = isset($body['g1']) ? $body['g1'] : null;

        return $this;
    }

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

        if ($this->_token && WC_Credorax_Model_Config::isUsingVault() && $this->_order->get_meta(WC_Credorax_Gateway::KEY_CC_SAVE)) {
            WC_Credorax_Model_CardTokenization::saveCcToken($this->_order, $this->_token);
        }

        return $this;
    }

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

    /**
     * @return array
     *
    protected function getRequiredResponseDataKeys()
    {
        return array_merge_recursive(
            parent::getRequiredResponseDataKeys(),
            [
                'g1',
            ]
        );
    }
     */
}
