summaryrefslogtreecommitdiff
path: root/lib/Business/OnlinePayment/Jety.pm
blob: f653e8aba96965fe4ac4e8474fb6de7c7f7f56fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
225
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package Business::OnlinePayment::Jety;

use strict;
use Carp 'croak';
use Business::OnlinePayment 3;
use Business::OnlinePayment::HTTPS;
use vars qw($VERSION @ISA $me $DEBUG);

use Date::Format;
use Tie::IxHash;

@ISA = qw(Business::OnlinePayment::HTTPS);
$VERSION = '0.06';
$me = 'Business::OnlinePayment::Jety';

$DEBUG = 0;

my %trans_type = (
  'normal authorization' => 'echeck',
  'void'                 => 'ereturn',
  );

my %map = (
# 'function' will always be prepended
'normal authorization' => [ # note array-ness
  'username'      => 'login',
  'password'      => 'password',
  'firstname'     => 'first_name',
  'lastname'      => 'last_name',
  'address1'      => 'address',
  'address2'      => 'address2',
  'city'          => 'city',
  'state'         => 'state',
  'zip'           => 'zip',
  'email'         => 'email',
  'phone'         => 'phone',
  'programdesc'   => 'description',
  'ref'           => sub { my %c = @_; 
                           $c{'order_number'} || 
                           substr( time2str('%Y%m%d%H%M%S',time). int(rand(10000)), -15 ) 
                           },
  'bankname'      => 'bank_name',
  'bankcity'      => 'bank_city',
  'bankstate'     => 'bank_state',
  'accountaba'    => 'routing_code',
  'accountdda'    => 'account_number',
  'amount'        => sub { my %c = @_; sprintf("%.02f",$c{'amount'}) },
],
'void' => [
  'username'      => 'login',
  'password'      => 'password',
  'ref'           => 'order_number',
  'accountdda'    => 'account_number',
  'amount'        => sub { my %c = @_; sprintf("%.02f",$c{'amount'}) },
],
);

my %defaults = ( # using the B:OP names
  'phone'         => '111-111-1111',
  'bank_name'     => 'unknown',
  'bank_city'     => 'unknown',
  'bank_state'    => 'XX',
  );

my %required = (
'normal authorization' => [ qw(
  type
  action
  login
  password
  first_name
  last_name
  address
  city
  state
  zip
  email
  account_number
  routing_code
  amount
  description
) ],
'void' => [ qw(
  type
  action
  login
  password
  order_number
  account_number
  amount
) ],
);

sub set_defaults {
  my $self = shift;
  $self->server('api.cardservicesportal.com');
  $self->port(443);
  $self->path('/servlet/drafts.echeck');
  return;
}

sub submit {
  my $self = shift;
  $Business::OnlinePayment::HTTPS::DEBUG = $DEBUG;
  $DB::single = $DEBUG; 

  # strip existent but empty fields so that required_fields works right
  foreach(keys(%{$self->{_content}})) {
    delete $self->{_content}->{$_} 
      if (!defined($self->{_content}->{$_} ) or
           $self->{_content}->{$_} eq '');
  }

  my %content = $self->content();
  my $action = lc($content{'action'});

  croak "Jety only supports ECHECK payments.\n"
    if( lc($content{'type'}) ne 'echeck' );
  croak "Unsupported transaction type: '$action'\n"
    if( !exists($trans_type{$action}) );

  $self->required_fields(@{ $required{$action} });

  my @fields = @{ $map{$action} } ;
  tie my %request, 'Tie::IxHash', ( 'function' => $trans_type{$action} );
  while(@fields) {
    my ($key, $value) = (shift (@fields), shift (@fields));
    if( ref($value) eq 'CODE' ) {
      $request{$key} = $value->(%content);
    }
    elsif (defined($content{$value}) and $content{$value} ne '') {
      $request{$key} = $content{$value};
    }
    elsif (exists($defaults{$value})) {
      $request{$key} = $defaults{$value};
    } # else do nothing
  }

  $DB::single = $DEBUG;
  if($self->test_transaction()) {
    print "https://".$self->server.$self->path."\n";
    print "$_\t".$request{$_}."\n" foreach keys(%request);
    $self->error_message('test mode not supported');
    $self->is_success(0);
    return;
  }
  my ($reply, $response, %reply_headers) = $self->https_post(\%request);
  
  if(not $response =~ /^200/) {
    croak "HTTPS error: '$response'";
  }

  # string looks like this:
  # P1=1234&P2=General Status&P3=Specific Status
  # P3 is not always there, though.
  if($reply =~ /^P1=(\d+)&P2=([\w ]*)(&P3=(\S+))?/) {
    if($1 == 0) {
      $self->is_success(1);
      $self->authorization($4);
    }
    else {
      $self->is_success(0);
      $self->error_message($2.($4 ? "($4)" : ''));
    }
  }
  else {
    croak "Malformed server response: '$reply'";
  }

  return;
}

