<?php
/**
 * Apple Pay sale request.
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways\Apple_Pay\Requests;

use Exception;
use Flo_Checkout\Gateways\Shift4_APM\Abstract_APM_Gateway;
use Flo_Checkout\Gateways\Shift4_APM\Requests\Abstract_Request_Gateway_Payment;
use Flo_Checkout\Gateways\Shift4_Apple_Pay;

defined( 'ABSPATH' ) || exit;

/**
 * Apple Pay sale request.
 */
class Apple_Pay_Sale_Request extends Abstract_Request_Gateway_Payment {

	/**
	 * Get the response handler type.
	 *
	 * @return string
	 */
	protected function get_response_handler_type(): string {
		return Shift4_Apple_Pay::SHIFT4_APM_SALE_RESPONSE_TYPE;
	}

	/**
	 * {@inheritDoc}
	 */
	protected function get_endpoint(): string {
		return 'sale';
	}

	/**
	 * {@inheritDoc}
	 */
	protected function get_params(): array {
		return array_replace_recursive(
			parent::get_params(),
			array(
				'payment_details' => array(
					'token' => $this->get_token(),
				),
			)
		);
	}

	/**
	 * Get Apple Pay token.
	 *
	 * @return string
	 * @throws Exception If token is not found.
	 */
	protected function get_token(): string {
		$token = $this->order->get_meta( Abstract_APM_Gateway::ORDER_META_KEY_TOKEN );

		if ( empty( $token ) ) {
			throw new Exception(
				esc_html__(
					'Apple Pay token not found in order.',
					'flo-checkout-plugin'
				)
			);
		}

		return $token;
	}
}
