r80638 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80637‎ | r80638 | r80639 >
Date:19:32, 20 January 2011
Author:awjrichards
Status:deferred
Tags:
Comment:
Added a logging function to insert or update a log record in the queue2civicrm_log table in drupal database. The idea is to capture the raw message from the queue before processing and log it to the db for easy replay later should something go wrong and the message has already been removed from the queue. Upon succesful consumption, the log record is marked as 'verified'. Also added an new key to the array on normalization to track the gateway's original transaction id (because we append/prepend some things to it depending on the trxn) for storage in the log record.
Modified paths:
  • /civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm.module (modified) (history)
  • /civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm_common.inc (modified) (history)
  • /civicrm/trunk/sites/all/modules/queue2civicrm/recurring/recurring.module (modified) (history)

Diff [purge]

Index: civicrm/trunk/sites/all/modules/queue2civicrm/recurring/recurring.module
@@ -169,8 +169,27 @@
170170 function recurring_import( $msg ) {
171171 global $txn_subscr_payment, $txn_subscr_acct;
172172 civicrm_initialize(true);
 173+
 174+ // store the original message for logging later
 175+ $msg_orig = $msg;
 176+
173177 $msg = recurring_normalize_msg( $msg );
174178
 179+ /**
 180+ * prepare data for logging
 181+ *
 182+ * if we don't have a gateway_txn_id, we'll store the transaction type + the subscriber id instead -
 183+ * this should happen for all non-payment transactions.
 184+ */
 185+ $log = array(
 186+ 'gateway' => 'recurring_paypal',
 187+ 'gateway_txn_id' => ( strlen( $msg[ 'gateway_txn_id_orig' ] ) ? $msg[ 'gateway_txn_id_orig' ] : $msg[ 'txn_type' ] . ":" . $msg[ 'subscr_id' ] ),
 188+ 'data' => ( is_array( $msg_orig ) ? json_encode( $msg_orig ) : $msg_orig ),
 189+ 'timestamp' => time(),
 190+ 'verified' => 0,
 191+ );
 192+ $cid = _queue2civicrm_log( $log );
 193+
175194 // log the message
176195 watchdog('recurring', 'Recurring msg:<pre>' . check_plain(print_r($msg, TRUE)) . '</pre>');
177196
@@ -196,6 +215,14 @@
197216 $ret_val = 0;
198217 }
199218
 219+ // update the log
 220+ if ( $ret_val && $cid ) {
 221+ $log[ 'cid' ] = $cid;
 222+ $log[ 'timestamp' ] = time();
 223+ $log[ 'verified' ] = 1;
 224+ _queue2civicrm_log( $log );
 225+ }
 226+
200227 return $ret_val;
201228 }
202229
Index: civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm_common.inc
@@ -68,7 +68,8 @@
6969 }
7070
7171 watchdog('queue2civicrm', 'Contribution (pre-conversion):<pre>' . check_plain(print_r($msg, TRUE)) . '</pre>');
72 -
 72+
 73+ $msg[ 'gateway_txn_id_orig' ] = $msg[ 'gateway_txn_id' ];
7374 $msg['gateway_txn_id'] .= ' ' . time();
7475
7576 return $msg;
@@ -372,4 +373,46 @@
373374 }
374375
375376 return $dbs;
 377+}
 378+
 379+/**
 380+ * Log a transaction to queue2civicrm_log in the database
 381+ *
 382+ * The array needs to contain the following keys:
 383+ * gateway, gateway_txn_id, data, timestamp, verified
 384+ * And optionally:
 385+ * cid (which is the ID of the log record to be updated)
 386+ *
 387+ * If 'cid' is present, this will update the log record, otherwise
 388+ * this will insert a new record.
 389+ *
 390+ * @param $log
 391+ * @return mixed false on fail, cid on insert, otherwise true
 392+ */
 393+function _queue2civicrm_log( $log=array() ) {
 394+ if ( empty( $log ) ) {
 395+ return false;
 396+ }
 397+
 398+ // make sure we're using the default db
 399+ $dbs = _queue2civicrm_get_dbs();
 400+ $dbs->use_default();
 401+
 402+ // if cid is set in the log array, we need to update
 403+ if ( $log[ 'cid' ] ) {
 404+ $query = "UPDATE {queue2civicrm_log} SET gateway='%s', gateway_txn_id='%s', data='%s', timestamp=%d, verified=%d WHERE cid=%d";
 405+ $result = db_query( $query, $log[ 'gateway' ], $log[ 'gateway_txn_id' ], $log[ 'data' ], $log[ 'timestamp' ], $log[ 'verified' ], $log[ 'cid' ] );
 406+ if ( $result ) $result = true;
 407+ } else {
 408+ $query = "INSERT INTO {queue2civicrm_log} ( gateway, gateway_txn_id, data, timestamp, verified ) VALUES ( '%s', '%s', '%s', %d, %d )";
 409+ $result = db_query( $query, $log[ 'gateway' ], $log[ 'gateway_txn_id' ], $log[ 'data' ], $log[ 'timestamp' ], $log[ 'verified' ] );
 410+ if ( $result ) {
 411+ $result = db_last_insert_id( 'queue2civicrm_log', 'cid' );
 412+ }
 413+ }
 414+
 415+ if ( !$result ) {
 416+ watchdog( 'queue2civicrm', 'Failed logging the transaction: %log', array( "%log" => print_r( $log, true )), WATCHDOG_ERROR );
 417+ }
 418+ return $result;
376419 }
\ No newline at end of file
Index: civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm.module
@@ -179,10 +179,28 @@
180180 * Process one contribution from the queue to CiviCRM.
181181 */
182182 function queue2civicrm_import( $msg ) {
 183+ // save the original message for logging
 184+ $msg_orig = $msg;
 185+
183186 civicrm_initialize(true);
184187
185188 $msg = _queue2civicrm_normalize_msg( $msg );
186 -
 189+
 190+ /**
 191+ * prepare data for logging
 192+ *
 193+ * if we don't have a gateway_txn_id, we'll store the transaction type + the subscriber id instead -
 194+ * this should happen for all non-payment transactions.
 195+ */
 196+ $log = array(
 197+ 'gateway' => $msg[ 'gateway' ],
 198+ 'gateway_txn_id' => $msg[ 'gateway_txn_id' ],
 199+ 'data' => ( is_array( $msg_orig ) ? json_encode( $msg_orig ) : $msg_orig ),
 200+ 'timestamp' => time(),
 201+ 'verified' => 0,
 202+ );
 203+ $cid = _queue2civicrm_log( $log );
 204+
187205 // set the correct amount fields/data and do exchange rate conversions.
188206 $msg = _queue2civicrm_normalize_contrib_amnts( $msg );
189207
@@ -211,6 +229,14 @@
212230 'msg' => $msg,
213231 );
214232
 233+ // update the log if things went well
 234+ if ( $cid && !$contribution[ 'is_error' ] ) {
 235+ $log[ 'cid' ] = $cid;
 236+ $log[ 'verrified' ] = 1;
 237+ $log[ 'timestamp' ] = time();
 238+ _queue2civicrm_log( $log );
 239+ }
 240+
215241 // Send thank you email, other post-import things
216242 module_invoke_all( 'queue2civicrm_import', $contribution_info );
217243

Status & tagging log