Automattic\WooCommerce\Internal\Fulfillments
FulfillmentUtils::validate_ups_1z_check_digit
Validate UPS 1Z tracking number using Mod 10 check digit.
Method of the class: FulfillmentUtils{}
No Hooks.
Returns
true|false. True if valid, false otherwise.
Usage
$result = FulfillmentUtils::validate_ups_1z_check_digit( $tracking_number ): bool;
- $tracking_number(string) (required)
- The UPS 1Z tracking number.
FulfillmentUtils::validate_ups_1z_check_digit() FulfillmentUtils::validate ups 1z check digit code WC 10.3.3
public static function validate_ups_1z_check_digit( string $tracking_number ): bool {
if ( ! preg_match( '/^1Z[0-9A-Z]{15,16}$/', $tracking_number ) ) {
return false;
}
// Extract the trackable part (remove 1Z prefix).
$trackable = substr( $tracking_number, 2 );
$check_digit = (int) substr( $trackable, -1 );
$trackable = substr( $trackable, 0, -1 );
$sum = 0;
$odd_position = true;
// Process each character from right to left.
for ( $i = strlen( $trackable ) - 1; $i >= 0; $i-- ) {
$char = $trackable[ $i ];
$value = is_numeric( $char ) ? (int) $char : ord( $char ) - 55; // A=10, B=11, etc.
if ( $odd_position ) {
$value *= 2;
if ( $value > 9 ) {
$value = (int) ( $value / 10 ) + ( $value % 10 );
}
}
$sum += $value;
$odd_position = ! $odd_position;
}
$calculated_check = ( 10 - ( $sum % 10 ) ) % 10;
return $calculated_check === $check_digit;
}