Chapter 29. USPS Confirmation Barcodes
Prev | Part VII. Case studies | Next

## Chapter 29. USPS Confirmation Barcodes

US Postal Service (USPS) uses EAN-128 barcodes to tag confirmation codes for the sender of parcels and letters in the case the sender is a registered business entity. The confirmation barcode gives feedback to the sender on the date, ZIP Code and the time the article was delivered.

Figure 29.1. usps_exhibit44.png

The full standard describing this is available in "`Confirmation Services Technical Guide`"

In order to create labels adhering to this strict standard it is possible to use JpGraph as a basic building block in order to create the barcode part (which uses UCC/EAN128 as mandatory coding from 2004 and onwards). The standard describes two basic forms of labels :

1. Inclusion of postal service routing information (destination ZIP code)

2. Exclusion of the destination ZIP code. This will then only include the Dun & Bradstreet Number (DUNS). The DUNS uniquely identifies businesses at specific geographical locations. For more information regarding this and how to obtain such a number please refer to USPS technical documentation.

## 29.1. Creating the confirmation codes

In order to create the final Package Identification Code (PIC) the following three steps must be taken

1. Determine the basic element of the code. This consists of identifying the Zip code (of the addressee), USPS service type, the DUNS and Sequence number of parcel (as determined by the business). These number are input and the process of how to get those number is not described further in this short note. It is assumed that a potential client will now how these numbers are obtained.

2. Determine what type of confirmation code should be used (with or without Zip code)

3. Calculate the additional checkdigit for the code and append that code to the digit sequence.

The final code that one arrives at in the final stage is then the barcode that should be created. However creating this code requires knowledge of the EAN-128 barcode format and the rules for creating barcodes that follow the highly standardized format for a EAN-128 barcode.

EAN-128 barcodes are the same as CODE-128 barcodes from a technical barcode point of view but the data to be encoded must follow a rigorous standard. The JpGraph library has built in validation to ensure that any data that is encoded using the EAN-128 symbology follows this standard.

This standard requires that special control character is inserted at specific points in the data stream. In the discussion below we will simply state what those control characters are and not discuss the general format of the EAN-128 barcodes in too much details.

In order to do this we will create a utility class with methods that does the following three things

1. Create the additional USPS Modulo-10 check digit.

2. Create a confirmation code without the ZIP number

3. Create a confirmation code with ZIP number

The USPS_Confirmation Utility class

For both types of confirmation code USPS uses its own checkdigit (a variant of a Modulo-10 checkdigit) at the end of the complete Package Identification Code (PIC), (the exact process for calculating this number is also described in the technical documentation released by USPS). The utility class will therefore consist of three methods, one to calculate the checkdigit and one method each to create PIC with and without ZIP code. The three methods are listed below

1. `function _USPS_chkd($aData)`

       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       13
       14
       15
       16
       17
       18
       19
       20
       21
       22
       23
       24
       25
       <?php
       // Calculate the single digit check digit from sequence of numbers
       // in a string
       function _USPS_chkd($aData) {
           $n = strlen($aData);

           // Add all numbers at position 0,2,4,.. from the end
           $et = 0 ;
           for( $i=1; $i <= $n; $i+=2 ) {
           $d = intval(substr($aData,-$i,1));
           $et += $d;
           }

           // Add all numbers at position 1,3,5,... from the end
           $ot = 0 ;
           for( $i=2; $i <= $n; $i+=2 ) {
           $d = intval(substr($aData,-$i,1));
           $ot += $d;
           }

           // Calculate the checkdigit
           $tot = 3*$et + $ot;
           return (10 - ($tot % 10))%10;
       }
       ?>

2. `function GetPIC($aServiceType,$aDUNS,$aSeqNbr)`

       1
       2
       3
       4
       5
       6
       7
       8
       9
       <?php
       // Get type 2 of confirmation code (without ZIP)
       function GetPIC($aServiceType,$aDUNS,$aSeqNbr) {
         // Convert to USPS format with AI=91
         $data = '91' . $aServiceType . $aDUNS . $aSeqNbr;
         $cd = $this->_USPS_chkd($data);
         return $data . $cd;
       }
       ?>

3. `function GetPICwithZIP($aZIP,$aServiceType,$aDUNS,$aSeqNbr)`

       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       13
       14
       15
       <?php
       // Get type 1 of confirmation code (with ZIP)
       function GetPICwithZIP($aZIP,$aServiceType,$aDUNS,$aSeqNbr) {
          // Convert to USPS format with AI=420 and extension starting
          // with AI=91
          $data = '420'. $aZIP . '91' . $aServiceType .
                    $aDUNS . $aSeqNbr;
          // Only calculate the checkdigit from the AI=91 and forward
          // and do not include the ~1 (FUNC1) in the calculation
          $cd = $this->_USPS_chkd(substr($data,8));
          $data = '420'. $aZIP . '~191' . $aServiceType .
                     $aDUNS . $aSeqNbr;
          return $data . $cd;
       }
       ?>

   All that now remains is to tie this together with the EAN-128 standard barcode generation in order to crate the confirmation code. The following script shows how this can be done

       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       13
       14
       15
       <?php
       $zip     = '92663';     // Zip code
       $service = '21';        // Service 21 = Priority Mail
       $DUNS    = '805213907'; // DUNS
       $seqnr   = '04508735';  // Seqnr

       $usps = new USPS_Confirmation();
       $data = $usps->GetPICwithZIP($zip,$service,$DUNS,$seqnr);

       $encoder = BarcodeFactory::Create(ENCODING_EAN128);
       $e = BackendFactory::Create(BACKEND_IMAGE,$encoder);
       $e->SetModuleWidth(2);
       $e->SetFont(FF_ARIAL,FS_NORMAL,14);
       $e->Stroke($data);
       ?>

   The script above will then generate the following barcode

   Figure 29.2. USPS example 1

### Caution

Even though only numbers are input to the PIC, integers should not be used since initial "0":s will be lost. Use strings as the example above shows.

Additional example

The following script shows how to generate PIC that does not use Zip codes.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?php
    $service = '01';         // Priority mail
    $DUNS    = '123456789';  // DUNS
    $seqnr   = '00000001';   // Seqnr

    $usps = new USPS_Confirmation();
    $data = $usps->GetPIC($service,$DUNS,$seqnr);

    $encoder = BarcodeFactory::Create(ENCODING_EAN128);
    $e = BackendFactory::Create(BACKEND_IMAGE,$encoder);
    $e->SetModuleWidth(2);
    $e->SetFont(FF_ARIAL,FS_NORMAL,14);
    $e->Stroke($data);
    ?>

The script above will generate the following confirmation barcode.

Figure 29.3. USPS example 2

A complete script to implement this is available in the distribution as "`barcode/examples/barcode_usps_example.php`"

Prev | Up | Next
Chapter 28. Synchronized Y-axis | Home | Chapter 30. Showing SPAM statistics