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