| 1 | package Shipment::Purolator; | ||
| 2 | $Shipment::Purolator::VERSION = '3.11'; | ||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | |||
| 7 | 1 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Shipment::SOAP::WSDL; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use MooX::Types::MooseLike::Base qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | extends 'Shipment::Base'; | ||
| 14 | |||
| 15 | |||
| 16 | has 'key' => ( | ||
| 17 | is => 'rw', | ||
| 18 | isa => Str, | ||
| 19 | ); | ||
| 20 | |||
| 21 | has 'password' => ( | ||
| 22 | is => 'rw', | ||
| 23 | isa => Str, | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | has 'proxy_domain' => ( | ||
| 28 | is => 'rw', | ||
| 29 | isa => Enum [ | ||
| 30 | qw( | ||
| 31 | devwebservices.purolator.com | ||
| 32 | webservices.purolator.com | ||
| 33 | ) | ||
| 34 | ], | ||
| 35 | default => 'devwebservices.purolator.com', | ||
| 36 | ); | ||
| 37 | |||
| 38 | |||
| 39 | my %bill_type_map = ( | ||
| 40 | 'sender' => 'Sender', | ||
| 41 | 'recipient' => 'Receiver', | ||
| 42 | 'third_party' => 'ThirdParty', | ||
| 43 | ); | ||
| 44 | |||
| 45 | my %pickup_type_map = ( | ||
| 46 | 'pickup' => 'PreScheduled', | ||
| 47 | 'dropoff' => 'DropOff', | ||
| 48 | ); | ||
| 49 | |||
| 50 | my %package_type_map = ( | ||
| 51 | 'custom' => 'CustomerPackaging', | ||
| 52 | 'envelope' => 'ExpressEnvelope', | ||
| 53 | 'tube' => '', | ||
| 54 | 'box' => 'ExpressBox', | ||
| 55 | 'pack' => 'ExpressPack', | ||
| 56 | ); | ||
| 57 | |||
| 58 | my %units_type_map = ( | ||
| 59 | 'lb' => 'lb', | ||
| 60 | 'kg' => 'kg', | ||
| 61 | 'in' => 'in', | ||
| 62 | 'cm' => 'cm', | ||
| 63 | ); | ||
| 64 | |||
| 65 | |||
| 66 | my %printer_type_map = ( | ||
| 67 | 'pdf' => 'Regular', | ||
| 68 | 'thermal' => 'Thermal', | ||
| 69 | 'image' => '', | ||
| 70 | ); | ||
| 71 | |||
| 72 | |||
| 73 | has '+currency' => (default => 'CAD',); | ||
| 74 | |||
| 75 | |||
| 76 | sub _build_services { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | if ($self->from_address && $self->to_address) { | ||
| 80 | |||
| 81 | 1 | ▶ | use Shipment::Package; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 82 | 1 | ▶ | use Shipment::Service; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 83 | use | ||
| 84 | 0 | ▶ | Shipment::Purolator::WSDLV2::Interfaces::ServiceAvailabilityService::ServiceAvailabilityServiceEndpoint; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 85 | |||
| 86 | my $interface = | ||
| 87 | Shipment::Purolator::WSDLV2::Interfaces::ServiceAvailabilityService::ServiceAvailabilityServiceEndpoint | ||
| 88 | ->new( | ||
| 89 | { proxy_domain => $self->proxy_domain, | ||
| 90 | key => $self->key, | ||
| 91 | password => $self->password, | ||
| 92 | } | ||
| 93 | ); | ||
| 94 | my $response; | ||
| 95 | |||
| 96 | my %services; | ||
| 97 | |||
| 98 | $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1; | ||
| 99 | $response = $interface->GetServicesOptions( | ||
| 100 | { BillingAccountNumber => $self->account, | ||
| 101 | SenderAddress => { | ||
| 102 | City => $self->from_address()->city, | ||
| 103 | Province => $self->from_address()->province_code, | ||
| 104 | Country => $self->from_address()->country_code, | ||
| 105 | PostalCode => $self->from_address()->postal_code, | ||
| 106 | }, | ||
| 107 | ReceiverAddress => { | ||
| 108 | City => $self->to_address()->city, | ||
| 109 | Province => $self->to_address()->province_code, | ||
| 110 | Country => $self->to_address()->country_code, | ||
| 111 | PostalCode => $self->to_address()->postal_code, | ||
| 112 | }, | ||
| 113 | }, | ||
| 114 | { 'Version' => '2.0', | ||
| 115 | 'Language' => 'en', | ||
| 116 | 'GroupID' => 'xxx', | ||
| 117 | 'RequestReference' => 'Shipment::Purolator::_build_services' | ||
| 118 | }, | ||
| 119 | ); | ||
| 120 | $Shipment::SOAP::WSDL::Debug = 0; | ||
| 121 | warn "Response\n" . $response if $self->debug > 1; | ||
| 122 | |||
| 123 | try { | ||
| 124 | foreach my $service (@{$response->get_Services()->get_Service()}) { | ||
| 125 | if ($service->get_PackageType()->get_value eq | ||
| 126 | $package_type_map{$self->package_type}) | ||
| 127 | { | ||
| 128 | $services{$service->get_ID()->get_value} = | ||
| 129 | Shipment::Service->new( | ||
| 130 | id => $service->get_ID()->get_value, | ||
| 131 | name => $service->get_Description()->get_value, | ||
| 132 | ); | ||
| 133 | my %options; | ||
| 134 | foreach | ||
| 135 | my $option (@{$service->get_Options()->get_Option()}) | ||
| 136 | { | ||
| 137 | $options{$option->get_ID()->get_value} = | ||
| 138 | $option->get_ValueType()->get_value; | ||
| 139 | } | ||
| 140 | $services{$service->get_ID()->get_value} | ||
| 141 | ->options(\%options); | ||
| 142 | $services{ground} = | ||
| 143 | $services{$service->get_ID()->get_value} | ||
| 144 | if ($service->get_ID()->get_value =~ /PurolatorGround/); | ||
| 145 | $services{express} = | ||
| 146 | $services{$service->get_ID()->get_value} | ||
| 147 | if ( $service->get_ID()->get_value =~ /PurolatorExpress/ | ||
| 148 | && $service->get_ID()->get_value !~ | ||
| 149 | /(9AM|10:30AM|12:00|Evening)$/); | ||
| 150 | $services{priority} = | ||
| 151 | $services{$service->get_ID()->get_value} | ||
| 152 | if ( $service->get_ID()->get_value =~ /PurolatorExpress/ | ||
| 153 | && $service->get_ID()->get_value | ||
| 154 | =~ /(9AM|10:30AM|12:00|Evening)$/); | ||
| 155 | |||
| 156 | } | ||
| 157 | } | ||
| 158 | $services{ground} = $services{express} | ||
| 159 | if (!$services{ground} && $services{express}); | ||
| 160 | } | ||
| 161 | catch { | ||
| 162 | warn $_ if $self->debug; | ||
| 163 | try { | ||
| 164 | warn $response->get_ResponseInformation() | ||
| 165 | ->get_Errors() | ||
| 166 | ->get_Error()->[0]->get_Description | ||
| 167 | if $self->debug; | ||
| 168 | $self->error($response->get_ResponseInformation() | ||
| 169 | ->get_Errors() | ||
| 170 | ->get_Error()->[0]->get_Description->get_value); | ||
| 171 | } | ||
| 172 | catch { | ||
| 173 | warn $_ if $self->debug; | ||
| 174 | warn $response->get_faultstring if $self->debug; | ||
| 175 | $self->error($response->get_faultstring->get_value); | ||
| 176 | }; | ||
| 177 | }; | ||
| 178 | \%services; | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | warn 'services not fetched. both from and to address required.' | ||
| 182 | if $self->debug; | ||
| 183 | $self->error( | ||
| 184 | 'services not fetched. both from and to address required.'); | ||
| 185 | |||
| 186 | {}; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | sub rate { | ||
| 192 | my ($self, $service_id) = @_; | ||
| 193 | |||
| 194 | try { | ||
| 195 | $service_id = $self->services->{$service_id}->id; | ||
| 196 | } | ||
| 197 | catch { | ||
| 198 | warn $_ if $self->debug; | ||
| 199 | warn "service ($service_id) not available" if $self->debug; | ||
| 200 | $self->error("service ($service_id) not available"); | ||
| 201 | $service_id = ''; | ||
| 202 | }; | ||
| 203 | return unless $service_id; | ||
| 204 | |||
| 205 | my $total_weight; | ||
| 206 | $total_weight += $_->weight for @{$self->packages}; | ||
| 207 | |||
| 208 | my @options; | ||
| 209 | my $signature_option; | ||
| 210 | if ($self->signature_type =~ /^(required|default|adult)$/) { | ||
| 211 | $signature_option = "ResidentialSignatureDomestic" | ||
| 212 | if $self->services->{$service_id} | ||
| 213 | ->options->{ResidentialSignatureDomestic}; | ||
| 214 | $signature_option = "ResidentialSignatureIntl" | ||
| 215 | if $self->services->{$service_id} | ||
| 216 | ->options->{ResidentialSignatureIntl}; | ||
| 217 | } | ||
| 218 | elsif ($self->signature_type eq 'not_required') { | ||
| 219 | $signature_option = "OriginSignatureNotRequired"; | ||
| 220 | } | ||
| 221 | |||
| 222 | if ($signature_option) { | ||
| 223 | push @options, | ||
| 224 | { ID => $signature_option, | ||
| 225 | Value => 'true', | ||
| 226 | }; | ||
| 227 | } | ||
| 228 | |||
| 229 | my @pieces; | ||
| 230 | foreach (@{$self->packages}) { | ||
| 231 | if ($self->package_type eq 'custom') { | ||
| 232 | push @pieces, | ||
| 233 | { Weight => { | ||
| 234 | Value => sprintf("%.0f", $_->weight) || 1, | ||
| 235 | WeightUnit => $self->weight_unit, | ||
| 236 | }, | ||
| 237 | Length => { | ||
| 238 | Value => $_->length, | ||
| 239 | DimensionUnit => $self->dim_unit, | ||
| 240 | }, | ||
| 241 | Width => { | ||
| 242 | Value => $_->width, | ||
| 243 | DimensionUnit => $self->dim_unit, | ||
| 244 | }, | ||
| 245 | Height => { | ||
| 246 | Value => $_->height, | ||
| 247 | DimensionUnit => $self->dim_unit, | ||
| 248 | }, | ||
| 249 | }; | ||
| 250 | } | ||
| 251 | else { | ||
| 252 | push @pieces, | ||
| 253 | { Weight => { | ||
| 254 | Value => sprintf("%.0f", $_->weight) || 1, | ||
| 255 | WeightUnit => $self->weight_unit, | ||
| 256 | }, | ||
| 257 | }; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | use | ||
| 262 | Shipment::Purolator::WSDLV2::Interfaces::EstimatingService::EstimatingServiceEndpoint; | ||
| 263 | my $interface = | ||
| 264 | Shipment::Purolator::WSDLV2::Interfaces::EstimatingService::EstimatingServiceEndpoint | ||
| 265 | ->new( | ||
| 266 | { proxy_domain => $self->proxy_domain, | ||
| 267 | key => $self->key, | ||
| 268 | password => $self->password, | ||
| 269 | } | ||
| 270 | ); | ||
| 271 | |||
| 272 | $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1; | ||
| 273 | my $response = $interface->GetFullEstimate( | ||
| 274 | { Shipment => { | ||
| 275 | SenderInformation => { | ||
| 276 | Address => { | ||
| 277 | Name => $self->from_address->name, | ||
| 278 | Company => $self->from_address->company, | ||
| 279 | StreetNumber => | ||
| 280 | $self->from_address->address_components->{number}, | ||
| 281 | StreetName => | ||
| 282 | $self->from_address->address_components->{street} | ||
| 283 | . ' ' | ||
| 284 | . $self->from_address->address_components | ||
| 285 | ->{direction}, | ||
| 286 | StreetAddress2 => $self->from_address->address2, | ||
| 287 | City => $self->from_address->city, | ||
| 288 | Province => $self->from_address->province_code, | ||
| 289 | Country => $self->from_address->country_code, | ||
| 290 | PostalCode => $self->from_address->postal_code, | ||
| 291 | PhoneNumber => { | ||
| 292 | CountryCode => | ||
| 293 | $self->from_address->phone_components->{country}, | ||
| 294 | AreaCode => | ||
| 295 | $self->from_address->phone_components->{area}, | ||
| 296 | Phone => | ||
| 297 | $self->from_address->phone_components->{phone}, | ||
| 298 | }, | ||
| 299 | }, | ||
| 300 | }, | ||
| 301 | ReceiverInformation => { | ||
| 302 | Address => { | ||
| 303 | Name => $self->to_address->name, | ||
| 304 | Company => $self->to_address->company, | ||
| 305 | StreetNumber => | ||
| 306 | $self->to_address->address_components->{number}, | ||
| 307 | StreetName => | ||
| 308 | $self->to_address->address_components->{street} . ' ' | ||
| 309 | . $self->to_address->address_components->{direction}, | ||
| 310 | StreetAddress2 => $self->to_address->address2, | ||
| 311 | City => $self->to_address->city, | ||
| 312 | Province => $self->to_address->province_code, | ||
| 313 | Country => $self->to_address->country_code, | ||
| 314 | PostalCode => $self->to_address->postal_code, | ||
| 315 | PhoneNumber => { | ||
| 316 | CountryCode => | ||
| 317 | $self->to_address->phone_components->{country}, | ||
| 318 | AreaCode => | ||
| 319 | $self->to_address->phone_components->{area}, | ||
| 320 | Phone => | ||
| 321 | $self->to_address->phone_components->{phone}, | ||
| 322 | }, | ||
| 323 | }, | ||
| 324 | }, | ||
| 325 | PackageInformation => { | ||
| 326 | ServiceID => $service_id, | ||
| 327 | TotalWeight => { | ||
| 328 | Value => sprintf("%.0f", $total_weight) || 1, | ||
| 329 | WeightUnit => $self->weight_unit, | ||
| 330 | }, | ||
| 331 | TotalPieces => scalar @{$self->packages}, | ||
| 332 | PiecesInformation => {Piece => \@pieces,}, | ||
| 333 | OptionsInformation => | ||
| 334 | {Options => {OptionIDValuePair => \@options,},}, | ||
| 335 | }, | ||
| 336 | PaymentInformation => { | ||
| 337 | PaymentType => 'Sender', | ||
| 338 | RegisteredAccountNumber => $self->account, | ||
| 339 | BillingAccountNumber => $self->account, | ||
| 340 | }, | ||
| 341 | PickupInformation => { | ||
| 342 | PickupType => $pickup_type_map{$self->pickup_type} | ||
| 343 | || $self->pickup_type, | ||
| 344 | }, | ||
| 345 | TrackingReferenceInformation => { | ||
| 346 | Reference1 => $self->get_reference(0), | ||
| 347 | Reference2 => $self->get_reference(1), | ||
| 348 | Reference3 => $self->get_reference(2), | ||
| 349 | Reference4 => $self->get_reference(3), | ||
| 350 | }, | ||
| 351 | }, | ||
| 352 | ShowAlternativeServicesIndicator => "false", | ||
| 353 | }, | ||
| 354 | { 'Version' => '2.0', | ||
| 355 | 'Language' => 'en', | ||
| 356 | 'GroupID' => 'xxx', | ||
| 357 | 'RequestReference' => 'Shipment::Purolator::rate' | ||
| 358 | }, | ||
| 359 | ); | ||
| 360 | $Shipment::SOAP::WSDL::Debug = 0; | ||
| 361 | warn "Response\n" . $response if $self->debug > 1; | ||
| 362 | |||
| 363 | try { | ||
| 364 | use Data::Currency; | ||
| 365 | use Shipment::Service; | ||
| 366 | my ($y, $m, $d) = split('-', | ||
| 367 | $response->get_ShipmentEstimates()->[0]->get_ShipmentEstimate() | ||
| 368 | ->get_ShipmentDate() | ||
| 369 | ->get_value); | ||
| 370 | my $ship_date = {year => $y, month => $m, day => $d}; | ||
| 371 | ($y, $m, $d) = split('-', | ||
| 372 | $response->get_ShipmentEstimates()->[0]->get_ShipmentEstimate() | ||
| 373 | ->get_ExpectedDeliveryDate() | ||
| 374 | ->get_value); | ||
| 375 | my $eta = {year => $y, month => $m, day => $d}; | ||
| 376 | $self->service( | ||
| 377 | Shipment::Service->new( | ||
| 378 | id => $service_id, | ||
| 379 | name => $self->services->{$service_id}->name, | ||
| 380 | etd => $response->get_ShipmentEstimates()->[0] | ||
| 381 | ->get_ShipmentEstimate() | ||
| 382 | ->get_EstimatedTransitDays() | ||
| 383 | ->get_value, | ||
| 384 | ship_date => $ship_date, | ||
| 385 | eta => $eta, | ||
| 386 | cost => Data::Currency->new( | ||
| 387 | $response->get_ShipmentEstimates()->[0] | ||
| 388 | ->get_ShipmentEstimate()->get_TotalPrice, | ||
| 389 | 'CAD' | ||
| 390 | ), | ||
| 391 | ) | ||
| 392 | ); | ||
| 393 | } | ||
| 394 | catch { | ||
| 395 | warn $_ if $self->debug; | ||
| 396 | try { | ||
| 397 | warn $response->get_ResponseInformation() | ||
| 398 | ->get_Errors() | ||
| 399 | ->get_Error()->[0]->get_Description | ||
| 400 | if $self->debug; | ||
| 401 | $self->error($response->get_ResponseInformation() | ||
| 402 | ->get_Errors() | ||
| 403 | ->get_Error()->[0]->get_Description->get_value); | ||
| 404 | } | ||
| 405 | catch { | ||
| 406 | warn $_ if $self->debug; | ||
| 407 | warn $response->get_faultstring if $self->debug; | ||
| 408 | $self->error($response->get_faultstring->get_value); | ||
| 409 | }; | ||
| 410 | }; | ||
| 411 | |||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | sub ship { | ||
| 416 | my ($self, $service_id) = @_; | ||
| 417 | |||
| 418 | try { | ||
| 419 | $service_id = $self->services->{$service_id}->id; | ||
| 420 | } | ||
| 421 | catch { | ||
| 422 | warn $_ if $self->debug; | ||
| 423 | warn "service ($service_id) not available" if $self->debug; | ||
| 424 | $self->error("service ($service_id) not available"); | ||
| 425 | $service_id = ''; | ||
| 426 | }; | ||
| 427 | return unless $service_id; | ||
| 428 | |||
| 429 | $self->rate($service_id); | ||
| 430 | |||
| 431 | my $total_weight; | ||
| 432 | $total_weight += $_->weight for @{$self->packages}; | ||
| 433 | |||
| 434 | my @options; | ||
| 435 | my $signature_option; | ||
| 436 | if ($self->signature_type =~ /^(required|default|adult)$/) { | ||
| 437 | $signature_option = "ResidentialSignatureDomestic" | ||
| 438 | if $self->services->{$service_id} | ||
| 439 | ->options->{ResidentialSignatureDomestic}; | ||
| 440 | $signature_option = "ResidentialSignatureIntl" | ||
| 441 | if $self->services->{$service_id} | ||
| 442 | ->options->{ResidentialSignatureIntl}; | ||
| 443 | } | ||
| 444 | elsif ($self->signature_type eq 'not_required') { | ||
| 445 | $signature_option = "OriginSignatureNotRequired"; | ||
| 446 | } | ||
| 447 | |||
| 448 | if ($signature_option) { | ||
| 449 | push @options, | ||
| 450 | { ID => $signature_option, | ||
| 451 | Value => 'true', | ||
| 452 | }; | ||
| 453 | } | ||
| 454 | |||
| 455 | my $notification_information; | ||
| 456 | if ($self->to_address->email) { | ||
| 457 | $notification_information->{AdvancedShippingNotificationEmailAddress1} | ||
| 458 | = $self->to_address->email; | ||
| 459 | } | ||
| 460 | |||
| 461 | my @pieces; | ||
| 462 | foreach (@{$self->packages}) { | ||
| 463 | if ($self->package_type eq 'custom') { | ||
| 464 | push @pieces, | ||
| 465 | { Weight => { | ||
| 466 | Value => sprintf("%.0f", $_->weight) || 1, | ||
| 467 | WeightUnit => $self->weight_unit, | ||
| 468 | }, | ||
| 469 | Length => { | ||
| 470 | Value => $_->length, | ||
| 471 | DimensionUnit => $self->dim_unit, | ||
| 472 | }, | ||
| 473 | Width => { | ||
| 474 | Value => $_->width, | ||
| 475 | DimensionUnit => $self->dim_unit, | ||
| 476 | }, | ||
| 477 | Height => { | ||
| 478 | Value => $_->height, | ||
| 479 | DimensionUnit => $self->dim_unit, | ||
| 480 | }, | ||
| 481 | }; | ||
| 482 | } | ||
| 483 | else { | ||
| 484 | push @pieces, | ||
| 485 | { Weight => { | ||
| 486 | Value => sprintf("%.0f", $_->weight) || 1, | ||
| 487 | WeightUnit => $self->weight_unit, | ||
| 488 | }, | ||
| 489 | }; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | use | ||
| 494 | Shipment::Purolator::WSDLV2::Interfaces::ShippingService::ShippingServiceEndpoint; | ||
| 495 | my $interface = | ||
| 496 | Shipment::Purolator::WSDLV2::Interfaces::ShippingService::ShippingServiceEndpoint | ||
| 497 | ->new( | ||
| 498 | { proxy_domain => $self->proxy_domain, | ||
| 499 | key => $self->key, | ||
| 500 | password => $self->password, | ||
| 501 | } | ||
| 502 | ); | ||
| 503 | |||
| 504 | $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1; | ||
| 505 | my $response = $interface->CreateShipment( | ||
| 506 | { Shipment => { | ||
| 507 | SenderInformation => { | ||
| 508 | Address => { | ||
| 509 | Name => $self->from_address->name, | ||
| 510 | Company => $self->from_address->company, | ||
| 511 | StreetNumber => | ||
| 512 | $self->from_address->address_components->{number}, | ||
| 513 | StreetName => | ||
| 514 | $self->from_address->address_components->{street} | ||
| 515 | . ' ' | ||
| 516 | . $self->from_address->address_components | ||
| 517 | ->{direction}, | ||
| 518 | StreetAddress2 => $self->from_address->address2, | ||
| 519 | City => $self->from_address->city, | ||
| 520 | Province => $self->from_address->province_code, | ||
| 521 | Country => $self->from_address->country_code, | ||
| 522 | PostalCode => $self->from_address->postal_code, | ||
| 523 | PhoneNumber => { | ||
| 524 | CountryCode => | ||
| 525 | $self->from_address->phone_components->{country}, | ||
| 526 | AreaCode => | ||
| 527 | $self->from_address->phone_components->{area}, | ||
| 528 | Phone => | ||
| 529 | $self->from_address->phone_components->{phone}, | ||
| 530 | }, | ||
| 531 | }, | ||
| 532 | }, | ||
| 533 | ReceiverInformation => { | ||
| 534 | Address => { | ||
| 535 | Name => $self->to_address->name, | ||
| 536 | Company => $self->to_address->company, | ||
| 537 | StreetNumber => | ||
| 538 | $self->to_address->address_components->{number}, | ||
| 539 | StreetName => | ||
| 540 | $self->to_address->address_components->{street} . ' ' | ||
| 541 | . $self->to_address->address_components->{direction}, | ||
| 542 | StreetAddress2 => $self->to_address->address2, | ||
| 543 | City => $self->to_address->city, | ||
| 544 | Province => $self->to_address->province_code, | ||
| 545 | Country => $self->to_address->country_code, | ||
| 546 | PostalCode => $self->to_address->postal_code, | ||
| 547 | PhoneNumber => { | ||
| 548 | CountryCode => | ||
| 549 | $self->to_address->phone_components->{country}, | ||
| 550 | AreaCode => | ||
| 551 | $self->to_address->phone_components->{area}, | ||
| 552 | Phone => | ||
| 553 | $self->to_address->phone_components->{phone}, | ||
| 554 | }, | ||
| 555 | }, | ||
| 556 | }, | ||
| 557 | PackageInformation => { | ||
| 558 | ServiceID => $service_id, | ||
| 559 | TotalWeight => { | ||
| 560 | Value => sprintf("%.0f", $total_weight) || 1, | ||
| 561 | WeightUnit => $self->weight_unit, | ||
| 562 | }, | ||
| 563 | TotalPieces => scalar @{$self->packages}, | ||
| 564 | PiecesInformation => {Piece => \@pieces,}, | ||
| 565 | OptionsInformation => | ||
| 566 | {Options => {OptionIDValuePair => \@options,},}, | ||
| 567 | }, | ||
| 568 | PaymentInformation => { | ||
| 569 | PaymentType => $bill_type_map{$self->bill_type} | ||
| 570 | || $self->bill_type, | ||
| 571 | RegisteredAccountNumber => $self->account, | ||
| 572 | BillingAccountNumber => $self->bill_account, | ||
| 573 | }, | ||
| 574 | PickupInformation => { | ||
| 575 | PickupType => $pickup_type_map{$self->pickup_type} | ||
| 576 | || $self->pickup_type, | ||
| 577 | }, | ||
| 578 | TrackingReferenceInformation => { | ||
| 579 | Reference1 => $self->get_reference(0), | ||
| 580 | Reference2 => $self->get_reference(1), | ||
| 581 | Reference3 => $self->get_reference(2), | ||
| 582 | Reference4 => $self->get_reference(3), | ||
| 583 | }, | ||
| 584 | NotificationInformation => $notification_information, | ||
| 585 | OtherInformation => | ||
| 586 | {SpecialInstructions => $self->special_instructions,}, | ||
| 587 | }, | ||
| 588 | PrinterType => $printer_type_map{$self->printer_type} | ||
| 589 | || $self->printer_type, | ||
| 590 | }, | ||
| 591 | { 'Version' => '2.0', | ||
| 592 | 'Language' => 'en', | ||
| 593 | 'GroupID' => 'xxx', | ||
| 594 | 'RequestReference' => 'Shipment::Purolator::ship' | ||
| 595 | }, | ||
| 596 | ); | ||
| 597 | $Shipment::SOAP::WSDL::Debug = 0; | ||
| 598 | warn "Response\n" . $response if $self->debug > 1; | ||
| 599 | |||
| 600 | try { | ||
| 601 | $self->tracking_id( | ||
| 602 | $response->get_ShipmentPIN()->get_Value()->get_value); | ||
| 603 | use Shipment::Label; | ||
| 604 | my $package_index = 0; | ||
| 605 | foreach (@{$response->get_PiecePINs()->get_PIN()}) { | ||
| 606 | $self->get_package($package_index) | ||
| 607 | ->tracking_id($_->get_Value()->get_value); | ||
| 608 | $self->get_package($package_index)->label( | ||
| 609 | Shipment::Label->new( | ||
| 610 | {tracking_id => $_->get_Value()->get_value,}, | ||
| 611 | ) | ||
| 612 | ); | ||
| 613 | $package_index++; | ||
| 614 | } | ||
| 615 | } | ||
| 616 | catch { | ||
| 617 | try { | ||
| 618 | warn $_ if $self->debug; | ||
| 619 | warn $response->get_ResponseInformation() | ||
| 620 | ->get_Errors() | ||
| 621 | ->get_Error()->[0]->get_Description | ||
| 622 | if $self->debug; | ||
| 623 | $self->error($response->get_ResponseInformation() | ||
| 624 | ->get_Errors() | ||
| 625 | ->get_Error()->[0]->get_Description->get_value); | ||
| 626 | } | ||
| 627 | catch { | ||
| 628 | warn $_ if $self->debug; | ||
| 629 | warn $response->get_faultstring if $self->debug; | ||
| 630 | $self->error($response->get_faultstring->get_value); | ||
| 631 | }; | ||
| 632 | }; | ||
| 633 | |||
| 634 | $self->fetch_documents(); | ||
| 635 | |||
| 636 | } | ||
| 637 | |||
| 638 | |||
| 639 | sub fetch_documents { | ||
| 640 | my $self = shift; | ||
| 641 | |||
| 642 | return unless $self->tracking_id; | ||
| 643 | |||
| 644 | use | ||
| 645 | Shipment::Purolator::WSDL::Interfaces::ShippingDocumentsService::ShippingDocumentsServiceEndpoint; | ||
| 646 | my $interface = | ||
| 647 | Shipment::Purolator::WSDL::Interfaces::ShippingDocumentsService::ShippingDocumentsServiceEndpoint | ||
| 648 | ->new( | ||
| 649 | { proxy_domain => $self->proxy_domain, | ||
| 650 | key => $self->key, | ||
| 651 | password => $self->password, | ||
| 652 | } | ||
| 653 | ); | ||
| 654 | |||
| 655 | $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1; | ||
| 656 | my $response = $interface->GetDocuments( | ||
| 657 | { DocumentCriterium => | ||
| 658 | {DocumentCriteria => {PIN => {Value => $self->tracking_id,},},}, | ||
| 659 | }, | ||
| 660 | { 'Version' => '1.3', | ||
| 661 | 'Language' => 'en', | ||
| 662 | 'GroupID' => 'xxx', | ||
| 663 | 'RequestReference' => 'Shipment::Purolator::fetch_documents' | ||
| 664 | }, | ||
| 665 | ); | ||
| 666 | $Shipment::SOAP::WSDL::Debug = 0; | ||
| 667 | warn "Response\n" . $response if $self->debug > 1; | ||
| 668 | |||
| 669 | my $document_url; | ||
| 670 | try { | ||
| 671 | $document_url = | ||
| 672 | $response->get_Documents()->get_Document()->get_DocumentDetails() | ||
| 673 | ->[0]->get_DocumentDetail()->get_URL()->get_value; | ||
| 674 | } | ||
| 675 | catch { | ||
| 676 | warn $_ if $self->debug; | ||
| 677 | try { | ||
| 678 | warn $response->get_ResponseInformation() | ||
| 679 | ->get_Errors() | ||
| 680 | ->get_Error()->[0]->get_Description | ||
| 681 | if $self->debug; | ||
| 682 | $self->error($response->get_ResponseInformation() | ||
| 683 | ->get_Errors() | ||
| 684 | ->get_Error()->[0]->get_Description->get_value); | ||
| 685 | } | ||
| 686 | catch { | ||
| 687 | warn $_ if $self->debug; | ||
| 688 | warn $response->get_faultstring if $self->debug; | ||
| 689 | $self->error($response->get_faultstring->get_value); | ||
| 690 | }; | ||
| 691 | }; | ||
| 692 | |||
| 693 | use LWP::UserAgent; | ||
| 694 | use Shipment::Label; | ||
| 695 | my $ua = LWP::UserAgent->new('Shipping::Purolator'); | ||
| 696 | my $req = HTTP::Request->new(GET => $document_url); | ||
| 697 | |||
| 698 | ## for multi-piece shipments, the labels are not always ready immediately after generating the shipment... try 10 times, sleeping for a second in between each try. | ||
| 699 | my $label_success; | ||
| 700 | my $res; | ||
| 701 | for (1 .. 10) { | ||
| 702 | $res = $ua->request($req); | ||
| 703 | sleep 1 && next unless $res->is_success && $res->content; | ||
| 704 | |||
| 705 | $label_success = 1; | ||
| 706 | $self->documents( | ||
| 707 | Shipment::Label->new( | ||
| 708 | tracking_id => $self->tracking_id, | ||
| 709 | content_type => $res->header('Content-Type'), | ||
| 710 | data => $res->content, | ||
| 711 | file_name => $self->tracking_id . '-documents.pdf', | ||
| 712 | ) | ||
| 713 | ); | ||
| 714 | |||
| 715 | foreach ($self->all_packages) { | ||
| 716 | $_->label->content_type($res->header('Content-Type')); | ||
| 717 | $_->label->data($res->content); | ||
| 718 | $_->label->file_name($_->tracking_id . '.pdf'); | ||
| 719 | } | ||
| 720 | } | ||
| 721 | |||
| 722 | if (!$label_success) { | ||
| 723 | if (!$res->is_success) { | ||
| 724 | warn $res->status_line if $self->debug; | ||
| 725 | $self->error("Failed to retrieve label(s) from " | ||
| 726 | . $document_url . ": " | ||
| 727 | . $res->status_line); | ||
| 728 | } | ||
| 729 | else { | ||
| 730 | warn "No content returned from label url: " . $document_url | ||
| 731 | if $self->debug; | ||
| 732 | $self->error("Failed to retrieve label(s) from " . $document_url); | ||
| 733 | } | ||
| 734 | $self->cancel; | ||
| 735 | } | ||
| 736 | } | ||
| 737 | |||
| 738 | |||
| 739 | sub cancel { | ||
| 740 | my $self = shift; | ||
| 741 | |||
| 742 | if (!$self->tracking_id) { | ||
| 743 | $self->error('no tracking id provided'); | ||
| 744 | return; | ||
| 745 | } | ||
| 746 | |||
| 747 | use | ||
| 748 | Shipment::Purolator::WSDLV2::Interfaces::ShippingService::ShippingServiceEndpoint; | ||
| 749 | my $interface = | ||
| 750 | Shipment::Purolator::WSDLV2::Interfaces::ShippingService::ShippingServiceEndpoint | ||
| 751 | ->new( | ||
| 752 | { proxy_domain => $self->proxy_domain, | ||
| 753 | key => $self->key, | ||
| 754 | password => $self->password, | ||
| 755 | } | ||
| 756 | ); | ||
| 757 | |||
| 758 | $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1; | ||
| 759 | my $response = $interface->VoidShipment( | ||
| 760 | {PIN => {Value => $self->tracking_id,},}, | ||
| 761 | { 'Version' => '2.0', | ||
| 762 | 'Language' => 'en', | ||
| 763 | 'GroupID' => 'xxx', | ||
| 764 | 'RequestReference' => 'Shipment::Purolator::cancel' | ||
| 765 | }, | ||
| 766 | ); | ||
| 767 | $Shipment::SOAP::WSDL::Debug = 0; | ||
| 768 | warn "Response\n" . $response if $self->debug > 1; | ||
| 769 | |||
| 770 | my $success; | ||
| 771 | try { | ||
| 772 | $success = $response->get_ShipmentVoided->get_value; | ||
| 773 | } | ||
| 774 | catch { | ||
| 775 | try { | ||
| 776 | warn $_ if $self->debug; | ||
| 777 | warn $response->get_ResponseInformation() | ||
| 778 | ->get_Errors() | ||
| 779 | ->get_Error()->[0]->get_Description | ||
| 780 | if $self->debug; | ||
| 781 | $self->error($response->get_ResponseInformation() | ||
| 782 | ->get_Errors() | ||
| 783 | ->get_Error()->[0]->get_Description->get_value); | ||
| 784 | } | ||
| 785 | catch { | ||
| 786 | warn $_ if $self->debug; | ||
| 787 | warn $response->get_faultstring if $self->debug; | ||
| 788 | $self->error($response->get_faultstring->get_value); | ||
| 789 | }; | ||
| 790 | }; | ||
| 791 | |||
| 792 | return $success; | ||
| 793 | } | ||
| 794 | |||
| 795 | |||
| 796 | sub end_of_day { | ||
| 797 | my $self = shift; | ||
| 798 | |||
| 799 | use DateTime; | ||
| 800 | use | ||
| 801 | Shipment::Purolator::WSDL::Interfaces::ShippingDocumentsService::ShippingDocumentsServiceEndpoint; | ||
| 802 | my $interface = | ||
| 803 | Shipment::Purolator::WSDL::Interfaces::ShippingDocumentsService::ShippingDocumentsServiceEndpoint | ||
| 804 | ->new( | ||
| 805 | { proxy_domain => $self->proxy_domain, | ||
| 806 | key => $self->key, | ||
| 807 | password => $self->password, | ||
| 808 | } | ||
| 809 | ); | ||
| 810 | |||
| 811 | #TODO: call Consolidate before getting manifest document | ||
| 812 | |||
| 813 | $Shipment::SOAP::WSDL::Debug = 1 if $self->debug > 1; | ||
| 814 | my $response = $interface->GetShipmentManifestDocument( | ||
| 815 | { ShipmentManifestDocumentCriterium => { | ||
| 816 | ShipmentManifestDocumentCriteria => | ||
| 817 | {ManifestDate => DateTime->now->ymd,} | ||
| 818 | } | ||
| 819 | }, | ||
| 820 | { 'Version' => '1.3', | ||
| 821 | 'Language' => 'en', | ||
| 822 | 'GroupID' => 'xxx', | ||
| 823 | 'RequestReference' => 'Shipment::Purolator::end_of_day' | ||
| 824 | }, | ||
| 825 | ); | ||
| 826 | $Shipment::SOAP::WSDL::Debug = 0; | ||
| 827 | warn "Response\n" . $response if $self->debug > 1; | ||
| 828 | |||
| 829 | try { | ||
| 830 | use LWP::UserAgent; | ||
| 831 | my $ua = LWP::UserAgent->new('Shipping::Purolator'); | ||
| 832 | my $req = HTTP::Request->new( | ||
| 833 | GET => $response->get_ManifestBatches()->[0]->get_ManifestBatch() | ||
| 834 | ->get_ManifestBatchDetails->get_ManifestBatchDetail->get_URL() | ||
| 835 | ->get_value); | ||
| 836 | my $res = $ua->request($req); | ||
| 837 | $self->manifest( | ||
| 838 | Shipment::Label->new( | ||
| 839 | content_type => $res->header('Content-Type'), | ||
| 840 | data => $res->content, | ||
| 841 | file_name => 'manifest_' . DateTime->now->ymd('_') . '.pdf', | ||
| 842 | ) | ||
| 843 | ); | ||
| 844 | } | ||
| 845 | catch { | ||
| 846 | warn $_ if $self->debug; | ||
| 847 | try { | ||
| 848 | warn $response->get_ResponseInformation() | ||
| 849 | ->get_Errors() | ||
| 850 | ->get_Error()->[0]->get_Description | ||
| 851 | if $self->debug; | ||
| 852 | $self->error($response->get_ResponseInformation() | ||
| 853 | ->get_Errors() | ||
| 854 | ->get_Error()->[0]->get_Description->get_value); | ||
| 855 | } | ||
| 856 | catch { | ||
| 857 | warn $_ if $self->debug; | ||
| 858 | warn $response->get_faultstring if $self->debug; | ||
| 859 | $self->error($response->get_faultstring->get_value); | ||
| 860 | }; | ||
| 861 | }; | ||
| 862 | |||
| 863 | } | ||
| 864 | |||
| 865 | |||
| 866 | 1; | ||
| 867 | |||
| 868 | __END__ | ||