summaryrefslogtreecommitdiff
path: root/lib/Business/OnlinePayment/Jety.pm
blob: a272e8eb9c228031f11abe1da4afdd2de799ad69 (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
package Business::OnlinePayment::Jety;

use strict;
use Carp;
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.03';
$me = 'Business::OnlinePayment::Jety';

$DEBUG = 0;

my @fields = (qw(
  username
  password
  function
  firstname
  lastname
  address1
  address2
  city
  state
  zip
  email
  phone
  programdesc
  ref
  bankname
  bankcity
  bankstate
  accountaba
  accountdda
  amount
));

my %map = (
  'login'          => 'username',
  'first_name'     => 'firstname',
  'last_name'      => 'lastname',
  'address'        => 'address1',
  'bank_name'      => 'bankname',
  'bank_city'      => 'bankcity',
  'bank_state'     => 'bankstate',
  'account_number' => 'accountdda',
  'routing_code'   => 'accountaba',
  'description'    => 'programdesc',
);

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

sub map_fields {
  my $self = shift;
  my $content = $self->{_content};

  $self->remap_fields(%map);

  die "Jety API only supports ECHECK payments.\n" 
    if(lc($content->{type}) ne 'echeck');
  die "Jety interface only supports Normal Authorization.\n"
    if(lc($content->{action}) ne 'normal authorization');

  $content->{'function'} = 'echeck';
  $content->{'ref'} = time2str('%Y%m%d',time).'-'.int(rand(1000000));

  $content->{'phone'} ||= '111-111-1111';

  $content->{'bankname'} ||= 'unknown';
  $content->{'bankcity'} ||= 'unknown';
  $content->{'bankstate'} ||= 'XX';

  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 '');
  }

  $self->required_fields(qw(
    type
    action
    first_name
    last_name
    address
    city
    state
    zip
    email
    phone
    account_number
    routing_code
    amount
    ) );
  $self->map_fields;
  tie my %request, 'Tie::IxHash', (
    map { $_, $self->{_content}->{$_} } @fields
    );

  $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;
}

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    => 'Business::OnlinePayment test',
      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.

=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