Skip to content

Commit

Permalink
Add gateway_log to AuthorizeNet.pm
Browse files Browse the repository at this point in the history
* Expanded out gateway_log fields to include new Authnet-specific
  return values.

* Migrated chunk of log_it() code that would clearly be exactly
  duplicated between implementations into a new write() sub that
  actually updates the database and logs any errors that occur.
  • Loading branch information
msjohns1 committed Nov 4, 2017
1 parent c366818 commit 11ad20b
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 71 deletions.
2 changes: 1 addition & 1 deletion dist/standard/products/gateway_log.txt
@@ -1 +1 @@
gateway_log_id trans_type processor catalog result_code response_msg request_id order_number email session_id request_source request_date request_duration request response
gateway_log_id trans_type processor catalog order_md5 result_code result_subcode reason_code response_msg request_id order_number email session_id request_source request_date request_duration request response
97 changes: 96 additions & 1 deletion lib/Vend/Payment/AuthorizeNet.pm
Expand Up @@ -525,10 +525,34 @@ sub authorizenet {
## doesn't need it
delete $query{x_Trans_ID} if $no_trans_id{ $query{x_Type} };

#::logDebug("Authorizenet query: " . ::uneval(\%query));
my $gwl = Vend::Payment::AuthorizeNet
-> new({
Enabled => charge_param('gwl_enabled'),
LogTable => charge_param('gwl_table'),
})
;

{
my %munged_query = %query;
$munged_query{x_Card_Num} =~ s/^(\d{4})(.*)/$1 . ('X' x length($2))/e
if defined $munged_query{x_Card_Num};

$munged_query{$_} = 'X'
for grep { defined $munged_query{$_} } qw/x_Password x_Tran_Key/;

$munged_query{$_} =~ s/./X/g
for grep { defined $munged_query{$_} }
qw/x_Card_Code x_bank_aba_code x_bank_acct_num/
;
#::logDebug("Authorizenet query: " . ::uneval(\%munged_query));
$gwl->request(\%munged_query);
}

$opt->{extra_headers} = { Referer => $referer };

$gwl->start;
my $thing = post_data($opt, \%query);
$gwl->stop;
my $page = $thing->{result_page};
my $response = $thing->{status_line};

Expand Down Expand Up @@ -595,6 +619,7 @@ sub authorizenet {
}
= split (/\037/,$page);

$gwl->response(\%result);
#::logDebug(qq{authorizenet response_reason_text=$result{x_response_reason_text} response_code: $result{x_response_code}});

for (keys %result_map) {
Expand Down Expand Up @@ -633,4 +658,74 @@ sub authorizenet {

package Vend::Payment::AuthorizeNet;

use Vend::Payment::GatewayLog;
use base qw/Vend::Payment::GatewayLog/;

# log_it() must be overridden.
sub log_it {
my $self = shift;

my $request = $self->request;
unless ($request) {
::logDebug('Nothing to write to %s: no request present', $self->table);
return;
}

unless ($self->response) {

if ($Vend::Payment::Global_Timeout) {
my $msg = errmsg('No response. Global timeout triggered');
::logDebug($msg);
$self->response({
x_response_code => -2,
x_response_reason_text => $Vend::Payment::Global_Timeout,
});
}
else {
my $msg = errmsg('No response. Reason unknown');
::logDebug($msg);
$self->response({
x_response_code => -3,
x_response_reason_text => $msg,
});
}
}
my $response = $self->response;

my %fields = (
trans_type => $request->{x_Type} || 'x',
processor => 'authorizenet',
catalog => $Vend::Cfg->{CatalogName},
result_code => $response->{x_response_code} || '',
result_subcode => $response->{x_response_subcode} || '',
reason_code => $response->{x_response_reason_code} || '',
response_msg => $response->{x_response_reason_text} || '',
request_id => $response->{x_trans_id} || '',
order_number => $response->{x_invoice_num} || $request->{x_Invoice_Num} || '',
request_duration => $self->duration,
request_date => $self->timestamp,
email => $request->{x_Email} || $response->{x_email} || '',
request => ::uneval($request) || '',
response => ::uneval($response) || '',
session_id => $::Session->{id},
);

my $hostname = `hostname -s`;
chomp $hostname;
$fields{request_source} = $hostname;

$fields{order_md5} =
Digest::MD5::md5_hex(
$request->{x_Email},
$request->{x_Type},
$request->{x_Auth_Code},
$request->{x_Amount},
$::Session->{id},
map { ($_->{code}, $_->{quantity}) } @$Vend::Items
)
;

$self->write(\%fields);
}

1;
34 changes: 34 additions & 0 deletions lib/Vend/Payment/GatewayLog.pm
Expand Up @@ -92,6 +92,40 @@ sub log_it {
die 'Must override log_it() in subclass';
}

sub write {
my $self = shift;
my $data = shift;

eval {
my $table = $self->table;
my $db = ::database_exists_ref($table)
or die "'$table' not a valid Interchange table";
$db = $db->ref;

$db->set_slice(
[ { dml => 'insert' } ],
$data
)
or die "set_slice for $table failed";
}; # End eval

if ($@) {
my $err = $@;
::logDebug(
q{Couldn't write to %s: %s -- request: %s -- response: %s},
$self->table,
$err,
::uneval($self->request),
::uneval($self->response)
);
}
else {
$self->clean;
}

return 1;
}

sub table {
return shift()->{_log_table};
}
Expand Down
114 changes: 45 additions & 69 deletions lib/Vend/Payment/PayflowPro.pm
Expand Up @@ -865,94 +865,70 @@ use base qw/Vend::Payment::GatewayLog/;
# log_it() must be overridden.
sub log_it {
my $self = shift;
my $request = $self->request
or ::logDebug('Cannot write to %s: no request present', $self->table),
return;

my $response = $self->response;
unless ($response) {
my $request = $self->request;
unless ($request) {
::logDebug('Nothing to write to %s: no request present', $self->table);
return;
}

unless ($self->response) {

if ($Vend::Payment::Global_Timeout) {
my $msg = errmsg('No response. Global timeout triggered');
::logDebug($msg);
$response = {
$self->response({
RESULT => -2,
RESPMSG => $Vend::Payment::Global_Timeout,
};
});
}
else {
my $msg = errmsg('No response. Reason unknown');
::logDebug($msg);
$response = {
$self->response({
RESULT => -3,
RESPMSG => $msg,
};
});
}
}
my $response = $self->response;

eval {
my $table = $self->table;
my $db = ::database_exists_ref($table)
or die "'$table' not a valid Interchange table";
$db = $db->ref;

my %fields = (
trans_type => $request->{TRXTYPE} || 'x',
processor => 'payflowpro',
catalog => $Vend::Cfg->{CatalogName},
result_code =>
defined ($response->{RESULT})
&& $response->{RESULT} =~ /^-?\d+$/
? $response->{RESULT}
: undef,
response_msg => $response->{RESPMSG} || '',
request_id => $response->{PNREF} || '',
order_number => $request->{COMMENT1} || '',
request_duration => $self->duration,
request_date => $self->timestamp,
email => $request->{EMAIL} || '',
request => ::uneval($request) || '',
response => ::uneval($response) || '',
session_id => $::Session->{id},
);
my %fields = (
trans_type => $request->{TRXTYPE} || 'x',
processor => 'payflowpro',
catalog => $Vend::Cfg->{CatalogName},
result_code =>
defined ($response->{RESULT})
&& $response->{RESULT} =~ /^-?\d+$/
? $response->{RESULT}
: undef,
response_msg => $response->{RESPMSG} || '',
request_id => $response->{PNREF} || '',
order_number => $request->{COMMENT1} || '',
request_duration => $self->duration,
request_date => $self->timestamp,
email => $request->{EMAIL} || '',
request => ::uneval($request) || '',
response => ::uneval($response) || '',
session_id => $::Session->{id},
);

my $hostname = `hostname -s`;
chomp $hostname;
$fields{request_source} = $hostname;

$fields{order_md5} =
Digest::MD5::md5_hex(
$request->{EMAIL},
$request->{TRXTYPE},
$request->{ORIGID},
$request->{AMT},
$::Session->{id},
map { ($_->{code}, $_->{quantity}) } @$Vend::Items
)
;

$db->set_slice(
[ { dml => 'insert' } ],
\%fields
my $hostname = `hostname -s`;
chomp $hostname;
$fields{request_source} = $hostname;

$fields{order_md5} =
Digest::MD5::md5_hex(
$request->{EMAIL},
$request->{TRXTYPE},
$request->{ORIGID},
$request->{AMT},
$::Session->{id},
map { ($_->{code}, $_->{quantity}) } @$Vend::Items
)
or die "set_slice for $table failed";
}; # End eval

if ($@) {
my $err = $@;
::logDebug(
q{Couldn't write to %s: %s -- request: %s -- response: %s},
$self->table,
$err,
::uneval($request),
::uneval($response)
);
}
else {
$self->clean;
}
;

return 1;
$self->write(\%fields);
}

1;

0 comments on commit 11ad20b

Please sign in to comment.