File Coverage

blib/lib/Business/OnlinePayment/Braintree.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Business::OnlinePayment::Braintree;
2              
3 1     1   15432 use 5.006;
  1         2  
  1         30  
4 1     1   5 use strict;
  1         1  
  1         23  
5 1     1   7 use warnings;
  1         4  
  1         27  
6              
7 1     1   460 use Business::OnlinePayment 3.01;
  1         2300  
  1         22  
8 1     1   179 use Net::Braintree;
  0            
  0            
9              
10             use base 'Business::OnlinePayment';
11              
12             =head1 NAME
13              
14             Business::OnlinePayment::Braintree - Online payment processing through Braintree
15              
16             =head1 VERSION
17              
18             Version 0.010
19              
20             =cut
21              
22             our $VERSION = '0.010';
23              
24             =head1 SYNOPSIS
25              
26             use Business::OnlinePayment;
27              
28             $tx = new Business::OnlinePayment('Braintree',
29             merchant_id => 'your merchant id',
30             public_key => 'your public key',
31             private_key => 'your private key',
32             );
33              
34             $tx->test_transaction(1); # sandbox transaction for development and tests
35            
36             $tx->content(amount => 100,
37             card_number => '4111 1111 1111 1111',
38             expiration => '1212');
39              
40             $tx->submit();
41              
42             if ($tx->is_success) {
43             print "Card processed successfully: " . $tx->authorization . "\n";
44             } else {
45             print "Card was rejected: " . $tx->error_message . "\n";
46             }
47              
48             =head1 DESCRIPTION
49              
50             Online payment processing through Braintree based on L<Net::Braintree>.
51              
52             The documentation for L<Net::Braintree> is located at
53             L<https://www.braintreepayments.com/docs/perl>.
54              
55             =head1 NOTES
56              
57             This is supposed to cover the complete Braintree Perl API finally.
58              
59             =head1 METHODS
60              
61             =head2 submit
62              
63             Submits transaction to Braintree gateway.
64              
65             =cut
66              
67             sub submit {
68             my $self = shift;
69             my $config = Net::Braintree->configuration;
70             my %content = $self->content;
71             my ($action, $result, $transaction, $result_code);
72              
73             # sandbox vs production
74             if ($self->test_transaction) {
75             $config->environment('sandbox');
76             }
77             else {
78             $config->environment('production');
79             }
80              
81             # transaction
82             $action = lc($content{action});
83              
84             if ($action eq 'normal authorization' ) {
85             $result = $self->sale(1);
86             }
87             elsif ($action eq 'authorization only') {
88             $result = $self->sale(0);
89             }
90             elsif ($action eq 'post authorization') {
91             $result = Net::Braintree::Transaction->submit_for_settlement($content{order_number}, $content{amount});
92             }
93             elsif ($action eq 'credit' ) {
94             $result = Net::Braintree::Transaction->refund($content{order_number}, $content{amount});
95             }
96             else {
97             $self->error_message( "unsupported action for Braintree: $content{action}" );
98             return 0;
99             }
100              
101             my %result_codes = (
102             2000 => 'declined',
103             2001 => 'nsf',
104             2002 => 'nsf',
105             2003 => 'nsf',
106             2010 => 'declined',
107             2012 => 'declined',
108             2013 => 'declined',
109             2014 => 'declined',
110             2022 => 'declined',
111             2038 => 'declined',
112             2041 => 'declined',
113             2044 => 'declined',
114             2046 => 'declined',
115             2047 => 'pickup',
116             2053 => 'stolen',
117             );
118              
119             $transaction = $result->transaction;
120              
121             if ( $transaction ) {
122             $result_code = $transaction->processor_response_code;
123              
124             $self->result_code($result_code);
125             $self->order_number($transaction->id);
126             }
127              
128             if ($result->is_success()) {
129             $self->is_success(1);
130             $self->authorization($transaction->id);
131             }
132             else {
133             $self->is_success(0);
134             $self->error_message($result->message);
135             $self->failure_status($result_codes{$result_code})
136             if ( $result_code && $result_codes{$result_code} );
137             }
138             }
139              
140             =head2 sale $submit
141              
142             Performs sale transaction with Braintree. Used both
143             for settlement ($submit is a true value) and
144             authorization ($submit is a false value).
145              
146             =cut
147              
148             sub sale {
149             my ($self, $submit) = @_;
150             my %content = $self->content;
151              
152             # get rid of slash inside expiration value
153             $content{expiration} =~ s%/%%;
154              
155             my %args = (
156             amount => $content{amount},
157             order_id => $content{invoice_number},
158             credit_card => {
159             number => $content{card_number},
160             expiration_month => substr($content{expiration},0,2),
161             expiration_year => substr($content{expiration},2,2),
162             cvv => $content{cvv},
163             },
164             billing => {
165             first_name => $content{first_name},
166             last_name => $content{last_name},
167             company => $content{company},
168             street_address => $content{address},
169             locality => $content{city},
170             region => $content{state},
171             postal_code => $content{zip},
172             country_code_alpha2 => $content{country}
173             },
174             options => {
175             submit_for_settlement => $submit,
176             }
177             );
178              
179             if (exists $content{merchant_account_id}
180             && $content{merchant_account_id}) {
181             $args{merchant_account_id} = $content{merchant_account_id};
182             }
183              
184             my $result = Net::Braintree::Transaction->sale(\%args);
185              
186             return $result;
187             }
188              
189             =head2 set_defaults
190              
191             Sets defaults for the Braintree merchant id, public and private key.
192              
193             =cut
194            
195             sub set_defaults {
196             my ($self, %opts) = @_;
197             my $config = Net::Braintree->configuration;
198              
199             $config->merchant_id($opts{merchant_id});
200             $config->public_key($opts{public_key});
201             $config->private_key($opts{private_key});
202              
203             return;
204             }
205              
206             =head1 AUTHOR
207              
208             Stefan Hornburg (Racke), C<< <racke at linuxia.de> >>
209              
210             =head1 BUGS
211              
212             Please report any bugs or feature requests to C<bug-business-onlinepayment-braintree at rt.cpan.org>, or through
213             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Business-OnlinePayment-Braintree>. I will be notified, and then you'll
214             automatically be notified of progress on your bug as I make changes.
215              
216             You can find documentation for this module with the perldoc command.
217              
218             perldoc Business::OnlinePayment::Braintree
219              
220              
221             You can also look for information at:
222              
223             =over 4
224              
225             =item * Github issues (report bugs here)
226              
227             L<https://github.com/interchange/Business-OnlinePayment-Braintree/issues>
228              
229             =item * AnnoCPAN: Annotated CPAN documentation
230              
231             L<http://annocpan.org/dist/Business-OnlinePayment-Braintree>
232              
233             =item * CPAN Ratings
234              
235             L<http://cpanratings.perl.org/d/Business-OnlinePayment-Braintree>
236              
237             =item * Search CPAN
238              
239             L<http://search.cpan.org/dist/Business-OnlinePayment-Braintree/>
240              
241             =back
242              
243              
244             =head1 ACKNOWLEDGEMENTS
245              
246             Grant for the following enhancements (RT #88525):
247              
248             =over 4
249              
250             =item billing address transmission
251              
252             =item order number transmission
253              
254             =item refund ability
255              
256             =item added submit_for_settlement to complete the "sale" action
257              
258             =back
259              
260             Peter Mottram for the following enhancements (GH #5):
261              
262             =over 4
263              
264             =item Failure status
265              
266             Set failure status from error codes provided by Braintree.
267              
268             =item CVV
269              
270             Pass cvv to Braintree.
271              
272             =back
273              
274             Evan Brown (GH #7):
275              
276             Add support for post authorization action.
277              
278             =head1 LICENSE AND COPYRIGHT
279              
280             Copyright 2011-2014 Stefan Hornburg (Racke).
281              
282             This program is free software; you can redistribute it and/or modify it
283             under the terms of either: the GNU General Public License as published
284             by the Free Software Foundation; or the Artistic License.
285              
286             See http://dev.perl.org/licenses/ for more information.
287              
288             =head1 SEE ALSO
289              
290             L<Net::Braintree>
291              
292             =cut
293              
294             1; # End of Business::OnlinePayment::Braintree