Shipment/UPS.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.4% 42 / 44 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Shipment/Temando.pm ↑ Summary Shipment/CanadaPost/XSD.pm »
1 package Shipment::UPS;
2 $Shipment::UPS::VERSION = '3.11';
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Shipment::SOAP::WSDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 extends 'Shipment::Base';
16  
17  
18 has 'username' => (
19 is => 'rw',
20 isa => Str,
21 );
22  
23 has 'password' => (
24 is => 'rw',
25 isa => Str,
26 );
27  
28 has 'key' => (
29 is => 'rw',
30 isa => Str,
31 );
32  
33  
34 has 'proxy_domain' => (
35 is => 'rw',
36 isa => Enum [
37 qw(
38 wwwcie.ups.com
39 onlinetools.ups.com
40 )
41 ],
42 default => 'wwwcie.ups.com',
43 );
44  
45  
46 has 'negotiated_rates' => (
47 is => 'rw',
48 isa => Bool,
49 default => 0,
50 );
51  
52  
53 has 'residential_address' => (
54 is => 'rw',
55 isa => Bool,
56 default => 0,
57 );
58  
59  
60 has 'address_validation' => (
61 is => 'rw',
62 isa => Bool,
63 default => 1,
64 );
65  
66  
67 has 'label_height' => (
68 is => 'rw',
69 isa => Enum [qw( 6 8 )],
70 default => 6,
71 );
72  
73  
74 has 'control_log_receipt' => (
75 is => 'rw',
76 isa => InstanceOf ['Shipment::Label'],
77 );
78  
79  
80 has 'carbon_neutral' => (
81 is => 'rw',
82 isa => Bool,
83 default => undef,
84 );
85  
86  
87 has 'saturday_delivery' => (
88 is => 'rw',
89 isa => Bool,
90 default => undef,
91 );
92  
93  
94 has 'service_map' => (
95 is => 'ro',
96 isa => HashRef [],
97 default => sub {
98 { '01' => 'UPS Next Day Air',
99 '02' => 'UPS Second Day Air',
100 '03' => 'UPS Ground',
101 '07' => 'UPS Worldwide Express',
102 '08' => 'UPS Worldwide Expedited',
103 '11' => 'UPS Standard',
104 '12' => 'UPS Three-Day Select',
105 '13' => 'UPS Next Day Air Saver',
106 '14' => 'UPS Next Day Air Early A.M.',
107 '54' => 'UPS Worldwide Express Plus',
108 '59' => 'UPS Second Day Air A.M.',
109 '65' => 'UPS Saver',
110 '82' => 'UPS Today Standard',
111 '83' => 'UPS Today Dedicated Courier',
112 '85' => 'UPS Today Express',
113 '86' => 'UPS Today Express Saver',
114 '92' => 'UPS SurePost Less than 1 lb',
115 '93' => 'UPS SurePost 1 lb or Greater',
116 '94' => 'UPS SurePost BPM',
117 '95' => 'UPS SurePost Media',
118 'CA' => {
119 '01' => 'UPS Express',
120 '13' => 'UPS Express Saver',
121 '65' => 'UPS Worldwide Express Saver',
122 '02' => 'UPS Expedited',
123 },
124 };
125 }
126 );
127  
128 ## Rating code to Shipping code map for cases when they differ
129 has 'service_code_map' => (
130 is => 'ro',
131 isa => HashRef [],
132 default => sub {
133 { 'CA' => {
134 '07' => '01',
135 '13' => '65',
136 '02' => '08',
137 },
138 };
139 }
140 );
141  
142  
143 has 'bill_type_map' => (
144 is => 'ro',
145 isa => HashRef [],
146 default => sub {
147 { 'sender' => 'BillShipper',
148 'recipient' => 'BillReceiver',
149 'third_party' => 'BillThirdParty',
150 };
151 }
152 );
153  
154 has 'signature_type_map' => (
155 is => 'ro',
156 isa => HashRef [],
157 default => sub {
158 { 'default' => undef,
159 'required' => '2',
160 'not_required' => undef,
161 'adult' => '3',
162 };
163 }
164 );
165  
166  
167 has 'package_type_map' => (
168 is => 'ro',
169 isa => HashRef [],
170 default => sub {
171 { 'custom' => '02',
172 'envelope' => '01',
173 'tube' => '03',
174 'box' => '21',
175 'pack' => '04',
176 '25kg_box' => '24',
177 '10kg_box' => '25',
178 'pallet' => '30',
179 'small_express_box' => '2a',
180 'medium_express_box' => '2b',
181 'large_express_box' => '2c',
182 };
183 }
184 );
185  
186 has 'units_type_map' => (
187 is => 'ro',
188 isa => HashRef [],
189 default => sub {
190 { 'lb' => 'LBS',
191 'kg' => 'KGS',
192 'in' => 'IN',
193 'cm' => 'CM',
194 'oz' => 'OZS',
195 };
196 }
197 );
198  
199  
200 has '+package_type' => (
201 isa => Enum [
202 qw( custom envelope tube box pack 25kg_box 10kg_box pallet small_express_box medium_express_box large_express_box )
203 ]
204 );
205  
206 has 'printer_type_map' => (
207 is => 'ro',
208 isa => HashRef [],
209 default => sub {
210 { 'pdf' => '',
211 'thermal' => 'EPL',
212 'image' => 'GIF',
213 'ZPL' => 'ZPL',
214 'SPL' => 'SPL',
215 'STARPL' => 'STARPL',
216 };
217 }
218 );
219  
220 has 'label_content_type_map' => (
221 is => 'ro',
222 isa => HashRef [],
223 default => sub {
224 { 'thermal' => 'text/ups-epl',
225 'image' => 'image/gif',
226 'ZPL' => 'text/ups-zpl',
227 'SPL' => 'text/ups-spl',
228 'STARPL' => 'text/ups-starpl',
229 };
230 }
231 );
232  
233  
234 # FIXME: check whether this is needed:
235 #enum 'PrinterOptions' => [qw( thermal image ZPL SPL STARPL )];
236  
237 has '+printer_type' => (default => 'image',);
238  
239  
240 has '+currency' => (default => 'USD',);
241  
242  
243 has 'surepost' => (
244 is => 'rw',
245 isa => Bool,
246 default => undef,
247 );
248  
249  
250 has 'surepost_bpm' => (
251 is => 'rw',
252 isa => Bool,
253 default => undef,
254 );
255  
256  
257 has 'surepost_media' => (
258 is => 'rw',
259 isa => Bool,
260 default => undef,
261 );
262  
263  
264 sub _build_services {
265 my $self = shift;
266  
267 4 ▶ use Shipment::Package;
Subroutine BEGIN: called CC=1 SCAR=0.0
268 4 ▶ use Shipment::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 0 ▶ use Shipment::UPS::WSDL::RateInterfaces::RateService::RatePort;
Subroutine BEGIN: called CC=1 SCAR=2.6
270  
271 my $interface =
272 Shipment::UPS::WSDL::RateInterfaces::RateService::RatePort->new(
273 {proxy_domain => $self->proxy_domain,});
274 my $response;
275 my %services;
276  
277 if ($self->weight_unit ne 'oz') {
278 my $options;
279 $options->{DeliveryConfirmation}->{DCISType} =
280 $self->signature_type_map->{$self->signature_type}
281 if defined $self->signature_type_map->{$self->signature_type};
282 $options->{DeclaredValue}->{CurrencyCode} = $self->currency;
283  
284 my $rating_options;
285 $rating_options->{NegotiatedRatesIndicator} = 1
286 if $self->negotiated_rates;
287  
288 my $shipment_options;
289 $shipment_options->{UPScarbonneutralIndicator} = ''
290 if $self->carbon_neutral;
291 $shipment_options->{SaturdayDeliveryIndicator} = ''
292 if $self->saturday_delivery;
293  
294 my @pieces;
295 foreach (@{$self->packages}) {
296 $options->{DeclaredValue}->{MonetaryValue} =
297 $self->insured_value->value;
298  
299 push @pieces,
300 { PackagingType => {
301 Code => $self->package_type_map->{$self->package_type}
302 || $self->package_type,
303 },
304 Dimensions => {
305 UnitOfMeasurement => {
306 Code => $self->units_type_map->{$self->dim_unit}
307 || $self->dim_unit,
308 },
309 Length => $_->length,
310 Width => $_->width,
311 Height => $_->height,
312 },
313 PackageWeight => {
314 UnitOfMeasurement => {
315 Code => $self->units_type_map->{$self->weight_unit}
316 || $self->weight_unit,
317 },
318 Weight => $_->weight,
319 },
320 PackageServiceOptions => $options,
321 };
322 }
323  
324 my @from_addresslines = (
325 $self->from_address->address1,
326 $self->from_address->address2,
327 $self->from_address->address3
328 );
329 my @to_addresslines = (
330 $self->to_address->address1,
331 $self->to_address->address2,
332 $self->to_address->address3
333 );
334  
335 my $shipto = {
336 Address => {
337 AddressLine => \@to_addresslines,
338 City => $self->to_address()->city,
339 StateProvinceCode => $self->to_address()->province_code,
340 PostalCode => $self->to_address()->postal_code,
341 CountryCode => $self->to_address()->country_code,
342 },
343 };
344 $shipto->{Address}->{ResidentialAddressIndicator} = 1
345 if $self->{residential_address};
346 $shipto->{Phone}{Number} = $self->to_address->phone
347 if $self->to_address->phone;
348  
349 try {
350 $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1;
351 $response = $interface->ProcessRate(
352 { Request => {RequestOption => 'Shop',},
353 Shipment => {
354 Shipper => {
355 ShipperNumber => $self->account,
356 Address => {
357 AddressLine => \@from_addresslines,
358 City => $self->from_address()->city,
359 StateProvinceCode =>
360 $self->from_address()->province_code,
361 PostalCode =>
362 $self->from_address()->postal_code,
363 CountryCode =>
364 $self->from_address()->country_code,
365 },
366 },
367 ShipTo => $shipto,
368 ShipmentRatingOptions => $rating_options,
369 Package => \@pieces,
370 ShipmentServiceOptions => $shipment_options,
371 },
372 },
373 { UsernameToken => {
374 Username => $self->username,
375 Password => $self->password,
376 },
377 ServiceAccessToken => {AccessLicenseNumber => $self->key,},
378 },
379 );
380 $Shipment::SOAP::WSDL::Debug = 0;
381 warn "Response\n" . $response if $self->debug > 1;
382  
383 $response->can('get_RatedShipment') or die 'no services available';
384  
385 foreach my $service (@{$response->get_RatedShipment()}) {
386 my $rate = $service->get_TotalCharges->get_MonetaryValue;
387 my $currency = $service->get_TotalCharges->get_CurrencyCode;
388 if ($self->negotiated_rates) {
389 if ($service->get_NegotiatedRateCharges) {
390 $rate =
391 $service->get_NegotiatedRateCharges->get_TotalCharge
392 ->get_MonetaryValue;
393 $currency =
394 $service->get_NegotiatedRateCharges->get_TotalCharge
395 ->get_CurrencyCode;
396 }
397 }
398 $services{$service->get_Service()->get_Code()->get_value} =
399 Shipment::Service->new(
400 id => $service->get_Service()->get_Code()->get_value,
401 name => (
402 $self->service_map->{
403 $self->from_address()->country_code
404 }->{$service->get_Service()->get_Code()->get_value}
405 || $self->service_map->{
406 $service->get_Service()->get_Code()->get_value
407 }
408 ),
409 cost => Data::Currency->new($rate, $currency),
410 );
411 }
412 $services{ground} = $services{'03'} || $services{'11'} || undef;
413 $services{express} =
414 $services{'02'} || $services{'13'} || $services{'65'} || undef;
415 $services{priority} = $services{'01'} || undef;
416 foreach (qw/ground express priority/) {
417 delete $services{$_} if !$services{$_};
418 }
419  
420 $self->notice('');
421 if ($response->get_Response->get_Alert) {
422 foreach my $alert (@{$response->get_Response->get_Alert}) {
423 warn "Notice: " . $alert->get_Description->get_value
424 if $self->debug;
425 $self->add_notice(
426 $alert->get_Description->get_value . "\n");
427 }
428 }
429  
430 }
431 catch {
432 warn $_ if $self->debug;
433 try {
434 my $error = join "\n", map {
435 $_->get_PrimaryErrorCode()->get_Code() . ' - '
436 . $_->get_PrimaryErrorCode()->get_Description;
437 } @{$response->get_detail()->get_Errors()->get_ErrorDetail()
438 };
439 warn $error if $self->debug;
440 $self->error($error);
441 }
442 catch {
443 warn $_ if $self->debug;
444 warn "Error: " . $response->get_faultstring if $self->debug;
445 $self->error($response->get_faultstring->get_value);
446 };
447 };
448 }
449  
450 if ($self->surepost) {
451 if ($self->error) {
452 $self->add_notice(
453 'All services other than SurePost failed due to error: '
454 . $self->error
455 . "\n");
456 $self->error('');
457 }
458  
459 if ($self->weight_unit eq 'oz') {
460 $services{92} = Shipment::Service->new(
461 id => '92',
462 name => $self->service_map->{92},
463 );
464  
465 $services{surepost_oz} = $services{92};
466  
467 }
468 else {
469 $services{93} = Shipment::Service->new(
470 id => '93',
471 name => $self->service_map->{93},
472 );
473 $services{surepost} = $services{93};
474  
475 # These are contract specific with UPS
476 # They are disabled by default
477 if ($self->surepost_bpm) {
478 $services{94} = Shipment::Service->new(
479 id => '94',
480 name => $self->service_map->{94},
481 );
482 $services{surepost_bpm} = $services{94};
483 }
484  
485 if ($self->surepost_media) {
486  
487 $services{95} = Shipment::Service->new(
488 id => '95',
489 name => $self->service_map->{95},
490 );
491 $services{surepost_media} = $services{95};
492 }
493  
494 }
495  
496  
497 }
498  
499 \%services;
500 }
501  
502  
503 sub rate {
504 my ($self, $service_id) = @_;
505  
506 try {
507 $service_id = $self->services->{$service_id}->id;
508 }
509 catch {
510 warn $_ if $self->debug;
511 warn "service ($service_id) not available" if $self->debug;
512 $self->error("service ($service_id) not available");
513 $service_id = '';
514 };
515 return unless $service_id;
516  
517 my $options;
518 $options->{DeliveryConfirmation}->{DCISType} =
519 $self->signature_type_map->{$self->signature_type}
520 if defined $self->signature_type_map->{$self->signature_type};
521 $options->{DeclaredValue}->{CurrencyCode} = $self->currency;
522  
523 my $rating_options;
524 $rating_options->{NegotiatedRatesIndicator} = 1 if $self->negotiated_rates;
525  
526 my $shipment_options;
527 $shipment_options->{UPScarbonneutralIndicator} = ''
528 if $self->carbon_neutral;
529 $shipment_options->{SaturdayDeliveryIndicator} = ''
530 if $self->saturday_delivery;
531  
532 my @pieces;
533 foreach (@{$self->packages}) {
534 $options->{DeclaredValue}->{MonetaryValue} =
535 $self->insured_value->value;
536  
537 ## SurePost doesn't accept service options
538 $options = undef if $self->surepost && $service_id =~ /9[2-5]/;
539  
540 push @pieces,
541 { PackagingType => {
542 Code => $self->package_type_map->{$self->package_type}
543 || $self->package_type,
544 },
545 Dimensions => {
546 UnitOfMeasurement => {
547 Code => $self->units_type_map->{$self->dim_unit}
548 || $self->dim_unit,
549 },
550 Length => $_->length,
551 Width => $_->width,
552 Height => $_->height,
553 },
554 PackageWeight => {
555 UnitOfMeasurement => {
556 Code => $self->units_type_map->{$self->weight_unit}
557 || $self->weight_unit,
558 },
559 Weight => $_->weight,
560 },
561 PackageServiceOptions => $options,
562 };
563 }
564  
565 my @from_addresslines = (
566 $self->from_address->address1,
567 $self->from_address->address2,
568 $self->from_address->address3
569 );
570 my @to_addresslines = (
571 $self->to_address->address1,
572 $self->to_address->address2,
573 $self->to_address->address3
574 );
575  
576  
577 my $shipto = {
578 Address => {
579 AddressLine => \@to_addresslines,
580 City => $self->to_address()->city,
581 StateProvinceCode => $self->to_address()->province_code,
582 PostalCode => $self->to_address()->postal_code,
583 CountryCode => $self->to_address()->country_code,
584 },
585 };
586 $shipto->{Address}->{ResidentialAddressIndicator} = 1
587 if $self->{residential_address};
588 $shipto->{Phone}{Number} = $self->to_address->phone
589 if $self->to_address->phone;
590  
591 use Shipment::UPS::WSDL::RateInterfaces::RateService::RatePort;
592  
593 my $interface =
594 Shipment::UPS::WSDL::RateInterfaces::RateService::RatePort->new(
595 {proxy_domain => $self->proxy_domain,});
596  
597 my $response;
598 try {
599 $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1;
600 $response = $interface->ProcessRate(
601 { Request => {RequestOption => 'Rate',},
602 Shipment => {
603 Shipper => {
604 ShipperNumber => $self->account,
605 Address => {
606 AddressLine => \@from_addresslines,
607 City => $self->from_address->city,
608 StateProvinceCode =>
609 $self->from_address->province_code,
610 PostalCode => $self->from_address->postal_code,
611 CountryCode => $self->from_address->country_code,
612 },
613 },
614 ShipTo => $shipto,
615 ShipmentRatingOptions => $rating_options,
616 Service => {Code => $service_id,},
617 Package => \@pieces,
618 ShipmentServiceOptions => $shipment_options,
619 },
620 },
621 { UsernameToken => {
622 Username => $self->username,
623 Password => $self->password,
624 },
625 ServiceAccessToken => {AccessLicenseNumber => $self->key,},
626 },
627 );
628 $Shipment::SOAP::WSDL::Debug = 0;
629 warn "Response\n" . $response if $self->debug > 1;
630  
631 $response->can('get_RatedShipment') or die 'service not available';
632  
633 use Data::Currency;
634 use Shipment::Service;
635 my $rate =
636 $response->get_RatedShipment->get_TotalCharges->get_MonetaryValue;
637 my $currency =
638 $response->get_RatedShipment->get_TotalCharges->get_CurrencyCode;
639 if ($self->negotiated_rates) {
640 if ($response->get_RatedShipment->get_NegotiatedRateCharges) {
641 $rate = $response->get_RatedShipment->get_NegotiatedRateCharges
642 ->get_TotalCharge->get_MonetaryValue;
643 $currency =
644 $response->get_RatedShipment->get_NegotiatedRateCharges
645 ->get_TotalCharge->get_CurrencyCode;
646 }
647 }
648 $self->service(
649 Shipment::Service->new(
650 id => $service_id,
651 name => (
652 $self->service_map->{$self->from_address()->country_code}
653 ->{
654 $response->get_RatedShipment->get_Service->get_Code
655 ->get_value
656 }
657 || $self->service_map->{
658 $response->get_RatedShipment->get_Service->get_Code
659 ->get_value
660 }
661 ),
662 cost => Data::Currency->new($rate, $currency),
663 )
664 );
665  
666 $self->notice('');
667 if ($response->get_Response->get_Alert) {
668 foreach my $alert (@{$response->get_Response->get_Alert}) {
669 warn $alert->get_Description->get_value if $self->debug;
670 $self->add_notice($alert->get_Description->get_value . "\n");
671 }
672 }
673 }
674 catch {
675 warn $_ if $self->debug;
676 try {
677 my $error = join "\n", map {
678 $_->get_PrimaryErrorCode()->get_Code() . ' - '
679 . $_->get_PrimaryErrorCode()->get_Description;
680 } @{$response->get_detail()->get_Errors()->get_ErrorDetail()};
681 warn $error if $self->debug;
682 $self->error($error);
683 }
684 catch {
685 warn $_ if $self->debug;
686 warn $response->get_faultstring if $self->debug;
687 $self->error($response->get_faultstring->get_value);
688 };
689 };
690  
691 }
692  
693  
694 sub ship {
695 my ($self, $service_id) = @_;
696  
697 try {
698 $service_id = $self->services->{$service_id}->id;
699 }
700 catch {
701 warn $_ if $self->debug;
702 warn "service ($service_id) not available" if $self->debug;
703 $self->error("service ($service_id) not available");
704 $service_id = '';
705 };
706 return unless $service_id;
707  
708 my $package_options;
709 $package_options->{DeliveryConfirmation}->{DCISType} =
710 $self->signature_type_map->{$self->signature_type}
711 if defined $self->signature_type_map->{$self->signature_type};
712 $package_options->{DeclaredValue}->{CurrencyCode} = $self->currency;
713  
714 my $shipment_options;
715 if ($self->to_address->email) {
716 $shipment_options->{Notification}->{NotificationCode} = '6';
717 $shipment_options->{Notification}->{EMail}->{EMailAddress} =
718 $self->to_address->email;
719 $shipment_options->{Notification}->{EMail}->{SubjectCode} = '03';
720 }
721 $shipment_options->{UPScarbonneutralIndicator} = ''
722 if $self->carbon_neutral;
723 $shipment_options->{SaturdayDeliveryIndicator} = ''
724 if $self->saturday_delivery;
725  
726 my $rating_options;
727 $rating_options->{NegotiatedRatesIndicator} = 1 if $self->negotiated_rates;
728  
729 my @pieces;
730 my $reference_index = 1;
731 foreach (@{$self->packages}) {
732 $package_options->{DeclaredValue}->{MonetaryValue} =
733 $self->insured_value->value;
734  
735 ## SurePost doesn't accept service options
736 $package_options = undef if $self->surepost && $service_id =~ /9[2-5]/;
737  
738 my @references;
739 if ( $self->references
740 && $self->from_address->country_code =~ /(US|PR)/
741 && $self->to_address->country_code =~ /(US|PR)/
742 && $self->from_address->country_code eq
743 $self->to_address->country_code)
744 {
745 foreach ($self->get_reference(0), $self->get_reference(1)) {
746 next if !$_;
747  
748 my ($code, $val);
749 if (ref($_) eq "HASH") {
750 $code = (keys %$_)[0];
751 $val = (values %$_)[0];
752 }
753 else {
754 $code = $reference_index;
755 $val = $_;
756 }
757 push @references, {Code => $code, Value => $val,};
758 $reference_index++;
759 }
760 }
761 push @pieces,
762 { Packaging => {
763 Code => $self->package_type_map->{$self->package_type}
764 || $self->package_type,
765 },
766 Dimensions => {
767 UnitOfMeasurement => {
768 Code => $self->units_type_map->{$self->dim_unit}
769 || $self->dim_unit,
770 },
771 Length => $_->length,
772 Width => $_->width,
773 Height => $_->height,
774 },
775 PackageWeight => {
776 UnitOfMeasurement => {
777 Code => $self->units_type_map->{$self->weight_unit}
778 || $self->weight_unit,
779 },
780 Weight => $_->weight,
781 },
782 ReferenceNumber => \@references,
783 PackageServiceOptions => $package_options,
784 };
785 }
786  
787 my $payment_option;
788 $payment_option->{Type} = '01';
789 $payment_option->{$self->bill_type_map->{$self->bill_type}}
790 ->{AccountNumber} = $self->bill_account;
791 $payment_option->{$self->bill_type_map->{$self->bill_type}}->{Address}
792 ->{PostalCode} = $self->bill_address->postal_code
793 if $self->bill_address && $self->bill_type =~ /(recipient|third_party)/;
794 $payment_option->{$self->bill_type_map->{$self->bill_type}}->{Address}
795 ->{CountryCode} = $self->bill_address->country_code
796 if $self->bill_address && $self->bill_type eq 'third_party';
797  
798 my @from_addresslines = (
799 $self->from_address->address1,
800 $self->from_address->address2,
801 $self->from_address->address3
802 );
803 my @to_addresslines = (
804 $self->to_address->address1,
805 $self->to_address->address2,
806 $self->to_address->address3
807 );
808  
809 my $shipto = {
810 Name => $self->to_address->company,
811 AttentionName => $self->to_address->name,
812 Address => {
813 AddressLine => \@to_addresslines,
814 City => $self->to_address->city,
815 StateProvinceCode => $self->to_address->province_code,
816 PostalCode => $self->to_address->postal_code,
817 CountryCode => $self->to_address->country_code,
818 },
819 };
820 $shipto->{Address}->{ResidentialAddressIndicator} = 1
821 if $self->{residential_address};
822 $shipto->{Phone}{Number} = $self->to_address->phone
823 if $self->to_address->phone;
824  
825 use Shipment::UPS::WSDL::ShipInterfaces::ShipService::ShipPort;
826  
827 my $interface =
828 Shipment::UPS::WSDL::ShipInterfaces::ShipService::ShipPort->new(
829 {proxy_domain => $self->proxy_domain,});
830  
831 my $response;
832 try {
833 $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1;
834 $response = $interface->ProcessShipment(
835 { Request => {
836 RequestOption => ($self->address_validation)
837 ? 'validate'
838 : 'nonvalidate',
839 },
840 Shipment => {
841 Shipper => {
842 Name => $self->from_address->company,
843 AttentionName => $self->from_address->name,
844 ShipperNumber => $self->account,
845 Address => {
846 AddressLine => \@from_addresslines,
847 City => $self->from_address->city,
848 StateProvinceCode =>
849 $self->from_address->province_code,
850 PostalCode => $self->from_address->postal_code,
851 CountryCode => $self->from_address->country_code,
852 },
853 },
854 ShipTo => $shipto,
855 ShipmentRatingOptions => $rating_options,
856 Service => {
857 Code => (
858 $self->service_code_map->{
859 $self->from_address->country_code
860 }->{$service_id}
861 || $service_id
862 ),
863 },
864 Package => \@pieces,
865 PaymentInformation => {ShipmentCharge => $payment_option,},
866 ShipmentServiceOptions => $shipment_options,
867 },
868 LabelSpecification => {
869 LabelImageFormat => {
870 Code => $self->printer_type_map->{$self->printer_type},
871 },
872 LabelStockSize => {
873 Height => $self->label_height,
874 Width => 4,
875 },
876 },
877 },
878 { UsernameToken => {
879 Username => $self->username,
880 Password => $self->password,
881 },
882 ServiceAccessToken => {AccessLicenseNumber => $self->key,},
883 },
884 );
885 $Shipment::SOAP::WSDL::Debug = 0;
886 warn "Response\n" . $response if $self->debug > 1;
887  
888 $response->can('get_ShipmentResults')
889 or die 'failed to generate shipment';
890  
891 $self->tracking_id($response->get_ShipmentResults()
892 ->get_ShipmentIdentificationNumber()
893 ->get_value);
894 use Data::Currency;
895 use Shipment::Service;
896 my $rate = 0;
897 my $currency = $self->currency;
898 if ( $response->get_ShipmentResults->get_ShipmentCharges
899 && $response->get_ShipmentResults->get_ShipmentCharges
900 ->get_TotalCharges)
901 {
902 $rate = $response->get_ShipmentResults->get_ShipmentCharges
903 ->get_TotalCharges->get_MonetaryValue;
904 $currency = $response->get_ShipmentResults->get_ShipmentCharges
905 ->get_TotalCharges->get_CurrencyCode;
906 }
907 if ($self->negotiated_rates) {
908 if ( $response->get_ShipmentResults->get_NegotiatedRateCharges
909 && $response->get_ShipmentResults->get_NegotiatedRateCharges
910 ->get_TotalCharge)
911 {
912 $rate =
913 $response->get_ShipmentResults->get_NegotiatedRateCharges
914 ->get_TotalCharge->get_MonetaryValue;
915 $currency =
916 $response->get_ShipmentResults->get_NegotiatedRateCharges
917 ->get_TotalCharge->get_CurrencyCode;
918 }
919 }
920 $self->service(
921 Shipment::Service->new(
922 id => $service_id,
923 name => $self->services->{$service_id}->name,
924 cost => Data::Currency->new($rate, $currency),
925 )
926 );
927  
928 use Shipment::Label;
929 use MIME::Base64;
930 my $package_index = 0;
931 foreach (@{$response->get_ShipmentResults()->get_PackageResults()}) {
932 $self->get_package($package_index)
933 ->tracking_id($_->get_TrackingNumber()->get_value);
934  
935 ## For EPL labels, force Top Orientation by inserting the ZT command at the beginning of the file.
936 ## This is needed for cases when the printer defaults to the incorrect orientation.
937 my $data = "ZT\n"
938 if $self->printer_type_map->{$self->printer_type} eq 'EPL';
939 $data .= decode_base64(
940 $_->get_ShippingLabel()->get_GraphicImage->get_value);
941  
942 $self->get_package($package_index)->label(
943 Shipment::Label->new(
944 { tracking_id => $_->get_TrackingNumber()->get_value,
945 content_type =>
946 $self->label_content_type_map->{$self->printer_type},
947 data => $data,
948 file_name => $_->get_TrackingNumber()->get_value . '.'
949 . lc $self->printer_type_map->{$self->printer_type},
950 },
951 )
952 );
953 $package_index++;
954 }
955  
956 if (ref $response->get_ShipmentResults()->get_ControlLogReceipt eq
957 'ARRAY')
958 {
959 my $receipt_index = 0;
960 foreach (
961 @{$response->get_ShipmentResults()->get_ControlLogReceipt})
962 {
963 ## For EPL labels, force Top Orientation by inserting the ZT command at the beginning of the file.
964 ## This is needed for cases when the printer defaults to the incorrect orientation.
965 my $data = "ZT\n"
966 if $self->printer_type_map->{$self->printer_type} eq 'EPL';
967 $data .= decode_base64($_->get_GraphicImage->get_value);
968  
969 push @{$self->control_log_receipt},
970 Shipment::Label->new(
971 { content_type =>
972 $self->label_content_type_map->{$self->printer_type},
973 data => $data,
974 file_name => 'control_log_receipt_'
975 . $receipt_index . '.'
976 . lc $self->printer_type_map->{$self->printer_type},
977 }
978 );
979 $receipt_index++;
980 }
981 }
982  
983 $self->notice('');
984 if ($response->get_Response->get_Alert) {
985 foreach my $alert (@{$response->get_Response->get_Alert}) {
986 warn $alert->get_Description->get_value if $self->debug;
987 $self->add_notice($alert->get_Description->get_value . "\n");
988 }
989 }
990  
991 }
992 catch {
993 warn $_ if $self->debug;
994 try {
995 my $error = join "\n", map {
996 $_->get_PrimaryErrorCode()->get_Code() . ' - '
997 . $_->get_PrimaryErrorCode()->get_Description;
998 } @{$response->get_detail()->get_Errors()->get_ErrorDetail()};
999 warn $error if $self->debug;
1000 $self->error($error);
1001 }
1002 catch {
1003 warn $_ if $self->debug;
1004 warn $response->get_faultstring if $self->debug;
1005 $self->error($response->get_faultstring->get_value);
1006 };
1007 };
1008  
1009 }
1010  
1011  
1012 sub return {
1013 my ($self, $service_id, $rc) = @_;
1014  
1015 try {
1016 $service_id = $self->services->{$service_id}->id;
1017 }
1018 catch {
1019 warn $_ if $self->debug;
1020 warn "service ($service_id) not available" if $self->debug;
1021 $self->error("service ($service_id) not available");
1022 $service_id = '';
1023 };
1024 return unless $service_id;
1025  
1026 my $package_options;
1027 $package_options->{DeclaredValue}->{CurrencyCode} = $self->currency;
1028  
1029 # default return code is 9 which means we print a return label
1030 my $return_code = $rc ? $rc : 9;
1031  
1032 my @pieces;
1033 my $reference_index = 1;
1034 foreach (@{$self->packages}) {
1035 $package_options->{DeclaredValue}->{MonetaryValue} =
1036 $self->insured_value->value;
1037 my @references;
1038 if ( $self->references
1039 && $self->from_address->country_code =~ /(US|PR)/
1040 && $self->to_address->country_code =~ /(US|PR)/
1041 && $self->from_address->country_code eq
1042 $self->to_address->country_code)
1043 {
1044  
1045 foreach ($self->get_reference(0), $self->get_reference(1)) {
1046 next if !$_;
1047  
1048 my ($code, $val);
1049 if (ref($_) eq "HASH") {
1050 $code = (keys %$_)[0];
1051 $val = (values %$_)[0];
1052 }
1053 else {
1054 $code = $reference_index;
1055 $val = $_;
1056 }
1057 push @references,
1058 { Code => $code,
1059 Value => $val,
1060 };
1061 $reference_index++;
1062 }
1063 }
1064 push @pieces,
1065 { Description => 'n/a',
1066 Packaging => {
1067 Code => $self->package_type_map->{$self->package_type}
1068 || $self->package_type,
1069 },
1070 Dimensions => {
1071 UnitOfMeasurement => {
1072 Code => $self->units_type_map->{$self->dim_unit}
1073 || $self->dim_unit,
1074 },
1075 Length => $_->length,
1076 Width => $_->width,
1077 Height => $_->height,
1078 },
1079 PackageWeight => {
1080 UnitOfMeasurement => {
1081 Code => $self->units_type_map->{$self->weight_unit}
1082 || $self->weight_unit,
1083 },
1084 Weight => $_->weight,
1085 },
1086 ReferenceNumber => \@references,
1087 PackageServiceOptions => $package_options,
1088 };
1089 }
1090  
1091 my $payment_option;
1092 $payment_option->{Type} = '01';
1093 $payment_option->{$self->bill_type_map->{$self->bill_type}}
1094 ->{AccountNumber} = $self->bill_account;
1095 $payment_option->{$self->bill_type_map->{$self->bill_type}}->{Address}
1096 ->{PostalCode} = $self->bill_address->postal_code
1097 if $self->bill_address && $self->bill_type =~ /(recipient|third_party)/;
1098 $payment_option->{$self->bill_type_map->{$self->bill_type}}->{Address}
1099 ->{CountryCode} = $self->bill_address->country_code
1100 if $self->bill_address && $self->bill_type eq 'third_party';
1101  
1102 my @from_addresslines = (
1103 $self->from_address->address1,
1104 $self->from_address->address2,
1105 $self->from_address->address3
1106 );
1107 my @to_addresslines = (
1108 $self->to_address->address1,
1109 $self->to_address->address2,
1110 $self->to_address->address3
1111 );
1112  
1113 use Shipment::UPS::WSDL::ShipInterfaces::ShipService::ShipPort;
1114  
1115 my $interface =
1116 Shipment::UPS::WSDL::ShipInterfaces::ShipService::ShipPort->new(
1117 {proxy_domain => $self->proxy_domain,});
1118  
1119 my %body = (
1120 Request => {
1121 RequestOption => ($self->address_validation)
1122 ? 'validate'
1123 : 'nonvalidate',
1124 },
1125 Shipment => {
1126 ReturnService => {Code => $return_code,},
1127 Shipper => {
1128 Name => $self->from_address->company,
1129 AttentionName => $self->from_address->name,
1130 ShipperNumber => $self->account,
1131 Address => {
1132 AddressLine => \@from_addresslines,
1133 City => $self->from_address->city,
1134 StateProvinceCode => $self->from_address->province_code,
1135 PostalCode => $self->from_address->postal_code,
1136 CountryCode => $self->from_address->country_code,
1137 },
1138 },
1139 ShipFrom => {
1140 Name => $self->to_address->company,
1141 AttentionName => $self->to_address->name,
1142 Address => {
1143 AddressLine => \@to_addresslines,
1144 City => $self->to_address->city,
1145 StateProvinceCode => $self->to_address->province_code,
1146 PostalCode => $self->to_address->postal_code,
1147 CountryCode => $self->to_address->country_code,
1148 },
1149 },
1150 ShipTo => {
1151 Name => $self->from_address->company,
1152 AttentionName => $self->from_address->name,
1153 Address => {
1154 AddressLine => \@from_addresslines,
1155 City => $self->from_address->city,
1156 StateProvinceCode => $self->from_address->province_code,
1157 PostalCode => $self->from_address->postal_code,
1158 CountryCode => $self->from_address->country_code,
1159 },
1160 EMailAddress => $self->to_address->email,
1161 },
1162 Service => {Code => $service_id,},
1163 Package => \@pieces,
1164 PaymentInformation => {ShipmentCharge => $payment_option,},
1165 },
1166 LabelSpecification => {
1167 LabelImageFormat =>
1168 {Code => $self->printer_type_map->{$self->printer_type},},
1169 LabelStockSize => {
1170 Height => $self->label_height,
1171 Width => 4,
1172 },
1173 },
1174 );
1175  
1176 my $body = \%body;
1177  
1178 if ($return_code == 8) {
1179 $body->{Shipment}->{ShipmentServiceOptions} = {
1180 LabelDelivery => {
1181 EMail => {
1182 EMailAddress => $self->from_address->email,
1183 UndeliverableEMailAddress => $self->to_address->email,
1184 FromEMailAddress => $self->to_address->email,
1185 },
1186 LabelLinksIndicator => '',
1187 },
1188 };
1189 }
1190  
1191  
1192 my %header = (
1193 UsernameToken => {
1194 Username => $self->username,
1195 Password => $self->password,
1196 },
1197 ServiceAccessToken => {AccessLicenseNumber => $self->key,},
1198 );
1199  
1200 my $response;
1201 try {
1202 $response = $interface->ProcessShipment(\%body, \%header);
1203  
1204 #warn $response;
1205  
1206 $response->can('get_ShipmentResults') or die 'failed to get shipment';
1207  
1208 $self->tracking_id($response->get_ShipmentResults()
1209 ->get_ShipmentIdentificationNumber()
1210 ->get_value);
1211 use Data::Currency;
1212 use Shipment::Service;
1213 $self->service(
1214 Shipment::Service->new(
1215 id => $service_id,
1216 name => $self->services->{$service_id}->name,
1217 cost => Data::Currency->new(
1218 $response->get_ShipmentResults()
1219 ->get_ShipmentCharges->get_TotalCharges()
1220 ->get_MonetaryValue,
1221 $response->get_ShipmentResults()
1222 ->get_ShipmentCharges()
1223 ->get_TotalCharges()
1224 ->get_CurrencyCode
1225 ),
1226 )
1227 );
1228  
1229 # return_code 9 means we are getting a label back to print
1230 if ($return_code == 9) {
1231 use Shipment::Label;
1232 use MIME::Base64;
1233 my $package_index = 0;
1234 foreach (@{$response->get_ShipmentResults()->get_PackageResults()})
1235 {
1236  
1237 ## For EPL labels, force Top Orientation by inserting the ZT command at the beginning of the file.
1238 ## This is needed for cases when the printer defaults to the incorrect orientation.
1239 my $data = "ZT\n"
1240 if $self->printer_type_map->{$self->printer_type} eq 'EPL';
1241 $data .= decode_base64(
1242 $_->get_ShippingLabel()->get_GraphicImage->get_value);
1243  
1244 $self->get_package($package_index)
1245 ->tracking_id($_->get_TrackingNumber()->get_value);
1246 $self->get_package($package_index)->label(
1247 Shipment::Label->new(
1248 { tracking_id => $_->get_TrackingNumber()->get_value,
1249 content_type => $self->label_content_type_map->{
1250 $self->printer_type
1251 },
1252 data => $data,
1253 file_name => $_->get_TrackingNumber()->get_value
1254 . '.'
1255 . lc
1256 $self->printer_type_map->{$self->printer_type},
1257 },
1258 )
1259 );
1260 $package_index++;
1261 }
1262  
1263 if (ref $response->get_ShipmentResults()->get_ControlLogReceipt eq
1264 'ARRAY')
1265 {
1266 my $receipt_index = 0;
1267 foreach (
1268 @{$response->get_ShipmentResults()->get_ControlLogReceipt})
1269 {
1270  
1271 ## For EPL labels, force Top Orientation by inserting the ZT command at the beginning of the file.
1272 ## This is needed for cases when the printer defaults to the incorrect orientation.
1273 my $data = "ZT\n"
1274 if $self->printer_type_map->{$self->printer_type} eq
1275 'EPL';
1276 $data .= decode_base64($_->get_GraphicImage->get_value);
1277  
1278 push @{$self->control_log_receipt},
1279 Shipment::Label->new(
1280 { content_type => $self->label_content_type_map->{
1281 $self->printer_type
1282 },
1283 data => $data,
1284 file_name => 'control_log_receipt_'
1285 . $receipt_index . '.'
1286 . lc
1287 $self->printer_type_map->{$self->printer_type},
1288 }
1289 );
1290 $receipt_index++;
1291 }
1292 }
1293  
1294 }
1295 elsif ($return_code == 8) {
1296 use Shipment::Label;
1297 use MIME::Base64;
1298 my $package_index = 0;
1299 my $label_url =
1300 $response->get_ShipmentResults()->get_LabelURL()->get_value;
1301  
1302 # don't think this foreach makes sense here. I think you can only return one package get package_index is always 0
1303 foreach (@{$response->get_ShipmentResults()->get_PackageResults()})
1304 {
1305  
1306 $self->get_package($package_index)
1307 ->tracking_id($_->get_TrackingNumber()->get_value);
1308 $self->get_package($package_index)->label(
1309 Shipment::Label->new(
1310 { tracking_id => $_->get_TrackingNumber()->get_value,
1311 content_type => 'url',
1312 data => $label_url,
1313 },
1314 )
1315 );
1316 $package_index++;
1317 }
1318  
1319 }
1320 elsif ($return_code == 2) {
1321 use MIME::Base64;
1322 my $package_index = 0;
1323  
1324 # don't think this foreach makes sense here. I think you can only return one package get package_index is always 0
1325 foreach (@{$response->get_ShipmentResults()->get_PackageResults()})
1326 {
1327 $self->get_package($package_index)
1328 ->tracking_id($_->get_TrackingNumber()->get_value);
1329 $package_index++;
1330 }
1331  
1332 }
1333 $self->notice('');
1334 if ($response->get_Response->get_Alert) {
1335 foreach my $alert (@{$response->get_Response->get_Alert}) {
1336 warn $alert->get_Description->get_value;
1337 $self->add_notice($alert->get_Description->get_value . "\n");
1338 }
1339 }
1340  
1341 }
1342 catch {
1343 warn $_ if $self->debug;
1344 try {
1345 my $error = join "\n", map {
1346 $_->get_PrimaryErrorCode()->get_Code() . ' - '
1347 . $_->get_PrimaryErrorCode()->get_Description;
1348 } @{$response->get_detail()->get_Errors()->get_ErrorDetail()};
1349 warn $error if $self->debug;
1350 $self->error($error);
1351 }
1352 catch {
1353 warn $_ if $self->debug;
1354 warn $response->get_faultstring if $self->debug;
1355 $self->error($response->get_faultstring->get_value);
1356 };
1357 };
1358  
1359 }
1360  
1361  
1362 sub cancel {
1363 my $self = shift;
1364  
1365 if (!$self->tracking_id) {
1366 $self->error('no tracking id provided');
1367 return;
1368 }
1369  
1370 my $void->{ShipmentIdentificationNumber} = $self->tracking_id;
1371  
1372 my @tracking_ids;
1373 foreach ($self->all_packages) {
1374 push @tracking_ids, $_->tracking_id;
1375 }
1376 if ($#tracking_ids) {
1377 $void->{TrackingNumber} = \@tracking_ids;
1378 }
1379  
1380 use Shipment::UPS::WSDL::ShipInterfaces::VoidService::VoidPort;
1381 my $interface =
1382 Shipment::UPS::WSDL::ShipInterfaces::VoidService::VoidPort->new(
1383 {proxy_domain => $self->proxy_domain,});
1384  
1385 my $response;
1386 my $success;
1387  
1388 try {
1389 $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1;
1390 $response = $interface->ProcessVoid(
1391 { Request => {RequestOption => '',},
1392 VoidShipment => $void,
1393 },
1394 { UsernameToken => {
1395 Username => $self->username,
1396 Password => $self->password,
1397 },
1398 ServiceAccessToken => {AccessLicenseNumber => $self->key,},
1399 },
1400 );
1401 $Shipment::SOAP::WSDL::Debug = 0;
1402 warn "Response\n" . $response if $self->debug > 1;
1403  
1404 $response->can('get_SummaryResult') or die 'failed to cancel shipment';
1405  
1406 $success =
1407 $response->get_SummaryResult->get_Status->get_Description->get_value;
1408  
1409 $self->notice('');
1410 if ($response->get_Response->get_Alert) {
1411 foreach my $alert (@{$response->get_Response->get_Alert}) {
1412 warn $alert->get_Description->get_value if $self->debug;
1413 $self->add_notice($alert->get_Description->get_value . "\n");
1414 }
1415 }
1416  
1417 }
1418 catch {
1419 warn $_ if $self->debug;
1420 try {
1421 my $error = join "\n", map {
1422 $_->get_PrimaryErrorCode()->get_Code() . ' - '
1423 . $_->get_PrimaryErrorCode()->get_Description;
1424 } @{$response->get_detail()->get_Errors()->get_ErrorDetail()};
1425 warn $error if $self->debug;
1426 $self->error($error);
1427 }
1428 catch {
1429 warn $_ if $self->debug;
1430 warn $response->get_faultstring if $self->debug;
1431 $self->error($response->get_faultstring->get_value);
1432 };
1433 };
1434  
1435 return $success;
1436  
1437 }
1438  
1439  
1440 sub xav {
1441 my ($self, $request_option) = @_;
1442  
1443 use Shipment::UPS::WSDL::XAVInterfaces::XAVService::XAVPort;
1444 my $interface =
1445 Shipment::UPS::WSDL::XAVInterfaces::XAVService::XAVPort->new(
1446 {proxy_domain => $self->proxy_domain,});
1447  
1448 $request_option //= 1;
1449  
1450 my $response;
1451 my $success;
1452 my $result;
1453 my $classification;
1454 my @candidates;
1455  
1456 my @to_addresslines = (
1457 $self->to_address->address1,
1458 $self->to_address->address2,
1459 $self->to_address->address3
1460 );
1461  
1462 try {
1463  
1464 $response = $interface->ProcessXAV(
1465 { Request => {RequestOption => $request_option,},
1466 AddressKeyFormat => {
1467 AddressLine => \@to_addresslines,
1468 PoliticalDivision2 => $self->to_address->city,
1469 PoliticalDivision1 => $self->to_address->province_code,
1470 PostcodePrimaryLow => $self->to_address->postal_code,
1471 CountryCode => $self->to_address->country_code,
1472 }
1473 },
1474 { UsernameToken => {
1475 Username => $self->username,
1476 Password => $self->password,
1477 },
1478 ServiceAccessToken => {AccessLicenseNumber => $self->key,},
1479 },
1480  
1481 );
1482 warn $response if $self->debug > 1;
1483  
1484 if ($request_option =~ m/[23]/) {
1485 try {
1486 my $ac = $response->get_AddressClassification;
1487 $classification->{code} = $ac->get_Code->get_value();
1488 $classification->{description} =
1489 $ac->get_Description->get_value();
1490 }
1491 catch { };
1492 }
1493  
1494 try {
1495 if (defined($response->get_ValidAddressIndicator->get_value())) {
1496 $result = "valid";
1497 }
1498 }
1499 catch { };
1500  
1501 try {
1502 if (defined($response->get_AmbiguousAddressIndicator->get_value()))
1503 {
1504 $result = "invalid";
1505 }
1506  
1507 }
1508 catch { };
1509  
1510 try {
1511 if (defined($response->get_NoCandidatesIndicator->get_value())) {
1512 $result = "nocandidates";
1513 }
1514  
1515 }
1516 catch { };
1517  
1518 if ($result && $result ne "nocandidates") {
1519  
1520 # If we are asking for address classification, canidites will also
1521 # include classification results
1522 try {
1523  
1524 for my $candidate (@{$response->get_Candidate()}) {
1525 my %a_hash = (
1526 address1 => $candidate->get_AddressKeyFormat()
1527 ->get_AddressLine()
1528 ->get_value(),
1529 city => $candidate->get_AddressKeyFormat()
1530 ->get_PoliticalDivision2()
1531 ->get_value(),
1532 province => $candidate->get_AddressKeyFormat()
1533 ->get_PoliticalDivision1()
1534 ->get_value(),
1535 postal_code => $candidate->get_AddressKeyFormat()
1536 ->get_PostcodePrimaryLow()
1537 ->get_value() . "-"
1538 . $candidate->get_AddressKeyFormat()
1539 ->get_PostcodeExtendedLow()
1540 ->get_value(),
1541 country => $candidate->get_AddressKeyFormat()
1542 ->get_CountryCode()
1543 ->get_value(),
1544 );
1545  
1546 if ($request_option == 3) {
1547 $a_hash{classification}{code} =
1548 $candidate->get_AddressClassification->get_Code
1549 ->get_value();
1550 $a_hash{classification}{description} =
1551 $candidate->get_AddressClassification
1552 ->get_Description->get_value();
1553 }
1554 push @candidates, \%a_hash;
1555 }
1556  
1557 }
1558 catch { warn $_ };
1559 }
1560  
1561 }
1562 catch {
1563 warn $_ if $self->debug;
1564 try {
1565 my $error = join "\n", map {
1566 $_->get_PrimaryErrorCode()->get_Code() . ' - '
1567 . $_->get_PrimaryErrorCode()->get_Description;
1568 } @{$response->get_detail()->get_Errors()->get_ErrorDetail()};
1569 warn $error if $self->debug;
1570 $self->error($error);
1571 }
1572 catch {
1573 warn $_ if $self->debug;
1574 warn $response->get_faultstring if $self->debug;
1575 $self->error($response->get_faultstring->get_value);
1576 };
1577 };
1578  
1579 return {
1580 'result' => $result,
1581 'candidate' => \@candidates,
1582 'classification' => $classification
1583 };
1584 }
1585  
1586  
1587 sub track {
1588 my ($self) = @_;
1589  
1590 use Shipment::Activity;
1591  
1592 if (!$self->tracking_id) {
1593 $self->error('no tracking id provided');
1594 return;
1595 }
1596  
1597 use Shipment::UPS::WSDL::TrackInterfaces::TrackService::TrackPort;
1598 my $interface =
1599 Shipment::UPS::WSDL::TrackInterfaces::TrackService::TrackPort->new(
1600 {proxy_domain => $self->proxy_domain,});
1601  
1602 my $response;
1603  
1604 try {
1605  
1606 $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1;
1607 $response = $interface->ProcessTrack(
1608 { Request => { # Shipment::UPS::WSDL::TrackTypes::RequestType
1609 RequestOption => 0,
1610 },
1611 InquiryNumber => $self->tracking_id,
1612 TrackingOption => "02",
1613 },
1614 { UsernameToken => {
1615 Username => $self->username,
1616 Password => $self->password,
1617 },
1618 ServiceAccessToken => {AccessLicenseNumber => $self->key,},
1619 },
1620 );
1621  
1622 $Shipment::SOAP::WSDL::Debug = 0;
1623 warn "Response\n" . $response if $self->debug > 1;
1624  
1625 $response->can('get_Shipment') or die 'failed to get tracking';
1626  
1627 for my $activity (
1628 @{$response->get_Shipment()->get_Package()->get_Activity()})
1629 {
1630 my ($city, $state, $country);
1631 if ( $activity->get_ActivityLocation
1632 && $activity->get_ActivityLocation()->get_Address())
1633 {
1634 $city =
1635 $activity->get_ActivityLocation()
1636 ->get_Address()
1637 ->get_City()
1638 ->get_value();
1639 $state =
1640 $activity->get_ActivityLocation()
1641 ->get_Address()
1642 ->get_StateProvinceCode()
1643 ->get_value();
1644 $country =
1645 $activity->get_ActivityLocation()
1646 ->get_Address()
1647 ->get_CountryCode()
1648 ->get_value();
1649 }
1650 $self->add_activity(
1651 Shipment::Activity->new(
1652 description =>
1653 $activity->get_Status()->get_Description()->get_value(),
1654 date => DateTime::Format::ISO8601->parse_datetime(
1655 $activity->get_Date()->get_value() . 'T'
1656 . $activity->get_Time()->get_value()
1657 ),
1658 location => Shipment::Address->new(
1659 city => ($city || ''),
1660 state => ($state || ''),
1661 country => ($country || ''),
1662 ),
1663 )
1664 );
1665 }
1666 $self->ship_date(
1667 DateTime::Format::ISO8601->parse_datetime(
1668 $response->get_Shipment()->get_PickupDate->get_value()
1669 )
1670 );
1671  
1672 }
1673 catch {
1674 warn $_ if $self->debug;
1675 try {
1676 my $error = join "\n", map {
1677 $_->get_PrimaryErrorCode()->get_Code() . ' - '
1678 . $_->get_PrimaryErrorCode()->get_Description;
1679 } @{$response->get_detail()->get_Errors()->get_ErrorDetail()};
1680 warn $error if $self->debug;
1681 $self->error($error);
1682 }
1683 catch {
1684 warn $_ if $self->debug;
1685 warn $response->get_faultstring if $self->debug;
1686 $self->error($response->get_faultstring->get_value);
1687 };
1688 };
1689  
1690 }
1691  
1692  
1693 1;
1694  
1695 __END__
« Shipment/Temando.pm ↑ Summary Shipment/CanadaPost/XSD.pm »