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     $self->remap_fields(
32         card_number => 'cardnumber',
33         amount      => 'Sum',
34         login       => 'Username',
35         cvv2        => 'cvv',
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           cvv
100         );
101 }
102
103 1;
104
105 __END__
106
107 =head1 NAME
108
109 Business::OnlinePayment::Cardcom - Cardcom backend module for Business::OnlinePayment
110
111 =head1 SYNOPSIS
112
113   use Business::OnlinePayment::Cardcom;
114
115   ####
116   # One step transaction, the simple case.
117   ####
118
119   my $tx = new Business::OnlinePayment("Cardcom");
120   $tx->content(
121       type           => 'CC',
122       login          => 'Cardcom Username',
123       password       => 'Cardcom Password',
124       terminalnumber => 'Cardcom Terminal Number',
125       action         => 'Normal Authorization',
126       amount         => '49.95',
127       card_number    => '4005550000000019',
128       expiration     => '08/06',
129   );
130   $tx->submit();
131
132   if($tx->is_success()) {
133       print "Card processed successfully: ".$tx->authorization."\n";
134   } else {
135       print "Card was rejected: ".$tx->error_message."\n";
136   }
137
138 =head1 SUPPORTED TRANSACTION TYPES
139
140 =head2 CC
141
142 Content required: type, login, password, action, amount, card_number, expiration, terminalnumber.
143
144 =head1 PREREQUISITES
145
146   URI::Escape
147   Tie::IxHash
148
149   Net::SSLeay _or_ ( Crypt::SSLeay and LWP )
150
151 =head1 DESCRIPTION
152
153 For detailed information see L<Business::OnlinePayment>.
154
155 =head1 AUTHOR
156
157 Erik Levinson <levinse@freeside.biz>
158
159 =head1 SEE ALSO
160
161 perl(1). L<Business::OnlinePayment>.
162
163 =cut