<?php
/**
 * PeachPay Stripe integration hook functions. Utility functions should not be defined here.
 *
 * @phpcs:disable WordPress.Security.NonceVerification.Recommended
 *
 * @package PeachPay
 */

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

/**
 * Adds the feature flag for enabling stripe gateways.
 *
 * @param array $feature_list The list of features.
 */
function peachpay_stripe_register_feature( $feature_list ) {
	$feature_list['peachpay_stripe_gateways'] = array(
		'enabled'  => (bool) PeachPay_Stripe_Integration::connected() && PeachPay_Stripe_Integration::has_gateway_enabled(),
		'metadata' => array(
			'connect_id'         => PeachPay_Stripe_Integration::connect_id(),
			'public_key'         => PeachPay_Stripe_Integration::public_key(),
			'country'            => PeachPay_Stripe_Integration::connect_country(),
			'locale'             => 'auto',
			'setup_intent_url'   => WC_AJAX::get_endpoint( 'pp-stripe-setup-intent' ),
			'setup_intent_nonce' => wp_create_nonce( 'peachpay-stripe-setup-intent' ),

		),
	);

	return $feature_list;
}

/**
 * Displays some stripe charge details for a order.
 *
 * @param WC_Order $order The given order to display information for.
 */
function peachpay_stripe_display_order_transaction_details( $order ) {

	if ( did_action( 'woocommerce_admin_order_data_after_billing_address' ) > 1 ) {
		return;
	}
	if ( ! PeachPay_Stripe_Integration::is_payment_gateway( $order->get_payment_method() ) ) {
		return;
	}

	$payment_intent_id = PeachPay_Stripe_Order_Data::get_payment_intent( $order, 'id' );
	$payment_method_id = PeachPay_Stripe_Order_Data::get_payment_method( $order, 'id' );
	if ( null !== $payment_intent_id || null !== $payment_method_id ) {
		include PeachPay::get_plugin_path() . '/core/payments/stripe/admin/views/html-stripe-payment-info.php';
	}
}

/**
 * Displays the stripe capture/void form on the order dashboard.
 *
 * @param int $order_id Id for the order.
 */
function peachpay_stripe_display_order_transaction_capture_void_form( $order_id ) {
	if ( did_action( 'woocommerce_admin_order_totals_after_total' ) > 1 ) {
		return;
	}

	$order = wc_get_order( $order_id );

	if ( ! PeachPay_Stripe_Integration::is_payment_gateway( $order->get_payment_method() ) || $order->is_paid() ) {
		return;
	}

	$amount_capturable = PeachPay_Stripe_Order_Data::get_payment_intent( $order, 'amount_capturable' );
	if ( null !== $amount_capturable && $amount_capturable > 0 ) {
		include PeachPay::get_plugin_path() . '/core/payments/stripe/admin/views/html-stripe-capture-void-form.php';
	}
}

/**
 * Displays the Stripe balance transfer fee and net payout lines for the order dashboard.
 *
 * @param int $order_id Id for the order.
 */
function peachpay_stripe_display_balance_transaction_fee_lines( $order_id ) {
	if ( did_action( 'woocommerce_admin_order_totals_after_total' ) > 1 ) {
		return;
	}

	$order    = wc_get_order( $order_id );
	$refunded = floatval( $order->get_total_refunded() );

	if ( ! PeachPay_Stripe_Integration::is_payment_gateway( $order->get_payment_method() ) || ( ! ( $order->is_paid() ) && 0 === $refunded ) ) {
		return;
	}

	$balance_transaction = PeachPay_Stripe_Order_Data::get_charge( $order, 'balance_transaction' );
	if ( null !== $balance_transaction ) {
		include PeachPay::get_plugin_path() . '/core/payments/stripe/admin/views/html-stripe-transaction-fees.php';
	}
}

/**
 * Captures the payment if the order payment was authorized and not captured immediately.
 *
 * @param int      $order_id The order id.
 * @param string   $status_from The order status the order is being changed from.
 * @param string   $status_to The order status the order is being changed too.
 * @param WC_Order $order The instance of the order.
 */
