initial commit of BOP Cardcom
[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 # -CVV
25 # -Identity number
26 # -Configurable currency
27 # -Configurable deal code
28 # -Refunds or whatever this thing calls them
29 sub submit {
30     my($self) = @_;
31
32     $self->remap_fields(
33         card_number => 'cardnumber',
34         amount      => 'Sum',
35         login       => 'Username',
36     );
37
38     die "invalid action" unless 
39             $self->{_content}{'action'} =~ /^\s*normal\s*authorization\s*$/i;
40
41     $self->{_content}{'expiration'} =~ /^(\d+)\D+\d*(\d{2})$/
42         or croak "unparsable expiration ". $self->{_content}{expiration};
43     my( $month, $year ) = ( $1, $2 );
44     $month = '0'. $month if $month =~ /^\d$/;
45     $self->{_content}{cardvalidityyear} = $year;
46     $self->{_content}{cardvaliditymonth} = $month;
47
48     $self->{_content}{amount} = sprintf('%.2f', $self->{_content}{amount} );
49     $self->{_content}{languages} = 'en';
50     
51     $self->terminalnumber =~ /^\d+$/ or die "invalid TerminalNumber";
52     $self->{_content}{TerminalNumber} = $self->terminalnumber;
53     
54     tie my %fields, 'Tie::IxHash', $self->get_fields( $self->fields );
55     my $post_data =   join('&', map "$_=$fields{$_}", keys %fields );
56     warn "POSTING: ".$post_data if $DEBUG > 1;
57
58     my( $page, $response, @reply_headers) = $self->https_post( $post_data );
59
60     if ($response !~ /^200/)  {
61         # Connection error
62         $response =~ s/[\r\n]+/ /g;  # ensure single line
63         $self->is_success(0);
64         my $diag_message = $response || "connection error";
65         die $diag_message;
66     }
67     
68     $self->server_response($page);
69
70     unless ( $page =~ /^(\d+);(\d+);(.*?)$/ ) {
71        die "unparsable response received from gateway" . 
72             ( $DEBUG ? ": $page" : '' ); 
73     }
74
75     my $result = $1;
76     my $authorization = $2;
77     my $message = $3;
78
79     $self->authorization($authorization);
80     if ( $result == 0 ) {
81         $self->is_success(1);
82     } else {
83         $self->is_success(0);
84         $self->error_message($message);
85     }
86 }
87
88 sub fields {
89         my $self = shift;
90
91         qw(
92           TerminalNumber
93           Sum
94           cardnumber
95           cardvalidityyear
96           cardvaliditymonth
97           Username
98           languages
99         );
100 }
101
102 1;
103
104 __END__
105
106 =head1 NAME
107
108 Business::OnlinePayment::Cardcom - Cardcom backend module for Business::OnlinePayment
109
110 =head1 SYNOPSIS
111
112   use Business::OnlinePayment::Cardcom;
113
114   ####
115   # One step transaction, the simple case.
116   ####
117
118   my $tx = new Business::OnlinePayment("Cardcom");
119   $tx->content(
120       type           => 'CC',
121       login          => 'Cardcom Username',
122       password       => 'Cardcom Password',
123       terminalnumber => 'Cardcom Terminal Number',
124       action         => 'Normal Authorization',
125       amount         => '49.95',
126       card_number    => '4005550000000019',
127       expiration     => '08/06',
128   );
129   $tx->submit();
130
131   if($tx->is_success()) {
132       print "Card processed successfully: ".$tx->authorization."\n";
133   } else {
134       print "Card was rejected: ".$tx->error_message."\n";
135   }
136
137 =head1 SUPPORTED TRANSACTION TYPES
138
139 =head2 CC
140
141 Content required: type, login, password, action, amount, card_number, expiration, terminalnumber.
142
143 =head1 PREREQUISITES
144
145   URI::Escape
146   Tie::IxHash
147
148   Net::SSLeay _or_ ( Crypt::SSLeay and LWP )
149
150 =head1 DESCRIPTION
151
152 For detailed information see L<Business::OnlinePayment>.
153
154 =head1 AUTHOR
155
156 Erik Levinson <levinse@freeside.biz>
157
158 =head1 SEE ALSO
159
160 perl(1). L<Business::OnlinePayment>.
161
162 =cut