<?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 abstract gateway request model.
 */
abstract class WC_Credorax_Model_Request_AbstractGateway extends WC_Credorax_Model_AbstractRequest
{
    /**
     * Gateway methods.
     */
    const GATEWAY_CAPTURE_METHOD = 'gateway_capture';
    const GATEWAY_REFUND_METHOD = 'gateway_refund';
    const GATEWAY_VOID_METHOD = 'gateway_cancel';

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

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

    /**
     * AbstractPayment constructor.
     *
     * @param WC_Order     $order
     * @param float|null     $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::getCredoraxGatewayUrl();
    }

    /**
     * Return request params.
     *
     * @return array
     * @throws \Exception
     */
    protected function getParams()
    {
        return array_replace_recursive(
            parent::getParams(),
            $this->getOrderData($this->_order),
            [
                'g2' => $this->_order->get_meta(WC_Credorax_Gateway::KEY_CREDORAX_RESPONSE_ID),
                'g3' => $this->_order->get_meta(WC_Credorax_Gateway::KEY_CREDORAX_AUTH_CODE),
                'a4' => $this->amountFormat($this->_amount, $this->_order->get_currency()),
            ]
        );
    }
}
