blib/lib/Business/OnlinePayment/InternetSecure.pm

filter: Statementstmt 88.6% 133 / 150 Branchbran 74.1% 46 / 62 Conditioncond 67.6% 23 / 34 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 95.8% 23 / 24 Podpod 9.0% 1 / 11 totaltotal 77.3% 232 / 300 CC 47 SCAR 22.9
CC
47
Cov
82%
submit
20.0
to_xml
16.8
prod_string
16.6
CRAP 9.8
↑ Summary
1 package Business::OnlinePayment::InternetSecure;
2  
3 9 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Net::SSLeay qw(make_form post_https);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use XML::Simple qw(xml_in xml_out);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use base qw(Business::OnlinePayment Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 9 our $VERSION = '0.10';
16  
17  
18 9 ▶use constant SUCCESS_CODES => qw(2000 90000 900P1);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶use constant CARD_TYPES => {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 AM => 'American Express',
22 JB => 'JCB',
23 MC => 'MasterCard',
24 NN => 'Discover',
25 VI => 'Visa',
26 9 };
27  
28  
29 # Convenience functions to avoid undefs and escape products strings
30 26 ▶sub _def($) { defined $_[0] ? $_[0] : '' }
Subroutine _def: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 6 20 26
31 26 ▶sub _esc($) { local $_ = shift; tr/|:/ /s; tr/"`/'/s; return $_ }
Subroutine _esc: called CC=1 SCAR=0.0
32  
33  
34 8 ▶sub set_defaults {
Subroutine set_defaults: called CC=1 SCAR=0.0
Pod undocumented
35 8 my ($self) = @_;
36  
37 8 $self->server('secure.internetsecure.com');
38 8 $self->port(443);
39 8 $self->path('/process.cgi');
40  
41 8 $self->build_subs(qw(
42 receipt_number order_number uuid guid
43 date
44 card_type cardholder
45 total_amount tax_amounts
46 avs_code cvv2_response
47 ));
48  
49 # Just in case someone tries to call tax_amounts() *before* submit()
50 8 $self->tax_amounts( {} );
51 }
52  
53 # Backwards-compatible support for renamed fields
54 1 ▶sub avs_response { shift()->avs_code(@_) }
Subroutine avs_response: called CC=1 SCAR=0.0
Pod undocumented
55 1 ▶sub sales_number { shift()->order_number(@_) }
Subroutine sales_number: called CC=1 SCAR=0.0
Pod undocumented
56  
57  
58 # Combine get_fields and remap_fields for convenience. Unlike OnlinePayment's
59 # remap_fields, this doesn't modify content(), and can therefore be called
60 # more than once. Also, unlike OnlinePayment's get_fields in 3.x, this doesn't
61 # exclude undefs.
62 #
63 11 ▶sub get_remap_fields {
Subroutine get_remap_fields: called CC=2 SCAR=6.9
Pod undocumented
64 11 my ($self, %map) = @_;
65  
66 11 my %content = $self->content();
67 11 my %data;
68  
69 11 ▶ while (my ($to, $from) = each %map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %map) 220 11 231
70 220 $data{$to} = $content{$from};
71 }
72  
73 11 return %data;
74 }
75  
76 # Since there's no standard format for expiration dates, we try to do our best
77 #
78 11 ▶sub parse_expdate {
Subroutine parse_expdate: called CC=8 SCAR=21.6
Pod undocumented
79 11 my ($self, $str) = @_;
80  
81 11 local $_ = $str;
82  
83 11 my ($y, $m);
84  
85 11 ▶ if (/^(\d{4})\W(\d{1,2})$/ || # YYYY.MM or YYYY-M
Branch: 2 branches
BranchTrueFalseTotal
if (/^(\d{4})\W(\d{1,2})$/ or /^(\d\d)\W(\d)$/ or /^(\d\d)[.-](\d\d)$/) { } 3 8 11
elsif (/^(\d{1,2})\W(\d{4})$/ or /^(\d)\W(\d\d)$/ or m[^(\d\d)/?(\d\d)$]) { } 8 0 8
Condition66%4/6
/^(\d{4})\W(\d{1,2})$/ or /^(\d\d)\W(\d)$/ or /^(\d\d)[.-](\d\d)$/
l!l&&r!l&&!r
308
/^(\d{4})\W(\d{1,2})$/ or /^(\d\d)\W(\d)$/
l!l&&r!l&&!r
308
MC/DC33%1/3
/^(\d{4})\W(\d{1,2})$/ or /^(\d\d)\W(\d)$/ or /^(\d\d)[.-](\d\d)$/
/^(\d{4})\W(\d{1,2})$/ /^(\d\d)\W(\d)$/ /^(\d\d)[.-](\d\d)$/
Truth table50%2/4
A or B or C
A= /^(\d{4})\W(\d{1,2})$/B= /^(\d\d)\W(\d)$/C= /^(\d\d)[.-](\d\d)$/
ABCresult
1XX1
01X1
0011
0000
86 /^(\d\d)\W(\d)$/ || # YY/M or YY-M
87 /^(\d\d)[.-](\d\d)$/) { # YY-MM
88 3 ($y, $m) = ($1, $2);
89 ▶ } elsif (/^(\d{1,2})\W(\d{4})$/ || # MM-YYYY or M/YYYY
Condition66%4/6
/^(\d{1,2})\W(\d{4})$/ or /^(\d)\W(\d\d)$/ or m[^(\d\d)/?(\d\d)$]
l!l&&r!l&&!r
170
/^(\d{1,2})\W(\d{4})$/ or /^(\d)\W(\d\d)$/
l!l&&r!l&&!r
107
MC/DC0%0/3
/^(\d{1,2})\W(\d{4})$/ or /^(\d)\W(\d\d)$/ or m[^(\d\d)/?(\d\d)$]
/^(\d{1,2})\W(\d{4})$/ /^(\d)\W(\d\d)$/ m[^(\d\d)/?(\d\d)$]
Truth table50%2/4
A or B or C
A= /^(\d{1,2})\W(\d{4})$/B= /^(\d)\W(\d\d)$/C= m[^(\d\d)/?(\d\d)$]
ABCresult
1XX1
01X1
0011
0000
90 /^(\d)\W(\d\d)$/ || # M/YY or M-YY
91 /^(\d\d)\/?(\d\d)$/) { # MM/YY or MMYY
92 8 ($y, $m) = ($2, $1);
93 } else {
94 0 croak "Unable to parse expiration date: $str";
95 }
96  
97 11 ▶ $y += 2000 if $y < 2000; # Aren't we glad Y2K is behind us?
Branch: 1 branch
BranchTrueFalseTotal
if $y < 2000 7 4 11
98  
99 11 return ($y, $m);
100 }
101  
102 # Convert a single product into a product string
103 #
104 13 ▶sub prod_string {
Subroutine prod_string: called CC=12 SCAR=28.1
Pod undocumented
105 13 my ($self, $currency, %data) = @_;
106  
107 13 ▶ croak "Missing amount in product" unless defined $data{amount};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{"amount"} 0 13 13
108  
109 13 my @flags = ($currency);
110  
111 13 my @taxes;
112 13 ▶ if (ref $data{taxes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data{'taxes'}) { } 1 12 13
elsif ($data{'taxes'}) { } 3 9 12
113 1 @taxes = @{ $data{taxes} };
114 } elsif ($data{taxes}) {
115 3 @taxes = split ' ' => $data{taxes};
116 }
117  
118 13 foreach (@taxes) {
119 6 ▶ croak "Unknown tax code $_" unless /^(GST|PST|HST)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless /^(GST|PST|HST)$/i 0 6 6
120 6 push @flags, uc $_;
121 }
122  
123 13 ▶ if ($self->test_transaction) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->test_transaction) 2 11 13
124 2 ▶ push @flags, $self->test_transaction < 0 ? 'TESTD' : 'TEST';
Branch: 1 branch
BranchTrueFalseTotal
$self->test_transaction < 0 ? : 1 1 2
125 }
126  
127 # recurring can come as string or hashref
128 13 ▶ if ($data{recurring}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"recurring"}) 0 13 13
129 0 ▶ if (ref $data{recurring}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data{'recurring'}) { } 0 0 0
130 0 my @options;
131 0 push @options, 'RB';
132 0 foreach my $key ( sort keys %{ $data{recurring} } ) {
133 0 push @options, $key . '=' . $data{recurring}{$key};
134 }
135 0 push @flags, join ' ', @options;
136 } else {
137 0 push @flags, "RB $data{recurring}";
138 }
139 }
140  
141 return join '::' =>
142 sprintf('%.2f' => $data{amount}),
143 $data{quantity} || 1,
144 _esc _def $data{sku},
145 _esc _def $data{description},
146 13 ▶ join('' => map "{$_}" => @flags),
Condition100%2/2
$data{'quantity'} || 1
l!l
112
MC/DC100%1/1
$data{'quantity'} || 1
$data{'quantity'}
Truth table100%2/2
A
A= $data{'quantity'}
Aresult
11
00
147 ;
148 }
149  
150 # Generate the XML document for this transaction
151 #
152 11 ▶sub to_xml {
Subroutine to_xml: called CC=16 SCAR=28.2
Pod undocumented
153 11 my ($self) = @_;
154  
155 11 my %content = $self->content;
156  
157 # Backwards-compatible support for exp_date
158 11 ▶ if (exists $content{exp_date} && ! exists $content{expiration}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $content{"exp_date"} and not exists $content{"expiration"}) 4 7 11
Condition66%2/3
exists $content{"exp_date"} and not exists $content{"expiration"}
!ll&&!rl&&r
704
MC/DC50%1/2
exists $content{"exp_date"} and not exists $content{"expiration"}
exists $content{"exp_date"} not exists $content{"expiration"}
Truth table66%2/3
A and not(B)
A= exists $content{"exp_date"}B= not exists $content{"expiration"}
ABresult
0X0
100
111
159 4 $content{expiration} = delete $content{exp_date};
160 4 $self->content(%content);
161 }
162  
163 11 $self->required_fields(qw(action card_number expiration));
164  
165 croak "Unsupported card type: $content{type}"
166 if $content{type} &&
167 ! grep lc($content{type}) eq lc($_),
168 11 ▶ values %{+CARD_TYPES}, 'CC';
Branch: 1 branch
BranchTrueFalseTotal
if $content{"type"} and not grep((lc $content{"type"} eq lc $_), values %{{"AM" => "American Express","JB" => "JCB","MC" => "MasterCard","NN" => "Discover","VI" => "Visa"};}, "CC") 0 11 11
Condition66%2/3
$content{"type"} and not grep((lc $content{"type"} eq lc $_), values %{{"AM" => "American Express","JB" => "JCB","MC" => "MasterCard","NN" => "Discover","VI" => "Visa"};}, "CC")
!ll&&!rl&&r
740
MC/DC0%0/2
$content{"type"} and not grep((lc $content{"type"} eq lc $_), values %{{"AM" => "American Express","JB" => "JCB","MC" => "MasterCard","NN" => "Discover","VI" => "Visa"};}, "CC")
$content{"type"} not grep((lc $content{"type"} eq lc $_), values %{{"AM" => "American Express","JB" => "JCB","MC" => "MasterCard","NN" => "Discover","VI" => "Visa"};}, "CC")
Truth table66%2/3
A and not(B)
A= $content{"type"}B= not grep((lc $content{"type"} eq lc $_), values %{{"AM" => "American Express","JB" => "JCB","MC" => "MasterCard","NN" => "Discover","VI" => "Visa"};}, "CC")
ABresult
0X0
100
111
169  
170 croak 'Unsupported action'
171 11 ▶ unless $content{action} =~ /^(Normal|Card) Authori[zs]ation$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $content{"action"} =~ /^(Normal|Card) Authori[zs]ation$/i 0 11 11
172  
173 11 ▶ $content{currency} = uc($content{currency} || 'CAD');
Condition100%2/2
$content{'currency'} || 'CAD'
l!l
29
MC/DC100%1/1
$content{'currency'} || 'CAD'
$content{'currency'}
Truth table100%2/2
A
A= $content{'currency'}
Aresult
11
00
174 croak "Unknown currency code ", $content{currency}
175 11 ▶ unless $content{currency} =~ /^(CAD|USD)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $content{"currency"} =~ /^(CAD|USD)$/ 0 11 11
176  
177 11 my %data = $self->get_remap_fields(qw(
178 xxxCard_Number card_number
179  
180 xxxName name
181 xxxCompany company
182 xxxAddress address
183 xxxCity city
184 xxxProvince state
185 xxxPostal zip
186 xxxCountry country
187 xxxPhone phone
188 xxxEmail email
189  
190 xxxShippingName ship_name
191 xxxShippingCompany ship_company
192 xxxShippingAddress ship_address
193 xxxShippingCity ship_city
194 xxxShippingProvince ship_state
195 xxxShippingPostal ship_zip
196 xxxShippingCountry ship_country
197 xxxShippingPhone ship_phone
198 xxxShippingEmail ship_email
199  
200 xxxCustomerDB cimb_store
201 ));
202  
203 11 $data{MerchantNumber} = $self->merchant_id;
204  
205 11 $data{xxxCard_Number} =~ tr/- //d;
206 11 ▶ $data{xxxCard_Number} =~ s/^[^3-6]/4/ if $self->test_transaction;
Branch: 1 branch
BranchTrueFalseTotal
if $self->test_transaction 2 9 11
207  
208 11 my ($y, $m) = $self->parse_expdate($content{expiration});
209 11 $data{xxxCCYear} = sprintf '%.4u' => $y;
210 11 $data{xxxCCMonth} = sprintf '%.2u' => $m;
211  
212 11 ▶ delete $data{xxxCustomerDB} unless $data{xxxCustomerDB};
Branch: 1 branch
BranchTrueFalseTotal
unless $data{"xxxCustomerDB"} 9 2 11
213  
214 # Recurring
215 11 ▶ if (defined $content{recurring} && $content{recurring} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content{"recurring"} and $content{"recurring"} ne "") 0 11 11
Condition33%1/3
defined $content{"recurring"} and $content{"recurring"} ne ""
!ll&&!rl&&r
1100
MC/DC0%0/2
defined $content{"recurring"} and $content{"recurring"} ne ""
defined $content{"recurring"} $content{"recurring"} ne ""
Truth table33%1/3
A and B
A= defined $content{"recurring"}B= $content{"recurring"} ne ""
ABresult
0X0
100
111
216 0 $data{xxxCardInput} = 8;
217 }
218  
219 11 ▶ if (defined $content{cvv2} && $content{cvv2} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content{'cvv2'} and $content{'cvv2'} ne '') { } 1 10 11
Condition66%2/3
defined $content{"cvv2"} and $content{"cvv2"} ne ""
!ll&&!rl&&r
1001
MC/DC50%1/2
defined $content{"cvv2"} and $content{"cvv2"} ne ""
defined $content{"cvv2"} $content{"cvv2"} ne ""
Truth table66%2/3
A and B
A= defined $content{"cvv2"}B= $content{"cvv2"} ne ""
ABresult
0X0
100
111
220 1 $data{CVV2} = $content{cvv2};
221 1 $data{CVV2Indicator} = 1;
222 } else {
223 10 $data{CVV2} = '';
224 10 $data{CVV2Indicator} = 0;
225 }
226  
227 11 ▶ if ($content{action} =~ /^Card Authori[zs]ation$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content{'action'} =~ /^Card Authori[zs]ation$/i) { } 1 10 11
228 1 $data{xxxTransType} = 22;
229  
230 $data{Products} = $self->prod_string(
231 $content{currency},
232 1 taxes => 0,
233 amount => 0.0,
234 description => 'CardAuth and Store',
235 );
236 } else {
237  
238 10 ▶ if (ref $content{description}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content{'description'}) { } 1 9 10
239 $data{Products} = join '|' => map $self->prod_string(
240 $content{currency},
241 taxes => $content{taxes},
242 %$_),
243 1 @{ $content{description} };
244 } else {
245 9 $self->required_fields(qw(amount));
246 $data{Products} = $self->prod_string(
247 $content{currency},
248 taxes => $content{taxes},
249 amount => $content{amount},
250 description => $content{description},
251 recurring => $content{recurring},
252 9 );
253 }
254  
255 }
256  
257 # The encode() makes sure to a) strip off non-Latin-1 characters, and
258 # b) turn off the utf8 flag, which confuses XML::Simple
259 11 encode('ISO-8859-1', xml_out(\%data,
260 NoAttr => 1,
261 RootName => 'TranxRequest',
262 SuppressEmpty => undef,
263 XMLDecl => '<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>',
264 ));
265 }
266  
267 # Map the various fields from the response, and put their values into our
268 # object for retrieval.
269 #
270 4 ▶sub infuse {
Subroutine infuse: called CC=2 SCAR=6.9
Pod undocumented
271 4 my ($self, $data, %map) = @_;
272  
273 4 ▶ while (my ($k, $v) = each %map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %map) 52 4 56
274 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 52 $self->$k($data->{$v});
276 }
277 }
278  
279 4 ▶sub extract_tax_amounts {
Subroutine extract_tax_amounts: called CC=6 SCAR=18.0
Pod undocumented
280 4 my ($self, $response) = @_;
281  
282 4 my %tax_amounts;
283  
284 4 my $products = $response->{Products};
285 4 ▶ return unless $products;
Branch: 1 branch
BranchTrueFalseTotal
unless $products 2 2 4
286  
287 2 foreach my $node (@$products) {
288 9 my $flags = $node->{flags};
289 9 ▶ if ($flags &&
Branch: 1 branch
BranchTrueFalseTotal
if ($flags and grep(($_ eq "{TAX}"), @$flags) and grep(($_ eq "{CALCULATED}"), @$flags)) 3 6 9
Condition66%4/6
$flags and grep(($_ eq "{TAX}"), @$flags) and grep(($_ eq "{CALCULATED}"), @$flags)
!ll&&!rl&&r
603
$flags and grep(($_ eq "{TAX}"), @$flags)
!ll&&!rl&&r
063
MC/DC33%1/3
$flags and grep(($_ eq "{TAX}"), @$flags) and grep(($_ eq "{CALCULATED}"), @$flags)
$flags grep(($_ eq "{TAX}"), @$flags) grep(($_ eq "{CALCULATED}"), @$flags)
Truth table50%2/4
A and B and C
A= $flagsB= grep(($_ eq "{TAX}"), @$flags)C= grep(($_ eq "{CALCULATED}"), @$flags)
ABCresult
0XX0
10X0
1100
1111
290 grep($_ eq '{TAX}', @$flags) &&
291 grep($_ eq '{CALCULATED}', @$flags))
292 {
293 3 $tax_amounts{ $node->{code} } = $node->{subtotal};
294 }
295 }
296  
297 2 return %tax_amounts;
298 }
299  
300 # Parse the server's response and set various fields
301 #
302 4 ▶sub parse_response {
Subroutine parse_response: called CC=3 SCAR=11.0
Pod undocumented
303 4 my ($self, $response) = @_;
304  
305 4 $self->server_response($response);
306  
307 4 local $/ = "\n"; # Make sure to avoid bug #17687
308  
309 4 $response = xml_in($response,
310 ForceArray => [qw(product flag)],
311 GroupTags => { qw(Products product flags flag) },
312 KeyAttr => [],
313 SuppressEmpty => undef,
314 );
315  
316 4 $self->infuse($response,
317 result_code => 'Page',
318 error_message => 'Verbiage',
319 authorization => 'ApprovalCode',
320 avs_code => 'AVSResponseCode',
321 cvv2_response => 'CVV2ResponseCode',
322  
323 receipt_number => 'ReceiptNumber',
324 order_number => 'SalesOrderNumber',
325 uuid => 'GUID',
326 guid => 'GUID',
327  
328 date => 'Date',
329 cardholder => 'xxxName',
330 card_type => 'CardType',
331 total_amount => 'TotalAmount',
332 );
333  
334 4 $self->is_success(scalar grep $self->result_code eq $_, SUCCESS_CODES);
335  
336 # Completely undocumented field that sometimes override <Verbiage>
337 4 ▶ $self->error_message($response->{Error}) if $response->{Error};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"Error"} 1 3 4
338  
339 # Delete error_message if transaction was successful
340 4 ▶ $self->error_message(undef) if $self->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_success 3 1 4
341  
342 4 $self->card_type(CARD_TYPES->{$self->card_type});
343  
344 4 $self->tax_amounts( { $self->extract_tax_amounts($response) } );
345  
346 4 return $self;
347 }
348  
349 0 ▶sub submit {
Subroutine submit: not called CC=4 SCAR=30.0
Pod documented
350 0 my ($self) = @_;
351  
352 croak "Missing required argument 'merchant_id'"
353 0 ▶ unless defined $self->{merchant_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"merchant_id"} 0 0 0
354  
355 0 my ($page, $response, %headers) =
356 post_https(
357 $self->server,
358 $self->port,
359 $self->path,
360 undef,
361 make_form(
362 xxxRequestMode => 'X',
363 xxxRequestData => $self->to_xml,
364 )
365 );
366  
367 0 ▶ croak 'Error connecting to server' unless $page;
Branch: 1 branch
BranchTrueFalseTotal
unless $page 0 0 0
368 0 ▶ croak 'Server responded, but not in XML' unless $page =~ /^<\?xml/;
Branch: 1 branch
BranchTrueFalseTotal
unless $page =~ /^<\?xml/ 0 0 0
369  
370 # The response is marked UTF-8, but it's really Latin-1. Sigh.
371 0 $page =~ s/^(<\?xml.*?) encoding="utf-8"/$1 encoding="iso-8859-1"/si;
372  
373 0 $self->parse_response($page);
374 }
375  
376  
377 9 1;
378  
379 __END__
↑ Summary