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