File Coverage

blib/lib/Finance/BankVal/UK.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             package Finance::BankVal::UK;
3            
4 1     1   714 use 5.008000;
  1         4  
  1         40  
5 1     1   5 use strict;
  1         2  
  1         30  
6 1     1   5 use warnings;
  1         5  
  1         44  
7 1     1   4 use vars qw(@params $size $format $account $error $sortcode $uid $pin &responseString $ua);
  1         2  
  1         132  
8 1     1   3176 use LWP::UserAgent;
  1         68619  
  1         34  
9 1     1   1725 use XML::Simple;
  0            
  0            
10             use JSON;
11             require Exporter;
12             our @ISA = qw(Exporter);
13             our @EXPORT_OK = qw(bankValUK new);
14             our $VERSION = '0.04';
15            
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 $format; #format the response should be in
21             my $sortcode; #sortcode to be validated
22             my $url; #the URL built for the REST call
23             my $responseString; #the return to the calling method
24             my $error; #holds any error messages etc from the module, erors from the web servoce will be returned in $response string
25            
26             #constructor
27             sub new {
28             my $proto = shift;
29             my $class = ref($proto) || $proto;
30             my $self = {};
31             bless ($self, $class);
32             return $self;
33             }
34            
35             #
36             # Exportable sub can take parameter array of 5,4,3,2 elements
37             # these must be in the order detailed in the perldoc for this module
38             #
39             sub bankValUK{
40             $error = "";
41             local @params = @_;
42             $size = @params;
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             }else{ #otherwise
50             unshift(@_, $refCheck); #put it back
51             }
52             $format = lc($_[0]); #ensure format element is in lower case
53             $sortcode = $_[1]; #set the sortcode
54            
55            
56             #strip sortcode of seperating - or spaces
57             $sortcode =~ s/-| //g;
58            
59             #Switch to handle different amount of parameters
60             SWITCH:{
61             $size == 5 && do {
62             $account = $_[2];
63             $uid = $_[3];
64             $pin = $_[4];
65             last SWITCH;
66             };
67             $size == 4 && do {
68             $uid = $_[2];
69             $pin = $_[3];
70             last SWITCH;
71             };
72             $size == 3 && do {
73             $account = $_[2];
74             &loadUidPin;
75             last SWITCH;
76             };
77             $size == 2 && do {
78             &loadUidPin;
79             last SWITCH;
80             };
81             }
82            
83             #call validation sub now all elements are loaded
84             &validateFormat;
85            
86             if ($error){
87             $responseString = "$error";
88             &formatErrorMsg;
89             return $responseString;
90             }
91            
92             #call validation sub
93             &goValidate;
94             return $responseString;
95             }
96             #call main servers REST services passing url with relevant parameters
97             sub goValidate{
98             #create user agent
99             local $ua = LWP::UserAgent->new();
100            
101             #build the URL (differs depending on parameters supplied - ie account val or branch val)
102             #params divisible by 2 have no account number
103             if (($size%2)==0){
104             my $baseUrl = 'https://www.unifiedsoftware.co.uk/services/bankvaluk/branchdets2';
105             $url = "$baseUrl/userid/$uid/pin/$pin/sortcode/$sortcode/$format/";
106             }else{
107             my $baseUrl = 'https://www.unifiedsoftware.co.uk/services/bankvaluk/bankvalplus2';
108             $url = "$baseUrl/userid/$uid/pin/$pin/sortcode/$sortcode/account/$account/$format/";
109             }
110             #call the service
111             my $response = $ua->get($url);
112            
113             #Check the response code anything under 200 or over 399 is an error with main server try backup
114             if($response->code<200||$response->code>399){
115             &goFallOver;
116             } else {
117             $responseString = $response->content(); #otherwise return the returned
118             }
119             }
120             #sub to call backup servers
121             sub goFallOver{
122             #build the URL
123             if (($size%2)==0){
124             my $baseUrl = 'https://www.unifiedservices.co.uk/services/bankvaluk/branchdets2';
125             $url = "$baseUrl/userid/$uid/pin/$pin/sortcode/$sortcode/$format/";
126             }else{
127             my $baseUrl = 'https://www.unifiedservices.co.uk/services/bankvaluk/bankvalplus2';
128             $url = "$baseUrl/userid/$uid/pin/$pin/sortcode/$sortcode/account/$account/$format/";
129             }
130            
131             #call the service
132             my $response = $ua->get($url);
133            
134             #Check the response code
135             if($response->code<200||$response->code>399){
136             $responseString .= $response->code; #still a problem so return error code
137             } else {
138             $responseString = $response->content(); #ok this time so return the returned
139             }
140             }
141            
142             #sub to validate the parameters input
143             sub validateFormat {
144             #Validate response format must match json, xml, or, csv
145             if ($format !~ /^json$|^xml$|^csv$/){
146             $error .= "INVALID - Result Format";
147             return;
148             }
149             #Validate sortcode all numeric 6 chars (-'s and ws stripped in calling routine)
150             if ($sortcode !~ /^\d\d\d\d\d\d$/){
151             $error .= "INVALID - Sortcode";
152             return;
153             }
154             #Validate account all numeric between 6 and 12 digits
155             if (($account)&&((length($account) < 6) || (length($account) > 12) || ($account !~ /^\d+\d$/))){
156             $error .= "INVALID - Account";
157             }
158             #Validate PIN all numeric 5 characters
159             if ($pin !~ /^\d\d\d\d\d$/){
160             $error .= "ERROR - Invalid User ID/PIN";
161             return;
162             }
163             #Validate UID must end with 3 numerics exactly and start with 3 Alpha variable length otherwise
164             if ($uid !~ /^[a-zA-Z\-_][a-zA-Z][a-zA-Z]*\D\d\d\d$/){
165             $error .= "ERROR - Invalid User ID/PIN";
166             return;
167             }
168             }
169            
170             #sub to load PIN and UserID from LoginConfig.txt file if they weren't passed in with the method call
171             #returns an error if unsuccessful
172             sub loadUidPin {
173             my $fileOpened = open UIDCONF, "LoginConfig.txt";
174             if ( ! $fileOpened){
175             $error .= "No UserID / PIN supplied, please visit http://www.unifiedsoftware.co.uk/freetrial/free-trial-home.html: ";
176             }else{
177             while (){
178             if ($_ =~ /^UserID/){
179             chomp(my @line = split (/ |\t/,$_));
180             $uid = $line[-1];
181             }elsif($_ =~ /^PIN/){
182             chomp(my @line = split (/ |\t/,$_));
183             $pin = $line[-1];
184             }
185            
186             }
187             #check to see if conf file has empty params - if so return error message directing to free trial page
188             if (($uid !~ /\w/) || ($pin !~ /\w/)){
189             $error .= "No UserID / PIN supplied, please visit http://www.unifiedsoftware.co.uk/freetrial/free-trial-home.html: ";
190             }
191             close UIDCONF;
192             }
193             }
194            
195             #sub to format the error message in the correct expected format with all nodes etc
196             sub formatErrorMsg{
197             if($format eq "xml" && ($size%2)==0){
198             $responseString = "" . $responseString . ""
199             . ""
200             . ""
201             . ""
202            
203             . ""
204             . ""
205             . ""
206             . ""
207             . ""
208             . ""
209             . ""
210             . ""
211             . ""
212             . ""
213             . ""
214             . ""
215             . ""
216             . ""
217             . ""
218             . ""
219             . ""
220             . ""
221             . ""
222             . ""
223             . "";
224             }elsif($format eq "xml" && ($size%2)!=0){
225             $responseString = "" . $responseString . "INVALID - Sortcode"
226             . ""
227             . ""
228             . ""
229             . ""
230             . ""
231             . ""
232             . ""
233             . ""
234             . ""
235             . ""
236             . ""
237             . ""
238             . ""
239             . ""
240             . ""
241             . ""
242             . ""
243             . ""
244             . ""
245             . ""
246             . ""
247             . ""
248             . ""
249             . ""
250             . ""
251             . "";
252             }elsif($format eq "json" && ($size%2)== 0 ){
253             $responseString = "{\"result\":\"" . $responseString . "\",\"sortcode\":\"\",\"bicbank\":\"\",\"bicbranch\":\"\","
254             . "\"subbranchsuffix\":\"\",\"bankname\":\"\",\"owningbank\":\"\",\"longbank1\":\"\",\"longbank2\":\"\",\"ownbc\":"
255             . "\"\",\"ccode\":\"\",\"supervisorybody\":\"\",\"deletedate\":\"\",\"changedate\":\"\",\"printindicator\":\"\",\"bacsstatus\":"
256             . "\"\",\"bacschangedate\":\"\",\"bacsclosedate\":\"\",\"bacsredirectfrom\":\"\",\"bacsredtoscode\":\"\",\"bacssettbank\":\"\",\""
257             . "bacssettsec\":\"\",\"bacssettsubsec\":\"\",\"bacshandbank\":\"\",\"bacshandst\":\"\",\"bacsaccnumflag\":\"\",\"bacsddiflg\":\"\""
258             . ",\"bacsdrdisallowed\":\"\",\"bacscrdisallowed\":\"\",\"bacscudisallowed\":\"\",\"bacsprdisallowed\""
259             . ":\"\",\"bacsbsdisallowed\":\"\",\"bacsdvdisallowed\":\"\",\"bacsaudisallowed\":\"\",\"spare1\":\"\",\"spare2\":\"\""
260             . ",\"spare3\":\"\",\"spare4\":\"\",\"chapsretind\":\"\",\"chapssstatus\":\"\",\"chapsschangedate\":\"\",\"chapssclosedate\""
261             . ":\"\",\"chapsssettmem\":\"\",\"chapssrbicbank\":\"\",\"chapssrbicbr\":\"\",\"chapsestatus\":\"\",\"chapsechangedate\":\"\""
262             . ",\"chapseclosedate\":\"\",\"chapserbicbank\":\"\",\"chapserbicbr\":\"\",\"chapsesettmem\":\"\",\"chapseretind\":\"\""
263             . ",\"chapseswift\":\"\",\"spare5\":\"\",\"ccccstatus\":\"\",\"ccccchangedate\":\"\",\"ccccclosedate\":\"\",\"ccccsettbank\""
264             . ":\"\",\"ccccdasc\":\"\",\"ccccretind\":\"\",\"ccccgbni\":\"\",\"fpsstatus\":\"\",\"fpschangedate\":\"\",\"fpsclosedate\""
265             . ":\"\",\"fpsredirectfrom\":\"\",\"fpsredirecttosc\":\"\",\"fpssettbankct\":\"\",\"fpsspare1\":\"\",\"fpssettbankbc\""
266             . ":\"\",\"fpshandbankct\":\"\",\"fpsspare2\":\"\",\"fpshandbankbc\":\"\",\"fpsaccnumflag\":\"\",\"fpsagencytype\":\"\",\""
267             . "fpsspare3\":\"\",\"printbti\":\"\",\"printmainsc\":\"\",\"printmajlocname\":\"\",\"printminlocname\":\"\",\"printbranchname\""
268             . ":\"\",\"printsecentryind\":\"\",\"printsecbrname\":\"\",\"printfbrtit1\":\"\",\"printfbrtit2\":\"\",\"printfbrtit3\":\"\",\""
269             . "printaddr1\":\"\",\"printaddr2\":\"\",\"printaddr3\":\"\",\"printaddr4\":\"\",\"printtown\":\"\",\"printcounty\""
270             . ":\"\",\"printpcode1\":\"\",\"printpcode2\":\"\",\"printtelarea\":\"\",\"printtelno\":\"\",\"printtelarea2\":\"\",\""
271             . "printtelno2\":\"\"}";
272             }elsif($format eq "json" && ($size%2) !=0 ){
273             $responseString = "{\"result\":\"" . $responseString . "\",\"transposedsortcode\":\"\",\"transposedaccount\":\"\",\"sortcode\":\"\",\"bicbank\":\"\",\"bicbranch\":\"\",\"subbranchsuffix\":\"\",\""
274             . "bankname\":\"\",\"owningbank\":\"\",\"longbank1\":\"\",\"longbank2\":\"\",\"ownbc\":\"\",\"ccode\":\"\",\"supervisorybody\":\"\",\"deletedate\":\"\",\""
275             . "changedate\":\"\",\"printindicator\":\"\",\"bacsstatus\":\"\",\"bacschangedate\":\"\",\"bacsclosedate\":\"\",\"bacsredirectfrom\":\"\",\""
276             . "bacsredtoscode\":\"\",\"bacssettbank\":\"\",\"bacssettsec\":\"\",\"bacssettsubsec\":\"\",\"bacshandbank\":\"\",\"bacshandst\":\"\",\""
277             . "bacsaccnumflag\":\"\",\"bacsddiflg\":\"\",\"bacsdrdisallowed\":\"\",\"bacscrdisallowed\":\"\",\"bacscudisallowed\":\"\",\""
278             . "bacsprdisallowed\":\"\",\"bacsbsdisallowed\":\"\",\"bacsdvdisallowed\":\"\",\"bacsaudisallowed\":\"\",\"spare1\":\"\",\"spare2\":\"\",\""
279             . "spare3\":\"\",\"spare4\":\"\",\"chapsretind\":\"\",\"chapssstatus\":\"\",\"chapsschangedate\":\"\",\"chapssclosedate\":\"\",\""
280             . "chapsssettmem\":\"\",\"chapssrbicbank\":\"\",\"chapssrbicbr\":\"\",\"chapsestatus\":\"\",\"chapsechangedate\":\"\",\""
281             . "chapseclosedate\":\"\",\"chapserbicbank\":\"\",\"chapserbicbr\":\"\",\"chapsesettmem\":\"\",\"chapseretind\":\"\",\""
282             . "chapseswift\":\"\",\"spare5\":\"\",\"ccccstatus\":\"\",\"ccccchangedate\":\"\",\"ccccclosedate\":\"\",\"ccccsettbank\":\"\",\""
283             . "ccccdasc\":\"\",\"ccccretind\":\"\",\"ccccgbni\":\"\",\"fpsstatus\":\"\",\"fpschangedate\":\"\",\"fpsclosedate\":\"\",\""
284             . "fpsredirectfrom\":\"\",\"fpsredirecttosc\":\"\",\"fpssettbankct\":\"\",\"fpsspare1\":\"\",\"fpssettbankbc\":\"\",\""
285             . "fpshandbankct\":\"\",\"fpsspare2\":\"\",\"fpshandbankbc\":\"\",\"fpsaccnumflag\":\"\",\"fpsagencytype\":\"\",\"fpsspare3\":\"\",\""
286             . "printbti\":\"\",\"printmainsc\":\"\",\"printmajlocname\":\"\",\"printminlocname\":\"\",\"printbranchname\":\"\",\""
287             . "printsecentryind\":\"\",\"printsecbrname\":\"\",\"printfbrtit1\":\"\",\"printfbrtit2\":\"\",\"printfbrtit3\":\"\",\""
288             . "printaddr1\":\"\",\"printaddr2\":\"\",\"printaddr3\":\"\",\"printaddr4\":\"\",\"printtown\":\"\",\"printcounty\":\"\",\""
289             . "printpcode1\":\"\",\"printpcode2\":\"\",\"printtelarea\":\"\",\"printtelno\":\"\",\"printtelarea2\":\"\",\"printtelno2\":\"\"}";
290             }
291             }
292            
293             1;
294             __END__