From d2e55307eddea14db05642260f6252cdb434aa38 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 30 Jan 2008 15:59:09 +0000 Subject: [PATCH] Set up enhanced logging of transactions. Note that now, all transactions are logged, whether they fail or not. Failed transactions are logged with status canceled. ITransact transactions have additional data logged. --- docs/changelog/7.x.x.txt | 2 + docs/upgrades/upgrade_7.5.0-7.5.1.pl | 12 ++++ lib/WebGUI/Commerce/Payment.pm | 15 +++++ lib/WebGUI/Commerce/Payment/ITransact.pm | 30 ++++++++++ lib/WebGUI/Commerce/Transaction.pm | 74 +++++++++++++++++++++++- lib/WebGUI/Operation/Commerce.pm | 10 +++- 6 files changed, 140 insertions(+), 3 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index edb281391..e7699f88b 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -51,6 +51,8 @@ - Added migration utility for Gallery - Removed old .tmpl files from upgrades. ALL templates in upgrades must now be in Packages! + - Changed transaction logging in the Commerce system, so that all connection and transaction errors + are now logged automatically, but listed as Canceled with the error message as to why. - add: widgetize asset macro. called like so: ^Widget(assetId, width, height, templateId); where assetId is the asset of the widget to widgetize and templateId is the template for the widget itself. If diff --git a/docs/upgrades/upgrade_7.5.0-7.5.1.pl b/docs/upgrades/upgrade_7.5.0-7.5.1.pl index 863281bd5..5eaafe102 100644 --- a/docs/upgrades/upgrade_7.5.0-7.5.1.pl +++ b/docs/upgrades/upgrade_7.5.0-7.5.1.pl @@ -23,6 +23,7 @@ my $quiet; # this line required my $session = start(); # this line required removeOldPhotoGallery($session); speedUp($session); +enhanceITransactLogging($session); finish($session); # this line required @@ -33,6 +34,17 @@ sub speedUp { $session->db->write("alter table assetData add index assetId_status (assetId,status)"); } +#---------------------------------------------------------------------------- +# Add more data to the transaction table +sub enhanceITransactLogging { + my $session = shift; + print "\tAdd additional ITransact data to the transaction table..." unless $quiet; + $session->db->write('alter table transaction add column XID varchar(100) default null'); + $session->db->write('alter table transaction add column authcode varchar(100) default null'); + $session->db->write('alter table transaction add column message text default null'); + print "DONE!\n" unless $quiet; +} + #------------------------------------------------- sub removeOldPhotoGallery { my $session = shift; diff --git a/lib/WebGUI/Commerce/Payment.pm b/lib/WebGUI/Commerce/Payment.pm index 1eafcb8e4..9fdc832fc 100644 --- a/lib/WebGUI/Commerce/Payment.pm +++ b/lib/WebGUI/Commerce/Payment.pm @@ -618,5 +618,20 @@ sub validateFormData { return $self->session->errorHandler->fatal("You must override the validateFormData method in the payment plugin."); } +=head2 logExtraTransactionData ( $transaction ) + +This method puts extra, Payment plugin specific data into the transaction log. This +needs to be be overridden to actually do the logging. + +=head3 $transaction + +A WebGUI::Commerce::Transaction object to do the logging. + +=cut + +sub logExtraTransactionData { + return 1; +} + 1; diff --git a/lib/WebGUI/Commerce/Payment/ITransact.pm b/lib/WebGUI/Commerce/Payment/ITransact.pm index 288fd806d..6a2db4916 100644 --- a/lib/WebGUI/Commerce/Payment/ITransact.pm +++ b/lib/WebGUI/Commerce/Payment/ITransact.pm @@ -668,5 +668,35 @@ sub validateFormData { return \@error; } +sub logExtraTransactionData { + my ($self, $transaction) = @_; + $self->session->errorHandler->warn('transaction: '.$transaction->{_transactionId}); + if ( exists($self->{_connectionError}) && $self->{_connectionError}) { ##Bad connection + $self->session->errorHandler->warn('Connection error'); + $transaction->message($self->connectionError); + return; + } + elsif (exists($self->{_transactionError}) && $self->{_transactionError}) { ##Bad transaction + $self->session->errorHandler->warn('Transaction error'); + $transaction->message($self->resultMessage); + $transaction->xid($self->{_response}->{XID}); + return; + } + else { ##Everything went well + $self->session->errorHandler->warn('OK transaction'); + if ($self->{_response}->{Status} eq 'OK') { + $transaction->message($self->{_response}->{Status}); + } + else { + $transaction->message($self->resultMessage); + } + $transaction->xid($self->{_response}->{XID}); + if (! ref $self->{_response}->{AuthCode} ) { + $transaction->authcode($self->{_response}->{AuthCode}); + } + return; + } +} + 1; diff --git a/lib/WebGUI/Commerce/Transaction.pm b/lib/WebGUI/Commerce/Transaction.pm index 4833d86c5..9041cd727 100644 --- a/lib/WebGUI/Commerce/Transaction.pm +++ b/lib/WebGUI/Commerce/Transaction.pm @@ -70,6 +70,30 @@ sub addItem { #------------------------------------------------------------------- +=head2 authcode ( authcode ) + +Returns the authcode connected to the transaction. If authcode is given the authcode property is set to that. +Presently, this is only used by the ITransact module. + +=head3 authcode + +The authcode of the current transaction. + +=cut + +sub authcode { + my ($self, $authcode) = @_; + + if ($authcode) { + $self->{_properties}{authcode} = $authcode; + $self->session->db->write("update transaction set authcode=? where transactionId=?",[$authcode, $self->{_transactionId}]); + } + + return $self->{_properties}{authcode}; +} + +#------------------------------------------------------------------- + =head2 cancelTransaction ( ) Cancels a recurring transaction. This is done by trying to cancel the subscription at the gateway @@ -419,6 +443,30 @@ sub lastPayedTerm { #------------------------------------------------------------------- +=head2 message ( message ) + +Returns the message connected to the transaction. If message is given the message property is set to that. +Presently, this is only used by the ITransact module. + +=head3 message + +The message of the current transaction. + +=cut + +sub message { + my ($self, $message) = @_; + + if ($message) { + $self->{_properties}{message} = $message; + $self->session->db->write("update transaction set message=? where transactionId=?",[$message, $self->{_transactionId}]); + } + + return $self->{_properties}{message}; +} + +#------------------------------------------------------------------- + =head2 new ( transactionId, [ gateway, [ userId ] ] ) Constructor. Returns a transaction object. If transactionId is set to 'new' a new transaction is created. @@ -687,6 +735,30 @@ sub transactionsByUser { return \@transactions; } - + +#------------------------------------------------------------------- + +=head2 xid ( xid ) + +Returns the xid connected to the transaction. If xid is given the xid property is set to that. +Presently, this is only used by the ITransact module. + +=head3 xid + +The xid of the current transaction. + +=cut + +sub xid { + my ($self, $xid) = @_; + + if ($xid) { + $self->{_properties}{xid} = $xid; + $self->session->db->write("update transaction set xid=? where transactionId=?",[$xid, $self->{_transactionId}]); + } + + return $self->{_properties}{xid}; +} + 1; diff --git a/lib/WebGUI/Operation/Commerce.pm b/lib/WebGUI/Operation/Commerce.pm index 4463ac071..c49507b6a 100644 --- a/lib/WebGUI/Operation/Commerce.pm +++ b/lib/WebGUI/Operation/Commerce.pm @@ -503,6 +503,10 @@ sub www_checkoutSubmit { $transaction->gatewayId($plugin->gatewayId); $transaction->gateway($plugin->namespace); + + ##New transaction logging code for ITransact module + ##goes here. + $plugin->logExtraTransactionData($transaction); # check transaction result unless ($plugin->connectionError) { @@ -519,14 +523,16 @@ sub www_checkoutSubmit { $var->{status} = $i18n->get('transaction error'); $var->{error} = $plugin->transactionError; $var->{errorCode} = $plugin->errorCode; - $transaction->delete; + $transaction->status('Canceled'); + #$transaction->delete; } } else { $checkoutError = 1; $var->{status} = $i18n->get('connection error'); $var->{error} = $plugin->connectionError; $var->{errorCode} = $plugin->errorCode; - $transaction->delete; + $transaction->status('Canceled'); + #$transaction->delete; } push(@resultLoop, $var);