function peachpay_stripe_handle_order_complete( $order_id, $status_from, $status_to, $order ) {
	if ( ! PeachPay_Stripe_Integration::is_payment_gateway( $order->get_payment_method() ) ) {
		return;
	}

	$transaction_id = $order->get_transaction_id();
	if ( ! $transaction_id || 'on-hold' !== $status_from || 'completed' !== $status_to ) {
		return;
	}

	$amount_capturable = PeachPay_Stripe_Order_Data::get_payment_intent( $order, 'amount_capturable' );
	if ( ! is_numeric( $amount_capturable ) || $amount_capturable <= 0 ) {
		return;
	}

	PeachPay_Stripe::capture_payment( $order, $amount_capturable );

	$charge_id = PeachPay_Stripe_Order_Data::get_charge( $order, 'id' );

	// translators: %1$s Payment method title,  %2$s charge id.
	$order->add_order_note( sprintf( __( 'Stripe %1$s payment captured. Payment is now complete (Charge Id: %2$s)', 'peachpay-for-woocommerce' ), $order->get_payment_method_title(), $charge_id ) );
}

/**
 * Callback function that gets activated when merchant changes order status to cancelled.
 *
 * @param string $order_id The order id of the order that was cancelled.
 */
function peachpay_stripe_handle_order_cancelled( $order_id ) {
	$order = wc_get_order( $order_id );

	if ( ! $order || ! PeachPay_Stripe_Integration::is_payment_gateway( $order->get_payment_method() ) ) {
		return;
	}

	if ( PeachPay_Stripe_Advanced::get_setting( 'refund_on_cancel' ) !== 'yes' ) {
		return;
	}

	if ( $order->get_transaction_id() && $order->get_total() > 0 ) {
		$amount             = $order->get_total() - $order->get_total_refunded();
		$refund_amount      = PeachPay_Stripe::format_amount( $amount, $order->get_currency() );
		$transaction_status = PeachPay_Stripe_Order_Data::get_payment_intent( $order, 'status' );

		$charge_id = PeachPay_Stripe_Order_Data::get_charge( $order, 'id' );

		$refund_result = null;
		if ( 'succeeded' === $transaction_status ) {
			$refund_result = PeachPay_Stripe::refund_payment( $order, $refund_amount );
		} elseif ( 'requires_capture' === $transaction_status ) {
			$refund_result = PeachPay_Stripe::void_payment( $order );
		} else {
			$refund_result = array(
				'success' => false,
				// translators: %1$s the payment method title, %2$s Charge Id.
				'message' => sprintf( __( 'Stripe %1$s could not be voided or refunded. (Transaction Id: %2$s)', 'peachpay-for-woocommerce' ), $order->get_payment_method_title(), $charge_id ),
			);
		}

		if ( ! $refund_result['success'] ) {
			$order->add_order_note( $refund_result['message'] );
			return;
		}

		$refund = new WC_Order_Refund();
		$refund->set_amount( $amount );
		$refund->set_parent_id( $order->get_id() );
		$refund->set_reason( 'Order was canceled or removed.' );
		$refund->set_refunded_by( get_current_user_id() );
		$refund->save();

		// translators: %1$s the payment method title, %3$s Refund amount, %4$s Refund Id.
		$order->add_order_note( sprintf( __( 'Stripe %1$s payment refunded %2$s because order was cancelled. (Transaction Id: %3$s)', 'peachpay-for-woocommerce' ), $order->get_payment_method_title(), wc_price( $amount, array( 'currency' => $order->get_currency() ) ), $charge_id ) );
		$order->save();
	}
}

/**
 * Links a WooCommerce refund record to a Stripe refund ID for admin-initiated refunds.
 *
 * @param int   $refund_id WooCommerce refund ID.
 * @param array $args Refund creation arguments.
 */
function peachpay_stripe_handle_wc_refund_created( $refund_id, $args ) {
	if ( empty( $args['refund_payment'] ) || empty( $args['order_id'] ) ) {
		return;
	}

	$order = wc_get_order( $args['order_id'] );
	if ( ! $order || ! PeachPay_Stripe_Integration::is_payment_gateway( $order->get_payment_method() ) ) {
		return;
	}

	if ( ! class_exists( 'PeachPay_Stripe' ) || ! method_exists( 'PeachPay_Stripe', 'consume_pending_wc_refund_id' ) ) {
		return;
	}

	$stripe_refund_id = PeachPay_Stripe::consume_pending_wc_refund_id( $order->get_id() );
	if ( empty( $stripe_refund_id ) ) {
		return;
	}

	$refund = wc_get_order( $refund_id );
	if ( ! $refund || ! is_a( $refund, 'WC_Order_Refund' ) ) {
		return;
	}

	$refund->update_meta_data( '_pp_stripe_refund_id', $stripe_refund_id );
	$refund->save();
}

