added cvv2_code (and added avs_code to build_subs)
[Business-OnlinePayment-PayflowPro.git] / PayflowPro.pm
1 package Business::OnlinePayment::PayflowPro;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
5 use Carp qw(croak);
6 use AutoLoader;
7 use Business::OnlinePayment;
8
9 #PayflowPRO SDK from Verisign
10 use PFProAPI qw( pfpro );
11
12 require Exporter;
13
14 @ISA = qw(Exporter AutoLoader Business::OnlinePayment);
15 @EXPORT = qw();
16 @EXPORT_OK = qw();
17 $VERSION = '0.02_02';
18
19 sub set_defaults {
20     my $self = shift;
21
22     #$self->server('staging.linkpt.net');
23     $self->server('payflow.verisign.com');
24     $self->port('443');
25
26     $self->build_subs(qw(
27       vendor partner order_number cert_path avs_code cvv2_code
28     ));
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' => 'S', #Sale
39                    'authorization only'   => 'A', #Authorization
40                    'credit'               => 'C', #Credit (refund)
41                    'post authorization'   => 'D', #Delayed Capture
42                    'void'                 => 'V',
43                   );
44     $content{'action'} = $actions{lc($content{'action'})} || $content{'action'};
45
46     # TYPE MAP
47     my %types = ('visa'               => 'C',
48                  'mastercard'         => 'C',
49                  'american express'   => 'C',
50                  'discover'           => 'C',
51                  'cc'                 => 'C'
52                  #'check'              => 'ECHECK',
53                 );
54     $content{'type'} = $types{lc($content{'type'})} || $content{'type'};
55     $self->transaction_type($content{'type'});
56
57     # stuff it back into %content
58     $self->content(%content);
59 }
60
61 sub build_subs {
62     my $self = shift;
63     foreach(@_) {
64         #no warnings; #not 5.005
65         local($^W)=0;
66         eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
67     }
68 }
69
70 sub remap_fields {
71     my($self,%map) = @_;
72
73     my %content = $self->content();
74     foreach(keys %map) {
75         $content{$map{$_}} = $content{$_};
76     }
77     $self->content(%content);
78 }
79
80 sub revmap_fields {
81     my($self, %map) = @_;
82     my %content = $self->content();
83     foreach(keys %map) {
84 #    warn "$_ = ". ( ref($map{$_})
85 #                         ? ${ $map{$_} }
86 #                         : $content{$map{$_}} ). "\n";
87         $content{$_} = ref($map{$_})
88                          ? ${ $map{$_} }
89                          : $content{$map{$_}};
90     }
91     $self->content(%content);
92 }
93
94 sub get_fields {
95     my($self,@fields) = @_;
96
97     my %content = $self->content();
98     my %new = ();
99     foreach( grep defined $content{$_}, @fields) { $new{$_} = $content{$_}; }
100     return %new;
101 }
102
103 sub submit {
104     my($self) = @_;
105
106     $self->map_fields();
107
108     my %content = $self->content;
109
110     my($month, $year, $zip);
111
112     #unless ( $content{action} eq 'BillOrders' ) {
113
114         if (  $self->transaction_type() eq 'C' ) {
115         } else {
116             Carp::croak("PayflowPro can't (yet?) handle transaction type: ".
117                         $self->transaction_type());
118         }
119
120       if ( exists($content{'expiration'}) && defined($content{'expiration'})
121            && length($content{'expiration'})                                 ) {
122         $content{'expiration'} =~ /^(\d+)\D+\d*(\d{2})$/
123           or croak "unparsable expiration $content{expiration}";
124
125         ( $month, $year ) = ( $1, $2 );
126         $month = '0'. $month if $month =~ /^\d$/;
127       }
128
129       $zip = $content{'zip'} =~ s/\D//;
130     #}
131
132     #$content{'address'} =~ /^(\S+)\s/;
133     #my $addrnum = $1;
134
135     $self->server('test-payflow.verisign.com') if $self->test_transaction;
136
137     $self->revmap_fields(
138       ACCT       => 'card_number',
139       EXPDATE     => \( $month.$year ),
140       AMT         => 'amount',
141       USER        => 'login',
142       #VENDOR      => \( $self->vendor ),
143       VENDOR      => 'login',
144       PARTNER     => \( $self->partner ),
145       PWD         => 'password',
146       TRXTYPE     => 'action',
147       TENDER      => 'type',
148
149       STREET      => 'address',
150       ZIP         => \$zip,
151
152       CITY        => 'city',
153       COMMENT1    => 'description',
154       COMMENT2    => 'invoice_number',
155       COMPANYNAME => 'company',
156       COUNTRY     => 'country',
157       FIRSTNAME   => 'first_name',
158       LASTNAME    => 'last_name',
159       NAME        => 'name',
160       EMAIL       => 'email',
161       STATE       => 'state',
162
163       CVV2        => 'cvv2',
164       ORIGID      => 'order_number'
165
166     );
167
168     my @required = qw( TRXTYPE TENDER PARTNER VENDOR USER PWD );
169     if (  $self->transaction_type() eq 'C' ) { #credit card
170       if ( $self->action() =~ /^[CDV]$/ && exists($content{'ORIGID'})
171            && defined($content{'ORIGID'}) && length($content{'ORIGID'}) ) {
172         push @required, qw(ORIGID);
173       } else {
174         push @required, qw(AMT ACCT EXPDATE);
175       }
176     }
177     $self->required_fields(@required);
178
179     my %params = $self->get_fields(qw(
180       ACCT EXPDATE AMT USER VENDOR PARTNER PWD TRXTYPE TENDER
181       STREET ZIP
182       CITY COMMENT1 COMMENT2 COMPANYNAME COUNTRY FIRSTNAME LASTNAME NAME EMAIL
183         STATE
184       CVV2 ORIGID
185     ));
186
187     #print "$_ => $params{$_}\n" foreach keys %params;
188
189     $ENV{'PFPRO_CERT_PATH'} = $self->cert_path;
190     my( $response, $resultstr ) = pfpro( \%params, $self->server, $self->port );
191
192     #if ( $response->{'RESULT'} == 0 ) {
193     if ( $response->{'RESULT'} eq '0' ) { #want an explicit zero, not just
194                                           #numerically equal
195       $self->is_success(1);
196       $self->result_code(   $response->{'RESULT'}   );
197       $self->error_message( $response->{'RESPMSG'}  );
198       $self->authorization( $response->{'AUTHCODE'} );
199       $self->order_number(  $response->{'PNREF'}    );
200       my $avs_code = '';
201       if ( $response->{AVSADDR} eq 'Y' && $response->{AVSZIP} eq 'Y' ) {
202         $avs_code = 'Y';
203       } elsif ( $response->{AVSADDR} eq 'Y' ) {
204         $avs_code = 'A';
205       } elsif ( $response->{AVSZIP} eq 'Y' ) {
206         $avs_code = 'Z';
207       } elsif ( $response->{AVSADDR} eq 'N' || $response->{AVSZIP} eq 'N' ) {
208         $avs_code = 'N';
209       }
210       $self->avs_code(      $avs_code               );
211       $self->cvv2_code(     $response->{'CVV2MATCH'});
212     } else {
213       $self->is_success(0);
214       $self->result_code(   $response->{'RESULT'}  );
215       $self->error_message( $response->{'RESPMSG'} );
216     }
217
218 }
219
220 1;
221 __END__
222
223 =head1 NAME
224
225 Business::OnlinePayment::PayflowPro - Verisign PayflowPro backend for Business::OnlinePayment
226
227 =head1 SYNOPSIS
228
229   use Business::OnlinePayment;
230
231   my $tx = new Business::OnlinePayment( 'PayflowPro',
232     'vendor'    => 'your_vendor',
233     'partner'   => 'your_partner',
234     'cert_path' => '/path/to/your/certificate/file/', #just the dir
235   );
236
237   $tx->content(
238       type           => 'VISA',
239       action         => 'Normal Authorization',
240       description    => 'Business::OnlinePayment test',
241       amount         => '49.95',
242       invoice_number => '100100',
243       customer_id    => 'jsk',
244       name           => 'Jason Kohles',
245       address        => '123 Anystreet',
246       city           => 'Anywhere',
247       state          => 'UT',
248       zip            => '84058',
249       email          => 'ivan-payflowpro@420.am',
250       card_number    => '4007000000027',
251       expiration     => '09/04',
252
253       #advanced params
254       cvv2           => '420',
255       order_number   => 'string', # returned by $tx->order_number() from an
256                                   # "authorization only" or
257                                   # "normal authorization" action, used by a
258                                   # "credit", "void", or "post authorization"
259   );
260   $tx->submit();
261
262   if($tx->is_success()) {
263       print "Card processed successfully: ".$tx->authorization."\n";
264       print "order number: ". $tx->order_number. "\n";
265       print "AVS code: ". $tx->avs_code. "\n"; # Y - Address and ZIP match
266                                                # A - Address matches but not ZIP
267                                                # Z - ZIP matches bu tnot address
268                                                # N - no match
269                                                # E - AVS error or unsupported
270                                                # (null) - AVS error
271       print "CVV2 code: ". $tx->cvv2_code. "\n";
272
273   } else {
274       print "Card was rejected: ".$tx->error_message;
275       print " (CVV2 mismatch)" if $tx->result_code == 114;
276       print "\n";
277   }
278
279 =head1 SUPPORTED TRANSACTION TYPES
280
281 =head2 Visa, MasterCard, American Express, JCB, Discover/Novus, Carte blanche/Diners Club, CC
282
283 =head1 SUPPORTED ACTIONS
284
285 =head2 Normal Authorization, Authorization Only, Post Authorization, Credit, Void
286
287 =head1 DESCRIPTION
288
289 For detailed information see L<Business::OnlinePayment>.
290
291 =head1 COMPATIBILITY
292
293 This module implements an interface to the PayflowPro Perl API, which can
294 be downloaded at https://manager.verisign.com/ with a valid login.
295
296 =head1 BUGS
297
298 =head1 AUTHOR
299
300 Ivan Kohler <ivan-payflowpro@420.am>
301
302 Based on Busienss::OnlinePayment::AuthorizeNet written by Jason Kohles.
303
304 =head1 SEE ALSO
305
306 perl(1), L<Business::OnlinePayment>.
307
308 =cut
309