File Coverage

blib/lib/Dancer2/Plugin/Interchange6/Business/OnlinePayment.pm
Criterion Covered Total %
statement 43 43 100.0
branch 8 8 100.0
condition 4 4 100.0
subroutine 5 5 100.0
pod 1 2 50.0
total 61 62 98.3


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Interchange6::Business::OnlinePayment;
2              
3 4     4   471299 use Business::OnlinePayment 3.02;
  4         9204  
  4         89  
4 4     4   442 use Dancer2 ':syntax';
  4         223132  
  4         21  
5 4     4   126075 use Moo;
  4         7  
  4         22  
6              
7             =head1 NAME
8              
9             Dancer2::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 8     8 0 4923 my ( $class, @args ) = @_;
116 8         10 my ( %params );
117              
118             # first argument is the provider
119 8         18 $params{provider} = shift @args;
120 8         20 $params{provider_args} = {@args};
121              
122 8         112 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 8     8 1 1201 my ( $self, %args ) = @_;
135 8         8 my ( $provider_settings, $bop_object );
136              
137             # reset values
138 8         22 $self->_set_is_success(0);
139 8         14 $self->_set_authorization('');
140 8         14 $self->_set_order_number('');
141 8         15 $self->_set_error_code('');
142 8         13 $self->_set_error_message('');
143              
144 8         15 $provider_settings = $self->provider_args;
145              
146 8         48 $bop_object = Business::OnlinePayment->new($self->provider, %$provider_settings);
147              
148 8 100       4272 if ($provider_settings->{server}) {
149 1         16 $bop_object->server( $provider_settings->{server} );
150             }
151              
152             # Sofortbanking expects amount as xx.xx
153 8         87 $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 8   100     149 action => $args{action} || $provider_settings->{action} || 'Authorization Only',
      100        
167             );
168              
169 8         405 eval { $bop_object->submit(); };
  8         18  
170              
171 8 100       671 if ($@) {
172 2         27 die "Payment with provider ", $self->{provider}, " failed: ", $@;
173             }
174              
175 6 100       86 if ( $bop_object->is_success() ) {
176 4         25 $self->_set_is_success(1);
177              
178 4 100       20 if ( $bop_object->can('popup_url') ) {
179 1         3 debug( "Success! Redirect browser to " . $bop_object->popup_url() );
180             }
181             else {
182 3         47 debug("Successful payment, authorization: ",
183             $bop_object->authorization);
184 3         2149 debug("Order number: ", $bop_object->order_number);
185 3         1125 $self->_set_authorization($bop_object->authorization);
186 3         57 $self->_set_order_number($bop_object->order_number);
187             }
188             }
189             else {
190 2         41 debug( 'Card was rejected by ', $self->provider, ': ' , $bop_object->error_message );
191 2         34872 $self->_set_error_code($bop_object->failure_status);
192 2         45 $self->_set_error_message($bop_object->error_message);
193 2         39 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-2016 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;