/**
 * Captures the payment if the order payment was authorized and not captured immediately.
 *
 * @param int      $order_id The order id.
 * @param string   $status_from The order status the order is being changed from.
 * @param string   $status_to The order status the order is being changed too.
 * @param WC_Order $order The instance of the order.
 */
function peachpay_stripe_handle_order_processing( $order_id, $status_from, $status_to, $order ) {
	if ( ! PeachPay_Stripe_Integration::is_payment_gateway( $order->get_payment_method() ) ) {
		return;
	}

	$transaction_id = $order->get_transaction_id();
	if ( ! $transaction_id || 'on-hold' !== $status_from || 'processing' !== $status_to ) {
		return;
	}

	$amount_authorized = PeachPay_Stripe_Order_Data::get_payment_intent( $order, 'amount_capturable' );
	if ( ! is_numeric( $amount_authorized ) || $amount_authorized <= 0 ) {
		return;
	}

	PeachPay_Stripe::capture_payment( $order, $amount_authorized );

	$charge_id = PeachPay_Stripe_Order_Data::get_charge( $order, 'id' );

	// translators: %1$s Payment method title,  %2$s charge id.
	$order->add_order_note( sprintf( __( 'Stripe %1$s payment captured. Payment is now complete (Charge Id: %2$s)', 'peachpay-for-woocommerce' ), $order->get_payment_method_title(), $charge_id ) );
}

/**
 * Indicates if Stripe Apple Pay domain is registered or not.
 */
function peachpay_stripe_apple_pay_domain_registered() {
	$config = peachpay_stripe_get_apple_pay_config();

	return $config['registered'];
}

/**
 * Gets Stripe Apple Pay domain config.
 */
function peachpay_stripe_get_apple_pay_config() {
	$config = get_option(
		'peachpay_stripe_applepay_config',
		array(
			'domain'       => wp_parse_url( get_site_url(), PHP_URL_HOST ),
			'registered'   => false,
			'auto_attempt' => false,
		)
	);

	return $config;
}

/**
 * Updates the Apple Pay domain registration config.
 *
 * @param array $config The Apple Pay domain config.
 */
function peachpay_stripe_update_apple_pay_config( $config ) {
	if ( ! $config || ! is_array( $config ) ) {
		return;
	}

	update_option( 'peachpay_stripe_applepay_config', $config );
}

/**
 * Handles Stripe ApplePay domain registration from TS side.
 */
function peachpay_stripe_handle_applepay_domain_registration() {
	if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'peachpay-applepay-domain-register' ) ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'Invalid nonce. Please refresh the page and try again.',
			)
		);
	}

	wp_send_json( peachpay_stripe_applepay_domain_register( true ) );
}

/**
 * Registers the current site domain for Stripe Apple Pay via the REST API.
 *
 * Replaces the former SDK call ($stripe->applePayDomains->create()) with
 * PeachPay_Stripe_HTTP::post(). Stripe API endpoint: POST /v1/apple_pay/domains.
 *
 * @param bool $force Forces the domain registration attempt even if auto_attempt has already occurred.
 * @return array Result array with 'success' (bool) and 'message' (string) keys.
 */
