get the amount right...
[Business-OnlinePayment-LinkPoint.git] / LinkPoint.pm
1 package Business::OnlinePayment::LinkPoint;
2
3 # $Id: LinkPoint.pm,v 1.12 2004-06-24 14:53:35 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       $year += 2000 if $year < 2000; #not y4k safe, oh shit
116     }
117
118     $content{'address'} =~ /^(\S+)\s/;
119     my $addrnum = $1;
120
121     my $result = $content{'result'};
122     if ( $self->test_transaction) {
123       $result ||= 'GOOD';
124       #$self->server('staging.linkpt.net');
125     } else {
126       $result ||= 'LIVE';
127     }
128
129     $self->revmap_fields(
130       hostname     => \( $self->server ),
131       port         => \( $self->port ),
132       #storename    => \( $self->storename ),
133       configfile   => \( $self->storename ),
134       keyfile      => \( $self->keyfile ),
135       addrnum      => \$addrnum,
136       result       => \$result,
137       cardNumber   => 'card_number',
138       cardExpMonth => \$month,
139       cardExpYear  => \$year,
140       chargetotal  => 'amount',
141     );
142
143     my $lperl = new LPPERL;
144
145     $self->required_fields(qw/
146       hostname port storename keyfile amount cardNumber cardExpMonth cardExpYear
147     /);
148
149     my %post_data = $self->get_fields(qw/
150       hostname port configfile keyfile
151       result
152       chargetotal cardNumber cardExpMonth cardExpYear
153       name email phone address city state zip country
154     /);
155
156     $post_data{'order_type'} = $content{action};
157
158     if ( $self->cvv2 ) { 
159       $post_data{cvmindicator} = 'provided';
160       $post_data{cvmvalue} = $self->cvv2;
161     }
162
163     warn "$_ => $post_data{$_}\n" foreach keys %post_data;
164
165     my %response;
166     #{
167     #  local($^W)=0;
168     #  %response = $lperl->$action(\%post_data);
169     #}
170     %response = $lperl->curl_process(\%post_data);
171
172     if ( $response{'r_approved'} eq 'APPROVED' ) {
173       $self->is_success(1);
174       $self->result_code($response{'r_code'});
175       $self->authorization($response{'r_ref'});
176       $self->order_number($response{'r_ordernum'});
177     } else {
178       $self->is_success(0);
179       $self->result_code('');
180       $self->error_message($response{'r_error'});
181     }
182
183 }
184
185 1;
186 __END__
187
188 =head1 NAME
189
190 Business::OnlinePayment::LinkPoint - LinkPoint (Cardservice) backend for Business::OnlinePayment
191
192 =head1 SYNOPSIS
193
194   use Business::OnlinePayment;
195
196   my $tx = new Business::OnlinePayment( 'LinkPoint',
197     'storename' => 'your_store_number',
198     'keyfile'   => '/path/to/keyfile.pem',
199   );
200
201   $tx->content(
202       type           => 'VISA',
203       action         => 'Normal Authorization',
204       description    => 'Business::OnlinePayment test',
205       amount         => '49.95',
206       invoice_number => '100100',
207       customer_id    => 'jsk',
208       name           => 'Jason Kohles',
209       address        => '123 Anystreet',
210       city           => 'Anywhere',
211       state          => 'UT',
212       zip            => '84058',
213       email          => 'ivan-linkpoint@420.am',
214       card_number    => '4007000000027',
215       expiration     => '09/99',
216   );
217   $tx->submit();
218
219   if($tx->is_success()) {
220       print "Card processed successfully: ".$tx->authorization."\n";
221   } else {
222       print "Card was rejected: ".$tx->error_message."\n";
223   }
224
225 =head1 SUPPORTED TRANSACTION TYPES
226
227 =head2 Visa, MasterCard, American Express, JCB, Discover/Novus, Carte blanche/Diners Club
228
229 =head1 DESCRIPTION
230
231 For detailed information see L<Business::OnlinePayment>.
232
233 =head1 COMPATIBILITY
234
235 This module implements an interface to the LinkPoint Perl Wrapper
236 http://www.linkpoint.com/product_solutions/internet/lperl/lperl_main.html
237
238 Version 0.4 of this module has been updated for the LinkPoint Perl Wrapper
239 version 3.5.
240
241 =head1 BUGS
242
243 =head1 AUTHOR
244
245 Ivan Kohler <ivan-linkpoint@420.am>
246
247 Based on Busienss::OnlinePayment::AuthorizeNet written by Jason Kohles.
248
249 =head1 SEE ALSO
250
251 perl(1), L<Business::OnlinePayment>.
252
253 =cut
254