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