Shipment/CanadaPost.pm

filter: Statementstmt 15.2% 50 / 327 Branchbran 0.0% 0 / 168 Conditioncond 0.0% 0 / 130 MC/DCmcdc 0.0% 0 / 62 Subroutinesub 54.5% 12 / 22 Podpod 100% 2 / 2 totaltotal 9.0% 64 / 711 CC 141 SCAR 77.1
CC
141
Cov
8%
ship
3896.8
_build_services
2070.0
rate
702.0
CRAP 2228.0
↑ Summary Shipment/Sendle.pm »
1 package Shipment::CanadaPost;
2 1 $Shipment::CanadaPost::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 1 ▶use Shipment::CanadaPost::XSD;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use XML::Compile::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Shipment::Label;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 extends 'Shipment::Base';
18  
19  
20 1 has 'username' => (
21 is => 'rw',
22 isa => Str,
23 );
24  
25 1 has 'password' => (
26 is => 'rw',
27 isa => Str,
28 );
29  
30  
31 1 has 'endpoint' => (
32 is => 'rw',
33 isa => Enum [
34 qw(
35 ct.soa-gw.canadapost.ca
36 soa-gw.canadapost.ca
37 )
38 ],
39 default => 'ct.soa-gw.canadapost.ca',
40 );
41  
42  
43 1 has 'contract_id' => (
44 is => 'rw',
45 isa => Str,
46 );
47  
48  
49 1 has '+currency' => (
50 isa => Enum [qw( CAD )],
51 default => 'CAD',
52 );
53  
54  
55 1 has 'customs_currency' => (
56 is => 'rw',
57 isa => Str,
58 default => 'CAD',
59 );
60  
61 1 has 'customs_conversion' => (
62 is => 'rw',
63 isa => Num,
64 default => 1,
65 );
66  
67 1 has 'customs_reason' => (
68 is => 'rw',
69 isa => Enum [qw( DOC SAM REP SOG )],
70 default => 'SOG',
71 );
72  
73  
74 1 has 'non_delivery_handling' => (
75 is => 'rw',
76 isa => Enum [qw( RASE RTS ABAN )],
77 );
78  
79  
80 1 has 'services_rates' => (
81 is => 'rw',
82 isa => Bool,
83 default => 0,
84 );
85  
86  
87 0 ▶sub _messages {
Subroutine _messages: not called CC=3 SCAR=24.8
88 0 my ($self, $xml) = @_;
89  
90 0 my $schema =
91 XML::Compile::Schema->new([$Shipment::CanadaPost::XSD::messages,]);
92  
93 0 my $reader = $schema->compile(
94 READER => '{http://www.canadapost.ca/ws/messages}messages');
95 0 my $data = $reader->($xml);
96  
97 0 my $messages = '';
98 0 foreach my $message (@{$data->{message}}) {
99 0 $messages .= $message->{code} . ': ' . $message->{description} . "\n";
100 }
101 0 ▶ warn $messages if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
102  
103 0 $messages;
104 }
105  
106  
107 0 ▶sub _build_services {
Subroutine _build_services: not called CC=45 SCAR=76.4
108 0 my $self = shift;
109  
110 0 my %services;
111  
112 0 my $ua = LWP::UserAgent->new();
113 0 $ua->credentials($self->endpoint . ':443',
114 'login', $self->username, $self->password);
115  
116 0 ▶ if ( $self->services_rates
Branch: 1 branch
BranchTrueFalseTotal
if ($self->services_rates and $self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code) { } 0 0 0
Condition0%0/12
$self->services_rates and $self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code
!ll&&!rl&&r
000
$self->services_rates and $self->from_address and $self->from_address->postal_code and $self->to_address
!ll&&!rl&&r
000
$self->services_rates and $self->from_address and $self->from_address->postal_code
!ll&&!rl&&r
000
$self->services_rates and $self->from_address
!ll&&!rl&&r
000
MC/DC0%0/5
$self->services_rates and $self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code
$self->services_rates $self->from_address $self->from_address->postal_code $self->to_address $self->to_address->postal_code
Truth table0%0/6
A and B and C and D and E
A= $self->services_ratesB= $self->from_addressC= $self->from_address->postal_codeD= $self->to_addressE= $self->to_address->postal_code
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
117 && $self->from_address()
118 && $self->from_address()->postal_code
119 && $self->to_address()
120 && $self->to_address()->postal_code)
121 {
122  
123 0 ▶ my $country_code = $self->to_address()->country_code || 'CA';
Condition0%0/2
$self->to_address->country_code || 'CA'
l!l
00
MC/DC0%0/1
$self->to_address->country_code || 'CA'
$self->to_address->country_code
Truth table0%0/2
A
A= $self->to_address->country_code
Aresult
11
00
124 0 my $data = {
125 'parcel-characteristics' => {'weight' => 1,},
126 'origin-postal-code' => $self->from_address()->postal_code,
127 };
128  
129 0 ▶ if ($self->account) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->account) { } 0 0 0
130 0 $data->{'customer-number'} = $self->account;
131 0 ▶ $data->{'contract-id'} = $self->contract_id if $self->contract_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->contract_id 0 0 0
132 }
133 else {
134 0 $data->{'quote-type'} = 'counter';
135 }
136  
137 0 ▶ if ($country_code eq 'CA') {
Branch: 2 branches
BranchTrueFalseTotal
if ($country_code eq 'CA') { } 0 0 0
elsif ($country_code eq 'US') { } 0 0 0
138 0 $data->{'destination'}->{'domestic'}->{'postal-code'} =
139 $self->to_address()->postal_code;
140 }
141 elsif ($country_code eq 'US') {
142 0 $data->{'destination'}->{'united-states'}->{'zip-code'} =
143 $self->to_address()->postal_code;
144 }
145 else {
146 0 $data->{'destination'}->{'international'}->{'country-code'} =
147 $country_code;
148 0 $data->{'destination'}->{'international'}->{'postal-code'} =
149 $self->to_address()->postal_code;
150 }
151  
152 0 my $schema =
153 XML::Compile::Schema->new($Shipment::CanadaPost::XSD::rating
154 . $Shipment::CanadaPost::XSD::common);
155 0 my $xml_data =
156 $schema->compile('WRITER',
157 '{http://www.canadapost.ca/ws/ship/rate-v4}mailing-scenario')
158 ->(XML::LibXML::Document->new('1.0', 'UTF-8'), $data)->toString;
159 0 ▶ warn 'XML Request: ' . $xml_data if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
160  
161 0 my $url = 'https://' . $self->endpoint . '/rs/ship/price';
162 0 my $header = [
163 'Content-Type' => 'application/vnd.cpc.ship.rate-v4+xml',
164 'Accept' => 'application/vnd.cpc.ship.rate-v4+xml',
165 ];
166 0 my $request = HTTP::Request->new(POST => $url, $header, $xml_data);
167  
168 0 my $response = $ua->request($request);
169 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
170 0 ▶ warn $response->status_line if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
171 0 $self->error('services request failed with status: '
172 . $response->status_line
173 . ' and message: '
174 . $self->_messages($response->content));
175 0 return \%services;
176 }
177  
178 0 ▶ warn 'XML Response: ' . $response->content if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
179  
180 0 my $reader = $schema->compile(READER =>
181 '{http://www.canadapost.ca/ws/ship/rate-v4}price-quotes');
182 0 $data = $reader->($response->content);
183 0 foreach my $service (@{$data->{'price-quote'}}) {
184 my ($y, $m, $d) = split('-',
185 0 $service->{'service-standard'}->{'expected-delivery-date'});
186 0 my $eta;
187 0 ▶ if ($y && $m && $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y and $m and $d) 0 0 0
Condition0%0/6
$y and $m and $d
!ll&&!rl&&r
000
$y and $m
!ll&&!rl&&r
000
MC/DC0%0/3
$y and $m and $d
$y $m $d
Truth table0%0/4
A and B and C
A= $yB= $mC= $d
ABCresult
0XX0
10X0
1100
1111
188 0 $eta = {year => $y, month => $m, day => $d};
189 }
190 $services{$service->{'service-code'}} = Shipment::Service->new(
191 id => $service->{'service-code'},
192 name => $service->{'service-name'},
193 cost => Data::Currency->new(
194 $service->{'price-details'}->{'due'}, 'CAD'
195 ),
196 etd =>
197 0 $service->{'service-standard'}->{'expected-transit-time'},
198 eta => $eta,
199 );
200 }
201  
202 }
203 else {
204  
205 0 ▶ my $url =
Branch: 1 branch
BranchTrueFalseTotal
$self->to_address && $self->to_address->country_code ? : 0 0 0
Condition0%0/3
$self->to_address and $self->to_address->country_code
!ll&&!rl&&r
000
MC/DC0%0/2
$self->to_address and $self->to_address->country_code
$self->to_address $self->to_address->country_code
Truth table0%0/3
A and B
A= $self->to_addressB= $self->to_address->country_code
ABresult
0X0
100
111
206 'https://'
207 . $self->endpoint
208 . '/rs/ship/service?country='
209 . (($self->to_address() && $self->to_address()->country_code)
210 ? $self->to_address()->country_code
211 : 'CA');
212 0 ▶ $url .= '&contract=' . $self->contract_id if $self->contract_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->contract_id 0 0 0
213 0 ▶ $url .= '&origpc=' . $self->from_address()->postal_code
Branch: 1 branch
BranchTrueFalseTotal
if $self->from_address and $self->from_address->postal_code 0 0 0
Condition0%0/3
$self->from_address and $self->from_address->postal_code
!ll&&!rl&&r
000
MC/DC0%0/2
$self->from_address and $self->from_address->postal_code
$self->from_address $self->from_address->postal_code
Truth table0%0/3
A and B
A= $self->from_addressB= $self->from_address->postal_code
ABresult
0X0
100
111
214 if $self->from_address() && $self->from_address()->postal_code;
215 0 ▶ $url .= '&destpc=' . $self->to_address()->postal_code
Branch: 1 branch
BranchTrueFalseTotal
if $self->to_address and $self->to_address->postal_code 0 0 0
Condition0%0/3
$self->to_address and $self->to_address->postal_code
!ll&&!rl&&r
000
MC/DC0%0/2
$self->to_address and $self->to_address->postal_code
$self->to_address $self->to_address->postal_code
Truth table0%0/3
A and B
A= $self->to_addressB= $self->to_address->postal_code
ABresult
0X0
100
111
216 if $self->to_address() && $self->to_address()->postal_code;
217  
218 0 my $response =
219 $ua->get($url, 'Accept' => 'application/vnd.cpc.ship.rate-v4+xml');
220  
221 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
222 0 ▶ warn $response->status_line if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
223 0 $self->error('services request failed with status: '
224 . $response->status_line);
225 0 return \%services;
226 }
227  
228 0 ▶ warn 'XML Response: ' . $response->content if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
229  
230 0 my $schema =
231 XML::Compile::Schema->new($Shipment::CanadaPost::XSD::discovery
232 . $Shipment::CanadaPost::XSD::common);
233 0 my $reader = $schema->compile(
234 READER => '{http://www.canadapost.ca/ws/ship/rate-v4}services');
235 0 my $data = $reader->($response->content);
236  
237 0 foreach my $service (@{$data->{service}}) {
238 $services{$service->{'service-code'}} = Shipment::Service->new(
239 id => $service->{'service-code'},
240 0 name => $service->{'service-name'},
241 );
242 }
243  
244 }
245  
246 $services{ground} =
247 $services{'DOM.RP'}
248 || $services{'DOM.EP'}
249 || $services{'USA.TP'}
250 || $services{'USA.EP'}
251 || $services{'INT.TP'}
252 || $services{'INT.IP.AIR'}
253 || $services{'INT.IP.SURF'}
254 0 ▶ || $services{'INT.XP'}
Condition0%0/23
$services{'DOM.RP'} || $services{'DOM.EP'} || $services{'USA.TP'} || $services{'USA.EP'} || $services{'INT.TP'} || $services{'INT.IP.AIR'} || $services{'INT.IP.SURF'} || $services{'INT.XP'} || undef
l!l
00
$services{"DOM.RP"} or $services{"DOM.EP"} or $services{"USA.TP"} or $services{"USA.EP"} or $services{"INT.TP"} or $services{"INT.IP.AIR"} or $services{"INT.IP.SURF"} or $services{"INT.XP"}
l!l&&r!l&&!r
000
$services{"DOM.RP"} or $services{"DOM.EP"} or $services{"USA.TP"} or $services{"USA.EP"} or $services{"INT.TP"} or $services{"INT.IP.AIR"} or $services{"INT.IP.SURF"}
l!l&&r!l&&!r
000
$services{"DOM.RP"} or $services{"DOM.EP"} or $services{"USA.TP"} or $services{"USA.EP"} or $services{"INT.TP"} or $services{"INT.IP.AIR"}
l!l&&r!l&&!r
000
$services{"DOM.RP"} or $services{"DOM.EP"} or $services{"USA.TP"} or $services{"USA.EP"} or $services{"INT.TP"}
l!l&&r!l&&!r
000
$services{"DOM.RP"} or $services{"DOM.EP"} or $services{"USA.TP"} or $services{"USA.EP"}
l!l&&r!l&&!r
000
$services{"DOM.RP"} or $services{"DOM.EP"} or $services{"USA.TP"}
l!l&&r!l&&!r
000
$services{"DOM.RP"} or $services{"DOM.EP"}
l!l&&r!l&&!r
000
MC/DC0%0/8
$services{'DOM.RP'} || $services{'DOM.EP'} || $services{'USA.TP'} || $services{'USA.EP'} || $services{'INT.TP'} || $services{'INT.IP.AIR'} || $services{'INT.IP.SURF'} || $services{'INT.XP'} || undef
$services{"DOM.RP"} $services{"DOM.EP"} $services{"USA.TP"} $services{"USA.EP"} $services{"INT.TP"} $services{"INT.IP.AIR"} $services{"INT.IP.SURF"} $services{"INT.XP"}
Truth table0%0/9
A or B or C or D or E or F or G or H
A= $services{"DOM.RP"}B= $services{"DOM.EP"}C= $services{"USA.TP"}D= $services{"USA.EP"}E= $services{"INT.TP"}F= $services{"INT.IP.AIR"}G= $services{"INT.IP.SURF"}H= $services{"INT.XP"}
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
255 || undef;
256 $services{express} =
257 $services{'DOM.XP'}
258 || $services{'USA.XP'}
259 0 ▶ || $services{'INT.XP'}
Condition0%0/8
$services{'DOM.XP'} || $services{'USA.XP'} || $services{'INT.XP'} || undef
l!l
00
$services{"DOM.XP"} or $services{"USA.XP"} or $services{"INT.XP"}
l!l&&r!l&&!r
000
$services{"DOM.XP"} or $services{"USA.XP"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$services{'DOM.XP'} || $services{'USA.XP'} || $services{'INT.XP'} || undef
$services{"DOM.XP"} $services{"USA.XP"} $services{"INT.XP"}
Truth table0%0/4
A or B or C
A= $services{"DOM.XP"}B= $services{"USA.XP"}C= $services{"INT.XP"}
ABCresult
1XX1
01X1
0011
0000
260 || undef;
261 $services{priority} =
262 $services{'DOM.PC'}
263 || $services{'USA.PW.PARCEL'}
264 0 ▶ || $services{'INT.PW.PARCEL'};
Condition0%0/6
$services{'DOM.PC'} || $services{'USA.PW.PARCEL'} || $services{'INT.PW.PARCEL'}
l!l&&r!l&&!r
000
$services{"DOM.PC"} or $services{"USA.PW.PARCEL"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$services{'DOM.PC'} || $services{'USA.PW.PARCEL'} || $services{'INT.PW.PARCEL'}
$services{"DOM.PC"} $services{"USA.PW.PARCEL"} $services{'INT.PW.PARCEL'}
Truth table0%0/4
A or B || C
A= $services{"DOM.PC"}B= $services{"USA.PW.PARCEL"}C= $services{'INT.PW.PARCEL'}
ABCresult
1XX1
01X1
0011
0000
265 0 foreach (qw/ground express priority/) {
266 0 ▶ delete $services{$_} if !$services{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $services{$_} 0 0 0
267 }
268  
269 0 \%services;
270 }
271  
272  
273 0 ▶sub rate {
Subroutine rate: not called CC=26 SCAR=65.5
Pod documented
274 0 my ($self, $service_id) = @_;
275  
276 0 ▶ if ($service_id eq 'ground') {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_id eq "ground") 0 0 0
277 0 foreach my $id (
278 qw/DOM.RP DOM.EP USA.TP USA.EP INT.TP INT.IP.AIR INT.IP.SURF INT.XP/
279 )
280 {
281 0 ▶ if ($self->services->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->services->{$id}) 0 0 0
282 0 $self->error('');
283 0 $self->rate($id);
284 0 ▶ if (!$self->error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->error) 0 0 0
285 0 return;
286 }
287 }
288 }
289 0 return;
290 }
291  
292 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
293 0 $service_id = $self->services->{$service_id}->id;
294 }
295 0 ▶ catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
296 0 ▶ warn $_ if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
297 0 ▶ warn "service ($service_id) not available" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
298 0 $self->error("service ($service_id) not available");
299 0 $service_id = '';
300 0 };
301 0 ▶ return unless $service_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $service_id 0 0 0
302  
303 0 my $ua = LWP::UserAgent->new();
304 0 $ua->credentials($self->endpoint . ':443',
305 'login', $self->username, $self->password);
306  
307 0 ▶ if ( $self->from_address()
Branch: 1 branch
BranchTrueFalseTotal
if ($self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code) { } 0 0 0
Condition0%0/9
$self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code
!ll&&!rl&&r
000
$self->from_address and $self->from_address->postal_code and $self->to_address
!ll&&!rl&&r
000
$self->from_address and $self->from_address->postal_code
!ll&&!rl&&r
000
MC/DC0%0/4
$self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code
$self->from_address $self->from_address->postal_code $self->to_address $self->to_address->postal_code
Truth table0%0/5
A and B and C and D
A= $self->from_addressB= $self->from_address->postal_codeC= $self->to_addressD= $self->to_address->postal_code
ABCDresult
0XXX0
10XX0
110X0
11100
11111
308 && $self->from_address()->postal_code
309 && $self->to_address()
310 && $self->to_address()->postal_code)
311 {
312  
313 0 ▶ my $country_code = $self->to_address()->country_code || 'CA';
Condition0%0/2
$self->to_address->country_code || 'CA'
l!l
00
MC/DC0%0/1
$self->to_address->country_code || 'CA'
$self->to_address->country_code
Truth table0%0/2
A
A= $self->to_address->country_code
Aresult
11
00
314 0 my $data = {
315 'services' => {'service-code' => [$service_id]},
316 'origin-postal-code' => $self->from_address()->postal_code,
317 };
318  
319 0 ▶ if ($self->account) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->account) { } 0 0 0
320 0 $data->{'customer-number'} = $self->account;
321 0 ▶ $data->{'contract-id'} = $self->contract_id if $self->contract_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->contract_id 0 0 0
322 }
323 else {
324 0 $data->{'quote-type'} = 'counter';
325 }
326  
327 0 ▶ if ($country_code eq 'CA') {
Branch: 2 branches
BranchTrueFalseTotal
if ($country_code eq 'CA') { } 0 0 0
elsif ($country_code eq 'US') { } 0 0 0
328 0 $data->{'destination'}->{'domestic'}->{'postal-code'} =
329 $self->to_address()->postal_code;
330 }
331 elsif ($country_code eq 'US') {
332 0 $data->{'destination'}->{'united-states'}->{'zip-code'} =
333 $self->to_address()->postal_code;
334 }
335 else {
336 0 $data->{'destination'}->{'international'}->{'country-code'} =
337 $country_code;
338 0 $data->{'destination'}->{'international'}->{'postal-code'} =
339 $self->to_address()->postal_code;
340 }
341  
342 0 my $schema =
343 XML::Compile::Schema->new($Shipment::CanadaPost::XSD::rating
344 . $Shipment::CanadaPost::XSD::common);
345  
346 0 my $cost = 0;
347 0 my $etd = 0;
348 0 my $eta;
349  
350 0 foreach my $package (@{$self->packages}) {
351  
352 0 my $weight = $package->weight;
353 0 ▶ if ($self->weight_unit eq 'lb') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->weight_unit eq 'lb') { } 0 0 0
elsif ($self->weight_unit eq 'oz') { } 0 0 0
354 0 $weight = sprintf('%.4f', $weight * 0.4535924);
355 }
356 elsif ($self->weight_unit eq 'oz') {
357 0 $weight = sprintf('%.4f', $weight * 0.02834952);
358 }
359 0 my ($l, $w, $h) =
360 ($package->length, $package->width, $package->height);
361 0 ▶ if ($self->dim_unit eq 'in') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dim_unit eq "in") 0 0 0
362 0 $l = sprintf('%.4f', $l * 2.54);
363 0 $w = sprintf('%.4f', $w * 2.54);
364 0 $h = sprintf('%.4f', $h * 2.54);
365 }
366 0 $data->{'parcel-characteristics'} = {
367 weight => $weight,
368 dimensions => {
369 length => $l,
370 width => $w,
371 height => $h,
372 },
373 };
374  
375 0 my $xml_data;
376 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
377 0 $xml_data = $schema->compile('WRITER',
378 '{http://www.canadapost.ca/ws/ship/rate-v4}mailing-scenario'
379 )->(XML::LibXML::Document->new('1.0', 'UTF-8'), $data)
380 ->toString;
381 0 ▶ warn 'XML Request: ' . $xml_data if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
382 }
383 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
384 0 ▶ warn $_ if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
385 0 my $error = $_;
386 0 $error =~ s/\`//g;
387 0 $self->error($error);
388 0 };
389 0 ▶ return unless $xml_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml_data 0 0 0
390  
391 0 my $url = 'https://' . $self->endpoint . '/rs/ship/price';
392 0 my $header = [
393 'Content-Type' => 'application/vnd.cpc.ship.rate-v4+xml',
394 'Accept' => 'application/vnd.cpc.ship.rate-v4+xml',
395 ];
396 0 my $request = HTTP::Request->new(POST => $url, $header, $xml_data);
397  
398 0 my $response = $ua->request($request);
399 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
400 0 ▶ warn $response->status_line if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
401 0 $self->error($self->_messages($response->content));
402 0 return;
403 }
404  
405 0 ▶ warn 'XML Response: ' . $response->content if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
406  
407 0 my $reader = $schema->compile(READER =>
408 '{http://www.canadapost.ca/ws/ship/rate-v4}price-quotes');
409 0 my $response_data = $reader->($response->content);
410 0 my $service = $response_data->{'price-quote'}->[0];
411 0 $cost += $service->{'price-details'}->{'due'};
412 0 $etd = $service->{'service-standard'}->{'expected-transit-time'};
413 my ($y, $m, $d) = split('-',
414 0 $service->{'service-standard'}->{'expected-delivery-date'});
415  
416 0 ▶ if ($y && $m && $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y and $m and $d) 0 0 0
Condition0%0/6
$y and $m and $d
!ll&&!rl&&r
000
$y and $m
!ll&&!rl&&r
000
MC/DC0%0/3
$y and $m and $d
$y $m $d
Truth table0%0/4
A and B and C
A= $yB= $mC= $d
ABCresult
0XX0
10X0
1100
1111
417 0 $eta = {year => $y, month => $m, day => $d};
418 }
419 }
420  
421 $self->service(
422 Shipment::Service->new(
423 id => $service_id,
424 0 name => $self->services->{$service_id}->name,
425 cost => Data::Currency->new($cost, 'CAD'),
426 etd => $etd,
427 eta => $eta,
428 )
429 );
430 }
431 else {
432 # from and to address required
433 0 $self->error("Both from and to address are required for rating.");
434 }
435  
436 0 return;
437 }
438  
439  
440 1 has 'tmp_file_dir' => (
441 is => 'rw',
442 isa => Str,
443 default => 'tmp',
444 );
445  
446 0 ▶sub ship {
Subroutine ship: not called CC=64 SCAR=82.7
Pod documented
447 0 my ($self, $service_id) = @_;
448  
449 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
450 0 $service_id = $self->services->{$service_id}->id;
451 }
452 0 ▶ catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
453 0 ▶ warn $_ if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
454 0 ▶ warn "service ($service_id) not available" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
455 0 $self->error("service ($service_id) not available");
456 0 $service_id = '';
457 0 };
458 0 ▶ return unless $service_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $service_id 0 0 0
459  
460 0 my $ua = LWP::UserAgent->new();
461 0 $ua->credentials($self->endpoint . ':443',
462 'login', $self->username, $self->password);
463  
464 0 ▶ if ( $self->from_address()
Branch: 1 branch
BranchTrueFalseTotal
if ($self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code) { } 0 0 0
Condition0%0/9
$self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code
!ll&&!rl&&r
000
$self->from_address and $self->from_address->postal_code and $self->to_address
!ll&&!rl&&r
000
$self->from_address and $self->from_address->postal_code
!ll&&!rl&&r
000
MC/DC0%0/4
$self->from_address and $self->from_address->postal_code and $self->to_address and $self->to_address->postal_code
$self->from_address $self->from_address->postal_code $self->to_address $self->to_address->postal_code
Truth table0%0/5
A and B and C and D
A= $self->from_addressB= $self->from_address->postal_codeC= $self->to_addressD= $self->to_address->postal_code
ABCDresult
0XXX0
10XX0
110X0
11100
11111
465 && $self->from_address()->postal_code
466 && $self->to_address()
467 && $self->to_address()->postal_code)
468 {
469  
470 0 ▶ my $country_code = $self->to_address()->country_code || 'CA';
Condition0%0/2
$self->to_address->country_code || 'CA'
l!l
00
MC/DC0%0/1
$self->to_address->country_code || 'CA'
$self->to_address->country_code
Truth table0%0/2
A
A= $self->to_address->country_code
Aresult
11
00
471 0 ▶ my $data = {
Condition0%0/4
$self->from_address->phone || '0000000000'
l!l
00
$self->to_address->phone || ''
l!l
00
MC/DC0%0/1
$self->from_address->phone || '0000000000'
$self->from_address->phone
MC/DC0%0/1
$self->to_address->phone || ''
$self->to_address->phone
Truth table0%0/2
A
A= $self->from_address->phone
Aresult
11
00
Truth table0%0/2
A
A= $self->to_address->phone
Aresult
11
00
472 'transmit-shipment' => 'true',
473 'cpc-pickup-indicator' => 'true',
474 'requested-shipping-point' => $self->from_address()->postal_code,
475 'provide-pricing-info' => 'true',
476 'delivery-spec' => {
477 'service-code' => $service_id,
478 'sender' => {
479 'company' => $self->from_address->company,
480 'name' => $self->from_address->name,
481 'address-details' => {
482 'address-line-1' => $self->from_address->address1,
483 'address-line-2' => $self->from_address->address2,
484 'city' => $self->from_address->city,
485 'prov-state' => $self->from_address->province_code,
486 'postal-zip-code' => $self->from_address->postal_code,
487 'country-code' => $self->from_address->country_code,
488 },
489 'contact-phone' => $self->from_address()->phone
490 || '0000000000',
491 },
492 'destination' => {
493 'company' => $self->to_address->name,
494 'name' => $self->to_address->company,
495 'address-details' => {
496 'address-line-1' => $self->to_address->address1,
497 'address-line-2' => $self->to_address->address2,
498 'city' => $self->to_address->city,
499 'prov-state' => $self->to_address->province_code,
500 'postal-zip-code' => $self->to_address->postal_code,
501 'country-code' => $self->to_address->country_code,
502 },
503 'client-voice-number' => $self->to_address()->phone || '',
504 },
505 'preferences' => {'show-packing-instructions' => 'false',},
506 'settlement-info' => {
507 'paid-by-customer' => $self->bill_account,
508 'contract-id' => $self->contract_id,
509 'intended-method-of-payment' => 'Account',
510 },
511 },
512 };
513  
514 0 ▶ if ($self->signature_type eq 'required') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->signature_type eq 'required') { } 0 0 0
elsif ($self->signature_type eq 'adult') { } 0 0 0
515 0 push @{$data->{'delivery-spec'}->{'options'}->{option}},
516 {'option-code' => 'SO'};
517 }
518 elsif ($self->signature_type eq 'adult') {
519 0 push @{$data->{'delivery-spec'}->{'options'}->{option}},
520 {'option-code' => 'PA18'};
521 }
522  
523 0 ▶ if ($self->to_address->country_code ne 'CA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->to_address->country_code ne "CA") 0 0 0
524 0 my $non_delivery_handling = $self->non_delivery_handling;
525 0 ▶ if (!$non_delivery_handling) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($non_delivery_handling) 0 0 0
526 0 ▶ if ($service_id eq 'USA.TP' || $service_id eq 'INT.TP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_id eq 'USA.TP' or $service_id eq 'INT.TP') { } 0 0 0
Condition0%0/3
$service_id eq "USA.TP" or $service_id eq "INT.TP"
l!l&&r!l&&!r
000
MC/DC0%0/2
$service_id eq "USA.TP" or $service_id eq "INT.TP"
$service_id eq "USA.TP" $service_id eq "INT.TP"
Truth table0%0/3
A or B
A= $service_id eq "USA.TP"B= $service_id eq "INT.TP"
ABresult
1X1
011
000
527 0 $non_delivery_handling = 'RTS';
528 }
529 else {
530 0 $non_delivery_handling = 'RASE';
531 }
532 }
533 0 push @{$data->{'delivery-spec'}->{'options'}->{option}},
534 {'option-code' => $non_delivery_handling};
535 }
536  
537 0 ▶ if ($self->to_address->email) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->to_address->email) 0 0 0
538 0 $data->{'delivery-spec'}->{'notification'} = {
539 'email' => $self->to_address->email,
540 'on-shipment' => 'true',
541 'on-exception' => 'true',
542 'on-delivery' => 'true',
543 };
544 }
545  
546 0 ▶ if ($self->printer_type =~ /(thermal|zpl)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->printer_type =~ /(thermal|zpl)/i) { } 0 0 0
547 0 $data->{'delivery-spec'}->{'print-preferences'} = {
548 'output-format' => '4x6',
549 'encoding' => 'ZPL',
550 };
551 }
552 else {
553 0 $data->{'delivery-spec'}->{'print-preferences'} = {
554 'output-format' => '8.5x11',
555 'encoding' => 'PDF',
556 };
557 }
558  
559 0 ▶ if ($self->get_reference(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_reference(0)) 0 0 0
560 0 $data->{'delivery-spec'}->{'references'}->{'customer-ref-1'} =
561 $self->get_reference(0);
562 }
563 0 ▶ if ($self->get_reference(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_reference(1)) 0 0 0
564 0 $data->{'delivery-spec'}->{'references'}->{'customer-ref-2'} =
565 $self->get_reference(1);
566 }
567  
568 0 my $schema =
569 XML::Compile::Schema->new($Shipment::CanadaPost::XSD::shipment
570 . $Shipment::CanadaPost::XSD::common);
571  
572 0 my $shipment_id;
573 0 my $cost = 0;
574 0 my $etd = 0;
575 0 my $eta;
576 0 my $package_index = 0;
577 0 foreach my $package (@{$self->packages}) {
578  
579 0 my $weight = $package->weight;
580 0 ▶ if ($self->weight_unit eq 'lb') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->weight_unit eq 'lb') { } 0 0 0
elsif ($self->weight_unit eq 'oz') { } 0 0 0
581 0 $weight = sprintf('%.4f', $weight * 0.4535924);
582 }
583 elsif ($self->weight_unit eq 'oz') {
584 0 $weight = sprintf('%.4f', $weight * 0.02834952);
585 }
586 0 my ($l, $w, $h) =
587 ($package->length, $package->width, $package->height);
588 0 ▶ if ($self->dim_unit eq 'in') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dim_unit eq "in") 0 0 0
589 0 $l = sprintf('%.4f', $l * 2.54);
590 0 $w = sprintf('%.4f', $w * 2.54);
591 0 $h = sprintf('%.4f', $h * 2.54);
592 }
593 0 $data->{'delivery-spec'}->{'parcel-characteristics'} = {
594 weight => $weight,
595 dimensions => {
596 length => $l,
597 width => $w,
598 height => $h,
599 },
600 };
601  
602 0 ▶ if ($self->to_address->country_code ne 'CA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->to_address->country_code ne "CA") 0 0 0
603 0 $data->{'delivery-spec'}->{'customs'} = {
604 'currency' => $self->customs_currency,
605 'conversion-from-cad' => $self->customs_conversion,
606 'reason-for-export' => $self->customs_reason,
607 'sku-list' => {'item' => []},
608 };
609 0 foreach my $item (@{$package->items}) {
610 0 my $origin_country;
611 0 ▶ if ($item->{origin_country}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"origin_country"}) 0 0 0
612 0 my $origin_country = $item->{origin_country};
613 0 ▶ if ($origin_country =~ /korea/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origin_country =~ /korea/i) 0 0 0
614 0 $origin_country = 'KR';
615 }
616 1 ▶ use Locale::SubCountry;
Subroutine BEGIN: called CC=1 SCAR=0.0
617 0 my $country = Locale::SubCountry->new($origin_country);
618 0 ▶ $origin_country = $country->country_code
Branch: 1 branch
BranchTrueFalseTotal
if $country and $country->country_code 0 0 0
Condition0%0/3
$country and $country->country_code
!ll&&!rl&&r
000
MC/DC0%0/2
$country and $country->country_code
$country $country->country_code
Truth table0%0/3
A and B
A= $countryB= $country->country_code
ABresult
0X0
100
111
619 if $country && $country->country_code;
620 }
621 0 my $item_weight = $item->{weight};
622 0 ▶ if ($item_weight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_weight) 0 0 0
623 0 ▶ if ($self->weight_unit eq 'lb') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->weight_unit eq 'lb') { } 0 0 0
elsif ($self->weight_unit eq 'oz') { } 0 0 0
624 0 $item_weight =
625 sprintf('%.4f', $item_weight * 0.4535924);
626 }
627 elsif ($self->weight_unit eq 'oz') {
628 0 $item_weight =
629 sprintf('%.4f', $item_weight * 0.02834952);
630 }
631 }
632 push @{$data->{'delivery-spec'}->{'customs'}->{'sku-list'}
633 0 ▶ ->{'item'}},
Condition0%0/9
$item->{'quantity'} || 1
l!l
00
substr($item->{'customs_description'}, 0, 44) || 'Misc'
l!l
00
$item_weight || $weight
l!l&&r!l&&!r
000
$item->{'customs_value'} || 1
l!l
00
MC/DC0%0/1
$item->{'quantity'} || 1
$item->{'quantity'}
MC/DC0%0/1
substr($item->{'customs_description'}, 0, 44) || 'Misc'
substr($item->{'customs_description'}, 0, 44)
MC/DC0%0/2
$item_weight || $weight
$item_weight $weight
MC/DC0%0/1
$item->{'customs_value'} || 1
$item->{'customs_value'}
Truth table0%0/2
A
A= $item->{'quantity'}
Aresult
11
00
Truth table0%0/2
A
A= substr($item->{'customs_description'}, 0, 44)
Aresult
11
00
Truth table0%0/3
A || B
A= $item_weightB= $weight
ABresult
1X1
011
000
Truth table0%0/2
A
A= $item->{'customs_value'}
Aresult
11
00
634 { 'customs-number-of-units' => $item->{quantity} || 1,
635 'customs-description' =>
636 substr($item->{customs_description}, 0, 44)
637 || 'Misc',
638 'unit-weight' => $item_weight || $weight,
639 'customs-value-per-unit' => $item->{customs_value}
640 0 || 1,
641 'country-of-origin' => $origin_country,
642 };
643 }
644 0 ▶ if (!scalar @{
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$data->{"delivery-spec"}{"customs"}{"sku-list"}{"item"};}) 0 0 0
645 $data->{'delivery-spec'}->{'customs'}->{'sku-list'}
646 ->{item}
647 0 }
648 )
649 {
650 0 push @{$data->{'delivery-spec'}->{'customs'}->{'sku-list'}
651 0 ->{'item'}},
652 { 'customs-number-of-units' => 1,
653 'customs-description' => 'Misc',
654 'unit-weight' => $weight,
655 'customs-value-per-unit' => 1,
656 };
657 }
658 }
659  
660 0 my $xml_data =
661 $schema->compile('WRITER',
662 '{http://www.canadapost.ca/ws/shipment-v8}shipment')
663 ->(XML::LibXML::Document->new('1.0', 'UTF-8'), $data)->toString;
664 0 ▶ warn 'XML Request: ' . $xml_data if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
665  
666 0 my $url =
667 'https://'
668 . $self->endpoint . '/rs/'
669 . $self->account . '/'
670 . $self->bill_account
671 . '/shipment';
672  
673 1 ▶ use File::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
674 0 my $f = File::Util->new();
675 0 my $tmp_file =
676 $self->tmp_file_dir . '/shipment_canada_post' . time();
677 0 $f->write_file(
678 file => $tmp_file,
679 bitmask => 0644,
680 content => $xml_data
681 );
682 0 my $username = $self->username;
683 0 my $password = $self->password;
684 0 my $response =
685 `curl -s -w "%{http_code}" -H "Content-Type: application/vnd.cpc.shipment-v8+xml" -H "Accept: application/vnd.cpc.shipment-v8+xml" -u $username:$password -d \@$tmp_file $url`;
686 0 unlink $tmp_file;
687 0 $response =~ s/(\d\d\d)$//;
688 0 my $status = $1;
689 0 ▶ warn 'HTTP Status: ' . $status if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
690 0 ▶ warn 'XML Response: ' . $response if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
691  
692 0 ▶ if ($status ne '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status ne 200) 0 0 0
693 0 $self->error('shipment request failed with message: '
694 . $self->_messages($response));
695 0 return;
696 }
697  
698 0 my $reader = $schema->compile(READER =>
699 '{http://www.canadapost.ca/ws/shipment-v8}shipment-info');
700 0 my $shipment = $reader->($response);
701  
702 0 ▶ $shipment_id ||= $shipment->{'shipment-id'};
Condition0%0/3
$shipment_id ||= $shipment->{"shipment-id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$shipment_id ||= $shipment->{"shipment-id"}
$shipment_id $shipment->{"shipment-id"}
Truth table0%0/3
A ||= B
A= $shipment_idB= $shipment->{"shipment-id"}
ABresult
1X1
011
000
703 0 $cost += $shipment->{'shipment-price'}->{'due-amount'};
704 $etd
705 ||= $shipment->{'shipment-price'}->{'service-standard'}
706 ->{'expected-transmit-time'}
707 || $shipment->{'shipment-price'}->{'service-standard'}
708 0 ▶ ->{'expected-transit-time'};
Condition0%0/6
$etd ||= $shipment->{"shipment-price"}{"service-standard"}{"expected-transmit-time"} || $shipment->{"shipment-price"}{"service-standard"}{"expected-transit-time"}
l!l&&r!l&&!r
000
$shipment->{'shipment-price'}{'service-standard'}{'expected-transmit-time'} || $shipment->{'shipment-price'}{'service-standard'}{'expected-transit-time'}
l!l&&r!l&&!r
000
MC/DC0%0/3
$etd ||= $shipment->{"shipment-price"}{"service-standard"}{"expected-transmit-time"} || $shipment->{"shipment-price"}{"service-standard"}{"expected-transit-time"}
$etd $shipment->{'shipment-price'}{'service-standard'}{'expected-transmit-time'} $shipment->{'shipment-price'}{'service-standard'}{'expected-transit-time'}
Truth table0%0/4
A ||= B || C
A= $etdB= $shipment->{'shipment-price'}{'service-standard'}{'expected-transmit-time'}C= $shipment->{'shipment-price'}{'service-standard'}{'expected-transit-time'}
ABCresult
1XX1
01X1
0011
0000
709 my ($y, $m, $d) = split('-',
710 $shipment->{'shipment-price'}->{'service-standard'}
711 0 ->{'expected-delivery-date'});
712 0 ▶ if ($y && $m && $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y and $m and $d) 0 0 0
Condition0%0/6
$y and $m and $d
!ll&&!rl&&r
000
$y and $m
!ll&&!rl&&r
000
MC/DC0%0/3
$y and $m and $d
$y $m $d
Truth table0%0/4
A and B and C
A= $yB= $mC= $d
ABCresult
0XX0
10X0
1100
1111
713 0 ▶ $eta ||= {year => $y, month => $m, day => $d};
Condition0%0/2
$eta ||= {"year", $y, "month", $m, "day", $d}
l!l
00
MC/DC0%0/1
$eta ||= {"year", $y, "month", $m, "day", $d}
$eta
Truth table0%0/2
A
A= $eta
Aresult
11
00
714 }
715  
716 0 my $tracking_id = $shipment->{'tracking-pin'};
717 0 $self->get_package($package_index)->id($shipment->{'shipment-id'});
718 0 $self->get_package($package_index)->tracking_id($tracking_id);
719 $self->get_package($package_index)->cost(
720 Data::Currency->new(
721 0 $shipment->{'shipment-price'}->{'due-amount'}, 'CAD'
722 )
723 );
724  
725 0 my $label_url;
726 my $commercial_invoice_url;
727 0 my $label_data;
728 0 foreach my $link (@{$shipment->{'links'}->{'link'}}) {
729 0 ▶ if ($link->{'rel'} eq 'label') {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->{"rel"} eq "label") 0 0 0
730 0 $label_url = $link->{'href'};
731 }
732 0 ▶ if ($link->{'rel'} eq 'commercialInvoice') {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->{"rel"} eq "commercialInvoice") 0 0 0
733 0 $commercial_invoice_url = $link->{'href'};
734 }
735 }
736 0 ▶ if ($label_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label_url) 0 0 0
737 0 ▶ my $response = $ua->get(
Branch: 1 branch
BranchTrueFalseTotal
$self->printer_type =~ /(thermal|zpl)/i ? : 0 0 0
738 $label_url,
739 'Accept' => (
740 $self->printer_type =~ /(thermal|zpl)/i
741 ? 'application/zpl'
742 : 'application/pdf'
743 )
744 );
745  
746 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->is_success) { } 0 0 0
747 0 ▶ warn $response->status_line if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
748 0 $self->error('label request failed with status: '
749 . $response->status_line);
750 }
751 else {
752  
753 0 ▶ warn 'LABEL Response: ' . $response->content
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
754 if $self->debug;
755 0 $label_data = $response->content;
756 }
757 }
758  
759 0 ▶ if ($commercial_invoice_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commercial_invoice_url) 0 0 0
760 0 ▶ my $response = $ua->get(
Branch: 1 branch
BranchTrueFalseTotal
$self->printer_type =~ /(thermal|zpl)/i ? : 0 0 0
761 $commercial_invoice_url,
762 'Accept' => (
763 $self->printer_type =~ /(thermal|zpl)/i
764 ? 'application/zpl'
765 : 'application/pdf'
766 )
767 );
768  
769 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->is_success) { } 0 0 0
770 0 ▶ warn $response->status_line if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
771 0 $self->error(
772 'commercial invoice request failed with status: '
773 . $response->status_line);
774 }
775 else {
776  
777 0 ▶ warn 'COMMERCIAL INVOICE Response: ' . $response->content
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
778 if $self->debug;
779 0 $label_data .= "\n\n" . $response->content;
780 }
781 }
782  
783 0 ▶ if ($label_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label_data) 0 0 0
784 $self->get_package($package_index)->label(
785 Shipment::Label->new(
786 { tracking_id => $tracking_id,
787 content_type => (
788 $self->printer_type =~ /(thermal|zpl)/i
789 ? 'text/canadapost-zpl'
790 : 'application/pdf'
791 ),
792 data => $label_data,
793 0 ▶ file_name => $shipment->{'tracking-pin'} . '.'
Branch: 2 branches
BranchTrueFalseTotal
$self->printer_type =~ /(thermal|zpl)/i ? : 0 0 0
$self->printer_type =~ /(thermal|zpl)/i ? : 0 0 0
794 . (
795 $self->printer_type =~ /(thermal|zpl)/i
796 ? 'zpl'
797 : 'pdf'
798 ),
799 },
800 )
801 );
802 }
803 0 $package_index++;
804  
805 }
806  
807 0 $self->tracking_id($shipment_id);
808 $self->service(
809 Shipment::Service->new(
810 id => $service_id,
811 0 name => $self->services->{$service_id}->name,
812 cost => Data::Currency->new($cost, 'CAD'),
813 etd => $etd,
814 eta => $eta,
815 )
816 );
817  
818 }
819 else {
820 # from and to address required
821 0 $self->error("Both from and to address are required for shipping.");
822 }
823  
824 0 return;
825  
826 }
827  
828  
829 1 1;
830  
831 __END__
↑ Summary Shipment/Sendle.pm »