Automattic\WooCommerce\Gateways\PayPal

Request::normalize_paypal_order_shipping_country_codeprivateWC 1.0

Normalize PayPal order shipping country code.

Method of the class: Request{}

No Hooks.

Returns

String|null.

Usage

// private - for code of main (parent) class only
$result = $this->normalize_paypal_order_shipping_country_code( $country_code ): ?string;
$country_code(string) (required)
Country code to normalize.

Request::normalize_paypal_order_shipping_country_code() code WC 10.7.0

private function normalize_paypal_order_shipping_country_code( string $country_code ): ?string {
	// Normalize to uppercase.
	$code = strtoupper( trim( (string) $country_code ) );

	// Check if it's a valid alpha-2 code.
	if ( strlen( $code ) === PayPalConstants::PAYPAL_COUNTRY_CODE_LENGTH ) {
		if ( PayPalHelper::is_country_supported_by_paypal( $code ) ) {
			return $code;
		}

		\WC_Gateway_Paypal::log( sprintf( 'Invalid country code: %s', $code ) );
		return null;
	}

	// Log when we get an unexpected country code length.
	\WC_Gateway_Paypal::log( sprintf( 'Unexpected country code length (%d) for country: %s', strlen( $code ), $code ) );

	// Truncate to the expected maximum length (3).
	$max_country_code_length = PayPalConstants::PAYPAL_COUNTRY_CODE_LENGTH + 1;
	if ( strlen( $code ) > $max_country_code_length ) {
		$code = substr( $code, 0, $max_country_code_length );
	}

	// Check if it's a valid alpha-3 code.
	$alpha2 = WC()->countries->get_country_from_alpha_3_code( $code );
	if ( null === $alpha2 ) {
		\WC_Gateway_Paypal::log( sprintf( 'Invalid alpha-3 country code: %s', $code ), 'error' );
		return null;
	}
	if ( ! PayPalHelper::is_country_supported_by_paypal( $alpha2 ) ) {
		\WC_Gateway_Paypal::log( sprintf( 'Country not supported by PayPal: %s (resolved from alpha-3: %s)', $alpha2, $code ) );
		return null;
	}

	return $alpha2;
}