1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
package Business::OnlinePayment::eWay;
use strict;
use Carp;
use Business::OnlinePayment;
use Business::CreditCard;
use Net::SSLeay qw( post_https );
use XML::Simple;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
require Exporter;
@ISA = qw(Exporter AutoLoader Business::OnlinePayment);
@EXPORT = qw();
@EXPORT_OK = qw();
$VERSION = '0.01';
$DEBUG = 1;
my $default_path = '/gateway/xmlpayment.asp';
my $default_cvn_path = '/gateway_cvn/xmlpayment.asp';
my $default_test_path = '/gateway/xmltest/testpage.asp';
my $default_cvn_test_path = '/gateway_cvn/xmltest/testpage.asp';
sub set_defaults {
my $self = shift;
$self->server('www.eway.com.au');
$self->port('443');
$self->path($default_path);
}
sub revmap_fields {
my($self,%map) = @_;
my %content = $self->content();
foreach(keys %map) {
$content{$_} = ref($map{$_})
? ${ $map{$_} }
: (exists($content{$map{$_}})
? $content{$map{$_}}
: ''
);
}
$self->content(%content);
}
sub get_fields {
my($self,@fields) = @_;
my %content = $self->content();
my %new = ();
foreach( grep defined $content{$_}, @fields) { $new{$_} = $content{$_}; }
return %new;
}
sub submit {
my($self) = @_;
my %content = $self->content;
my $action = lc($content{'action'});
die 'eSec only supports "Normal Authorization" transactions'
unless $action eq 'normal authorization';
$content{'expiration'} =~ /^(\d+)\D+(\d+)$/
or croak "unparsable expiration $content{expiration}";
my ($month, $year) = ( $1, $2 );
$month += 0;
$year %= 100; #not y2.1k safe
$content{'amount'} =~ /^(\d+\.+\d{0,2})$/
or croak "unparsable amount $content{amount}";
my $amount = $1*100;
my $address = $content{'address'} if exists $content{'address'};
$address .= $content{'city'} if exists $content{'city'};
$address .= $content{'state'} if exists $content{'state'};
$address .= '';
$self->revmap_fields(
ewayCustomerID => 'login',
ewayTotalAmount => \$amount,
ewayCustomerFirstName => 'first_name',
ewayCustomerLastName => 'last_name',
ewayCustomerEmail => 'email',
ewayCustomerAddress => \$address,
ewayCustomerPostcode => 'zip',
ewayCustomerInvoiceDescription => 'description',
ewayCustomerInvoiceRef => 'invoice_number',
ewayCardHoldersName => 'name',
ewayCardNumber => 'card_number',
ewayCardExpiryMonth => \$month,
ewayCardExpiryYear => \$year,
ewayTrxnNumber => 'transaction_number',
ewayOption1 => 'option1',
ewayOption2 => 'option1',
ewayOption3 => 'option1',
ewayCVN => 'cvv2',
);
%content = $self->content;
if ( $DEBUG ) {
warn "content:$_ => $content{$_}\n" foreach keys %content;
}
if ($self->transaction_type() eq 'CC' ) {
$self->required_fields(qw/action login amount name card_number expiration/);
} else {
croak("eWay can't handle transaction type: ".
$self->transaction_type());
}
my %post_data = $self->get_fields( map "$_", qw(
ewayCustomerID ewayTotalAmount ewayCustomerFirstName ewayCustomerLastName
ewayCustomerEmail ewayCustomerAddress ewayCustomerPostcode
ewayCustomerInvoiceDescription ewayCustomerInvoiceRef ewayCardHoldersName
ewayCardNumber ewayCardExpiryMonth ewayCardExpiryYear ewayTrxnNumber
ewayOption1 ewayOption2 ewayOption3 ewayCVN
) );
if ( $DEBUG ) {
warn "post_data:$_ => $post_data{$_}\n" foreach keys %post_data;
}
my $pd = XMLout({%post_data}, 'NoAttr' => 1, 'RootName' => 'ewaygateway');
if ( $DEBUG ) {
warn $pd,"\n";
}
my $server = $self->server();
my $port = $self->port();
my $path = $self->path();
if ($post_data{ewayCVN} && $path eq $default_path){
$path = $default_cvn_path;
}
if ($self->test_transaction) {
if ($path eq $default_path) {
$path = $default_test_path;
}elsif ($path eq $default_cvn_path){
$path = $default_cvn_test_path;
}else{
croak "Test mode not supported for path: $path\n";
}
}
my($page,$server_response) =
post_https($server,$port,$path,'',$pd);
if ( $DEBUG ) {
warn $page,"\n";
}
my $response;
if ($server_response =~ /HTTP\/1\.1 200 OK/){
$response = XMLin($page);
}else{
$response->{ewayTrxnError} = $server_response;
}
if ( $response->{ewayTrxnStatus} =~ /^True/ ) {
$self->is_success(1);
$self->authorization($response->{ewayAuthCode});
} else {
$self->is_success(0);
}
$self->error_message($response->{ewayTrxnError});
$self->server_response($response);
}
1;
__END__
=head1 NAME
Business::OnlinePayment::eWay - eWay backend for Business::OnlinePayment
=head1 SYNOPSIS
use Business::OnlinePayment;
my $tx = new Business::OnlinePayment("eWay");
$tx->content(
login => '87654321', #ewayCustomerID
action => 'Normal Authorization',
description => 'Business::OnlinePayment test',
amount => '49.95',
invoice_number => '100100',
name => 'Tofu Beast',
card_number => '46464646464646',
expiration => '11/08',
);
$tx->submit();
if($tx->is_success()) {
print "Card processed successfully: ".$tx->authorization."\n";
} else {
print "Card was rejected: ".$tx->error_message."\n";
}
=head1 DESCRIPTION
For detailed information see L<Business::OnlinePayment>.
=head1 NOTE
=head1 COMPATIBILITY
This module implements eWay's XML API. See
http://www.eway.com.au/support/xml.aspx for details.
=head1 AUTHOR
Jeff Finucane <jeff@cmh.net>
=head1 SEE ALSO
perl(1). L<Business::OnlinePayment>.
=cut
|