ClearingHouse.pm

filter: Statementstmt 91.0% 61 / 67 Branchbran 66.6% 8 / 12 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 86.6% 13 / 15 Podpod 100% 7 / 7 totaltotal 85.5% 95 / 111 CC 10 SCAR 11.1
CC
10
Cov
86%
settle
6.0
postauth
4.8
void
2.0
CRAP 3.0
↑ Summary ClearingHouse/Charge.pm »
1 package Business::Payment::ClearingHouse;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.01';
7  
8 has 'charges' => (
9 traits => [ 'Hash' ],
10 is => 'ro',
11 isa => 'HashRef[Business::Payment::ClearingHouse::Charge]',
12 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 2 handles => {
14 'delete_charge' => 'delete',
15 'get_charge' => 'get',
16 'set_charge' => 'set'
17 }
18 );
19  
20 has 'number_verifier' => (
21 traits => [ 'Code' ],
22 is => 'ro',
23 isa => 'CodeRef',
24 0 ▶ default => sub { sub { return 1 } },
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 2 handles => {
26 verify_number => 'execute'
27 }
28 );
29  
30 has 'states' => (
31 traits => [ 'Hash' ],
32 is => 'ro',
33 isa => 'HashRef[Str]',
34 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 2 handles => {
36 'delete_state' => 'delete',
37 'get_state' => 'get',
38 'set_state' => 'set'
39 }
40 );
41  
42 2 has 'uuid' => (
43 is => 'ro',
44 lazy_build => 1
45 );
46  
47 2 ▶sub _build_uuid {
Subroutine _build_uuid: called CC=1 SCAR=0.0
48 2 my ($self) = @_;
49  
50 2 return Data::UUID->new;
51 }
52  
53 2 ▶sub auth {
Subroutine auth: called CC=1 SCAR=0.0
Pod documented
54 2 my ($self, $charge) = @_;
55  
56 # unless($self->verify_number($charge->number)) {
57 # die "Invalid number\n";
58 # }
59  
60 2 my $uuid = $self->uuid->create_str;
61  
62 2 $self->set_charge($uuid, $charge);
63 2 $self->set_state($uuid, 'auth');
64  
65 2 return $uuid;
66 }
67  
68 1 ▶sub credit {
Subroutine credit: called CC=1 SCAR=0.0
Pod documented
69 1 my ($self, $charge) = @_;
70  
71 1 my $uuid = $self->uuid->create_str;
72  
73 1 $self->set_state($uuid, 'credit');
74 1 $self->set_charge($uuid, $charge);
75 }
76  
77 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
78 0 my ($self, $uuid) = @_;
79  
80 0 return $self->get_charge($uuid);
81 }
82  
83 3 ▶sub postauth {
Subroutine postauth: called CC=4 SCAR=15.8
Pod documented
84 3 my ($self, $uuid) = @_;
85  
86 3 my $charge = $self->get_charge($uuid);
87 3 ▶ unless(defined($charge)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $charge) 0 3 3
88 0 die "Unknown Charge\n";
89 }
90  
91 3 my $status = $self->get_state($uuid);
92 3 ▶ unless(defined($status) && ($status eq 'preauth')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $status and $status eq "preauth") 0 3 3
Condition33%1/3
defined $status and $status eq "preauth"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $status and $status eq "preauth"
defined $status $status eq "preauth"
Truth table33%1/3
A and B
A= defined $statusB= $status eq "preauth"
ABresult
0X0
100
111
93 0 die "Must preauth before postauthing\n";
94 }
95  
96 3 $self->set_state($uuid, 'postauth');
97  
98 3 return 1;
99 }
100  
101 3 ▶sub preauth {
Subroutine preauth: called CC=1 SCAR=0.0
Pod documented
102 3 my ($self, $charge) = @_;
103  
104 # unless($self->verify_number($charge->number)) {
105 # die "Invalid number\n";
106 # }
107  
108 3 my $uuid = $self->uuid->create_str;
109  
110 3 $self->set_charge($uuid, $charge);
111 3 $self->set_state($uuid, 'preauth');
112  
113 3 return $uuid;
114 }
115  
116 3 ▶sub settle {
Subroutine settle: called CC=6 SCAR=17.9
Pod documented
117 3 my ($self) = @_;
118  
119 3 my $total = 0;
120  
121 3 foreach my $uuid (keys %{ $self->states }) {
122 6 my $state = $self->get_state($uuid);
123 6 my $charge = $self->get_charge($uuid);
124  
125 6 ▶ if($state eq 'preauth') {
Branch: 3 branches
BranchTrueFalseTotal
if ($state eq 'preauth') { } 1 5 6
elsif ($state eq 'auth' or $state eq 'postauth') { } 4 1 5
elsif ($state eq 'credit') { } 1 0 1
126 # Skip these, as they are in flight
127 1 next;
128 ▶ } elsif($state eq 'auth' || $state eq 'postauth') {
Condition100%3/3
$state eq "auth" or $state eq "postauth"
l!l&&r!l&&!r
131
MC/DC100%2/2
$state eq "auth" or $state eq "postauth"
$state eq "auth" $state eq "postauth"
Truth table100%3/3
A or B
A= $state eq "auth"B= $state eq "postauth"
ABresult
1X1
011
000
129 4 $total += $charge->total;
130 } elsif($state eq 'credit') {
131 1 $total -= $charge->total;
132 }
133  
134 # Since we got here, we can just delete it now.
135 5 $self->delete_state($uuid);
136 5 $self->delete_charge($uuid);
137 }
138  
139 3 return $total;
140 }
141  
142 1 ▶sub void {
Subroutine void: called CC=2 SCAR=7.1
Pod documented
143 1 my ($self, $uuid) = @_;
144  
145 1 my $charge = $self->get_charge($uuid);
146  
147 1 ▶ if(defined($charge)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $charge) 1 0 1
148 1 $self->delete_charge($uuid);
149 1 $self->delete_state($uuid);
150 1 return 1;
151 }
152  
153 0 return 0;
154 }
155  
156 2 __PACKAGE__->meta->make_immutable;
157  
158 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 2 1;
161  
162 =head1 NAME
163  
164 Business::Payment::ClearingHouse - ClearingHouse for Business::Payment
165  
166 =head1 SYNOPSIS
167  
168 use Business::Payment::ClearingHouse;
169  
170 my $house = Business::Payment::ClearingHouse->new();
171  
172 # Create a charge
173 my $charge = Business::Payment::ClearingHouse::Charge->new(
174 subtotal => 100,
175 tax => 10
176 );
177 # Authorizate it and get the uuid
178 my $uuid = $house->auth($charge);
179  
180 # Settle!
181 my $total = $house->settle;
182  
183 =head1 DESCRIPTION
184  
185 Business::Payment::ClearingHouse provides an API that emulates a payment
186 processor like the ones used with credit cards. The operations it provides
187 are inspired by those present with a credit card processor: preauth (reserve),
188 postauth (consume the reserved funds), auth (immediately charge), credit
189 (refund) and void (remove a charge).
190  
191 This module is intended to provide a testbed for features of
192 L<Business::Payment> and to provide a testing processor for use in development
193 environments. The C<settle> method allows a developer to examine the net
194 results of a series of transactions.
195  
196 B<Notice>: This module is in development. The API will likely change.
197  
198 =head1 ATTRIBUTES
199  
200 =head2 charges
201  
202 Hashref of charges. Keys are the UUIDs and values are the charge objects.
203  
204 =head2 states
205  
206 Hashref of charge states. Keys are the UUIDs and the values are the strings
207 that represent state. One of preauth, postauth, auth or credit.
208  
209 =head2 uuid
210  
211 The UUID generator used by this object, a lazily insantiated Data::UUID
212 object.
213  
214 =head1 METHODS
215  
216 =head2 auth ($charge)
217  
218 Performs an immediate auth for the supplied charge.
219  
220 =head2 credit ($charge)
221  
222 Performs a credit for the supplied charge.
223  
224 =head2 info ($uuid)
225  
226 Returns the charge associated with the supplied UUID.
227  
228 =head2 postauth ($uuid)
229  
230 Performs a post-authorization for the charge tied to the supplied UUID. This
231 is the second operation after a C<preauth>.
232  
233 =head2 preauth ($charge)
234  
235 Performs a pre-authorization for the supplied charge. This should be followed
236 by a C<postauth>.
237  
238 =head2 settle
239  
240 Totals up and removes all pending transactions. Returns the total (sum of all
241 auth and postauth, less any credits). Any remaining preauth transactions
242 are left in the clearinghouse to be postauthed later.
243  
244 =head2 void ($uuid)
245  
246 Voids the charge with the supplied UUID. Removes any information about the
247 charge from the clearinghouse.
248  
249 =head1 AUTHOR
250  
251 Cory G Watson, C<< <gphat at cpan.org> >>
252  
253 =head1 COPYRIGHT & LICENSE
254  
255 Copyright 2009 Cold Hard Code, LLC.
256  
257 This program is free software; you can redistribute it and/or modify it
258 under the terms of either: the GNU General Public License as published
259 by the Free Software Foundation; or the Artistic License.
260  
261 See http://dev.perl.org/licenses/ for more information.
↑ Summary ClearingHouse/Charge.pm »