<?php
/**
 * Formidable Forms hosted-checkout payment gateway action.
 *
 * @package SHC4WC
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'FrmTransLiteActionsController' ) ) {
	return;
}

class FrmShc4wcHostedActionsController extends FrmTransLiteActionsController {

	/**
	 * @param WP_Post  $action Payment action.
	 * @param stdClass $entry  Entry.
	 * @param mixed    $form   Form.
	 * @return array
	 */
	public static function trigger_gateway( $action, $entry, $form ) {
		$response = array(
			'success'      => false,
			'run_triggers' => false,
			'show_errors'  => true,
		);

		$atts   = compact( 'action', 'entry', 'form' );
		$amount = self::prepare_amount( $action->post_content['amount'], $atts );

		if ( ! $amount || '000' === (string) $amount ) {
			$response['error'] = __( 'Please specify an amount for the payment', 'formidable' );
			return $response;
		}

		$error = SHC4WC_Hosted_Checkout_Flow::connection_error_message();
		if ( null !== $error ) {
			$response['error'] = $error;
			return $response;
		}

		if ( 'recurring' === ( $action->post_content['type'] ?? '' ) ) {
			$response['error'] = __( 'Square Hosted Checkout subscriptions are not supported in Formidable yet. Use one-time payment.', 'payments-connect-square' );
			return $response;
		}

		$form_title = is_object( $form ) && ! empty( $form->name ) ? (string) $form->name : __( 'Formidable form', 'payments-connect-square' );
		$currency   = ! empty( $action->post_content['currency'] ) ? strtoupper( (string) $action->post_content['currency'] ) : SHC4WC_Hosted_Checkout_Flow::default_currency();

		$started = SHC4WC_Hosted_Checkout_Flow::start_one_time_payment(
			array(
				'type'        => 'formidable',
				'amount'      => (float) $amount,
				'currency'    => $currency,
				'description' => FrmTransLiteAppHelper::process_shortcodes(
					array(
						'entry' => $entry,
						'form'  => $entry->form_id,
						'value' => $action->post_content['description'] ?? $form_title,
					)
				),
				'line_name'   => $form_title,
				'customer'    => SHC4WC_Formidable::extract_buyer_from_entry( $action, $entry ),
				'payload'     => array(
					'entry_id'    => (int) $entry->id,
					'action_id'   => (int) $action->ID,
					'form_id'     => (int) $entry->form_id,
					'amount'      => (float) $amount,
					'currency'    => $currency,
					'return_url'  => SHC4WC_Formidable::entry_return_url( (int) $entry->form_id ),
					'description' => (string) ( $action->post_content['description'] ?? '' ),
				),
			)
		);

		if ( is_wp_error( $started ) ) {
			$response['error'] = $started->get_error_message();
			return $response;
		}

		wp_redirect( esc_url_raw( $started['redirect'] ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
		exit;
	}

	/**
	 * Hosted checkout does not render an on-page card field.
	 *
	 * @param string             $callback Original callback.
	 * @param array|false|object $field    Field.
	 * @return string
	 */
	public static function maybe_show_card( $callback, $field = false ) {
		unset( $field );
		return $callback;
	}
}
