File Coverage

blib/lib/Finance/BankVal/UK.pm
Criterion Covered Total %
statement 39 100 39.0
branch 4 32 12.5
condition 0 18 0.0
subroutine 9 14 64.2
pod 0 8 0.0
total 52 172 30.2


line stmt bran cond sub pod time code
1             package Finance::BankVal::UK;
2              
3 1     1   473 use 5.008000;
  1         3  
4 1     1   4 use strict;
  1         2  
  1         20  
5 1     1   4 use warnings;
  1         2  
  1         40  
6 1     1   6 use vars qw(@params $size $account $error $sortcode $uid $pin &responseString $ua);
  1         2  
  1         152  
7 1     1   752 use LWP::UserAgent;
  1         50501  
  1         42  
8 1     1   809 use JSON;
  1         10815  
  1         7  
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(bankValUK new);
13             our $VERSION = '0.9';
14              
15             my $sortcode; #sortcode to be validated
16             my $account; #account number to be validated
17             my $uid; #userID
18             my $pin; #PIN
19             my $size; #holds length of param array
20             my $url; #the URL built for the REST call
21             my $responseString; #the return to the calling method
22             my $error; #holds any error messages etc from the module, erors from the web servoce will be returned in $response string
23             my $json; #holds the data to call the service with
24              
25             #constructor
26             sub new {
27 0     0 0 0 my $proto = shift;
28 0   0     0 my $class = ref($proto) || $proto;
29 0         0 my $self = {};
30 0         0 bless( $self, $class );
31 0         0 return $self;
32             }
33              
34             #
35             # Exportable sub can take parameter array of 4 or 2 elements
36             # these must be in the order detailed in the perldoc for this module
37             #
38             sub bankValUK {
39 1     1 0 86 $error = "";
40 1         6 my @params = @_;
41 1         3 $size = @params;
42              
43             #the following block checks to see if the first param is a reference
44             #if it is then the sub was called as an object ref so size is reduced
45             #accordingly
46             #my $refCheck = shift @_; #remove the leftmost array element
47             #if ( ref($refCheck) ) { #check if its a reference
48             # $size--; #if it is reduce the size value to account for it
49             # }
50             # else { #otherwise
51             # unshift( @_, $refCheck ); #put it back
52             # }
53 1         2 $sortcode = $_[0]; #set the sortcode
54              
55             #strip sortcode of seperating - or spaces
56 1         2 $sortcode =~ s/-| //g;
57 1         5 print STDOUT $sortcode;
58             #Switch to handle different amount of parameters
59             SWITCH: {
60 1 50       2 $size == 4 && do {
  1         5  
61 1         3 $account = $_[1];
62 1         2 $uid = $_[2];
63 1         2 $pin = $_[3];
64 1         3 last SWITCH;
65             };
66 0 0       0 $size == 3 && do { #todo this is now an error with only sortcode
67 0         0 $uid = $_[1];
68 0         0 $pin = $_[2];
69 0         0 last SWITCH;
70             };
71 0 0       0 $size == 2 && do {
72 0         0 $account = $_[1];
73 0         0 &loadUidPin;
74 0         0 last SWITCH;
75             };
76 0 0       0 $size == 1 && do { #todo this is now an error with only sortcode
77 0         0 &loadUidPin;
78 0         0 last SWITCH;
79             };
80             }
81              
82             #call validation sub now all elements are loaded
83 1         4 &validateFormat;
84              
85 1 50       3 if ($error) {
86 1         2 $responseString = "$error";
87 1         3 &formatErrorMsg;
88 1         6 return $responseString;
89             }
90              
91             #call validation sub
92 0         0 &goValidate;
93 0         0 print STDOUT $responseString;
94 0         0 return $responseString;
95             }
96              
97             #call main servers REST services passing url with relevant parameters
98             sub goValidate {
99 0     0 0 0 $responseString = "";
100             #create user agent
101 0         0 local $ua = LWP::UserAgent->new();
102              
103             #set the URL
104 0         0 $url = 'https://www.unifiedservices.co.uk/services/enhanced/bankval';
105              
106             #call the service
107 0         0 &loadContent;
108 0         0 my $req = HTTP::Request->new( 'POST', $url );
109 0         0 $req->header( 'Content-Type' => 'application/json' );
110 0         0 $req->content($json);
111 0         0 my $response = $ua->request($req);
112              
113             #Check the response code anything under 200 or over 399 is an error with main server try backup
114 0 0 0     0 if ( $response->code < 200 || $response->code > 399 ) {
115 0         0 &goFallOver;
116             }
117             else {
118 0         0 $responseString = $response->content(); #otherwise return the returned
119             }
120             }
121              
122             #sub to call backup servers
123             sub goFallOver {
124              
125             #set the URL
126 0     0 0 0 $url = 'https://www.unifiedsoftware.co.uk/services/enhanced/bankval';
127              
128             #call the service
129 0         0 my $req = HTTP::Request->new( 'POST', $url );
130 0         0 $req->header( 'Content-Type' => 'application/json' );
131 0         0 $req->content($json);
132 0         0 my $response = $ua->request($req);
133              
134             #Check the response code
135 0 0 0     0 if ( $response->code < 200 || $response->code > 399 ) {
136 0         0 $responseString .=
137             $response->code; #still a problem so return error code
138             }
139             else {
140 0         0 $responseString =
141             $response->content(); #ok this time so return the returned
142             }
143             }
144              
145             #sub to validate the parameters input
146             sub validateFormat {
147              
148             #Validate sortcode all numeric 6 chars (-'s and ws stripped in calling routine)
149 1 50   1 0 4 if ( $sortcode !~ /^\d\d\d\d\d\d$/ ) {
150 1         10 $error .= "INVALID - Sortcode";
151 1         2 return;
152             }
153              
154             #Validate account all numeric between 6 and 12 digits
155 0 0 0     0 if ( ( length($account) < 6 )
      0        
156             || ( length($account) > 12 )
157             || ( $account !~ /^\d+\d$/ ) )
158             {
159 0         0 $error .= "INVALID - Account";
160 0         0 return;
161             }
162              
163             #Validate PIN all numeric 5 characters
164 0 0       0 if ( $pin !~ /^\d\d\d\d\d$/ ) {
165 0         0 $error .= "ERROR - Invalid User ID/PIN";
166 0         0 return;
167             }
168              
169             #Validate UID must end with 3 numerics exactly and start with 3 Alpha variable length otherwise
170 0 0       0 if ( $uid !~ /^[a-zA-Z\-_][a-zA-Z][a-zA-Z]*\D\d\d\d$/ ) {
171 0         0 $error .= "ERROR - Invalid User ID/PIN";
172 0         0 return;
173             }
174             }
175              
176             #sub to load PIN and UserID from LoginConfig.txt file if they weren't passed in with the method call
177             #returns an error if unsuccessful
178             sub loadUidPin {
179 0     0 0 0 my $fileOpened = open UIDCONF, "LoginConfig.txt";
180 0 0       0 if ( !$fileOpened ) {
181 0         0 $error .=
182             "No UserID / PIN supplied, please visit https://www.unifiedsoftware.co.uk/request-a-free-trial/: ";
183             }
184             else {
185 0         0 while () {
186 0 0       0 if ( $_ =~ /^UserID/ ) {
    0          
187 0         0 chomp( my @line = split( / |\t/, $_ ) );
188 0         0 $uid = $line[-1];
189             }
190             elsif ( $_ =~ /^PIN/ ) {
191 0         0 chomp( my @line = split( / |\t/, $_ ) );
192 0         0 $pin = $line[-1];
193             }
194              
195             }
196              
197             #check to see if conf file has empty params - if so return error message directing to free trial page
198 0 0 0     0 if ( ( $uid !~ /\w/ ) || ( $pin !~ /\w/ ) ) {
199 0         0 $error .=
200             "No UserID / PIN supplied, please visit https://www.unifiedsoftware.co.uk/request-a-free-trial/: ";
201             }
202 0         0 close UIDCONF;
203             }
204             }
205              
206             sub loadContent {
207 0     0 0 0 $json =
208             "{'credentials':{'uname':'$uid','pin':'$pin'},'account':{'account':'$account','sortcode':'$sortcode'}}";
209             }
210              
211             #sub to format the error message in the correct expected format with all nodes etc
212             sub formatErrorMsg {
213 1 50   1 0 6 if ( substr( $responseString, 0, 7 ) eq 'INVALID' ) {
214 1         3 $responseString =
215             "{\"validationID\": \"\",\"BankValUK\": {\"result\":\""
216             . $responseString . "\"}}";
217             }
218             else {
219 0           $responseString = "{\"Error\": \"Invalid Credentials\"}";
220             }
221              
222             }
223              
224             1;
225             __END__