File Coverage

blib/lib/Business/Eway.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Business::Eway;
2            
3 2     2   67916 use warnings;
  2         6  
  2         78  
4 2     2   50 use strict;
  2         5  
  2         86  
5 2     2   12 use Carp qw/croak/;
  2         10  
  2         193  
6 2     2   1918 use URI::Escape qw/uri_escape/;
  2         6060  
  2         133  
7 2     2   2745 use LWP::UserAgent;
  2         155915  
  2         86  
8 2     2   2536 use XML::Simple qw/XMLin/;
  0            
  0            
9            
10             our $VERSION = '0.03';
11            
12             sub new {
13             my $class = shift;
14             my $args = scalar @_ % 2 ? shift : { @_ };
15            
16             # validate
17             ($args->{CustomerID} =~ /^\d+$/) or croak 'CustomerID is required';
18             $args->{UserName} or croak 'UserName is required';
19            
20             $args->{RequestURL} ||= 'https://payment.ewaygateway.com/Request';
21             $args->{ResultURL} ||= 'https://payment.ewaygateway.com/Result';
22            
23             unless ( $args->{ua} ) {
24             my $ua_args = delete $args->{ua_args} || {};
25             $args->{ua} = LWP::UserAgent->new(%$ua_args);
26             }
27            
28             bless $args, $class;
29             }
30            
31             sub request {
32             my $self = shift;
33             my $args = scalar @_ % 2 ? shift : { @_ };
34            
35             my $url = $self->request_url($args);
36             my $resp = $self->{ua}->get($url);
37             unless ($resp->is_success) {
38             croak $resp->status_line;
39             }
40             my $content = $resp->content;
41             my $rtn = XMLin($content, SuppressEmpty => undef);
42             if (wantarray) {
43             if ( $rtn->{Result} eq 'True' ) {
44             return (1, $rtn->{URI});
45             } else {
46             return (0, $rtn->{Error});
47             }
48             } else {
49             return $rtn;
50             }
51             }
52            
53             sub request_url {
54             my $self = shift;
55             my $args = scalar @_ % 2 ? shift : { @_ };
56            
57             # validate
58             my $Amount = $args->{Amount} || $self->{Amount} || croak 'Amount is required';
59             $Amount = sprintf ("%.2f", $Amount); # .XX format
60             my $Currency = $args->{Currency} || $self->{Currency} || croak 'Currency is required';
61             my $CancelURL = $args->{CancelURL} || $self->{CancelURL} || croak 'CancelURL is required';
62             my $ReturnUrl = $args->{ReturnUrl} || $self->{ReturnUrl} || croak 'ReturnUrl is required';
63            
64             # ReturnUrl can't contain '?'
65             if ( $ReturnUrl =~ /\?/ ) {
66             croak "ReturnUrl can't contain '?' inside, use MerchantOption1, MerchantOption2, MerchantOption3 instead\n";
67             }
68            
69             my $url = sprintf("$self->{RequestURL}?CustomerID=$self->{CustomerID}&UserName=$self->{UserName}&Amount=$Amount&Currency=$Currency&CancelURL=%s&ReturnUrl=%s",
70             uri_escape($CancelURL), uri_escape($ReturnUrl)
71             );;
72            
73             # other args
74             foreach my $k ( 'PageTitle', 'PageDescription', 'PageFooter',
75             'Language', 'CompanyName', 'CompanyLogo', 'PageBanner',
76             'CustomerFirstName', 'CustomerLastName', 'CustomerAddress', 'CustomerCity',
77             'CustomerState', 'CustomerPostCode', 'CustomerCountry', 'CustomerPhone',
78             'CustomerEmail', 'InvoiceDescription', 'MerchantReference', 'MerchantInvoice',
79             'MerchantOption1', 'MerchantOption2', 'MerchantOption3'
80             ) {
81             my $val = $args->{$k} || $self->{$k};
82             if ( defined $val ) {
83             $url .= sprintf("&$k=%s", uri_escape($val));
84             } else {
85             $url .= "&$k=";
86             }
87             }
88             foreach my $k ( 'UseAVS', 'UseZIP', 'ModifiableCustomerDetails' ) {
89             my $val = $args->{$k} || $self->{$k};
90             $val = ( not $val or $val =~ /false/i) ? 'false' : 'true';
91             $url .= "&$k=$val";
92             }
93            
94             return $url;
95             }
96            
97             sub result {
98             my ( $self, $AccessPaymentCode ) = @_;
99            
100             my $url = $self->result_url($AccessPaymentCode);
101             my $resp = $self->{ua}->get($url);
102             unless ($resp->is_success) {
103             croak $resp->status_line;
104             }
105             my $content = $resp->content;
106             return XMLin($content, SuppressEmpty => undef);
107             }
108            
109             sub result_url {
110             my ( $self, $AccessPaymentCode ) = @_;
111             return "https://payment.ewaygateway.com/Result?CustomerID=$self->{CustomerID}&UserName=$self->{UserName}&AccessPaymentCode=$AccessPaymentCode";
112             }
113            
114             1;
115             __END__