- PREREQ_PM on Business::OnlinePayment
[Business-OnlinePayment-LinkPoint.git] / LinkPoint.pm
1 package Business::OnlinePayment::LinkPoint;
2
3 # $Id: LinkPoint.pm,v 1.6 2002-08-14 01:32:54 ivan Exp $
4
5 use strict;
6 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
7 use Carp qw(croak);
8 use AutoLoader;
9 use Business::OnlinePayment;
10
11 use lperl; #lperl.pm from Linkpoint.
12
13 require Exporter;
14
15 @ISA = qw(Exporter AutoLoader Business::OnlinePayment);
16 @EXPORT = qw();
17 @EXPORT_OK = qw();
18 $VERSION = '0.02';
19
20 sub set_defaults {
21     my $self = shift;
22
23     #$self->server('staging.linkpt.net');
24     $self->server('secure.linkpt.net');
25     $self->port('1139');
26
27 }
28
29 sub map_fields {
30     my($self) = @_;
31
32     my %content = $self->content();
33
34     #ACTION MAP
35     my %actions = ('normal authorization' => 'ApproveSale',
36                    'authorization only'   => 'CapturePayment',
37                    'credit'               => 'ReturnOrder',
38                    'post authorization'   => 'BillOrders',
39                   );
40     $content{'action'} = $actions{lc($content{'action'})} || $content{'action'};
41
42     # stuff it back into %content
43     $self->content(%content);
44 }
45
46 sub build_subs {
47     my $self = shift;
48     foreach(@_) {
49         #no warnings; #not 5.005
50         local($^W)=0;
51         eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
52     }
53 }
54
55 sub remap_fields {
56     my($self,%map) = @_;
57
58     my %content = $self->content();
59     foreach(keys %map) {
60         $content{$map{$_}} = $content{$_};
61     }
62     $self->content(%content);
63 }
64
65 sub revmap_fields {
66     my($self, %map) = @_;
67     my %content = $self->content();
68     foreach(keys %map) {
69 #    warn "$_ = ". ( ref($map{$_})
70 #                         ? ${ $map{$_} }
71 #                         : $content{$map{$_}} ). "\n";
72         $content{$_} = ref($map{$_})
73                          ? ${ $map{$_} }
74                          : $content{$map{$_}};
75     }
76     $self->content(%content);
77 }
78
79 sub get_fields {
80     my($self,@fields) = @_;
81
82     my %content = $self->content();
83     my %new = ();
84     foreach( grep defined $content{$_}, @fields) { $new{$_} = $content{$_}; }
85     return %new;
86 }
87
88 sub submit {
89     my($self) = @_;
90
91
92     $self->map_fields();
93
94     my %content = $self->content;
95
96     my($month, $year);
97     unless ( $content{action} eq 'BillOrders' ) {
98
99         if (  $self->transaction_type() =~
100                 /^(cc|visa|mastercard|american express|discover)$/i
101            ) {
102         } else {
103             Carp::croak("LinkPoint can't handle transaction type: ".
104                         $self->transaction_type());
105         }
106
107       $content{'expiration'} =~ /^(\d+)\D+\d*(\d{2})$/
108         or croak "unparsable expiration $content{expiration}";
109
110       ( $month, $year ) = ( $1, $2 );
111       $month = '0'. $month if $month =~ /^\d$/;
112       $year += 2000 if $year < 2000; #not y4k safe, oh shit
113     }
114
115     $content{'address'} =~ /^(\S+)\s/;
116     my $addrnum = $1;
117
118     $self->server('staging.linkpt.net') if $self->test_transaction;
119
120     $self->revmap_fields(
121       hostname     => \( $self->server ),
122       port         => \( $self->port ),
123       storename    => \( $self->storename ),
124       keyfile      => \( $self->keyfile ),
125       addrnum      => \$addrnum,
126
127       cardNumber   => 'card_number',
128       cardExpMonth => \$month,
129       cardExpYear  => \$year,
130     );
131
132     my $lperl = new LPERL
133       $self->lbin,
134       'FILE',
135       $self->can('tmp')
136         ? $self->tmp
137         : '/tmp';
138     my $action = $content{action};
139
140     $self->required_fields(qw/
141       hostname port storename keyfile amount cardNumber cardExpMonth cardExpYear
142     /);
143
144     my %post_data = $self->get_fields(qw/
145       hostname port storename keyfile
146       result
147       amount cardNumber cardExpMonth cardExpYear
148       name email phone address city state zip country
149     /);
150
151     #print "$_ => $post_data{$_}\n" foreach keys %post_data;
152
153     my %response;
154     {
155       local($^W)=0;
156       %response = $lperl->$action(\%post_data);
157     }
158
159     if ( $response{'statusCode'} == 0 ) {
160       $self->is_success(0);
161       $self->result_code('');
162       $self->error_message($response{'statusMessage'});
163     } else {
164       $self->is_success(1);
165       $self->result_code($response{'AVCCode'});
166       $self->authorization($response{'trackingID'});
167 #      $self->order_number($response{'neworderID'});
168     }
169
170 }
171
172 1;
173 __END__
174
175 =head1 NAME
176
177 Business::OnlinePayment::LinkPoint - LinkPoint backend for Business::OnlinePayment
178
179 =head1 SYNOPSIS
180
181   use Business::OnlinePayment;
182
183   my $tx = new Business::OnlinePayment( 'LinkPoint',
184     'storename' => 'your_store_number',
185     'keyfile'   => '/path/to/keyfile.pem',
186     'lbin'      => '/path/to/binary/lbin',
187     'tmp'       => '/secure/tmp',          # a secure tmp directory
188   );
189
190   $tx->content(
191       type           => 'VISA',
192       action         => 'Normal Authorization',
193       description    => 'Business::OnlinePayment test',
194       amount         => '49.95',
195       invoice_number => '100100',
196       customer_id    => 'jsk',
197       name           => 'Jason Kohles',
198       address        => '123 Anystreet',
199       city           => 'Anywhere',
200       state          => 'UT',
201       zip            => '84058',
202       email          => 'ivan-linkpoint@420.am',
203       card_number    => '4007000000027',
204       expiration     => '09/99',
205   );
206   $tx->submit();
207
208   if($tx->is_success()) {
209       print "Card processed successfully: ".$tx->authorization."\n";
210   } else {
211       print "Card was rejected: ".$tx->error_message."\n";
212   }
213
214 =head1 SUPPORTED TRANSACTION TYPES
215
216 =head2 Visa, MasterCard, American Express, JCB, Discover/Novus, Carte blanche/Diners Club
217
218 =head1 DESCRIPTION
219
220 For detailed information see L<Business::OnlinePayment>.
221
222 =head1 COMPATIBILITY
223
224 This module implements an interface to the LinkPoint Perl Wrapper
225 http://www.linkpoint.com/product_solutions/internet/lperl/lperl_main.html
226
227 =head1 BUGS
228
229 =head1 AUTHOR
230
231 Ivan Kohler <ivan-linkpoint@420.am>
232
233 Based on Busienss::OnlinePayment::AuthorizeNet written by Jason Kohles.
234
235 =head1 SEE ALSO
236
237 perl(1), L<Business::OnlinePayment>.
238
239 =cut
240