blib/lib/Business/OnlinePayment/DLocal.pm untested

filter: Statementstmt 0.0% 0 / 250 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 456 CC 40 SCAR 29.6
CC
40
Cov
0%
_send_request
72.0
content
30.0
set_defaults
30.0
CRAP 19.3
1 package Business::OnlinePayment::DLocal;
2 use strict;
3 use warnings;
4  
5 use Business::OnlinePayment;
6 use Business::OnlinePayment::HTTPS;
7 use Digest::SHA qw(hmac_sha256_hex);
8 use HTTP::Tiny;
9 use URI::Escape;
10 use XML::Simple;
11 use JSON;
12 use vars qw(@ISA $me $VERSION);
13 use Log::Scrubber qw(disable $SCRUBBER scrubber :Carp scrubber_add_scrubber);
14 @ISA = qw(Business::OnlinePayment::HTTPS);
15 $me = 'Business::OnlinePayment::DLocal';
16  
17 our $VERSION = '0.006'; # VERSION
18 # PODNAME: Business::OnlinePayment::DLocal
19 # ABSTRACT: Business::OnlinePayment::DLocal - DLocal (astropay) backend for Business::OnlinePayment
20  
21  
22 sub _info {
23 return {
24 info_compat => '0.01',
25 gateway_name => 'DLocal',
26 gateway_url => 'http://www.dlocal.com',
27 module_version => $VERSION,
28 supported_types => ['CC'],
29 supported_actions => {
30 CC => [
31 'Tokenize',
32 'Normal Authorization',
33 'Post Authorization',
34 'Authorization Only',
35 'Credit',
36 'Auth Reversal',
37 'PayStatus',
38 'RefundStatus',
39 'CurrencyExchange',
40 ],
41 },
42 };
43 }
44  
45  
46 sub test_transaction {
47 my $self = shift;
48 my $testMode = shift;
49 if (! defined $testMode) { $testMode = $self->{'test_transaction'} || 0; }
50 $self->{'test_transaction'} = $testMode;
51 if($testMode) {
52 $self->server('sandbox.dlocal.com');
53 $self->port('443');
54 $self->path('/api_curl/cc/sale');
55 } else {
56 $self->server('api.dlocal.com');
57 $self->port('443');
58 $self->path('/api_curl/cc/sale');
59 }
60 return $self->{'test_transaction'};
61 }
62  
63  
64 sub field_map {
65 return (
66 # DLOCAL # => # BOP #
67 'x_login' => 'login', # reports_login
68 'x_trans_key' => 'password', # reports_key
69 'x_secret_key' => 'password2',
70 'x_version' => 'version',
71 'x_country' => 'country',
72 'x_invoice' => 'invoice_number',
73 'x_document' => 'order_number',
74 'x_refund' => 'order_number',
75 'x_amount' => 'amount',
76 'x_currency' => 'currency',
77 'x_description' => 'description',
78 'x_device_id' => 'device_id',
79 'x_cpf' => 'cpf', # govt id number
80 'x_name' => 'name', # needs a joiner of this in map fields
81 'x_email' => 'email',
82 'cc_number' => 'card_number',
83 'cc_exp_month' => 'expirationMM', # needs to be broken in two
84 'cc_exp_year' => 'expirationYY',
85 'cc_cvv' => 'cvv2',
86 'cc_token' => 'card_token',
87 'x_bank' => 'bank', # looks to be card, but others... different key
88 'cc_issuer' => 'issuer', # same
89 'cc_installments' => 'installments', # similar breakout one-shot vs a number
90 'cc_descriptor' => 'descriptor',
91 'x_ip' => 'customer_ip',
92 'x_confirm' => 'confirm', # a confirmation URL if passed, similar to paypal IPN
93 'x_bdate' => 'birthdate', # WTF is this needed
94 'x_iduser' => 'customer_id',
95 'x_address' => 'address',
96 'x_zip' => 'zip',
97 'x_city' => 'city',
98 'x_state' => 'state',
99 'x_phone' => 'phone',
100 'x_merchant_id' => 'merch_id', # sub-merchant id, lmk if you ever use this, no normal BOP standard here
101  
102 'x_auth_id' => 'order_number',
103  
104 control => 'control',
105 type => 'type',
106 );
107 }
108  
109  
110 sub content {
111 my $self = shift;
112 my %content = $self->SUPER::content(@_);
113  
114 # Adjust common %content BOP format to what DLOCAL needs
115 if ($content{'expiration'} && $content{'expiration'} =~ /^(\d\d)\/?(\d\d)/) {
116 $content{'expirationMM'} //= $1;
117 $content{'expirationYY'} //= '20'.$2;
118 }
119 if (! exists $content{'name'}) {
120 $content{'name'} = $content{'first_name'}//'';
121 $content{'name'} .= ' ' if length($content{'name'});
122 $content{'name'} .= $content{'last_name'} if length($content{'last_name'}//'');
123 }
124 $content{'version'} //= $self->api_version;
125 $content{'type'} = 'json';
126  
127 return %content;
128 }
129  
130  
131 sub submit {
132 my $self = shift;
133 my %content = $self->content();
134 die 'Missing action' unless $content{'action'};
135  
136 my $action;
137 foreach (@{$self->_info()->{'supported_actions'}->{'CC'}}) {
138 if (lc($_) eq lc($content{'action'})) {
139 $action = lc('_'.$_);
140 $action =~ s/ /\_/g;
141 }
142 }
143 if ($action && $self->can($action)) {
144 return $self->$action(\%content);
145 } else {
146 die 'Unsupported action';
147 }
148 }
149  
150  
151 sub _normal_authorization { shift->_authorization_only(@_); }
152  
153  
154 sub _authorization_only {
155 my ($self,$content) = @_;
156  
157 if ($content->{'card_token'}) {
158 # tokens fail if you try and send these as well
159 my %remap_fields = $self->field_map();
160 foreach ('x_country','x_cpf','x_name','x_email','cc_number','cc_exp_month','cc_exp_year','cc_cvv') {
161 delete $content->{$remap_fields{$_}};
162 }
163 }
164  
165 my $config = {
166 url => 'https://'.$self->server.'/api_curl/cc/'.(lc($content->{'action'})eq 'normal authorization' ? 'sale' : 'auth'),
167 control => ['x_invoice','x_amount','x_currency','x_email','cc_number','cc_exp_month','cc_cvv','cc_exp_year','x_cpf','x_country','cc_token'],
168 post_data => ['x_login','x_trans_key','x_version','x_invoice','x_amount','x_currency','x_description','x_device_id','x_country',
169 'x_phone','x_address','x_cpf','x_name','x_email','cc_number','cc_exp_month','cc_exp_year','cc_cvv','cc_token','control','type'],
170 };
171  
172 my $res = $self->_send_request($config,$content);
173 $self->error_message( $res->{'desc'} );
174 $self->result_code( $res->{'error_code'} );
175 $self->is_success( defined $res->{'result'} && $res->{'result'} =~ /^9|11$/ ? 1 : 0 );
176 $self->order_number( $res->{'x_document'} // $res->{'x_auth_id'} ); # sale vs auth
177 $res;
178 }
179  
180  
181 sub _post_authorization{
182 my ($self,$content) = @_;
183  
184 my $config = {
185 url => 'https://'.$self->server.'/api_curl/cc/capture',
186 control => ['x_invoice','x_auth_id','x_amount','x_currency'],
187 post_data => ['x_login','x_trans_key','x_version','x_invoice','x_amount','x_currency','x_auth_id','control','type'],
188 };
189  
190 my $res = $self->_send_request($config,$content);
191 $self->error_message( $res->{'desc'} );
192 $self->result_code( $res->{'error_code'} );
193 $self->is_success( defined $res->{'result'} && $res->{'result'} =~ /^9|11$/ ? 1 : 0 );
194 $self->order_number( $res->{'x_document'} // $res->{'x_auth_id'} ); # sale vs auth
195 $res;
196 }
197  
198  
199 sub _tokenize {
200 my ($self,$content) = @_;
201  
202 my $config = {
203 url => 'https://'.$self->server.'/api_curl/cc/save',
204 control => ['x_email','cc_number','cc_exp_month','cc_cvv','cc_exp_year','x_cpf','x_country'],
205 post_data => ['x_login','x_trans_key','x_version','x_country','x_cpf','x_name','x_email','cc_number','cc_exp_month','cc_exp_year','cc_cvv','control','type'],
206 };
207  
208 my $res = $self->_send_request($config,$content);
209 $self->error_message( $res->{'desc'} );
210 $self->result_code( $res->{'error_code'} );
211 $self->is_success( $res->{'cc_token'} ? 1 : 0 );
212 $self->card_token( $res->{'cc_token'} );
213 $res;
214 }
215  
216  
217 sub _credit {
218 my ($self,$content) = @_;
219  
220 my $config = {
221 url => 'https://'.$self->server.'/api_curl/cc/refund',
222 control => ['x_document','x_invoice','x_amount','x_currency'],
223 post_data => ['x_login','x_trans_key','x_version','x_invoice','x_document','x_amount','x_currency','control','type'],
224 };
225  
226 my $res = $self->_send_request($config,$content);
227 $self->error_message( $res->{'desc'} );
228 $self->result_code( $res->{'error_code'} );
229 $self->is_success( defined $res->{'result'} && $res->{'result'} eq '1' ? 1 : 0 );
230 $self->order_number( $res->{'x_document'} );
231 $res;
232 }
233  
234  
235 sub _paystatus {
236 my ($self,$content) = @_;
237  
238 my $config = {
239 url => 'https://'.$self->server.'/api_curl/query/paystatus',
240 control => [], # not used
241 post_data => ['x_login','x_trans_key','x_version','x_invoice','x_document','type'],
242 };
243  
244 # query api uses different credentials
245 local $content->{'login'} = $content->{'reports_login'};
246 local $content->{'password'} = $content->{'reports_key'};
247  
248 my $res = $self->_send_request($config,$content);
249 $self->error_message( $res->{'desc'} );
250 $self->result_code( $res->{'error_code'} );
251 $self->is_success( defined $res->{'result'} ); # any result is a positive think for a query call
252 $self->order_number( $res->{'x_document'} );
253 $res;
254 }
255  
256  
257 sub _refundstatus {
258 my ($self,$content) = @_;
259  
260 my $config = {
261 url => 'https://'.$self->server.'/api_curl/query/refundstatus',
262 control => [], # not used
263 post_data => ['x_login','x_trans_key','x_version','x_refund','type'],
264 };
265  
266 # query api uses different credentials
267 local $content->{'login'} = $content->{'reports_login'};
268 local $content->{'password'} = $content->{'reports_key'};
269  
270 my $res = $self->_send_request($config,$content);
271 $self->error_message( $res->{'desc'} );
272 $self->result_code( $res->{'error_code'} );
273 $self->is_success( defined $res->{'result'} ); # any result is a positive think for a query call
274 $self->order_number( $res->{'x_document'} );
275 $res;
276 }
277  
278  
279 sub _currencyexchange {
280 my ($self,$content) = @_;
281  
282 my $config = {
283 url => 'https://'.$self->server.'/api_curl/query/currencyexchange',
284 control => [], # not used
285 post_data => ['x_login','x_trans_key','x_country','type'],
286 };
287  
288 # query api uses different credentials
289 local $content->{'login'} = $content->{'reports_login'};
290 local $content->{'password'} = $content->{'reports_key'};
291  
292 my $res = $self->_send_request($config,$content);
293 if ($res =~ /^\d+(:?\.\d+)$/ && $res > 0 ) {
294 $self->is_success( 1 );
295 $self->order_number( $res );
296 } else {
297 $self->is_success( 0 );
298 $self->order_number( undef );
299 }
300 $res;
301 }
302  
303  
304 sub _send_request {
305 my ($self,$config,$content) = @_;
306 my %content = %$content;
307 my %remap_fields = $self->field_map();
308  
309 $self->_dlocal_scrubber_add_card($content{'card_number'});
310 scrubber_add_scrubber({'cc_cvv='.$content{'cvv2'}=>'cc_cvv=DELETED'}) if defined $content{'cvv2'};
311  
312 my $message = '';
313 foreach my $key ( @{$config->{'control'}} ) { $message .= $content{$remap_fields{$key}}//''; }
314 local $content{'control'} = uc(hmac_sha256_hex(pack('A*',$message), pack('A*',$content{'password2'})));
315  
316 my $post_data;
317 foreach my $key ( @{$config->{'post_data'}} ) {
318 $post_data .= uri_escape($key).'='.uri_escape($content{$remap_fields{$key}}).'&' if $content{$remap_fields{$key}};
319 }
320 my $url = $config->{'url'};
321 $self->server_request( $url.'?'.$post_data ); # yeah it's in GET, but it's easy to read that way
322 my $verify_ssl = 1;
323  
324 my $response;
325 if (ref $self->{'mocked'} eq 'ARRAY' && scalar @{$self->{'mocked'}}) {
326 my $mock = shift @{$self->{'mocked'}};
327 die "Unexpected mock action" unless lc($mock->{'action'}) eq lc($content->{'action'});
328 die "Unexpected mock login" unless $mock->{'login'} eq $content{'login'};
329 $response->{'content'} = $mock->{'resp'};
330 } else {
331 $response = HTTP::Tiny->new( verify_SSL=>$verify_ssl )->request('POST', $url, {
332 headers => {
333 'Content-Length' => length($post_data),
334 'Content-Type' => 'application/x-www-form-urlencoded',
335 'Accept' => 'application/json',
336 },
337 content => $post_data,
338 } );
339 }
340 $self->server_response( $response->{'content'} );
341 my $c = substr($response->{'content'},0,1);
342 my $res = $c eq '{' ? decode_json( $response->{'content'} )
343 : $c eq '<' ? $self->_parse_xml_response( $response->{'content'}, $response->{'status'} ) # just in case
344 : $response->{'content'}; # return raw (for currencyexchange)
345 $res;
346 }
347  
348  
349 sub _parse_xml_response {
350 my ( $self, $page, $status_code ) = @_;
351 my $response = {};
352 if ( $status_code =~ /^200/ ) {
353 if ( ! eval { $response = XMLin($page); } ) {
354 die "XML PARSING FAILURE: $@";
355 }
356 }
357 else {
358 $status_code =~ s/[\r\n\s]+$//; # remove newline so you can see the error in a linux console
359 if ( $status_code =~ /^(?:900|599)/ ) { $status_code .= ' - verify DLocal has whitelisted your IP'; }
360 die "CONNECTION FAILURE: $status_code";
361 }
362 return $response;
363 }
364  
365  
366 sub server_request {
367 my ( $self, $val, $tf ) = @_;
368 if ($val) {
369 $self->{server_request} = scrubber $val;
370 $self->server_request_dangerous($val,1) unless $tf;
371 }
372 return $self->{server_request};
373 }
374  
375  
376 sub server_request_dangerous {
377 my ( $self, $val, $tf ) = @_;
378 if ($val) {
379 $self->{server_request_dangerous} = $val;
380 $self->server_request($val,1) unless $tf;
381 }
382 return $self->{server_request_dangerous};
383 }
384  
385  
386 sub server_response {
387 my ( $self, $val, $tf ) = @_;
388 if ($val) {
389 $self->{server_response} = scrubber $val;
390 $self->server_response_dangerous($val,1) unless $tf;
391 }
392 return $self->{server_response};
393 }
394  
395  
396 sub server_response_dangerous {
397 my ( $self, $val, $tf ) = @_;
398 if ($val) {
399 $self->{server_response_dangerous} = $val;
400 $self->server_response($val,1) unless $tf;
401 }
402 return $self->{server_response_dangerous};
403 }
404  
405  
406  
407 sub set_defaults {
408 my $self = shift;
409 my %opts = @_;
410  
411 $self->build_subs(
412 qw( order_number card_token api_version )
413 );
414  
415 $self->test_transaction(0);
416  
417 if ( $opts{debug} ) {
418 $self->debug( $opts{debug} );
419 delete $opts{debug};
420 }
421  
422 ## load in the defaults
423 my %_defaults = ();
424 foreach my $key ( keys %opts ) {
425 $key =~ /^default_(\w*)$/ or next;
426 $_defaults{$1} = $opts{$key};
427 delete $opts{$key};
428 }
429  
430 $self->{_scrubber} = \&_default_scrubber;
431 if( defined $_defaults{'Scrubber'} ) {
432 my $code = $_defaults{'Scrubber'};
433 if( ref($code) ne 'CODE' ) {
434 warn('default_Scrubber is not a code ref');
435 }
436 else {
437 $self->{_scrubber} = $code;
438 }
439 }
440  
441 $self->api_version('4.0') unless $self->api_version;
442 }
443  
444 sub _default_scrubber {
445 my $cc = shift;
446 my $del = substr($cc,0,6).('X'x(length($cc)-10)).substr($cc,-4,4); # show first 6 and last 4
447 return $del;
448 }
449  
450 sub _dlocal_scrubber_add_card {
451 my ( $self, $cc ) = @_;
452 return if ! $cc;
453 my $scrubber = $self->{_scrubber};
454 scrubber_add_scrubber({$cc=>&{$scrubber}($cc)});
455 }
456  
457 1;
458  
459 __END__