New module for preCharge
[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 set_defaults {
15     my ($self) = @_;
16     $self->server('api.precharge.net');
17     $self->port(443);
18     $self->path('/charge');
19     $self->build_subs(qw /currency risk_level error_code
20                       precharge_id precharge_security1 precharge_security2 force_success / );
21     $self->currency('USD');
22     return $self;
23 }
24
25 sub submit {
26     my ($self) = @_;
27     if ($self->force_success()) {
28         $self->is_success(1);
29         $self->risk_level(100);
30         $self->result_code('1');
31         $self->error_message('No Error.  Force success path');
32         return $self;
33     }
34     my %content = $self->content();
35     Carp::croak("Action: $content{action} not supported.") unless
36         lc($content{action}) eq 'fraud detect';
37     
38     $self->required_fields(qw(
39                               amount card_number expiration
40                               first_name last_name state zip country phone email
41                               ip_address
42                               ));
43
44     $self->remap_fields( qw /
45                          ip_address             ecom_billto_online_ip 
46                          zip                    ecom_billto_postal_code 
47                          phone                  ecom_billto_telecom_phone_number 
48                          first_name             ecom_billto_postal_name_first
49                          last_name              ecom_billto_postal_name_last
50                          email                  ecom_billto_online_email 
51                          amount                 ecom_transaction_amount  /
52                          );
53
54
55     my %post_data = $self->get_fields(qw ( ecom_billto_online_ip ecom_billto_postal_code
56                                            ecom_billto_telecom_phone_number ecom_billto_online_email
57                                            ecom_transaction_amount currency
58                                            ));
59
60     # set up some reasonable defaults
61
62     #
63     # split out MM/YY from exp date
64     #
65
66     @post_data{qw/ecom_payment_card_expdate_month ecom_payment_card_expdate_year/} = $content{expiration} =~ m/(\d{1,2})(\d{2})/;
67     @post_data{qw/merchant_id security_1 security_2/} = ($self->precharge_id,
68                                                          $self->precharge_security1,
69                                                          $self->precharge_security2);
70
71     if ($self->test_transaction()) {
72         $post_data{test} = 1;
73     }
74
75 #    warn Dumper \%post_data;
76     my ($page, $response, %headers) = $self->https_post(\%post_data);
77
78     $self->server_response($page);
79
80     my @details = split ',',$page;
81
82     my %error_map = ( 101 => 'Invalid Request Method',
83                       102 => 'Invalid Request URL',
84                       103 => 'Invalid Security Code(s)',
85                       104 => 'Merchant Status not Verified',
86                       105 => 'Merchant Feed is Disabled',
87                       106 => 'Invalid Request Type',
88                       107 => 'Missing IP Address',
89                       108 => 'Invalid IP Address Syntax',
90                       109 => 'Missing First Name',
91                       110 => 'Invalid First Name',
92                       111 => 'Missing Last Name',
93                       112 => 'Invalid Last Name',
94                       113 => 'Invalid Address 1',
95                       114 => 'Invalid Address 2',
96                       115 => 'Invalid City',
97                       116 => 'Invalid State',
98                       117 => 'Invalid Country',
99                       118 => 'Missing Postal Code',
100                       119 => 'Invalid Postal Code',
101                       120 => 'Missing Phone Number',
102                       121 => 'Invalid Phone Number',
103                       122 => 'Missing Expiration Month',
104                       123 => 'Invalid Expiration Month',
105                       124 => 'Missing Expiration Year',
106                       125 => 'Invalid Expiration Year',
107                       126 => 'Expired Credit Card',
108                       127 => 'Missing Credit Card Number',
109                       128 => 'Invalid Credit Card Number',
110                       129 => 'Missing Email Address',
111                       130 => 'Invlaid Email Syntax',
112                       131 => 'Duplicate Transaction',
113                       132 => 'Invlaid Transaction Amount',
114                       133 => 'Invalid Currency',
115                       998 => 'Unknown Error',
116                       999 => 'Service Unavailable',
117                       1001 => 'No detail returned',
118                    );
119                    
120
121     my %output = ( error => 1001 );
122
123     foreach my $detail (@details) {
124         my ($k, $v) = split('=', $detail);
125         $output{$k} = $v;
126     }
127
128     if ($output{response} == 1 )  {
129         $self->is_success(1);
130         $self->risk_level($output{score});
131         $self->result_code($output{response});
132         $self->error_message('No Error.  Risk assesment transaction successful');
133     } else {
134         $self->is_success(0);
135         $self->result_code($output{error});
136         $self->error_message(exists $error_map{$output{error}} ? $error_map{$output{error}} :  "preCharge error $output{error} occurred.");
137     }
138 }
139
140 1;
141
142