function peachpay_stripe_applepay_domain_register( $force = false ) {
	if ( ! PeachPay_Stripe_Integration::connected() ) {
		return array(
			'success' => false,
			'message' => __( 'Stripe is not connected.', 'peachpay-for-woocommerce' ),
		);
	}

	$current_domain = wp_parse_url( get_site_url(), PHP_URL_HOST );
	$config         = peachpay_stripe_get_apple_pay_config();

	// Domain changed — reset registration state.
	if ( $current_domain !== $config['domain'] ) {
		$config['domain']       = $current_domain;
		$config['registered']   = false;
		$config['auto_attempt'] = false;

		peachpay_stripe_update_apple_pay_config( $config );
	}

	if ( ! $force ) {
		if ( $config['registered'] ) {
			return array(
				'success' => true,
				'message' => __( 'Domain is already registered.', 'peachpay-for-woocommerce' ),
			);
		}

		if ( $config['auto_attempt'] ) {
			return array(
				'success' => false,
				'message' => __( 'Automatic attempt has already occured. Use force=true to force a registration attempt.', 'peachpay-for-woocommerce' ),
			);
		}
	}

	update_option( 'peachpay_attempt_applepay', 'stripe' );

	if ( empty( PeachPay_Stripe_Credentials::get_secret_key() ) ) {
		$config['registered']   = false;
		$config['auto_attempt'] = true;
		peachpay_stripe_update_apple_pay_config( $config );
		return array(
			'success' => false,
			'message' => __( 'Stripe is not configured.', 'peachpay-for-woocommerce' ),
		);
	}

	// POST /v1/apple_pay/domains
	$result = PeachPay_Stripe_HTTP::post(
		'/v1/apple_pay/domains',
		array( 'domain_name' => $current_domain )
	);

	if ( is_wp_error( $result ) ) {
		$error_message = $result->get_error_message();

		// Stripe treats an already-registered domain as an InvalidRequest error —
		// check the message text and convert it to a success so callers don't retry.
		if ( false !== strpos( $error_message, 'already been registered' ) ) {
			$config['registered']   = true;
			$config['auto_attempt'] = true;
			peachpay_stripe_update_apple_pay_config( $config );

			return array(
				'success' => true,
				'message' => __( 'Domain is already registered for Apple Pay.', 'peachpay-for-woocommerce' ),
			);
		}

		$config['registered']   = false;
		$config['auto_attempt'] = true;
		peachpay_stripe_update_apple_pay_config( $config );

		return array(
			'success' => false,
			'message' => $error_message,
		);
	}

	$config['registered']   = true;
	$config['auto_attempt'] = true;
	peachpay_stripe_update_apple_pay_config( $config );

	return array(
		'success' => true,
		'message' => __( 'Successfully registered domain for Apple Pay.', 'peachpay-for-woocommerce' ),
	);
}

/**
 * WP ajax request to capture a stripe payment.
 */
function peachpay_stripe_handle_capture_payment() {
	if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'peachpay-stripe-capture-payment' ) ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'Invalid nonce. Please refresh the page and try again.',
			)
		);
	}

	if ( ! isset( $_POST['order_id'] ) ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'The field "order_id" is missing.',
			)
		);
	}
	$order_id = floatval( wp_unslash( $_POST['order_id'] ) );

	$order = wc_get_order( $order_id );
	if ( ! $order ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'The field "order_id" did not match any orders.',
			)
		);
	}

	if ( ! isset( $_POST['amount_to_capture'] ) ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'The field "amount_to_capture" is missing.',
			)
		);
	}
	$amount_to_capture = PeachPay_Stripe::format_amount( floatval( wp_unslash( $_POST['amount_to_capture'] ) ), $order->get_currency() );

	wp_send_json( PeachPay_Stripe::capture_payment( $order, $amount_to_capture ) );
}

/**
 * WP ajax request to void a stripe payment.
 */
function peachpay_stripe_handle_void_payment() {
	if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'peachpay-stripe-void-payment' ) ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'Invalid nonce. Please refresh the page and try again.',
			)
		);
	}

	if ( ! isset( $_POST['order_id'] ) ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'The field "order_id" is missing.',
			)
		);
	}
	$order_id = floatval( wp_unslash( $_POST['order_id'] ) );

	$order = wc_get_order( $order_id );
	if ( ! $order ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'The field "order_id" did not match any orders.',
			)
		);
	}

	wp_send_json( PeachPay_Stripe::void_payment( $order ) );
}

/**
 * WC ajax request to create a stripe setup intent
 */
