<?php
/**
 * PeachPay Authorize.net Credit/Debit card gateway.
 *
 * @package PeachPay
 */

defined( 'PEACHPAY_ABSPATH' ) || exit;

/**
 * Authorize.net Credit/Debit card gateway.
 */
class PeachPay_Authnet_Card_Gateway extends PeachPay_Authnet_Payment_Gateway {

	/**
	 * Authorize.net credit/debit card gateway constructor.
	 */
	public function __construct() {
		$this->id                = 'peachpay_authnet_card';
		$this->icons             = array(
			'full'  => array(
				'clear' => PeachPay::get_asset_url( 'img/marks/cc-quad.svg' ),
			),
			'small' => array(
				'clear' => PeachPay::get_asset_url( 'img/marks/card-small.svg' ),
			),
		);
		$this->settings_priority = 0;

		// Customer facing title and description.
		$this->title = 'Card';
		$this->description = '';

		$this->payment_method_family = __( 'Card', 'peachpay-for-woocommerce' );

		$this->form_fields = self::transaction_type_setting( $this->form_fields );

		$this->supports = array_merge(
			$this->supports,
			array(
				'refunds',
				'subscriptions',
				'multiple_subscriptions',
				'subscription_cancellation',
				'subscription_suspension',
				'subscription_reactivation',
				'subscription_amount_changes',
				'subscription_date_changes',
				'subscription_payment_method_change_customer',
				'subscription_payment_method_change_admin',
			)
		);

		parent::__construct();

		$saved_title = $this->get_option( 'title' );
		if ( $saved_title ) {
			$this->title = $saved_title;
		}
	}

	/**
	 * Render hosted Authorize.net payment form trigger for classic checkout.
	 */
	public function payment_method_form() {
		$mode                    = PeachPay_Authnet_Integration::mode();
		$payment_options         = wp_json_encode( array( 'showCreditCard' => true, 'showBankAccount' => false ) );
		$billing_address_options = wp_json_encode( array( 'show' => false, 'required' => false ) );
		?>
		<div class="pp-authnet-acceptui">
			<button
				type="button"
				class="AcceptUI pp-authnet-acceptui-button"
				data-apiLoginID="<?php echo esc_attr( PeachPay_Authnet_Integration::login_id( $mode ) ); ?>"
				data-clientKey="<?php echo esc_attr( PeachPay_Authnet_Integration::public_client_key( $mode ) ); ?>"
				data-paymentOptions="<?php echo esc_attr( $payment_options ); ?>"
				data-billingAddressOptions="<?php echo esc_attr( $billing_address_options ); ?>"
				data-responseHandler="peachpayAuthnetClassicResponseHandler"
				data-acceptUIFormBtnTxt="<?php esc_attr_e( 'Submit', 'peachpay-for-woocommerce' ); ?>"
				data-acceptUIFormHeaderTxt="<?php esc_attr_e( 'Card Information', 'peachpay-for-woocommerce' ); ?>"
			>
				<?php esc_html_e( 'Enter card details', 'peachpay-for-woocommerce' ); ?>
			</button>
			<p class="pp-authnet-acceptui-status" aria-live="polite">
				<?php esc_html_e( 'Your card details are collected securely by Authorize.net.', 'peachpay-for-woocommerce' ); ?>
			</p>
			<input type="hidden" id="pp-authnet-data-descriptor" name="peachpay_authnet_data_descriptor" value=""/>
			<input type="hidden" id="pp-authnet-data-value" name="peachpay_authnet_data_value" value=""/>
			<div class="pp-authnet-error" role="alert"></div>
		</div>
		<?php
	}

	/**
	 * Enqueue AcceptUI v3 and checkout scripts for classic checkout.
	 */
	public function enqueue_checkout_scripts() {
		if ( ! is_checkout() || 'yes' !== $this->enabled ) {
			return;
		}

		if ( has_block( 'woocommerce/checkout' ) ) {
			return;
		}

		$mode          = PeachPay_Authnet_Integration::mode();
		$accept_ui_url = 'test' === $mode
			? 'https://jstest.authorize.net/v3/AcceptUI.js'
			: 'https://js.authorize.net/v3/AcceptUI.js';

		$js_path = PEACHPAY_ABSPATH . 'core/payments/authnet/assets/js/checkout.js';
		wp_enqueue_script(
			'peachpay-authnet-checkout',
			plugins_url( 'peachpay-for-woocommerce/core/payments/authnet/assets/js/checkout.js' ),
			array( 'jquery' ),
			filemtime( $js_path ),
			true
		);

		wp_localize_script(
			'peachpay-authnet-checkout',
			'peachpayAuthnetConfig',
			array(
				'apiLoginID' => PeachPay_Authnet_Integration::login_id( $mode ),
				'clientKey'  => PeachPay_Authnet_Integration::public_client_key( $mode ),
				'gatewayId'  => $this->id,
				'isTestMode' => 'test' === $mode,
				'acceptSrc'  => $accept_ui_url,
			)
		);

		$css_path = PEACHPAY_ABSPATH . 'core/payments/authnet/assets/css/checkout.css';
		if ( file_exists( $css_path ) ) {
			wp_enqueue_style(
				'peachpay-authnet-checkout',
				plugins_url( 'peachpay-for-woocommerce/core/payments/authnet/assets/css/checkout.css' ),
				array(),
				filemtime( $css_path )
			);
		}
	}

}
