c154d215e487eeab597f4e91e9309043fdc6fe81
[Business-OnlinePayment-Jety.git] / lib / Business / OnlinePayment / Jety.pm
1 package Business::OnlinePayment::Jety;
2
3 use strict;
4 use Carp;
5 use Business::OnlinePayment 3;
6 use Business::OnlinePayment::HTTPS;
7 use vars qw($VERSION @ISA $me $DEBUG);
8
9 use Date::Format;
10 use Tie::IxHash;
11
12 @ISA = qw(Business::OnlinePayment::HTTPS);
13 $VERSION = '0.01';
14 $me = 'Business::OnlinePayment::Jety';
15
16 $DEBUG = 1;
17
18 my @fields = (qw(
19   username
20   password
21   function
22   firstname
23   lastname
24   address1
25   address2
26   city
27   state
28   zip
29   email
30   phone
31   programdesc
32   ref
33   bankname
34   accountaba
35   accountdda
36   amount
37 ));
38
39 my %map = (
40   login      => 'username',
41   first_name => 'firstname',
42   last_name  => 'lastname',
43   address    => 'address1',
44   bank_name  => 'bankname',
45   account_number => 'accountdda',
46   routing_code => 'accountaba',
47 );
48   
49
50 sub set_defaults {
51   my $self = shift;
52   $self->server('api.cardservicesportal.com');
53   $self->port(443);
54   $self->path('/servlet/drafts.echeck');
55   return;
56 }
57
58 sub map_fields {
59   my $self = shift;
60   my $content = $self->{_content};
61
62   $self->remap_fields(%map);
63
64   die "Jety API only supports ECHECK payments.\n" 
65     if(lc($content->{type}) ne 'echeck');
66   die "Jety interface only supports Normal Authorization.\n"
67     if(lc($content->{action}) ne 'normal authorization');
68
69   $content->{'function'} = 'ach';
70   $content->{'programdesc'} = '415-462-1624 Business::OnlinePayment::Jety';
71   $content->{'ref'} = time2str('%Y%m%d',time).'-'.int(rand(1000000));
72   return;
73 }
74
75 sub submit {
76   my $self = shift;
77   $Business::OnlinePayment::HTTPS::DEBUG = $DEBUG;
78   $DB::single = $DEBUG; 
79
80   $self->required_fields(qw(
81     type
82     action
83     first_name
84     last_name
85     address
86     city
87     state
88     zip
89     email
90     phone
91     account_number
92     routing_code
93     amount
94     ) );
95   $self->map_fields;
96   tie my %request, 'Tie::IxHash', (
97     map { $_, $self->{_content}->{$_} } @fields
98     );
99
100   $DB::single = $DEBUG;
101   if($self->test_transaction()) {
102     print "https://".$self->server.$self->path."\n";
103     print "$_\t".$request{$_}."\n" foreach keys(%request);
104     $self->error_message('test mode not supported');
105     $self->is_success(0);
106     return;
107   }
108   my ($reply, $response, %reply_headers) = $self->https_post(\%request);
109   
110   if(not $response =~ /^200/) {
111     croak "HTTPS error: '$response'";
112   }
113
114   # string looks like this:
115   # P1=1234&P2=General Status&P3=Specific Status
116   # P3 is not always there, though.
117   if($reply =~ /^P1=(\d+)&P2=([\w ]*)(&P3=(\S+))?/) {
118     if($1 == 0) {
119       $self->is_success(1);
120       $self->authorization($4);
121     }
122     else {
123       $self->is_success(0);
124       $self->error_message($2.($4 ? "($4)" : ''));
125     }
126   }
127   else {
128     croak "Malformed server response: '$reply'";
129   }
130
131   return;
132 }
133
134 1;
135 __END__
136
137 =head1 NAME
138
139 Business::OnlinePayment::Jety - Jety Payments ACH backend for Business::OnlinePayment
140
141 =head1 SYNOPSIS
142
143   use Business::OnlinePayment;
144
145   ####
146   # Electronic check authorization.  We only support 
147   # 'Normal Authorization'.
148   ####
149
150   my $tx = new Business::OnlinePayment("Jety");
151   $tx->content(
152       type           => 'ECHECK',
153       login          => 'testdrive',
154       password       => 'testpass',
155       action         => 'Normal Authorization',
156       description    => 'Business::OnlinePayment test',
157       amount         => '49.95',
158       invoice_number => '100100',
159       first_name     => 'Jason',
160       last_name      => 'Kohles',
161       address        => '123 Anystreet',
162       city           => 'Anywhere',
163       state          => 'UT',
164       zip            => '84058',
165       account_type   => 'personal checking',
166       account_number => '1000468551234',
167       routing_code   => '707010024',
168       check_number   => '1001', # optional
169   );
170   $tx->submit();
171
172   if($tx->is_success()) {
173       print "Check processed successfully: ".$tx->authorization."\n";
174   } else {
175       print "Check was rejected: ".$tx->error_message."\n";
176   }
177
178 =head1 SUPPORTED TRANSACTION TYPES
179
180 =head2 ECHECK
181
182 Content required: type, login, password, action, amount, first_name, last_name, account_number, routing_code.
183
184 =head1 DESCRIPTION
185
186 For detailed information see L<Business::OnlinePayment>.
187
188 =head1 METHODS AND FUNCTIONS
189
190 See L<Business::OnlinePayment> for the complete list. The following methods either override the methods in L<Business::OnlinePayment> or provide additional functions.  
191
192 =head2 result_code
193
194 Returns the four-digit result code.
195
196 =head2 error_message
197
198 Returns a useful error message.
199
200 =head1 Handling of content(%content) data:
201
202 =head2 action
203
204 The following actions are valid:
205
206   normal authorization
207
208 =head1 AUTHOR
209
210 Mark Wells <mark@freeside.biz>
211
212 =head1 SEE ALSO
213
214 perl(1). L<Business::OnlinePayment>.
215
216 =cut
217