1 package Business::OnlinePayment::WesternACH;
5 use Business::OnlinePayment 3;
6 use Business::OnlinePayment::HTTPS;
8 use Date::Format 'time2str';
9 use Date::Parse 'str2time';
10 use vars qw($VERSION @ISA $me $DEBUG);
12 @ISA = qw(Business::OnlinePayment::HTTPS);
14 $me = 'Business::OnlinePayment::WesternACH';
22 tender_type => 'check',
27 my $required = { map { $_ => 1 } ( qw(
41 # Structure of the XML request document
42 # Right sides of the hash entries are Business::OnlinePayment
43 # field names. Those that start with _ are local method names.
48 password => 'password',
53 TransactionRequest => {
58 type => '_payment_type',
60 # effective date: not supported
62 type => 'tender_type',
64 InvoiceNumber => { value => 'invoice_number' },
65 AccountHolder => { value => '_full_name' },
66 Address => { value => 'address' },
67 ClientID => { value => 'customer_id' },
69 routing => 'routing_code',
70 account => 'account_number',
71 check => 'check_number',
72 type => '_check_type',
73 verification => 'check_ver',
75 Authorization => { schedule => 'schedule' },
76 SECCode => { value => 'sec_code' },
83 my $returns_request = {
84 TransactionRequest => {
98 $self->server('www.webcheckexpress.com');
100 $self->path('/requester.php');
106 $Business::OnlinePayment::HTTPS::DEBUG = $DEBUG;
109 if ($self->{_content}->{command} eq 'get_returns') {
110 # Setting get_returns overrides anything else.
111 $xml_request = XMLout($self->build($returns_request), KeepRoot => 1);
114 # Error-check and prepare as a normal transaction.
117 # Return-with-error situations
118 croak "Unsupported transaction type: '" . $self->transaction_type . "'"
119 if(not $self->transaction_type =~ /^e?check$/i);
121 croak "Unsupported action: '" . $self->{_content}->{action} . "'"
122 if(!defined($self->_payment_type));
124 croak 'Test transactions not supported'
125 if($self->test_transaction());
129 $self->is_success(0);
130 $self->error_message($@);
134 $xml_request = XMLout($self->build($request), KeepRoot => 1);
137 my ($xml_reply, $response, %reply_headers) = $self->https_post({ 'Content-Type' => 'text/xml' }, $xml_request);
139 if(not $response =~ /^200/) {
140 croak "HTTPS error: '$response'";
143 $self->server_response($xml_reply);
144 my $reply = XMLin($xml_reply, KeepRoot => 1)->{TransactionResponse};
146 if(exists($reply->{Response})) {
147 $self->is_success( ( $reply->{Response}->{status} eq 'successful') ? 1 : 0);
148 $self->error_message($reply->{Response}->{ErrorMessage});
149 if(exists($reply->{Response}->{TransactionID})) {
150 # get_returns puts its results here
151 my $tid = $reply->{Response}->{TransactionID};
152 if($self->{_content}->{command} eq 'get_returns') {
153 $self->{_content}->{returns} = [ map { $_->{value} } @$tid ];
155 else { # It's not get_returns
156 $self->authorization($tid->{value});
160 elsif(exists($reply->{FatalException})) {
161 $self->is_success(0);
162 $self->error_message($reply->{FatalException});
165 $DB::single = 1 if $DEBUG;
172 my $content = $self->{_content};
173 if(exists($content->{'command'})) {
174 croak 'get_returns: command is already set on this transaction';
176 $content->{'command'} = 'get_returns';
178 if($self->is_success) {
179 if(exists($content->{'returns'})) {
180 return @{$content->{'returns'}};
187 # you need to check error_message() for details.
194 my $content = { $self->content };
197 if (ref($skel) ne 'HASH') { croak 'Failed to build non-hash' };
198 foreach my $k (keys(%$skel)) {
199 my $val = $skel->{$k};
200 # Rules for building from the skeleton:
201 # 1. If the value is a hashref, build it recursively.
202 if(ref($val) eq 'HASH') {
203 $data->{$k} = $self->build($val);
205 # 2. If the value starts with an underscore, it's treated as a method name.
206 elsif($val =~ /^_/ and $self->can($val)) {
207 $data->{$k} = $self->can($val)->($self);
209 # 3. If the value is undefined, keep it undefined.
210 elsif(!defined($val)) {
213 # 4. If the value is the name of a key in $self->content, look up that value.
214 elsif(exists($content->{$val})) {
215 $data->{$k} = $content->{$val};
217 # 5. If the value is a key in $defaults, use that value.
218 elsif(exists($defaults->{$val})) {
219 $data->{$k} = $defaults->{$val};
221 # 6. If the value is not required, use an empty string.
222 elsif(! $required->{$val}) {
227 croak "Missing request field: '$val'";
234 # For testing build().
236 return XMLout($self->build($request), KeepRoot => 1);
241 my $action = $self->{_content}->{action};
242 if(!defined($action) or $action =~ /^normal authorization$/i) {
245 elsif($action =~ /^credit$/i) {
255 my $type = $self->{_content}->{account_type};
256 return 'checking' if($type =~ /checking/i);
257 return 'savings' if($type =~ /savings/i);
258 croak "Invalid account_type: '$type'";
263 return join(' ',$self->{_content}->{first_name},$self->{_content}->{last_name});
268 my $start = time2str('%Y-%m-%d', str2time($self->{_content}->{start}));
269 croak "Invalid start date: '".$self->{_content}->{start} if !$start;
275 my $end = $self->{_content}->{end};
277 $end = time2str('%Y-%m-%d', str2time($end));
278 croak "Invalid end date: '".$self->{_content}->{end} if !$end;
282 return time2str('%Y-%m-%d', time);
291 Business::OnlinePayment::WesternACH - Western ACH backend for Business::OnlinePayment
295 use Business::OnlinePayment;
298 # Electronic check authorization. We only support
299 # 'Normal Authorization' and 'Credit'.
302 my $tx = new Business::OnlinePayment("WesternACH");
305 login => 'testdrive',
306 password => 'testpass',
307 action => 'Normal Authorization',
308 description => 'Business::OnlinePayment test',
310 invoice_number => '100100',
311 first_name => 'Jason',
312 last_name => 'Kohles',
313 address => '123 Anystreet',
317 account_type => 'personal checking',
318 account_number => '1000468551234',
319 routing_code => '707010024',
320 check_number => '1001', # optional
324 if($tx->is_success()) {
325 print "Check processed successfully: ".$tx->authorization."\n";
327 print "Check was rejected: ".$tx->error_message."\n";
330 my $tx = new Business::OnlinePayment("WesternACH");
332 login => 'testdrive',
333 password => 'testpass',
334 start => '2009-06-25', # optional; defaults to yesterday
335 end => '2009-06-26', # optional; defaults to today
340 =head1 SUPPORTED TRANSACTION TYPES
344 Content required: type, login, password|transaction_key, action, amount, first_name, last_name, account_number, routing_code, account_type.
348 For detailed information see L<Business::OnlinePayment>.
350 =head1 METHODS AND FUNCTIONS
352 See L<Business::OnlinePayment> for the complete list. The following methods either override the methods in L<Business::OnlinePayment> or provide additional functions.
356 Currently returns nothing; these transactions don't seem to have result codes.
360 Returns the response reason text. This can come from several locations in the response document or from certain local errors.
362 =head2 server_response
364 Returns the complete response from the server.
366 =head1 Handling of content(%content) data:
370 The following actions are valid:
377 Mark Wells <mark@freeside.biz> with advice from Ivan Kohler <ivan-westernach@freeside.biz>.
381 perl(1). L<Business::OnlinePayment>.