sub get_returns {
# Required parameters:
# ftp_user, ftp_pass, ftp_host, ftp_path
# Optional:
# start, end
  eval('use Date::Parse q!str2time!; use Net::FTP; use File::Temp q!tempdir!');
  die $@ if $@;

  my $self = shift;
# $self->required_fields, for processor options
  my @missing;
  my ($user, $pass, $host, $path) = map {
    if($self->can($_) and $self->$_) {
      $self->$_ ;
    } else {
      push @missing, $_; '';
    } 
  } qw(ftp_user ftp_pass ftp_host);
  die "missing gateway option(s): ".join(', ',@missing)."\n" if @missing;
  my $ftp_path = $self->ftp_path if $self->can('ftp_path');

  my $start = $self->{_content}->{start};
  $start &&= str2time($start);
  $start ||= time - 86400;
  $start = time2str('%Y%m%d',$start);

  my $end = $self->{_content}->{end};
  $end &&= str2time($end);
  $end ||= time;
  $end = time2str('%Y%m%d',$end);
  
  my $ftp = Net::FTP->new($host) 
    or die "FTP connection to '$host' failed.\n";
  $ftp->login($user, $pass) or die "FTP login failed: ".$ftp->message."\n";
  $ftp->cwd($path) or die "can't chdir to $path\n" if $path;
 
  my $tmp = tempdir(CLEANUP => 1);
  my @files;
  foreach my $filename ($ftp->ls) {
    if($filename =~ /^\w+_RET(\d{8}).csv$/ 
      and $1 >= $start 
      and $1 <= $end ) {
      $ftp->get($filename, "$tmp/$1") or die "Failed to download $filename: ".$ftp->message."\n";
      push @files, $1;
    }
  }
  $ftp->close;

  my @tids;
  foreach my $filename (@files) {
    open IN, '<', "$tmp/$filename";
    my @fields = split ',',<IN>; #fetch header row
    my ($i) = grep { $fields[$_] eq 'AccountToID' } 0..(scalar @fields - 1);
    $i ||= 1;
    while(<IN>) {
      my @fields = split ',', $_;
      push @tids, $fields[$i];
    }
    close IN;
  }
  return @tids;
}

1;
__END__

=head1 NAME

Business::OnlinePayment::Jety - Jety Payments ACH backend for Business::OnlinePayment

=head1 SYNOPSIS

  use Business::OnlinePayment;

  ####
  # Electronic check authorization.  We only support 
  # 'Normal Authorization'.
  ####

  my $tx = new Business::OnlinePayment("Jety");
  $tx->content(
      type           => 'ECHECK',
      login          => 'testdrive',
      password       => 'testpass',
      action         => 'Normal Authorization',
      description    => '111-111-1111 www.example.com',
      amount         => '49.95',
      invoice_number => '100100',
      first_name     => 'Jason',
      last_name      => 'Kohles',
      address        => '123 Anystreet',
      city           => 'Anywhere',
      state          => 'UT',
      zip            => '84058',
      account_type   => 'personal checking',
      account_number => '1000468551234',
      routing_code   => '707010024',
      check_number   => '1001', # optional
  );
  $tx->submit();

  if($tx->is_success()) {
      print "Check processed successfully: ".$tx->authorization."\n";
  } else {
      print "Check was rejected: ".$tx->error_message."\n";
  }

=head1 SUPPORTED TRANSACTION TYPES

=head2 ECHECK

Content required: type, login, password, action, amount, first_name, last_name, account_number, routing_code, description.

description should be set in the form "111-111-1111 www.example.com"

=head1 DESCRIPTION

For detailed information see L<Business::OnlinePayment>.

=head1 METHODS AND FUNCTIONS

See L<Business::OnlinePayment> for the complete list. The following methods either override the methods in L<Business::OnlinePayment> or provide additional functions.  

=head2 result_code

Returns the four-digit result code.

=head2 error_message

Returns a useful error message.

=head1 Handling of content(%content) data:

=head2 action

The following actions are valid:

  normal authorization

=head1 AUTHOR

Mark Wells <mark@freeside.biz>

=head1 SEE ALSO

perl(1). L<Business::OnlinePayment>.

=cut