initial release
[Business-OnlineThirdPartyPayment-eWayShared.git] / eWayShared.pm
1 package Business::OnlineThirdPartyPayment::eWayShared;
2
3 use strict;
4 use Business::OnlineThirdPartyPayment 3;
5 use vars qw($VERSION @ISA $DEBUG);
6
7 @ISA = qw(Business::OnlineThirdPartyPayment);
8 $VERSION = '0.01';
9
10 $DEBUG = 0;
11
12 sub set_defaults {
13   my $self = shift;
14
15   $self->server('www.eway.com.au') unless $self->server;
16   $self->port('443') unless $self->port;
17   $self->path('/gateway/payment.asp') unless $self->path;
18   $self->build_subs(qw(authorization order_number result_code error_message));
19 }
20
21 sub use_3dsecure {
22   my $self = shift;
23   $self->{'use_3dsecure'} = shift if ( @_ );
24   $self->path('/gateway_3d/payment.asp') if $self->{'use_3dsecure'};
25   return $self->{'use_3dsecure'};
26 }
27
28 sub reference {
29   # Situation: We've been POSTed back from the gateway's web site.  The 
30   # POST data is in the argument.  We need to set the state of the object
31   # and then return the reference number.
32   my ($self, $data) = @_;
33   my $status  = $data->{'ewayTrxnStatus'};
34   $self->order_number($data->{'ewayTrxnReference'});
35   if ( lc($status) eq 'true' ) { 
36     $self->is_success(1);
37     $self->authorization($data->{'eWAYAuthCode'}); 
38     # not spelled like this in the spec
39   }
40   else {
41     $self->is_success(0);
42     $self->result_code($data->{'eWAYresponsecode'});
43     $self->error_message($data->{'eWAYresponseText'});
44   }
45   return $data->{'ewayTrxnNumber'}; 
46 }
47
48 sub submit {
49   # One of two situations:
50   # "authorization only": We haven't sent anything yet and are just 
51   # creating a pending transaction locally.  Set popup_url and remap 
52   # transaction fields into collectitems.
53   # OR
54   # "post authorization: We've already called reference() with the 
55   # callback data.
56   my($self) = @_;
57   my %content = $self->content;
58
59   my $action = lc($content{'action'});
60   die 'Third Party Payment supports "Authorization Only" and '.
61   '"Post Authorization" transactions'
62   if !($action eq 'authorization only' || $action eq 'post authorization');
63   die 'eWay only supports credit card transactions' 
64   if $self->transaction_type() ne 'CC';
65
66   my @required = qw( amount reference );
67   $self->required_fields(@required);
68
69   if ( $action eq 'authorization only' ) {
70     $self->is_success(1);
71     my $url =
72     "https://". $self->server().
73     ($self->port != 443 ? ':'. $self->port() : ''). $self->path();
74     $self->popup_url( $url );
75
76     my %fields = (
77       'login'           => 'ewayCustomerID',
78       'amount'          => 'ewayTotalAmount',
79       'first_name'      => 'ewayCustomerFirstName',
80       'last_name'       => 'ewayCustomerLastName',
81       'email'           => 'ewayCustomerEmail',
82       'address'         => 'ewayCustomerAddress',
83       'zip'             => 'ewayCustomerPostcode',
84       'description'     => 'ewayCustomerInvoiceDescription',
85       'invoice_number'  => 'ewayCustomerInvoiceRef',
86       'reference'       => 'ewayTrxnNumber',
87       'callback_url'    => 'eWAYURL',
88     );
89     $self->required_fields(qw(login amount first_name last_name));
90     $self->remap_fields(%fields);
91     my %content = $self->content;
92     $content{'ewayTotalAmount'} *= 100;
93     $self->collectitems([ map { $_ => $content{$_} } values(%fields) ]);
94     $self->is_success(1);
95     return;
96   }
97   elsif ( $action eq 'post authorization' ) {
98     return; # everything has been set up already
99   }
100 }
101
102 1;
103 __END__
104
105 =head1 NAME
106
107 Business::OnlineThirdPartyPayment::eWayShared - eWay backend for Business::OnlineThirdPartyPayment
108
109 =head1 DESCRIPTION
110
111 For detailed information see L<Business::OnlineThirdPartyPayment>.
112
113 Call "$transaction->use_3dsecure(1)" (or pass "'use_3dsecure' => 1"
114 to I<new()>) to enable the 3D-Secure gateway.
115
116 =head1 NOTE
117
118 =head1 COMPATIBILITY
119
120 eWay Shared Payments and 3D-Secure API, as of December 2010.
121
122 =head1 AUTHOR
123
124 Mark Wells <mark@freeside.biz>
125
126 Based on Business::OnlineThirdPartyPayment::Interswitchng by 
127 Jeff Finucane <interswitchng@weasellips.com>
128
129 =head1 SEE ALSO
130
131 perl(1). L<Business::OnlineThirdPartyPayment>.
132
133 =cut
134