NAB/Acknowledgement.pm

filter: Statementstmt 100% 87 / 87 Branchbran 87.5% 14 / 16 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 23 / 23 Podpod 100% 8 / 8 totaltotal 95.7% 136 / 142 CC 12 SCAR 7.4
CC
12
Cov
96%
new_from_xml
4.0
_user_message_to_status
4.0
_is_status
3.1
CRAP 2.1
« NAB/Types.pm ↑ Summary NAB/BPAY/Payments.pm »
1 package Business::NAB::Acknowledgement;
2 1 $Business::NAB::Acknowledgement::VERSION = '0.11';
3 =head1 NAME
4  
5 Business::NAB::Acknowledgement
6  
7 =head1 SYNOPSIS
8  
9 my $Ack = $class->new_from_xml(
10 $path_to_xml_file, # or an XML string
11 );
12  
13 my $dom = $Ack->dom; # access to XML::LibXML::Document
14 my $DateTime = $Ack->date;
15  
16 if ( $Ack->is_accepted ) {
17 ...
18 }
19  
20 =head1 DESCRIPTION
21  
22 Class for parsing NAB file acknowledgements, which are XML files using
23 some long-forgotten schema from Oracle from the late 1990s. There is no XSD
24 or DTD...
25  
26 The various elements are described in section 4 of the NAB "Australian Direct
27 Entry Payments and Dishonour Report".
28  
29 =cut
30  
31 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use feature qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use autodie qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 with 'Business::NAB::Role::AttributeContainer';
40 1 extends 'Business::NAB::FileContainer';
41  
42 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶no warnings qw/ experimental::signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 1 ▶use Business::NAB::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 =head1 ATTRIBUTES
48  
49 =over
50  
51 =item dom (XML::LibXML::Document)
52  
53 The resulting object from parsing the XML, should you want to do
54 anything more bespoke with it
55  
56 =item date (NAB::Type::Date, required)
57  
58 DateTime the acknowledgement was generated
59  
60 =item result (Str, required)
61  
62 Inferred from the document's root element, usually the C<type>
63 attribute, which the documentation lists as:
64  
65 * info - "Standard Acknowledgement"
66 * warn - File not processed, requires intervention (approval)
67 * error - File not processed, requires review and resubmission
68  
69 =item status (Str, required)
70  
71 The acknowledgement status, inferred from the user_message element
72 or the root element:
73  
74 * accepted
75 * processed
76 * rejected
77 * pending
78 * declined
79  
80 =item customer_id (Str, required)
81  
82 NAB Direct Link Mailbox ID
83  
84 =item company_name (Str, required)
85  
86 Registered NAB Direct Link customer name
87  
88 =item original_message_id (Str, required)
89  
90 =item original_filename (Str, required)
91  
92 Original file name
93  
94 =item data_type (Str, optional)
95  
96 =item data_type_description (Str, optional)
97  
98 =item user_message (Str, optional)
99  
100 Short description of the current status of the file
101  
102 =item detailed_message (Str, optional)
103  
104 Long description of the current status of the file
105  
106 =item issue (ArrayRef[Business::NAB::Acknowledgement::Issue], optional)
107  
108 An arrayref of objects that describe the payment processing
109  
110 =back
111  
112 =cut
113  
114 1 has [ qw/ dom / ] => (
115 is => 'ro',
116 isa => 'XML::LibXML::Document',
117 required => 1,
118 );
119  
120 1 has 'date' => (
121 is => 'ro',
122 isa => 'NAB::Type::Date',
123 coerce => 1,
124 required => 1,
125 );
126  
127 1 has [
128 qw/
129 result
130 customer_id
131 company_name
132 original_message_id
133 original_filename
134 status
135 /
136 ] => (
137 is => 'ro',
138 isa => 'Str',
139 required => 1,
140 );
141  
142 1 has [
143 qw/
144 data_type
145 data_type_description
146 user_message
147 detailed_message
148 /
149 ] => (
150 is => 'ro',
151 isa => 'Str',
152 required => 0,
153 );
154  
155 1 __PACKAGE__->load_attributes(
156 'Business::NAB::Acknowledgement',
157 'Issue',
158 );
159  
160 =head1 METHODS
161  
162 =head2 new_from_xml
163  
164 Parses the given XML file (or string) and returns a new instance of the
165 class with the necessary attributes populates:
166  
167 my $Ack = Business::NAB::Acknowledgement->new_from_xml( $xml );
168  
169 =cut
170  
171 4 ▶sub new_from_xml ( $class, $file_or_string ) {
Subroutine new_from_xml: called CC=4 SCAR=13.9
Pod documented
172  
173 4 ▶ my $source = -f $file_or_string ? 'location' : 'string';
Branch: 1 branch
BranchTrueFalseTotal
-f $file_or_string ? : 3 1 4
174 4 my $dom = XML::LibXML->load_xml(
175 $source => $file_or_string,
176 );
177  
178 4 my %attributes;
179  
180 4 ▶ if ( my ( $Node ) = $dom->findnodes( '//MessageAcknowledgement' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my($Node) = $dom->findnodes('//MessageAcknowledgement')) { } 1 3 4
elsif (($Node) = $dom->findnodes('//PaymentsAcknowledgement')) { } 2 1 3
181  
182 # message ack type
183 1 %attributes = _parse_message_ack( $Node );
184  
185 } elsif ( ( $Node ) = $dom->findnodes( '//PaymentsAcknowledgement' ) ) {
186  
187 # payments ack type
188 2 %attributes = _parse_payments_ack( $Node );
189  
190 } else {
191 1 croak(
192 "Unknown acknowledgement type: "
193 . $dom->documentElement->nodeName
194 );
195 }
196  
197 3 return $class->new(
198 dom => $dom,
199 %attributes,
200 );
201 }
202  
203 =head2 is_accepted
204  
205 =head2 is_processed
206  
207 =head2 is_pending
208  
209 =head2 is_rejected
210  
211 =head2 is_declined
212  
213 =head2 is_received
214  
215 =head2 is_held
216  
217 Boolean checks on the acknowledgement:
218  
219 if ( $Ack->is_accepted ) {
220 ...
221 }
222  
223 =cut
224  
225 3 ▶sub is_accepted ( $self ) {
Subroutine is_accepted: called CC=2 SCAR=9.2
Pod documented
226 3 ▶ return $self->_is_status( 'accepted' )
Condition33%1/3
$self->_is_status('accepted') || $self->_is_status('success')
l!l&&r!l&&!r
003
MC/DC0%0/2
$self->_is_status('accepted') || $self->_is_status('success')
$self->_is_status('accepted') $self->_is_status('success')
Truth table33%1/3
A || B
A= $self->_is_status('accepted')B= $self->_is_status('success')
ABresult
1X1
011
000
227 || $self->_is_status( 'success' );
228 }
229  
230 3 ▶sub is_processed ( $self ) { return $self->_is_status( 'processed' ) }
Subroutine is_processed: called CC=1 SCAR=0.0
Pod documented
231 3 ▶sub is_pending ( $self ) { return $self->_is_status( 'pending' ) }
Subroutine is_pending: called CC=1 SCAR=0.0
Pod documented
232 3 ▶sub is_rejected ( $self ) { return $self->_is_status( 'rejected' ) }
Subroutine is_rejected: called CC=1 SCAR=0.0
Pod documented
233 3 ▶sub is_held ( $self ) { return $self->_is_status( 'held' ) }
Subroutine is_held: called CC=1 SCAR=0.0
Pod documented
234 3 ▶sub is_declined ( $self ) { return $self->_is_status( 'declined' ) }
Subroutine is_declined: called CC=1 SCAR=0.0
Pod documented
235 3 ▶sub is_received ( $self ) { return $self->_is_status( 'received' ) }
Subroutine is_received: called CC=1 SCAR=0.0
Pod documented
236  
237 24 ▶sub _is_status ( $self, $status ) {
Subroutine _is_status: called CC=3 SCAR=11.2
238 24 ▶ return $self->status eq $status
Branch: 2 branches
BranchTrueFalseTotal
$self->status eq $status ? : 3 21 24
$self->result =~ /$status/i ? : 0 21 21
239 ? 1
240 : $self->result =~ /$status/i ? 1 : 0;
241 }
242  
243 1 ▶sub _parse_message_ack ( $Node ) {
Subroutine _parse_message_ack: called CC=2 SCAR=6.9
244  
245 1 my %attributes = _parse_common_ack( $Node );
246  
247 1 $attributes{ status } = lc( $Node->getAttribute( 'type' ) );
248  
249 1 ▶ if ( my ( $Message ) = $Node->findnodes( './MessageDetails' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($Message) = $Node->findnodes("./MessageDetails")) 1 0 1
250  
251 $attributes{ original_message_id }
252 1 = $Message->findvalue( './OriginalMessageId' );
253 $attributes{ original_filename }
254 1 = $Message->findvalue( './OriginalFilename' );
255  
256 1 $attributes{ data_type } = $Message->findvalue( './Datatype' );
257 $attributes{ data_type_description }
258 1 = $Message->findvalue( './DatatypeDescription' );
259  
260 1 $attributes{ data_type_description } =~ s/\s+$//;
261 }
262  
263 1 return %attributes;
264 }
265  
266 2 ▶sub _parse_payments_ack ( $Node ) {
Subroutine _parse_payments_ack: called CC=1 SCAR=0.0
267  
268 2 my %attributes = _parse_common_ack( $Node );
269  
270 $attributes{ original_message_id }
271 2 = $Node->findvalue( './OriginalMessageId' );
272 $attributes{ original_filename }
273 2 = $Node->findvalue( './OriginalFilename' );
274  
275 2 $attributes{ user_message } = $Node->findvalue( './UserMessage' );
276  
277 $attributes{ status } = _user_message_to_status(
278 $attributes{ user_message }
279 2 );
280  
281 $attributes{ detailed_message }
282 2 = $Node->findvalue( './DetailedMessage' );
283  
284 2 return %attributes;
285 }
286  
287 37 ▶sub _user_message_to_status ( $message ) {
Subroutine _user_message_to_status: called CC=4 SCAR=13.9
288  
289 # XML? Free form text? WHY NOT BOTH!
290 37 my ( $status ) = $message
291 =~ /(ACCEPTED|PROCESSED|REJECTED|PENDING|DECLINED|SUCCESS|HELD)/i
292 ;
293  
294 37 ▶ if ( ! $status ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 19 18 37
295  
296 # try harder to get the status value
297 19 chomp( $message );
298 19 $message =~ s/Payment status is\s*//i;
299  
300 # normalise myriad status strings
301 $status = {
302 'DELETED' => 'REJECTED',
303 'CANCELLED' => 'REJECTED',
304 'FUTURE DATED' => 'ACCEPTED',
305 'REQUIRES APPROVAL' => 'HELD',
306 'REQUIRES AUTHORISATION' => 'HELD',
307 'REQUIRES BANK APPROVAL' => 'HELD',
308  
309 # the status of last resort
310 '' => undef,
311  
312 19 ▶ }->{ uc( $message ) } // undef;
Condition100%2/2
{"DELETED", "REJECTED", "CANCELLED", "REJECTED", "FUTURE DATED", "ACCEPTED", "REQUIRES APPROVAL", "HELD", "REQUIRES AUTHORISATION", "HELD", "REQUIRES BANK APPROVAL", "HELD", "", undef}->{uc $message} // undef
l!l
145
MC/DC100%1/1
{"DELETED", "REJECTED", "CANCELLED", "REJECTED", "FUTURE DATED", "ACCEPTED", "REQUIRES APPROVAL", "HELD", "REQUIRES AUTHORISATION", "HELD", "REQUIRES BANK APPROVAL", "HELD", "", undef}->{uc $message} // undef
{"DELETED", "REJECTED", "CANCELLED", "REJECTED", "FUTURE DATED", "ACCEPTED", "REQUIRES APPROVAL", "HELD", "REQUIRES AUTHORISATION", "HELD", "REQUIRES BANK APPROVAL", "HELD", "", undef}->{uc $message}
Truth table100%2/2
A
A= {"DELETED", "REJECTED", "CANCELLED", "REJECTED", "FUTURE DATED", "ACCEPTED", "REQUIRES APPROVAL", "HELD", "REQUIRES AUTHORISATION", "HELD", "REQUIRES BANK APPROVAL", "HELD", "", undef}->{uc $message}
Aresult
11
00
313 }
314  
315 37 ▶ return $status ? lc( $status ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 32 5 37
316 }
317  
318 3 ▶sub _parse_common_ack ( $Node ) {
Subroutine _parse_common_ack: called CC=2 SCAR=6.9
319  
320 3 my %attributes;
321  
322 3 $attributes{ result } = $Node->getAttribute( 'type' );
323 3 $attributes{ issue } = [];
324  
325 3 $attributes{ date } = $Node->findvalue( './DateTime' );
326 3 $attributes{ customer_id } = $Node->findvalue( './CustomerId' );
327 3 $attributes{ company_name } = $Node->findvalue( './CompanyName' );
328  
329 3 foreach my $Issue ( $Node->findnodes( './Issues/Issue' ) ) {
330  
331 push(
332 15 @{ $attributes{ issue } },
333 {
334 code => $Issue->getAttribute( 'type' ),
335 itemId => $Issue->getAttribute( 'itemId' ),
336 detail => $Issue->to_literal,
337 },
338 );
339 }
340  
341 3 return %attributes;
342 }
343  
344 =head1 SEE ALSO
345  
346 L<Business::NAB::Types>
347  
348 L<Business::NAB::Acknowledgement::Issue>
349  
350 =cut
351  
352 1 __PACKAGE__->meta->make_immutable;
« NAB/Types.pm ↑ Summary NAB/BPAY/Payments.pm »