9a77236a3b24e228c01591e02cae4615322abfc3
[Business-OnlinePayment-LinkPoint.git] / LinkPoint.pm
1 package Business::OnlinePayment::LinkPoint;
2
3 # $Id: LinkPoint.pm,v 1.1 2002-02-15 01:19:27 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.01';
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     #die;
144
145     my %response = $lperl->$action(\%post_data);
146
147     if ( $response{'statusCode'} == 0 ) {
148       $self->is_success(0);
149       $self->result_code('');
150       $self->error_message($response{'statusMessage'});
151     } else {
152       $self->is_success(1);
153       $self->result_code($response{'AVCCode'});
154       $self->authorization($response{'trackingID'});
155 #      $self->order_number($response{'neworderID'});
156     }
157
158 }
159
160 1;
161 __END__
162
163 =head1 NAME
164
165 Business::OnlinePayment::LinkPoint - LinkPoint backend for Business::OnlinePayment
166
167 =head1 SYNOPSIS
168
169   use Business::OnlinePayment;
170
171   my $tx = new Business::OnlinePayment( 'LinkPoint',
172     'storename' => 'your_store_number',
173     'keyfile'   => '/path/to/keyfile.pem',
174     'lbin'      => '/path/to/binary/lbin',
175     'tmp'       => '/secure/tmp',          # a secure tmp directory
176   );
177
178   $tx->content(
179       type           => 'VISA',
180       action         => 'Normal Authorization',
181       description    => 'Business::OnlinePayment test',
182       amount         => '49.95',
183       invoice_number => '100100',
184       customer_id    => 'jsk',
185       name           => 'Jason Kohles',
186       address        => '123 Anystreet',
187       city           => 'Anywhere',
188       state          => 'UT',
189       zip            => '84058',
190       email          => 'ivan-linkpoint@420.am',
191       card_number    => '4007000000027',
192       expiration     => '09/99',
193   );
194   $tx->submit();
195
196   if($tx->is_success()) {
197       print "Card processed successfully: ".$tx->authorization."\n";
198   } else {
199       print "Card was rejected: ".$tx->error_message."\n";
200   }
201
202 =head1 SUPPORTED TRANSACTION TYPES
203
204 =head2 Visa, MasterCard, American Express, JCB, Discover/Novus, Carte blanche/Diners Club
205
206 =head1 DESCRIPTION
207
208 For detailed information see L<Business::OnlinePayment>.
209
210 =head1 COMPATIBILITY
211
212 This module implements an interface to the LinkPoint Perl Wrapper
213 http://www.linkpoint.com/product_solutions/internet/inet_index.html
214
215 =head1 BUGS
216
217 =head1 AUTHOR
218
219 Ivan Kohler <ivan-linkpoint@420.am>
220
221 Based on Busienss::OnlinePayment::AuthorizeNet written by Jason Kohles.
222
223 =head1 SEE ALSO
224
225 perl(1). L<Business::OnlinePayment>.
226
227 =cut
228