self::locked( 'min' ) ? (string) constant( 'SYNTOPIA_PAY_MIN' ) : (string) ( $stored['min'] ?? '' ), 'secret' => self::locked( 'secret' ) ? (string) constant( 'SYNTOPIA_PAY_SECRET' ) : (string) ( $stored['secret'] ?? '' ), 'test_mode' => 'no' !== ( $stored['test_mode'] ?? 'yes' ), 'redirect_delay' => self::delay( $stored['redirect_delay'] ?? null ), ]; } private static function delay( mixed $value ): int { if ( ! is_int( $value ) && ! ( is_string( $value ) && ctype_digit( $value ) ) ) { return self::DEFAULT_DELAY; } return min( self::MAX_DELAY, absint( $value ) ); } /** * @return array */ public static function stored(): array { $stored = get_option( self::OPTION, [] ); return is_array( $stored ) ? $stored : []; } public static function locked( string $key ): bool { return match ( $key ) { 'min' => defined( 'SYNTOPIA_PAY_MIN' ), 'secret' => defined( 'SYNTOPIA_PAY_SECRET' ), default => false, }; } public static function is_configured(): bool { $settings = self::get(); return '' !== $settings['min'] && '' !== $settings['secret']; } public static function host(): string { return self::get()['test_mode'] ? 'https://demo.epay.bg' : 'https://www.epay.bg'; } public static function notify_url(): string { return add_query_arg( 'wc-api', 'syntopia_pay_notify', home_url( '/' ) ); } /** * @param array $input * @param array $stored * @return array{values: array, errors: list} */ public static function sanitize( array $input, array $stored ): array { $errors = []; $values = [ 'min' => (string) ( $stored['min'] ?? '' ), 'secret' => (string) ( $stored['secret'] ?? '' ), 'test_mode' => array_key_exists( 'test_mode', $input ) ? 'yes' : 'no', 'redirect_delay' => self::delay( $stored['redirect_delay'] ?? null ), ]; if ( array_key_exists( 'redirect_delay', $input ) ) { $delay = $input['redirect_delay']; if ( is_string( $delay ) && ctype_digit( trim( $delay ) ) ) { $values['redirect_delay'] = self::delay( trim( $delay ) ); } else { $errors[] = __( 'The seconds before going to the payment page were not saved. Enter a whole number from 0 to 20.', 'syntopia-pay' ); } } if ( ! self::locked( 'min' ) && array_key_exists( 'min', $input ) ) { $min = is_string( $input['min'] ) ? trim( $input['min'] ) : ''; if ( 1 === preg_match( '/^[A-Za-z0-9]+$/D', $min ) ) { $values['min'] = $min; } else { $errors[] = __( 'The merchant ID (MIN) was not saved. It must contain only letters and digits.', 'syntopia-pay' ); } } if ( ! self::locked( 'secret' ) ) { $secret = isset( $input['secret'] ) && is_string( $input['secret'] ) ? trim( $input['secret'] ) : ''; if ( '' === $secret ) { $values['secret'] = (string) ( $stored['secret'] ?? '' ); } elseif ( 1 === preg_match( '/^[\x21-\x7E]{64}$/D', $secret ) ) { $values['secret'] = $secret; } else { $errors[] = __( 'The secret word was not saved. It must be exactly 64 characters with no spaces.', 'syntopia-pay' ); } } return [ 'values' => $values, 'errors' => $errors, ]; } }