| 1 | package Business::FedEx::ShipRequest; | ||
| 2 | |||
| 3 | use LWP::Simple; | ||
| 4 | |||
| 5 | use Business::FedEx; | ||
| 6 | use Business::FedEx::Constants qw(:all); | ||
| 7 | |||
| 8 | our @ISA = qw(Business::FedEx); | ||
| 9 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||
| 10 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 11 | our @EXPORT = qw(); | ||
| 12 | |||
| 13 | our $VERSION = '0.01'; | ||
| 14 | |||
| 15 | our $errstr = ""; | ||
| 16 | our $err = 0; | ||
| 17 | |||
| 18 | |||
| 19 | #--/-INIT ARGS-/-------------------------------------------------------# | ||
| 20 | sub new { | ||
| 21 | my $proto = shift; | ||
| 22 | my $class = ref($proto) || $proto; | ||
| 23 | my %args = (transaction_id => '32826482', #--/-REQUIRED - Any number you want - used for error tracking | ||
| 24 | account_number => '', #--/-REQUIRED - Your 9 digit FedEx Account number | ||
| 25 | weight => '', #--/-REQUIRED | ||
| 26 | weight_units =>'LBS', | ||
| 27 | service => '1', | ||
| 28 | special_services => '', | ||
| 29 | ship_date => '', | ||
| 30 | declared_value => '', | ||
| 31 | cod_flag => 'N', | ||
| 32 | cod_amount => '', | ||
| 33 | cod_cashiers_check => '', | ||
| 34 | special_services => 'NNNNNNNNNNNNN', | ||
| 35 | dry_ice_weight => '', | ||
| 36 | meter_number => '', #--/-REQUIRED - Your 7 digit FedEx meter number | ||
| 37 | gif_label => '400', | ||
| 38 | tracking_num => '', | ||
| 39 | |||
| 40 | shipper_name => '', #--/-REQUIRED - | ||
| 41 | shipper_company => '', #--/-REQUIRED - | ||
| 42 | shipper_address1 => '', #--/-REQUIRED - | ||
| 43 | shipper_address2 => '', | ||
| 44 | shipper_city => '', #--/-REQUIRED - | ||
| 45 | shipper_zip_code => '', #--/-REQUIRED - | ||
| 46 | shipper_state => '', #--/-REQUIRED - | ||
| 47 | shipper_phone => '', #--/-REQUIRED - | ||
| 48 | |||
| 49 | dest_company => '', #--/-REQUIRED - | ||
| 50 | dest_address1 => '', #--/-REQUIRED - | ||
| 51 | dest_address2 => '', | ||
| 52 | dest_city => '', #--/-REQUIRED - | ||
| 53 | dest_zip_code => '', #--/-REQUIRED - | ||
| 54 | dest_state => '', #--/-REQUIRED - | ||
| 55 | dest_phone => '', #--/-REQUIRED - | ||
| 56 | recipient_department => '', | ||
| 57 | payor_account_number => '', #--/-REQUIRED - 10 digit fedex account # of payor | ||
| 58 | payment_code => '1', #--/-REQUIRED - | ||
| 59 | reference_info => '', | ||
| 60 | signature_release_flag => 'N', | ||
| 61 | # | ||
| 62 | hal_station_address1 => '', | ||
| 63 | hal_city => '', | ||
| 64 | hal_state => '', | ||
| 65 | hal_zip_code => '', | ||
| 66 | pkg_height => '', | ||
| 67 | pkg_width => '', | ||
| 68 | pkg_length => '', | ||
| 69 | dim_units => '', | ||
| 70 | future_day_ship => '', | ||
| 71 | dest_country_code => 'US', | ||
| 72 | ult_dest_country_code => 'US', | ||
| 73 | shipper_country_code => 'US', | ||
| 74 | #INTERNATIONAL ONLY----- | ||
| 75 | duties_pay_type => '', | ||
| 76 | duties_account_num => '', | ||
| 77 | pkg_description => '', | ||
| 78 | manufacture_country_code => '', | ||
| 79 | currency_type => '', | ||
| 80 | carriage_value => '', | ||
| 81 | unit_weight1 => '', | ||
| 82 | unit_value1 => '', | ||
| 83 | quantity1 => '', | ||
| 84 | unit_measure1 => '', | ||
| 85 | #local_ship_time => '', | ||
| 86 | customs_value => '', | ||
| 87 | ein_num => '', | ||
| 88 | commercial_invoice_flag => '', | ||
| 89 | hal_phone => '', | ||
| 90 | @_); | ||
| 91 | my $self = $class->SUPER::new(%args); | ||
| 92 | return $self; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub DESTROY { | ||
| 96 | # warn "DESTROYING: ", ref(shift), "\n"; | ||
| 97 | } | ||
| 98 | |||
| 99 | { | ||
| 100 | |||
| 101 | #--/-REQUEST A SHIPMENT-/--------------------------------------# | ||
| 102 | sub ship{ | ||
| 103 | my $self = shift; | ||
| 104 | my ($username, $password, $url, $trans_type) = @_; | ||
| 105 | my $response; | ||
| 106 | my $gif_data; | ||
| 107 | my %types = (domestic=>'0,"1"',international=>'0,"51"'); | ||
| 108 | my $buf = ($types{$trans_type} || '0,"1"'); | ||
| 109 | my $field; | ||
| 110 | my $response; | ||
| 111 | my $value; | ||
| 112 | my $gif; | ||
| 113 | my $data; | ||
| 114 | my $finish_data; | ||
| 115 | my $tmp_hex = ''; | ||
| 116 | my $count = 0; | ||
| 117 | my @fedex_response; | ||
| 118 | my @field_data; | ||
| 119 | my $tmpbuf = 'abc'; | ||
| 120 | |||
| 121 | my @Keys = keys(%$self); | ||
| 122 | foreach(@Keys){ | ||
| 123 | $field = join(',',eval(uc($_)),'"'.$self->{$_}.'"'); | ||
| 124 | $buf = $buf.$field; | ||
| 125 | } | ||
| 126 | |||
| 127 | #--/-SEND OUT REQUEST TO FEDEX THROUGH SHIPAPI OR VIA PROXY (fedex.pl)-/-----------------# | ||
| 128 | $response = $self->_send_buf($url, $buf, $username, $password);#get($args); | ||
| 129 | |||
| 130 | $response =~ m/(.*188,)(.*)(99.*)/; | ||
| 131 | #@fedex_response = split(/\"\w+,\"/,$response); | ||
| 132 | @fedex_response = split(//,$response); | ||
| 133 | foreach(@fedex_response){ | ||
| 134 | #Grab Buffer Before GIF data | ||
| 135 | if(! ($data =~ /188,\"/i)){ | ||
| 136 | if((! ($_ =~ / /))&&(! ($_ =~ /\n/))){ | ||
| 137 | $data = $data.$_; | ||
| 138 | $field_data[$count] = $field_data[$count].$_; | ||
| 139 | if($field_data[$count] =~ /\d+,\"\w*\"/){ | ||
| 140 | $count++; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | }else{ | ||
| 144 | #Grab Buffer After GIF data | ||
| 145 | if($_ =~ /\"/){$finish_data = 1;} | ||
| 146 | if(($finish_data == 1)&&($_ ne ' ')){ | ||
| 147 | $data = $data.$_; | ||
| 148 | $field_data[$count] = $field_data[$count].$_; | ||
| 149 | if($field_data[$count] =~ /\d+,\"\w*\"/){ | ||
| 150 | $count++; | ||
| 151 | } | ||
| 152 | }else{ | ||
| 153 | $raw_gif = $raw_gif.$_; | ||
| 154 | #Parse GIF Buffer (Escape seq = '%'), Refer to FedEx API for limmited details. | ||
| 155 | #The following is VERY touchy. Please backup before editing! | ||
| 156 | if($_ eq '%'){ | ||
| 157 | $tmp_hex = '0x'; | ||
| 158 | }else{ | ||
| 159 | if($tmp_hex eq '0x'){ | ||
| 160 | $tmp_hex = $tmp_hex.$_; | ||
| 161 | }else{ | ||
| 162 | if($tmp_hex =~ /0x\w/){ | ||
| 163 | $tmp_hex = $tmp_hex.$_; | ||
| 164 | if(chr(hex($tmp_hex)) eq '"'){ | ||
| 165 | #print '"'; | ||
| 166 | $gif_data = $gif_data.'"'; | ||
| 167 | }else{ | ||
| 168 | if(chr(hex($tmp_hex)) eq ' '){ | ||
| 169 | #print '%00'; | ||
| 170 | $gif_data = $gif_data.'%00'; | ||
| 171 | }else{ | ||
| 172 | if(chr(hex($tmp_hex)) eq '%'){ | ||
| 173 | #print '%'; | ||
| 174 | $gif_data = $gif_data.'%'; | ||
| 175 | }else{ | ||
| 176 | #print chr(hex($tmp_hex)); | ||
| 177 | $gif_data = $gif_data.chr(hex($tmp_hex)); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | $tmp_hex = ''; | ||
| 182 | }else{ | ||
| 183 | if($_ eq ' '){ | ||
| 184 | #print $_; | ||
| 185 | $gif_data = $gif_data.$_ | ||
| 186 | }else{ | ||
| 187 | if($_ eq '%'){ | ||
| 188 | #print '%25'; | ||
| 189 | $gif_data = $gif_data.'%25'; | ||
| 190 | }else{ | ||
| 191 | #print $_; | ||
| 192 | $gif_data = $gif_data.$_; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | } | ||
| 201 | $self->{'gif_data'} = $gif_data; | ||
| 202 | $self->{'raw_gif'} = $raw_gif; | ||
| 203 | #--Set The Private Variables--# | ||
| 204 | foreach(@field_data){ | ||
| 205 | ($field,$value) = split(/\,/,$_); | ||
| 206 | if(eval('FE'."$field")){ | ||
| 207 | $value =~ s/\"//g; | ||
| 208 | $self->{eval('FE'."$field")} = $value; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | #$self->_set_data($response); | ||
| 212 | } | ||
| 213 | |||
| 214 | #--/-GET A SHIPPING RATE-/--------------------------------------# | ||
| 215 | sub rate{ | ||
| 216 | my $self = shift; | ||
| 217 | my ($username, $password, $url, $transaction_type) = @_; | ||
| 218 | my $response; | ||
| 219 | my %types = (domestic=>'0,"2"',international=>'0,"52"'); | ||
| 220 | my $field; | ||
| 221 | my $value; | ||
| 222 | my $count = 0; | ||
| 223 | #Transaction type: Request Rate | ||
| 224 | my $buf = ($types{$transaction_type} || '0,"2"'); | ||
| 225 | my @fedex_response; | ||
| 226 | my @field_data; | ||
| 227 | my @Keys = keys(%$self); | ||
| 228 | my $tmp; | ||
| 229 | my $args; | ||
| 230 | my $response; | ||
| 231 | |||
| 232 | foreach(@Keys){ | ||
| 233 | $field = join(',',eval(uc($_)),'"'.$self->{$_}.'"'); | ||
| 234 | $buf = $buf.$field; | ||
| 235 | } | ||
| 236 | $response = $self->_send_buf($url,$buf,$username,$password); | ||
| 237 | #$response =~ s/\ //g; | ||
| 238 | #Group the buffer in a 'logical' sense (logical according to FedEx!) | ||
| 239 | #Store the data in Private Vars | ||
| 240 | $self->_set_data($response); | ||
| 241 | |||
| 242 | } | ||
| 243 | |||
| 244 | #--/-TRACK A FEDEX PACKAGE-/-----------------------------------# | ||
| 245 | sub track{ | ||
| 246 | my $self = shift; | ||
| 247 | my ($username, $password, $url, $tracking_num) = @_; | ||
| 248 | my $buf = '0,"402"'; | ||
| 249 | my $field; | ||
| 250 | my @fedex_response; | ||
| 251 | my @field_data; | ||
| 252 | my $count; | ||
| 253 | my $response; | ||
| 254 | |||
| 255 | $self->{tracking_num} = $tracking_num; | ||
| 256 | my @Keys = keys(%$self); | ||
| 257 | foreach(@Keys){ | ||
| 258 | $field = join(',',eval(uc($_)),'"'.$self->{$_}.'"'); | ||
| 259 | $buf = $buf.$field; | ||
| 260 | } | ||
| 261 | $response = $self->_send_buf($url,$buf,$username,$password); | ||
| 262 | #Store the data in Private Vars | ||
| 263 | $self->_set_data($response); | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | #--/-SEND BUFFER TO FEDEX-/------------------------------------# | ||
| 268 | sub _send_buf{ | ||
| 269 | my $self = shift; | ||
| 270 | my ($url,$buf,$username,$password) = @_; | ||
| 271 | my $response; | ||
| 272 | if(($url eq '')||($url eq 'localhost')){ | ||
| 273 | eval('use Business::FedEx::ShipAPI'); | ||
| 274 | my $fedex = Business::FedEx::ShipAPI->new(username=>$username, password=>$password); | ||
| 275 | $buf = $buf.'99,""'; | ||
| 276 | $fedex->connect(); | ||
| 277 | $response = $fedex->transaction($buf); | ||
| 278 | $fedex->disconnect(); | ||
| 279 | $fedex = undef; | ||
| 280 | }else{ | ||
| 281 | $response = get("$url\?buf=\'$buf\'"); | ||
| 282 | } | ||
| 283 | return $response; | ||
| 284 | } | ||
| 285 | |||
| 286 | #--/-SET VALUES-/---------------------------------------------# | ||
| 287 | sub _set_data{ | ||
| 288 | my $self = shift; | ||
| 289 | my ($response) = @_; | ||
| 290 | my @fedex_response; | ||
| 291 | my @field_data; | ||
| 292 | my $count; | ||
| 293 | |||
| 294 | @fedex_response = split(//,$response); | ||
| 295 | foreach(@fedex_response){ | ||
| 296 | $field_data[$count] = $field_data[$count].$_; | ||
| 297 | if($field_data[$count] =~ /\d+,\".*\"$/){ | ||
| 298 | $count++; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | foreach(@field_data){ | ||
| 302 | ($field,$value) = split(/\,/,$_); | ||
| 303 | if(eval('FE'."$field")){ | ||
| 304 | $value =~ s/\"//g; | ||
| 305 | $self->{eval('FE'."$field")} = $value; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | #--Helper function that formats the rate. | ||
| 311 | sub _format_rate{ | ||
| 312 | (my $rate) = @_; | ||
| 313 | my $dollars = substr($rate,0,-2); | ||
| 314 | my $cents = substr($rate,-2); | ||
| 315 | if(! $dollars){ | ||
| 316 | $dollars = 0; | ||
| 317 | } | ||
| 318 | $rate = join('.',$dollars,$cents); | ||
| 319 | return $rate; | ||
| 320 | } | ||
| 321 | |||
| 322 | #--/-RETURN STORED VALUES-/------------------------------------# | ||
| 323 | sub get_data{ | ||
| 324 | my $self = shift; | ||
| 325 | my ($field) = @_; | ||
| 326 | if($self->{$field}){ | ||
| 327 | if($field =~ /charge/){ | ||
| 328 | return _format_rate($self->{$field}); | ||
| 329 | }else{ | ||
| 330 | return $self->{$field}; | ||
| 331 | } | ||
| 332 | }else{ | ||
| 333 | return $self->{'error_mesg'}; | ||
| 334 | } | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | } | ||
| 339 | |||
| 340 | 1; | ||
| 341 | __END__ |