aa8bcb394ac4536e732a15530e46df83ba8aefcb
[Business-OnlinePayment.git] / FraudDetect / preCharge.pm
1
2 package Business::FraudDetect::preCharge;
3
4 use strict;
5 use Carp;
6 use vars qw($VERSION @ISA);
7 #use Data::Dumper;
8
9 use Business::OnlinePayment::HTTPS;
10 @ISA = qw / Business::OnlinePayment::HTTPS /;
11
12 $VERSION = '0.01';
13
14 sub _glean_parameters_from_parent {
15     my ($self, $parent) = @_;
16     foreach my $method (qw / precharge_id precharge_security1 precharge_security2 /) {
17         $self->$method($parent->$method);
18     }
19 }
20
21 sub set_defaults {
22     my ($self) = @_;
23     $self->server('api.precharge.net');
24     $self->port(443);
25     $self->path('/charge');
26     $self->build_subs(qw /currency fraud_score error_code
27                       precharge_id precharge_security1 precharge_security2 force_success / );
28     $self->currency('USD');
29     return $self;
30 }
31
32 sub submit {
33     my ($self) = @_;
34     if ($self->force_success()) {
35         $self->is_success(1);
36         $self->result_code('1');
37         $self->error_message('No Error.  Force success path');
38         return $self;
39     }
40     my %content = $self->content();
41     Carp::croak("Action: $content{action} not supported.") unless
42         lc($content{action}) eq 'fraud detect';
43     
44     $self->required_fields(qw(
45                               amount card_number expiration
46                               first_name last_name state zip country phone email
47                               ip_address
48                               ));
49
50     $self->remap_fields( qw /
51                          ip_address             ecom_billto_online_ip 
52                          zip                    ecom_billto_postal_code 
53                          phone                  ecom_billto_telecom_phone_number 
54                          first_name             ecom_billto_postal_name_first
55                          last_name              ecom_billto_postal_name_last
56                          email                  ecom_billto_online_email 
57                          amount                 ecom_transaction_amount  /
58                          );
59
60
61     my %post_data = $self->get_fields(qw ( ecom_billto_online_ip ecom_billto_postal_code
62                                            ecom_billto_telecom_phone_number ecom_billto_online_email
63                                            ecom_transaction_amount currency
64                                            ));
65
66     # set up some reasonable defaults
67
68     #
69     # split out MM/YY from exp date
70     #
71
72     @post_data{qw/ecom_payment_card_expdate_month ecom_payment_card_expdate_year/} = $content{expiration} =~ m/(\d{1,2})(\d{2})/;
73     @post_data{qw/merchant_id security_1 security_2/} = ($self->precharge_id,
74                                                          $self->precharge_security1,
75                                                          $self->precharge_security2);
76
77     if ($self->test_transaction()) {
78         $post_data{test} = 1;
79     }
80
81 #    warn Dumper \%post_data;
82     my ($page, $response, %headers) = $self->https_post(\%post_data);
83
84     $self->server_response($page);
85
86     my @details = split ',',$page;
87
88     my %error_map = ( 101 => 'Invalid Request Method',
89                       102 => 'Invalid Request URL',
90                       103 => 'Invalid Security Code(s)',
91                       104 => 'Merchant Status not Verified',
92                       105 => 'Merchant Feed is Disabled',
93                       106 => 'Invalid Request Type',
94                       107 => 'Missing IP Address',
95                       108 => 'Invalid IP Address Syntax',
96                       109 => 'Missing First Name',
97                       110 => 'Invalid First Name',
98                       111 => 'Missing Last Name',
99                       112 => 'Invalid Last Name',
100                       113 => 'Invalid Address 1',
101                       114 => 'Invalid Address 2',
102                       115 => 'Invalid City',
103                       116 => 'Invalid State',
104                       117 => 'Invalid Country',
105                       118 => 'Missing Postal Code',
106                       119 => 'Invalid Postal Code',
107                       120 => 'Missing Phone Number',
108                       121 => 'Invalid Phone Number',
109                       122 => 'Missing Expiration Month',
110                       123 => 'Invalid Expiration Month',
111                       124 => 'Missing Expiration Year',
112                       125 => 'Invalid Expiration Year',
113                       126 => 'Expired Credit Card',
114                       127 => 'Missing Credit Card Number',
115                       128 => 'Invalid Credit Card Number',
116                       129 => 'Missing Email Address',
117                       130 => 'Invlaid Email Syntax',
118                       131 => 'Duplicate Transaction',
119                       132 => 'Invlaid Transaction Amount',
120                       133 => 'Invalid Currency',
121                       998 => 'Unknown Error',
122                       999 => 'Service Unavailable',
123                       1001 => 'No detail returned',
124                    );
125                    
126
127     my %output = ( error => 1001 );
128
129     foreach my $detail (@details) {
130         my ($k, $v) = split('=', $detail);
131         $output{$k} = $v;
132     }
133
134     if ($output{response} == 1 )  {
135         $self->is_success(1);
136         $self->fraud_score($output{score});
137         $self->result_code($output{response});
138         $self->error_message('No Error.  Risk assesment transaction successful');
139     } else {
140         $self->is_success(0);
141         $self->result_code($output{error});
142         $self->error_message(exists $error_map{$output{error}} ? $error_map{$output{error}} :  "preCharge error $output{error} occurred.");
143     }
144 }
145
146
147
148
149 1;
150
151
152
153 =pod
154
155 =head1 NAME 
156
157 Business::FraudDetect::preCharge - backend for Business::FraudDetect (part of Business::OnlinePayment)
158
159 =head1 SYNOPSIS
160
161  use Business::OnlinePayment
162  my $tx = new Business::OnlinePayment ( 'someGateway',
163                                         fruad_detection => 'preCharge',
164                                         maximum_fraud_score => 500,
165                                         preCharge_id => '1000000000000001',
166                                         preCharge_security1 => 'abcdef0123',
167                                         preCharge_security2 => '3210fedcba',
168                                        );
169  $tx->content(  
170     first_name => 'Larry Walton',
171     last_name => 'Sanders',
172     login => 'testdrive',
173     password => '',
174     action => 'Normal Authorization',
175     type => 'VISA',
176     state => 'MA',
177     zip => '02145',
178     country => 'US',
179     phone => '617 555 8900',
180     email => 'lws@sanders.com',
181     ip_address => '18.62.0.6',
182     card_number => '4111111111111111',
183     expiration => '0307',
184     amount => '25.00',
185     );
186  $tx->submit();
187  if ($tx->is_success()) {
188     # successful charge
189  } else {
190     # unsucessful 
191  }
192
193 =head1 DESCRIPTION
194
195 This module provides a driver for the preCharge Risk Management Solutions API Verison 1.7 (16 Jan 2006).
196
197 See L<Business::OnlinePayment> and L<Business::FraudDetect> for more information.  
198
199
200 =head1 CONSTRUCTION
201
202 Whe constructing the Business::OnlinePayment object, three risk management parameters must be included for the preCharge object to be properly constructed.  
203
204 =over 4
205
206 =item * precharge_id
207
208 This field is called "merchant_id" in the preCharge API manual
209
210
211 =item * precharge_security1
212
213 This field is called "security_1" in the preCharge API manual
214
215 =item * precharge_secuirty2
216
217 This field is called "security_2" in the preCharge API manual
218
219 =back
220
221
222 =head1 METHODS
223
224 This module provides no public methods.  
225
226 =head1 AUTHORS
227
228 Lawrence Statton <lawrence@cluon.com>
229
230 =head1 DISCLAIMER
231
232 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
233
234 =head1 SEE ALSO
235
236 http://420.am/business-onlinepayment
237
238 =cut