|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Load required classes |
| 4 | + */ |
| 5 | +require_once('USPSBase.php'); |
| 6 | + |
| 7 | +/** |
| 8 | + */ |
| 9 | +class USPSPriorityLabel extends USPSBase { |
| 10 | + /** |
| 11 | + * @var string - the api version used for this type of call |
| 12 | + */ |
| 13 | + protected $apiVersion = 'ExpressMailLabel'; |
| 14 | + /** |
| 15 | + * @var array - route added so far. |
| 16 | + */ |
| 17 | + protected $fields = array(); |
| 18 | + /** |
| 19 | + * Perform the API call. |
| 20 | + * @return string |
| 21 | + */ |
| 22 | + public function createLabel() { |
| 23 | + // Add missing required |
| 24 | + $this->addMissingRequired(); |
| 25 | + |
| 26 | + // Sort them |
| 27 | + // Hack by the only way this will work properly |
| 28 | + // since usps wants the tags to be in |
| 29 | + // a certain order |
| 30 | + ksort($this->fields, SORT_NUMERIC); |
| 31 | + |
| 32 | + // remove the \d. from the key |
| 33 | + foreach($this->fields as $key => $value) { |
| 34 | + $newKey = str_replace('.', '', $key); |
| 35 | + $newKey = preg_replace('/\d+\:/', '', $newKey); |
| 36 | + unset($this->fields[$key]); |
| 37 | + $this->fields[$newKey] = $value; |
| 38 | + } |
| 39 | + |
| 40 | + return $this->doRequest(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Return the USPS confirmation/tracking number if we have one |
| 45 | + * @return string|bool |
| 46 | + */ |
| 47 | + public function getConfirmationNumber() { |
| 48 | + $response = $this->getArrayResponse(); |
| 49 | + // Check to make sure we have it |
| 50 | + if(isset($response['ExpressMailLabelResponse'])) { |
| 51 | + if(isset($response['ExpressMailLabelResponse']['EMConfirmationNumber'])) { |
| 52 | + return $response['ExpressMailLabelResponse']['EMConfirmationNumber']; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Return the USPS label as a base64 encoded string |
| 61 | + * @return string|bool |
| 62 | + */ |
| 63 | + public function getLabelContents() { |
| 64 | + $response = $this->getArrayResponse(); |
| 65 | + // Check to make sure we have it |
| 66 | + if(isset($response['ExpressMailLabelResponse'])) { |
| 67 | + if(isset($response['ExpressMailLabelResponse']['EMLabel'])) { |
| 68 | + return $response['ExpressMailLabelResponse']['EMLabel']; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * returns array of all fields added |
| 77 | + * @return array |
| 78 | + */ |
| 79 | + public function getPostFields() { |
| 80 | + return $this->fields; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Set the from address |
| 85 | + * @param string $firstName |
| 86 | + * @param string $lastName |
| 87 | + * @param string $company |
| 88 | + * @param string $address |
| 89 | + * @param string $city |
| 90 | + * @param string $state |
| 91 | + * @param string $zip |
| 92 | + * @param string $address2 |
| 93 | + * @param string $zip4 |
| 94 | + * @param string $phone |
| 95 | + * @return object |
| 96 | + */ |
| 97 | + public function setFromAddress($firstName, $lastName, $company, $address, $city, $state, $zip, $address2=null, $zip4=null, $phone=null) { |
| 98 | + $this->setField(5, 'FromFirstName', $firstName); |
| 99 | + $this->setField(6, 'FromLastName', $lastName); |
| 100 | + $this->setField(7, 'FromFirm', $company); |
| 101 | + $this->setField(8, 'FromAddress1', $address2); |
| 102 | + $this->setField(9, 'FromAddress2', $address); |
| 103 | + $this->setField(10, 'FromCity', $city); |
| 104 | + $this->setField(11, 'FromState', $state); |
| 105 | + $this->setField(12, 'FromZip5', $zip); |
| 106 | + $this->setField(13, 'FromZip4', $zip4); |
| 107 | + $this->setField(14, 'FromPhone', $phone); |
| 108 | + |
| 109 | + return $this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Set the to address |
| 114 | + * @param string $firstName |
| 115 | + * @param string $lastName |
| 116 | + * @param string $company |
| 117 | + * @param string $address |
| 118 | + * @param string $city |
| 119 | + * @param string $state |
| 120 | + * @param string $zip |
| 121 | + * @param string $address2 |
| 122 | + * @param string $zip4 |
| 123 | + * @param string $phone |
| 124 | + * @return object |
| 125 | + */ |
| 126 | + public function setToAddress($firstName, $lastName, $company, $address, $city, $state, $zip, $address2=null, $zip4=null, $phone=null) { |
| 127 | + $this->setField(15, 'ToFirstName', $firstName); |
| 128 | + $this->setField(16, 'ToLastName', $lastName); |
| 129 | + $this->setField(17, 'ToFirm', $company); |
| 130 | + $this->setField(18, 'ToAddress1', $address2); |
| 131 | + $this->setField(19, 'ToAddress2', $address); |
| 132 | + $this->setField(20, 'ToCity', $city); |
| 133 | + $this->setField(21, 'ToState', $state); |
| 134 | + $this->setField(22, 'ToZip5', $zip); |
| 135 | + $this->setField(23, 'ToZip4', $zip4); |
| 136 | + $this->setField(24, 'ToPhone', $phone); |
| 137 | + |
| 138 | + return $this; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Set package weight in ounces |
| 143 | + * |
| 144 | + */ |
| 145 | + public function setWeight($weight) { |
| 146 | + $this->setField(25, 'WeightInOunces', $weight); |
| 147 | + return $this; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Set any other requried string make sure you set the correct position as well |
| 152 | + * as the position of the items matters |
| 153 | + * @param int $position |
| 154 | + * @param string $key |
| 155 | + * @param string $value |
| 156 | + * @return object |
| 157 | + */ |
| 158 | + public function setField($position, $key, $value) { |
| 159 | + $this->fields[$position . ':' . $key] = $value; |
| 160 | + return $this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Add missing required elements |
| 165 | + * @return void |
| 166 | + */ |
| 167 | + protected function addMissingRequired() { |
| 168 | + $required = array( |
| 169 | + '1:Option' => '', |
| 170 | + '1.1:Revision' => '2', |
| 171 | + '2:EMCAAccount' => '', |
| 172 | + '3:EMCAPassword' => '', |
| 173 | + '4:ImageParameters' => '', |
| 174 | + '26:FlatRate' => '', |
| 175 | + '27:SundayHolidayDelivery' => '', |
| 176 | + '28:StandardizeAddress' => '', |
| 177 | + '29:WaiverOfSignature' => '', |
| 178 | + '30:NoHoliday' => '', |
| 179 | + '31:NoWeekend' => '', |
| 180 | + '32:SeparateReceiptPage' => '', |
| 181 | + '33:POZipCode' => '', |
| 182 | + '34:FacilityType' => 'DDU', |
| 183 | + '35:ImageType' => 'PDF', |
| 184 | + '36:LabelDate' => '', |
| 185 | + '37:CustomerRefNo' => '', |
| 186 | + '38:SenderName' => '', |
| 187 | + '39:SenderEMail' => '', |
| 188 | + '40:RecipientName' => '', |
| 189 | + '41:RecipientEMail' => '', |
| 190 | + '42:HoldForManifest' => '', |
| 191 | + '43:CommercialPrice' => 'false', |
| 192 | + '44:InsuredAmount' => '', |
| 193 | + '45:Container' => '', |
| 194 | + '46:Size' => 'REGULAR', |
| 195 | + '47:Width' => '', |
| 196 | + '48:Length' => '', |
| 197 | + '49:Height' => '', |
| 198 | + '50:Girth' => '', |
| 199 | + ); |
| 200 | + foreach($required as $item => $value) { |
| 201 | + $explode = explode(':', $item); |
| 202 | + |
| 203 | + if(!isset($this->fields[$explode[1]])) { |
| 204 | + $this->setField($explode[0], $explode[1], $value); |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments