<?php
/**
 * Abstract payment response.
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways\Shift4_APM\Responses;

use Exception;
use Flo_Checkout\Gateways\Shift4_APM\Responses\Models\Payment\Result_Model_Gateway_Payment;
use Flo_Checkout\Gateways\Shift4_APM\Responses\Models\Payment_Response_Model;
use WC_Data_Exception;

defined( 'ABSPATH' ) || exit;

/**
 * Class Abstract_Payment_Response
 */
abstract class Abstract_Response_Gateway_Payment extends Abstract_Response_Gateway {

	public const ORDER_META_RESPONSE    = '_shift4_apm_response';
	public const ORDER_META_REFUND_LINK = '_shift4_apm_refund_url';

	/**
	 * Payment response model.
	 *
	 * @var Payment_Response_Model
	 */
	protected Payment_Response_Model $payment_response;

	/**
	 * Create result object.
	 *
	 * @return void
	 * @throws Exception If the response is not successful.
	 */
	protected function create_result(): void {
		$this->result = new Result_Model_Gateway_Payment( $this->request_response_model->get_body() );
	}

	/**
	 * {@inheritDoc}
	 */
	public function process(): Abstract_Response_Gateway_Payment {
		parent::process();

		return $this->process_response_data();
	}

	/**
	 * Process response data.
	 *
	 * @return self
	 */
	protected function process_response_data(): self {
		$this->payment_response = new Payment_Response_Model( $this->result );
		return $this;
	}

	/**
	 * {@inheritDoc}
	 */
	protected function get_required_response_data_keys(): array {
		return array_replace_recursive(
			parent::get_required_response_data_keys(),
			array(
				'payment_id',
				'transaction_id',
				'request_id',
				'payment_method',
				'operation',
				'transaction_time',
			)
		);
	}
}
