Interchange6/Business/OnlinePayment.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 77 CC 5 SCAR 28.7
CC
5
Cov
0%
charge
30.0
BUILDARGS
2.0
BEGIN
2.0
CRAP 17.6
1 package Dancer::Plugin::Interchange6::Business::OnlinePayment;
2  
3 use Business::OnlinePayment 3.02;
4 use Dancer ':syntax';
5 use Moo;
6  
7 =head1 NAME
8  
9 Dancer::Plugin::Interchange6::Business::OnlinePayment - Interchange6 wrapper for Business:OnlinePayment
10  
11 =head1 CONFIGURATION
12  
13 Configuration for AuthorizeNet provider:
14  
15 plugins:
16 Interchange6:
17 Payment:
18 default_provider: AuthorizeNet
19 providers:
20 AuthorizeNet:
21 login: <API Login ID>
22 password: <Transaction Key>
23  
24 If you use a test account, please add the following
25 parameters:
26  
27 test_transaction: 1
28 server: test.authorize.net
29  
30 =head1 ATTRIBUTES
31  
32 =head2 provider
33  
34 Payment provider.
35  
36 =cut
37  
38 has provider => (
39 is => 'rwp',
40 );
41  
42 =head2 provider_args
43  
44 Payment provider settings, like login and password.
45  
46 =cut.
47  
48 has provider_args => (
49 is => 'rwp',
50 );
51  
52 =head2 payment_order
53  
54 Payment order object.
55  
56 =cut
57  
58 has payment_order => (
59 is => 'rw',
60 );
61  
62 =head2 is_success
63  
64 True if the payment was successful, false otherwise.
65  
66 =cut
67  
68 has is_success => (
69 is => 'rwp',
70 );
71  
72 =head2 authorization
73  
74 Returns authorization code from provider after a successful
75 payment.
76  
77 =cut
78  
79 has authorization => (
80 is => 'rwp',
81 );
82  
83 =head2 order_number
84  
85 Returns unique order number from provider after a successful
86 payment.
87  
88 =cut
89  
90 has order_number => (
91 is => 'rwp',
92 );
93  
94 =head2 error_code
95  
96 Returns error code in case of payment failure.
97  
98 =cut
99  
100 has error_code => (
101 is => 'rwp',
102 );
103  
104 =head2 error_message
105  
106 Returns error message in case of payment failure.
107  
108 =cut
109  
110 has error_message => (
111 is => 'rwp',
112 );
113  
114 sub BUILDARGS {
115 my ( $class, @args ) = @_;
116 my ( %params );
117  
118 # first argument is the provider
119 $params{provider} = shift @args;
120 $params{provider_args} = {@args};
121  
122 return \%params;
123 }
124  
125 =head1 METHODS
126  
127 =head2 charge
128  
129 Performs charge transaction with payment provider.
130  
131 =cut
132  
133 sub charge {
134 my ( $self, %args ) = @_;
135 my ( $provider_settings, $bop_object );
136  
137 # reset values
138 $self->_set_is_success(0);
139 $self->_set_authorization('');
140 $self->_set_order_number('');
141 $self->_set_error_code('');
142 $self->_set_error_message('');
143  
144 $provider_settings = $self->provider_args;
145  
146 $bop_object = Business::OnlinePayment->new($self->provider, %$provider_settings);
147  
148 if ($provider_settings->{server}) {
149 $bop_object->server( $provider_settings->{server} );
150 }
151  
152 # Sofortbanking expects amount as xx.xx
153 $args{amount} = sprintf( '%.2f', $args{amount} );
154  
155 $bop_object->content(
156 %$provider_settings,
157 amount => $args{amount},
158 card_number => $args{card_number},
159 expiration => $args{expiration},
160 cvc => $args{cvc},
161 first_name => $args{first_name},
162 last_name => $args{last_name},
163 login => $provider_settings->{login},
164 password => $provider_settings->{password},
165 type => $args{type} || $provider_settings->{type} || 'CC',
166 action => $args{action} || $provider_settings->{action} || 'Authorization Only',
167 );
168  
169 eval { $bop_object->submit(); };
170  
171 if ($@) {
172 die "Payment with provider ", $self->{provider}, " failed: ", $@;
173 }
174  
175 if ( $bop_object->is_success() ) {
176 $self->_set_is_success(1);
177  
178 if ( $bop_object->can('popup_url') ) {
179 debug( "Success! Redirect browser to " . $bop_object->popup_url() );
180 }
181 else {
182 debug("Successful payment, authorization: ",
183 $bop_object->authorization);
184 debug("Order number: ", $bop_object->order_number);
185 $self->_set_authorization($bop_object->authorization);
186 $self->_set_order_number($bop_object->order_number);
187 }
188 }
189 else {
190 debug( 'Card was rejected by ', $self->provider, ': ' , $bop_object->error_message );
191 $self->_set_error_code($bop_object->result_code);
192 $self->_set_error_message($bop_object->error_message);
193 return;
194 }
195 }
196  
197 =head1 AUTHOR
198  
199 Stefan Hornburg (Racke), <racke@linuxia.de>
200  
201 =head1 LICENSE AND COPYRIGHT
202  
203 Copyright 2012-2015 Stefan Hornburg (Racke) <racke@linuxia.de>.
204  
205 This program is free software; you can redistribute it and/or modify it
206 under the terms of either: the GNU General Public License as published
207 by the Free Software Foundation; or the Artistic License.
208  
209 See http://dev.perl.org/licenses/ for more information.
210  
211 =cut
212  
213 1;