function peachpay_stripe_handle_setup_intent() {
	$session_id = PeachPay_Payment::get_session();

	if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'peachpay-stripe-setup-intent' ) ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'Invalid nonce. Please refresh the page and try again.',
			)
		);
	}

	if ( ! isset( $_POST['payment_method_type'] ) ) {
		return wp_send_json(
			array(
				'success' => false,
				'message' => 'The field "payment_method_type" was invalid or missing.',
			)
		);
	}
	$payment_method_type = sanitize_text_field( wp_unslash( $_POST['payment_method_type'] ) );

	$payment_method = isset( $_POST['payment_method'] ) ? sanitize_text_field( wp_unslash( $_POST['payment_method'] ) ) : null;

	wp_send_json(
		PeachPay_Stripe::setup_payment(
			$session_id,
			array(
				'customer'               => PeachPay_Stripe::get_customer( get_current_user_id() ),
				'payment_method'         => $payment_method,
				'payment_method_types'   => array( $payment_method_type ),
				'usage'                  => 'off_session',
				'payment_method_options' => array(
					'us_bank_account' => array(
						'verification_method' => 'instant',
					),
				),
			)
		)
	);
}

/**
 * Controls the output for PeachPay stripe tokenized payment methods on the my account page.
 *
 * @param  array            $item         Individual list item from woocommerce_saved_payment_methods_list.
 * @param  WC_Payment_Token $payment_token The payment token associated with this method entry.
 * @return array                           Filtered item.
 */
function peachpay_stripe_get_account_saved_payment_methods_list_item( $item, $payment_token ) {
	if ( 'peachpay_stripe_card' === strtolower( $payment_token->get_type() ) ) {

		$item['method']['last4'] = $payment_token->get_last4();
		$item['method']['brand'] = $payment_token->get_card_type();
		$item['expires']         = sprintf( '%s / %s', $payment_token->get_expiry_month(), $payment_token->get_expiry_year() );

		return $item;
	}

	if ( 'peachpay_stripe_achdebit' === strtolower( $payment_token->get_type() ) ) {

		$item['method']['last4'] = $payment_token->get_last4();
		$item['method']['brand'] = 'ACH: ' . $payment_token->get_bank();

		return $item;
	}

	return $item;
}

/**
 * Filters display payment tokens.
 *
 * @param array $tokens the list of tokens to filter.
 */
function peachpay_get_customer_payment_tokens( $tokens ) {

	$current_mode = PeachPay_Stripe_Integration::mode();
	$connect_id   = PeachPay_Stripe_Integration::connect_id();

	foreach ( $tokens as $id => $token ) {
		if ( $token instanceof WC_Payment_Token_PeachPay_Stripe_Achdebit ) {
			if ( $token->get_mode() !== $current_mode ) {
				unset( $tokens[ $id ] );
			}
			if ( $token->get_connect_id() !== $connect_id ) {
				unset( $tokens[ $id ] );
			}
		}

		if ( $token instanceof WC_Payment_Token_PeachPay_Stripe_Card ) {
			if ( $token->get_mode() !== $current_mode ) {
				unset( $tokens[ $id ] );
			}
			if ( $token->get_connect_id() !== $connect_id ) {
				unset( $tokens[ $id ] );
			}
		}
	}

	return $tokens;
}

/**
 * Handles stripe settings actions.
 */
function peachpay_stripe_handle_admin_actions() {
	// Display stripe connect message.
	if ( isset( $_GET['connected_stripe'] ) && 'true' === $_GET['connected_stripe'] ) {
		// See PayPal version of this below for commentary.
		if ( ! is_array( get_option( 'peachpay_payment_options' ) ) ) {
			update_option( 'peachpay_payment_options', array() );
		}

		add_settings_error(
			'peachpay_messages',
			'peachpay_message',
			__( 'You have successfully connected your Stripe account. You may set up other payment methods in the "Payment methods" tab.', 'peachpay-for-woocommerce' ),
			'success'
		);
	}

	if ( isset( $_GET['unlink_stripe'] ) && PeachPay_Stripe_Integration::connected() ) {
		// Verify nonce to prevent CSRF attacks that could delete Stripe credentials.
		if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'peachpay-stripe-unlink' ) ) {
			wp_die( esc_html__( 'Security check failed. Please try again.', 'peachpay-for-woocommerce' ) );
		}

		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			wp_die( esc_html__( 'You do not have permission to perform this action.', 'peachpay-for-woocommerce' ) );
		}

		peachpay_unlink_stripe();
	}
}

