Automattic\WooCommerce\Internal\Fulfillments

FulfillmentUtils::validate_mod10_check_digitpublic staticWC 1.0

Validate Mod 10 check digit for numeric tracking numbers.

Method of the class: FulfillmentUtils{}

No Hooks.

Returns

true|false. True if valid, false otherwise.

Usage

$result = FulfillmentUtils::validate_mod10_check_digit( $tracking_number ): bool;
$tracking_number(string) (required)
The numeric tracking number.

FulfillmentUtils::validate_mod10_check_digit() code WC 10.3.3

public static function validate_mod10_check_digit( string $tracking_number ): bool {
	if ( ! preg_match( '/^\d+$/', $tracking_number ) || strlen( $tracking_number ) < 2 ) {
		return false;
	}

	$check_digit = (int) substr( $tracking_number, -1 );
	$number      = substr( $tracking_number, 0, -1 );

	$sum          = 0;
	$odd_position = true;

	// Process each digit from right to left.
	for ( $i = strlen( $number ) - 1; $i >= 0; $i-- ) {
		$digit = (int) $number[ $i ];

		if ( $odd_position ) {
			$digit *= 2;
			if ( $digit > 9 ) {
				$digit = (int) ( $digit / 10 ) + ( $digit % 10 );
			}
		}

		$sum         += $digit;
		$odd_position = ! $odd_position;
	}

	$calculated_check = ( 10 - ( $sum % 10 ) ) % 10;
	return $calculated_check === $check_digit;
}