blib/lib/Business/OnlinePayment/WesternACH.pm

filter: Statementstmt 22.2% 38 / 171 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 47.3% 9 / 19 Podpod 20.0% 1 / 5 totaltotal 15.6% 48 / 307 CC 51 SCAR 52.7
CC
51
Cov
14%
get_returns
462.0
submit
182.0
build
110.0
CRAP 193.8
↑ Summary
1 package Business::OnlinePayment::WesternACH;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Business::OnlinePayment 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Business::OnlinePayment::HTTPS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Date::Format 'time2str';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Date::Parse 'str2time';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw($VERSION @ISA $me $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 @ISA = qw(Business::OnlinePayment::HTTPS);
14 1 $VERSION = '0.08';
15 1 $me = 'Business::OnlinePayment::WesternACH';
16  
17 1 $DEBUG = 0;
18  
19 1 my $defaults = {
20 command => 'payment',
21 check_ver => 'yes',
22 sec_code => 'PPD',
23 tender_type => 'check',
24 check_number => 9999,
25 schedule => 'live',
26 };
27  
28 1 my $required = { map { $_ => 1 } ( qw(
29 login
30 password
31 command
32 amount
33 tender_type
34 _full_name
35 routing_code
36 check_number
37 _check_type
38 ))};
39  
40  
41  
42 # Structure of the XML request document
43 # Right sides of the hash entries are Business::OnlinePayment
44 # field names. Those that start with _ are local method names.
45  
46 1 my $auth = {
47 Authentication => {
48 username => 'login',
49 password => 'password',
50 }
51 };
52  
53 1 my $request = {
54 TransactionRequest => {
55 %$auth,
56 Request => {
57 command => 'command',
58 Payment => {
59 type => '_payment_type',
60 amount => 'amount',
61 # effective date: not supported
62 Tender => {
63 type => 'tender_type',
64 amount => 'amount',
65 InvoiceNumber => { value => 'invoice_number' },
66 AccountHolder => { value => '_full_name' },
67 Address => { value => 'address' },
68 ClientID => { value => 'customer_id' },
69 UserDefinedID => { value => 'email' },
70 CheckDetails => {
71 routing => 'routing_code',
72 account => 'account_number',
73 check => 'check_number',
74 type => '_check_type',
75 verification => 'check_ver',
76 },
77 Authorization => { schedule => 'schedule' },
78 SECCode => { value => 'sec_code' },
79 },
80 },
81 }
82 }
83 };
84  
85 1 my $returns_request = {
86 TransactionRequest => {
87 %$auth,
88 Request => {
89 command => 'command',
90 DateRange => {
91 start => '_start',
92 end => '_end',
93 },
94 },
95 }
96 };
97  
98 0 ▶sub set_defaults {
Subroutine set_defaults: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my $self = shift;
100 0 $self->server('www.webcheckexpress.com');
101 0 $self->port(443);
102 0 $self->path('/requester.php');
103 0 return;
104 }
105  
106 0 ▶sub submit {
Subroutine submit: not called CC=13 SCAR=52.0
Pod documented
107 0 my $self = shift;
108 0 $Business::OnlinePayment::HTTPS::DEBUG = $DEBUG;
109 0 $DB::single = $DEBUG; # If you're debugging this, you probably want to stop here.
110 0 my $xml_request;
111  
112 0 ▶ if ($self->{_content}->{command} eq 'get_returns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_content'}{'command'} eq 'get_returns') { } 0 0 0
113 # Setting get_returns overrides anything else.
114 0 $xml_request = XMLout($self->build($returns_request), KeepRoot => 1);
115 }
116 else {
117 # Error-check and prepare as a normal transaction.
118  
119 0 eval {
120 # Return-with-error situations
121 0 ▶ croak "Unsupported transaction type: '" . $self->transaction_type . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->transaction_type =~ /^e?check$/i 0 0 0
122 if(not $self->transaction_type =~ /^e?check$/i);
123  
124 0 ▶ croak "Unsupported action: '" . $self->{_content}->{action} . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_payment_type 0 0 0
125 if(!defined($self->_payment_type));
126  
127 0 ▶ croak 'Test transactions not supported'
Branch: 1 branch
BranchTrueFalseTotal
if $self->test_transaction 0 0 0
128 if($self->test_transaction());
129 };
130  
131 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
132 0 $self->is_success(0);
133 0 $self->error_message($@);
134 0 return;
135 }
136
137 0 $xml_request = XMLout($self->build($request), KeepRoot => 1);
138 }
139 0 my ($xml_reply, $response, %reply_headers) = $self->https_post({ 'Content-Type' => 'text/xml' }, $xml_request);
140
141 0 ▶ if(not $response =~ /^200/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response =~ /^200/) 0 0 0
142 0 croak "HTTPS error: '$response'";
143 }
144  
145 0 $self->server_response($xml_reply);
146 0 my $reply = XMLin($xml_reply, KeepRoot => 1)->{TransactionResponse};
147  
148 0 ▶ if(exists($reply->{Response})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $reply->{'Response'}) { } 0 0 0
elsif (exists $reply->{'FatalException'}) { } 0 0 0
149 0 ▶ $self->is_success( ( $reply->{Response}->{status} eq 'successful') ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$reply->{'Response'}{'status'} eq 'successful' ? : 0 0 0
150 0 $self->error_message($reply->{Response}->{ErrorMessage});
151 0 ▶ if(exists($reply->{Response}->{TransactionID})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $reply->{"Response"}{"TransactionID"}) 0 0 0
152 # get_returns puts its results here
153 0 my $tid = $reply->{Response}->{TransactionID};
154 0 ▶ if($self->{_content}->{command} eq 'get_returns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_content'}{'command'} eq 'get_returns') { } 0 0 0
155 0 ▶ if(ref($tid) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tid eq 'ARRAY') { } 0 0 0
156 0 $self->{_content}->{returns} = [ map { $_->{value} } @$tid ];
157 }
158 else {
159 0 $self->{_content}->{returns} = [ $tid->{value} ];
160 }
161 }
162 else { # It's not get_returns
163 0 $self->authorization($tid->{value});
164 }
165 }
166 }
167 elsif(exists($reply->{FatalException})) {
168 0 $self->is_success(0);
169 0 $self->error_message($reply->{FatalException});
170 }
171  
172  
173 0 return;
174 }
175  
176 0 ▶sub get_returns {
Subroutine get_returns: not called CC=21 SCAR=61.4
Pod undocumented
177 0 my $self = shift;
178 0 my $content = $self->{_content};
179 0 ▶ if(exists($content->{'command'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $content->{"command"}) 0 0 0
180 0 croak 'get_returns: command is already set on this transaction';
181 }
182 0 ▶ if ( exists($content->{'returns_method'}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $content->{'returns_method'} and $content->{'returns_method'} eq 'requester') { } 0 0 0
Condition0%0/3
exists $content->{"returns_method"} and $content->{"returns_method"} eq "requester"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $content->{"returns_method"} and $content->{"returns_method"} eq "requester"
exists $content->{"returns_method"} $content->{"returns_method"} eq "requester"
Truth table0%0/3
A and B
A= exists $content->{"returns_method"}B= $content->{"returns_method"} eq "requester"
ABresult
0X0
100
111
183 $content->{'returns_method'} eq 'requester') {
184 # Obsolete, deprecated method supported for now as a fallback option.
185 0 $content->{'command'} = 'get_returns';
186 0 $self->submit;
187 0 ▶ if($self->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_success) 0 0 0
188 0 ▶ if(exists($content->{'returns'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $content->{'returns'}) { } 0 0 0
189 0 return @{$content->{'returns'}};
190 }
191 else {
192 0 return ();
193 }
194 }
195 # you need to check error_message() for details.
196 0 return ();
197 }
198 else {
199 0 $Business::OnlinePayment::HTTPS::DEBUG = $DEBUG;
200 0 $DB::single = $DEBUG;
201 0 ▶ if (defined($content->{'login'}) and defined($content->{'password'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content->{'login'} and defined $content->{'password'}) { } 0 0 0
Condition0%0/3
defined $content->{"login"} and defined $content->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content->{"login"} and defined $content->{"password"}
defined $content->{"login"} defined $content->{"password"}
Truth table0%0/3
A and B
A= defined $content->{"login"}B= defined $content->{"password"}
ABresult
0X0
100
111
202 # transret.php doesn't respect date ranges. It returns anything from the
203 # same month as the date argument. Therefore we generate one request for
204 # each month in the date range, and then filter them by date later.
205 0 my $path = ('transret.php?style=csv&sort=id&date=');
206 0 my $starttime = str2time($self->_start);
207 0 my $endtime = str2time($self->_end) - 1;
208 0 my @months = map { s/^(....)(..)$/$1-$2-01/; $_ } (
209 time2str('%Y%m', $starttime)..time2str('%Y%m', $endtime)
210 );
211 my $headers = {
212 0 Authorization => 'Basic ' . MIME::Base64::encode($content->{'login'} . ':' . $content->{'password'})
213 };
214 0 my @tids;
215 0 foreach my $m (@months) {
216 0 $self->path($path . $m);
217 # B:OP:HTTPS::https_get doesn't use $DEBUG.
218 0 my ($page, $reply, %headers) =
219 $self->https_get(
220 { headers => $headers },
221 {},
222 );
223 0 ▶ if ($reply =~ /^200/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply =~ /^200/) { } 0 0 0
224 0 $self->is_success(1);
225 }
226 else {
227 0 $self->error_message($reply);
228 0 ▶ carp $reply if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Business::OnlinePayment::WesternACH::DEBUG 0 0 0
229 0 ▶ carp $page if $DEBUG >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if $Business::OnlinePayment::WesternACH::DEBUG >= 3 0 0 0
230 0 $self->is_success(0);
231 0 return;
232 }
233 0 my $index_date_ret = 2; # Usual position of 'Date Returned'
234 0 foreach my $trans (split("\cJ", $page)) {
235 0 my @fields = split(',', $trans);
236 # fields:
237 # id, Date Returned, Type, Amount, Name, Customer ID Number,
238 # Email Address, Invoice Number, Status Code, SEC
239  
240 # we only care about id and date.
241 0 ▶ next if scalar(@fields) < 10;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @fields < 10 0 0 0
242 0 ▶ if($fields[0] eq 'id') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[0] eq "id") 0 0 0
243 # Use this header row to find the 'Date Returned' field.
244 0 ($index_date_ret) = grep { lc($fields[$_]) eq 'date returned' } ( 0..scalar(@fields)-1 );
245 0 ▶ $index_date_ret ||= 2;
Condition0%0/2
$index_date_ret ||= 2
l!l
00
MC/DC0%0/1
$index_date_ret ||= 2
$index_date_ret
Truth table0%0/2
A
A= $index_date_ret
Aresult
11
00
246 }
247 0 ▶ next if not($fields[0] =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $fields[0] =~ /^\d+$/ 0 0 0
248 0 my $rettime = str2time($fields[$index_date_ret]);
249 0 ▶ next if (!$rettime or $rettime < $starttime or $rettime > $endtime);
Branch: 1 branch
BranchTrueFalseTotal
if not $rettime or $rettime < $starttime or $rettime > $endtime 0 0 0
Condition0%0/6
not $rettime or $rettime < $starttime or $rettime > $endtime
l!l&&r!l&&!r
000
not $rettime or $rettime < $starttime
l!l&&r!l&&!r
000
MC/DC0%0/3
not $rettime or $rettime < $starttime or $rettime > $endtime
not $rettime $rettime < $starttime $rettime > $endtime
Truth table0%0/4
not(A) or B or C
A= not $rettimeB= $rettime < $starttimeC= $rettime > $endtime
ABCresult
1XX1
01X1
0011
0000
250 0 ▶ carp $trans if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Business::OnlinePayment::WesternACH::DEBUG > 1 0 0 0
251 0 push @tids, $fields[0];
252 }
253 }
254 0 return @tids;
255 }
256 else {
257 0 croak 'login and password required';
258 }
259 }
260 }
261  
262 0 ▶sub build {
Subroutine build: not called CC=10 SCAR=47.0
Pod undocumented
263 0 my $self = shift;
264 0 my $content = { $self->content };
265 0 my $skel = shift;
266 0 my $data;
267 0 ▶ if (ref($skel) ne 'HASH') { croak 'Failed to build non-hash' };
Branch: 1 branch
BranchTrueFalseTotal
if (ref $skel ne "HASH") 0 0 0
268 0 foreach my $k (keys(%$skel)) {
269 0 my $val = $skel->{$k};
270 # Rules for building from the skeleton:
271 # 1. If the value is a hashref, build it recursively.
272 0 ▶ if(ref($val) eq 'HASH') {
Branch: 6 branches
BranchTrueFalseTotal
if (ref $val eq 'HASH') { } 0 0 0
elsif ($val =~ /^_/ and $self->can($val)) { } 0 0 0
elsif (not defined $val) { } 0 0 0
elsif (exists $content->{$val}) { } 0 0 0
elsif (exists $defaults->{$val}) { } 0 0 0
elsif (not $required->{$val}) { } 0 0 0
273 0 $data->{$k} = $self->build($val);
274 }
275 # 2. If the value starts with an underscore, it's treated as a method name.
276 ▶ elsif($val =~ /^_/ and $self->can($val)) {
Condition0%0/3
$val =~ /^_/ and $self->can($val)
!ll&&!rl&&r
000
MC/DC0%0/2
$val =~ /^_/ and $self->can($val)
$val =~ /^_/ $self->can($val)
Truth table0%0/3
A and B
A= $val =~ /^_/B= $self->can($val)
ABresult
0X0
100
111
277 0 $data->{$k} = $self->can($val)->($self);
278 }
279 # 3. If the value is undefined, keep it undefined.
280 elsif(!defined($val)) {
281 0 $data->{$k} = undef;
282 }
283 # 4. If the value is the name of a key in $self->content, look up that value.
284 elsif(exists($content->{$val})) {
285 0 $data->{$k} = $content->{$val};
286 }
287 # 5. If the value is a key in $defaults, use that value.
288 elsif(exists($defaults->{$val})) {
289 0 $data->{$k} = $defaults->{$val};
290 }
291 # 6. If the value is not required, use an empty string.
292 elsif(! $required->{$val}) {
293 0 $data->{$k} = '';
294 }
295 # 7. Fail.
296 else {
297 0 croak "Missing request field: '$val'";
298 }
299 }
300 0 return $data;
301 }
302  
303 0 ▶sub XML {
Subroutine XML: not called CC=1 SCAR=6.9
Pod undocumented
304 # For testing build().
305 0 my $self = shift;
306 0 return XMLout($self->build($request), KeepRoot => 1);
307 }
308  
309 0 ▶sub _payment_type {
Subroutine _payment_type: not called CC=4 SCAR=30.0
310 0 my $self = shift;
311 0 my $action = $self->{_content}->{action};
312 0 ▶ if(!defined($action) or $action =~ /^normal authorization$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $action or $action =~ /^normal authorization$/i) { } 0 0 0
elsif ($action =~ /^credit$/i) { } 0 0 0
Condition0%0/3
not defined $action or $action =~ /^normal authorization$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $action or $action =~ /^normal authorization$/i
not defined $action $action =~ /^normal authorization$/i
Truth table0%0/3
not(A) or B
A= not defined $actionB= $action =~ /^normal authorization$/i
ABresult
1X1
011
000
313 0 return 'debit';
314 }
315 elsif($action =~ /^credit$/i) {
316 0 return 'credit';
317 }
318 else {
319 0 return;
320 }
321 }
322  
323 0 ▶sub _check_type {
Subroutine _check_type: not called CC=3 SCAR=24.8
324 0 my $self = shift;
325 0 my $type = $self->{_content}->{account_type};
326 0 ▶ return 'checking' if($type =~ /checking/i);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /checking/i 0 0 0
327 0 ▶ return 'savings' if($type =~ /savings/i);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /savings/i 0 0 0
328 0 croak "Invalid account_type: '$type'";
329 }
330  
331 0 ▶sub _full_name {
Subroutine _full_name: not called CC=1 SCAR=6.9
332 0 my $self = shift;
333 0 return join(' ',$self->{_content}->{first_name},$self->{_content}->{last_name});
334 }
335  
336 0 ▶sub _start {
Subroutine _start: not called CC=3 SCAR=24.8
337 0 my $self = shift;
338 0 ▶ if($self->{_content}->{start}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_content'}{'start'}) { } 0 0 0
339 0 my $start = time2str('%Y-%m-%d', str2time($self->{_content}->{start}));
340 0 ▶ croak "Invalid start date: '".$self->{_content}->{start} if !$start;
Branch: 1 branch
BranchTrueFalseTotal
unless $start 0 0 0
341 0 return $start;
342 }
343 else {
344 0 return time2str('%Y-%m-%d', time - 86400);
345 }
346 }
347  
348 0 ▶sub _end {
Subroutine _end: not called CC=3 SCAR=24.8
349 0 my $self = shift;
350 0 my $end = $self->{_content}->{end};
351 0 ▶ if($end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end) { } 0 0 0
352 0 $end = time2str('%Y-%m-%d', str2time($end));
353 0 ▶ croak "Invalid end date: '".$self->{_content}->{end} if !$end;
Branch: 1 branch
BranchTrueFalseTotal
unless $end 0 0 0
354 0 return $end;
355 }
356 else {
357 0 return time2str('%Y-%m-%d', time);
358 }
359 }
360  
361 1 1;
362 __END__
↑ Summary