/**
 * Unlink merchant Stripe Account
 */
function peachpay_unlink_stripe() {
	if ( ! peachpay_unlink_stripe_request() ) {
		add_settings_error( 'peachpay_messages', 'peachpay_message', __( 'Unable to unlink Stripe account. Please try again or contact us if you need help.', 'peachpay-for-woocommerce' ), 'error' );
		return;
	}

	add_settings_error(
		'peachpay_messages',
		'peachpay_message',
		__( 'You have successfully unlinked your Stripe account.', 'peachpay-for-woocommerce' ),
		'success'
	);
}


/**
 * Unlink merchant Stripe - now just deletes local credentials.
 */
function peachpay_unlink_stripe_request() {
	// Delete local credentials.
	PeachPay_Stripe_Credentials::delete_credentials();

	// Clear Apple Pay registration.
	delete_option( 'peachpay_apple_pay_settings_v2' );

	return true;
}

/**
 * AJAX handler for saving Stripe credentials.
 */
function peachpay_stripe_handle_save_credentials() {
	if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'peachpay-stripe-credentials' ) ) {
		wp_send_json_error(
			array(
				'message' => __( 'Invalid security token. Please refresh the page and try again.', 'peachpay-for-woocommerce' ),
			)
		);
		return;
	}

	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		wp_send_json_error(
			array(
				'message' => __( 'You do not have permission to perform this action.', 'peachpay-for-woocommerce' ),
			)
		);
		return;
	}

	$keys             = array();
	$masked_indicator = '••••••••••••••••';
	$is_test_mode     = peachpay_is_test_mode();
	$mode             = $is_test_mode ? 'test' : 'live';

	// Collect keys for the current mode only.
	$pk_field = $mode . '_publishable_key';
	$sk_field = $mode . '_secret_key';

	// Get publishable key.
	if ( isset( $_POST[ $pk_field ] ) && ! empty( $_POST[ $pk_field ] ) ) {
		$keys[ $pk_field ] = sanitize_text_field( wp_unslash( $_POST[ $pk_field ] ) );
	}

	// Get secret key (handle masked value).
	if ( isset( $_POST[ $sk_field ] ) && ! empty( $_POST[ $sk_field ] ) ) {
		if ( $masked_indicator === $_POST[ $sk_field ] ) {
			// Keep existing secret key.
			$existing_secret = PeachPay_Stripe_Credentials::get_secret_key( $mode );
			if ( $existing_secret ) {
				$keys[ $sk_field ] = $existing_secret;
			}
		} else {
			$keys[ $sk_field ] = sanitize_text_field( wp_unslash( $_POST[ $sk_field ] ) );
		}
	}

	// Get webhook secret (handle masked value).
	if ( isset( $_POST['webhook_secret'] ) && ! empty( $_POST['webhook_secret'] ) ) {
		if ( $masked_indicator === $_POST['webhook_secret'] ) {
			$existing_webhook = PeachPay_Stripe_Credentials::get_webhook_secret();
			if ( $existing_webhook ) {
				$keys['webhook_secret'] = $existing_webhook;
			}
		} else {
			$keys['webhook_secret'] = sanitize_text_field( wp_unslash( $_POST['webhook_secret'] ) );
		}
	}

	$result = PeachPay_Stripe_Credentials::save_credentials( $keys );

	if ( $result['success'] ) {
		wp_send_json_success( $result );
	} else {
		wp_send_json_error( $result );
	}
}

/**
 * AJAX handler for disconnecting Stripe.
 */
function peachpay_stripe_handle_disconnect() {
	if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'peachpay-stripe-credentials' ) ) {
		wp_send_json_error(
			array(
				'message' => __( 'Invalid security token. Please refresh the page and try again.', 'peachpay-for-woocommerce' ),
			)
		);
		return;
	}

	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		wp_send_json_error(
			array(
				'message' => __( 'You do not have permission to perform this action.', 'peachpay-for-woocommerce' ),
			)
		);
		return;
	}

	PeachPay_Stripe_Credentials::delete_credentials();

	wp_send_json_success(
		array(
			'message' => __( 'Stripe has been disconnected.', 'peachpay-for-woocommerce' ),
		)
	);
}
