BOP Cardcom, RT13058
[Business-OnlinePayment-Cardcom.git] / Cardcom.pm
1 package Business::OnlinePayment::Cardcom;
2
3 use strict;
4 use Carp;
5 use Tie::IxHash;
6 use Business::OnlinePayment 3;
7 use Business::OnlinePayment::HTTPS 0.03;
8 use Data::Dumper;
9 use vars qw($VERSION $DEBUG @ISA);
10
11 @ISA = qw(Business::OnlinePayment::HTTPS);
12 $VERSION = '0.01';
13 $DEBUG = 0;
14
15 sub set_defaults {
16     my $self = shift;
17
18     $self->server('secure.cardcom.co.il'); 
19     $self->path('/BillGoldPost.aspx');
20     $self->port('443');
21 }
22
23 # XXX?
24 # -Identity number
25 # -Configurable currency
26 # -Configurable deal code
27 # -Refunds or whatever this thing calls them
28 sub submit {
29     my($self) = @_;
30
31     warn Dumper($self) if $DEBUG > 1;
32
33     $self->remap_fields(
34         card_number => 'cardnumber',
35         amount      => 'Sum',
36         login       => 'Username',
37         password    => 'userpassword',
38         cvv2        => 'cvv',
39     );
40
41     die "invalid action" unless 
42             $self->{_content}{'action'} =~ /^\s*normal\s*authorization\s*$/i;
43
44     $self->{_content}{'expiration'} =~ /^(\d+)\D+\d*(\d{2})$/
45         or croak "unparsable expiration ". $self->{_content}{expiration};
46     my( $month, $year ) = ( $1, $2 );
47     $month = '0'. $month if $month =~ /^\d$/;
48     $self->{_content}{cardvalidityyear} = $year;
49     $self->{_content}{cardvaliditymonth} = $month;
50
51     $self->{_content}{amount} = sprintf('%.2f', $self->{_content}{amount} );
52     $self->{_content}{languages} = 'en';
53     
54     $self->terminalnumber =~ /^\d+$/ or die "invalid TerminalNumber";
55     $self->{_content}{TerminalNumber} = $self->terminalnumber;
56     
57     $self->required_fields(
58         qw( login password TerminalNumber card_number amount )
59     );
60     
61     if($self->test_transaction) {
62         $self->{_content}{'Username'} = 'gali';
63         $self->{_content}{'userpassword'} = '7654321';
64         $self->{_content}{'TerminalNumber'} = '1000';
65     }
66       
67     tie my %fields, 'Tie::IxHash', $self->get_fields( $self->fields );
68     my $post_data =   join('&', map "$_=$fields{$_}", keys %fields );
69     warn "POSTING: ".$post_data if $DEBUG > 1;
70
71     my( $page, $response, @reply_headers) = $self->https_post( $post_data );
72
73     if ($response !~ /^200/)  {
74         # Connection error
75         $response =~ s/[\r\n]+/ /g;  # ensure single line
76         $self->is_success(0);
77         my $diag_message = $response || "connection error";
78         die $diag_message;
79     }
80     
81     $self->server_response($page);
82
83     unless ( $page =~ /^(\d+);(\d+);(.*?)$/ ) {
84        die "unparsable response received from gateway" . 
85             ( $DEBUG ? ": $page" : '' ); 
86     }
87
88     my $result = $1;
89     my $authorization = $2;
90     my $message = $3;
91
92     $self->result_code($result);
93     if ( $result == 0 ) {
94         $self->is_success(1);
95         $self->authorization($authorization);
96     } else {
97         $self->is_success(0);
98         $self->error_message($message);
99     }
100 }
101
102 sub fields {
103         my $self = shift;
104
105         qw(
106           TerminalNumber
107           Sum
108           cardnumber
109           cardvalidityyear
110           cardvaliditymonth
111           Username
112           userpassword
113           languages
114           cvv
115         );
116 }
117
118 sub _info {
119    {
120     'info_compat'       => '0.01',
121     'gateway_name'      => 'Cardcom',
122     'gateway_url'       => 'http://www.cardcom.co.il',
123     'module_version'    => $VERSION,
124     'support_types'     => [ 'CC' ],
125     'token_support'     => 0, # well technically the gateway supports it, but we haven't implemented it
126     'test_transaction'  => 1, 
127     'supported_actions' => [  # XXX refunds
128                             'Normal Authorization',
129                            ],
130    };
131 }
132
133 1;
134
135 __END__
136
137 =head1 NAME
138
139 Business::OnlinePayment::Cardcom - Cardcom backend module for Business::OnlinePayment
140
141 =head1 SYNOPSIS
142
143   use Business::OnlinePayment::Cardcom;
144
145   ####
146   # One step transaction, the simple case.
147   ####
148
149   my $tx = new Business::OnlinePayment("Cardcom");
150   $tx->content(
151       type           => 'CC',
152       login          => 'Cardcom Username',
153       password       => 'Cardcom Password',
154       terminalnumber => 'Cardcom Terminal Number',
155       action         => 'Normal Authorization',
156       amount         => '49.95',
157       card_number    => '4005550000000019',
158       expiration     => '08/06',
159       cvv2           => '123',
160   );
161   $tx->submit();
162
163   if($tx->is_success()) {
164       print "Card processed successfully: ".$tx->authorization."\n";
165   } else {
166       print "Card was rejected: ".$tx->error_message."\n";
167   }
168
169 =head1 SUPPORTED TRANSACTION TYPES
170
171 =head2 CC
172
173 Content required: type, login, password, action, amount, card_number, expiration, terminalnumber.
174
175 =head1 PREREQUISITES
176
177   Tie::IxHash
178
179 =head1 DESCRIPTION
180
181 For detailed information see L<Business::OnlinePayment>.
182
183 =head1 AUTHOR
184
185 Erik Levinson <levinse@freeside.biz>
186
187 =head1 SEE ALSO
188
189 perl(1). L<Business::OnlinePayment>.
190
191 =cut