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

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

/**
 * Credorax Credorax abstract payment request model.
 */
abstract class WC_Credorax_Model_Request_AbstractPayment extends WC_Credorax_Model_AbstractRequest
{
    /**
     * Payment methods.
     */
    const PAYMENT_AUTH_METHOD = 'payment_auth';
    const PAYMENT_AUTH_TOKENIZATION_METHOD = 'payment_auth_tokenization';
    const PAYMENT_AUTH_USE_TOKEN_METHOD = 'payment_auth_use_token';
    const PAYMENT_SALE_METHOD = 'payment_sale';
    const PAYMENT_SALE_TOKENIZATION_METHOD = 'payment_sale_tokenization';
    const PAYMENT_SALE_USE_TOKEN_METHOD = 'payment_sale_use_token';

    /**
     * @var WC_Order
     */
    protected $_order;

    /**
     * @var float
     */
    protected $_amount;

    /**
     * @method __construct
     * @param  WC_Order    $order
     * @param  float       $amount
     */
    public function __construct(
        WC_Order $order,
        $amount = 0.0
    ) {
        parent::__construct();
        $this->_order = $order;
        $this->_amount = $amount;
    }

    /**
     * {@inheritdoc}
     *
     * @return WC_Credorax_Model_ResponseInterface
     * @throws \Exception
     */
    protected function getResponseHandler()
    {
        $responseHandler = $this->_responseFactory->create(
            $this->getResponseHandlerType(),
            $this->_curlResponse,
            $this->_order
        );

        return $responseHandler;
    }

    /**
     * Return full endpoint to particular method for request call.
     *
     * @return string
     */
    protected function getEndpoint()
    {
        return WC_Credorax_Model_Config::getCredoraxPaymentUrl();
    }

    /**
     * Return 3d secure related request params.
     * @param WC_Order $order
     * @return array
     */
    protected function get3dSecureParams(WC_Order $order)
    {
        $params = [
            //'3ds_initiate' => '02' // Do not send this param if 3D secure is disabled.
        ];
        if (WC_Credorax_Model_Config::is3dSecureEnabled()) {
            if (WC_Credorax_Model_Config::isUsingSmart3d()) {
                $params['3ds_initiate'] = '03';
            } else {
                $params['3ds_initiate'] = '01';
            }
            if ($order->get_meta(WC_Credorax_Gateway::KEY_CREDORAX_3DS_METHOD)) {
                $params['3ds_compind'] = $order->get_meta(WC_Credorax_Gateway::KEY_CREDORAX_3DS_COMPIND) ?: 'N';
            }
            if((int)$order->get_billing_phone() && $order->get_billing_country()){
                $params['3ds_homephonecountry'] = $this->countryCodeToPhoneCode($order->get_billing_country());
            }
            $params['3ds_browseracceptheader'] = '3dsBrowseracceptheader';
            $params['3ds_browsercolordepth'] = '32';
            $params['3ds_browserscreenheight'] = '123';
            $params['3ds_browserscreenwidth'] = '1';
            $params['3ds_browserjavaenabled'] = 'false';
            $params['3ds_browsertz'] = '1';
            $params['3ds_challengewindowsize'] = '03';
            $params['3ds_transtype'] = '01';
            $params['3ds_purchasedate'] = date('YmdHis', strtotime($order->get_date_created()));
            $params['3ds_redirect_url'] = get_site_url(null, '?credorax_challenge_callback=' . $order->get_id());
            $params['d6'] = $order->get_meta(WC_Credorax_Gateway::KEY_CREDORAX_BROWSER_LANG) ?: 'en-US';
        }
        return $params;
    }

    /**
     * Return request params.
     *
     * @return array
     */
    protected function getParams()
    {
        return array_replace_recursive(
            parent::getParams(),
            $this->getOrderData($this->_order),
            $this->getBillingData($this->_order),
            $this->get3dSecureParams($this->_order)
        );
    }
}
