Index: civicrm/trunk/sites/all/modules/queue2civicrm/recurring/recurring.module |
— | — | @@ -169,8 +169,27 @@ |
170 | 170 | function recurring_import( $msg ) { |
171 | 171 | global $txn_subscr_payment, $txn_subscr_acct; |
172 | 172 | civicrm_initialize(true); |
| 173 | + |
| 174 | + // store the original message for logging later |
| 175 | + $msg_orig = $msg; |
| 176 | + |
173 | 177 | $msg = recurring_normalize_msg( $msg ); |
174 | 178 | |
| 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 | + |
175 | 194 | // log the message |
176 | 195 | watchdog('recurring', 'Recurring msg:<pre>' . check_plain(print_r($msg, TRUE)) . '</pre>'); |
177 | 196 | |
— | — | @@ -196,6 +215,14 @@ |
197 | 216 | $ret_val = 0; |
198 | 217 | } |
199 | 218 | |
| 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 | + |
200 | 227 | return $ret_val; |
201 | 228 | } |
202 | 229 | |
Index: civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm_common.inc |
— | — | @@ -68,7 +68,8 @@ |
69 | 69 | } |
70 | 70 | |
71 | 71 | 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' ]; |
73 | 74 | $msg['gateway_txn_id'] .= ' ' . time(); |
74 | 75 | |
75 | 76 | return $msg; |
— | — | @@ -372,4 +373,46 @@ |
373 | 374 | } |
374 | 375 | |
375 | 376 | 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; |
376 | 419 | } |
\ No newline at end of file |
Index: civicrm/trunk/sites/all/modules/queue2civicrm/queue2civicrm.module |
— | — | @@ -179,10 +179,28 @@ |
180 | 180 | * Process one contribution from the queue to CiviCRM. |
181 | 181 | */ |
182 | 182 | function queue2civicrm_import( $msg ) { |
| 183 | + // save the original message for logging |
| 184 | + $msg_orig = $msg; |
| 185 | + |
183 | 186 | civicrm_initialize(true); |
184 | 187 | |
185 | 188 | $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 | + |
187 | 205 | // set the correct amount fields/data and do exchange rate conversions. |
188 | 206 | $msg = _queue2civicrm_normalize_contrib_amnts( $msg ); |
189 | 207 | |
— | — | @@ -211,6 +229,14 @@ |
212 | 230 | 'msg' => $msg, |
213 | 231 | ); |
214 | 232 | |
| 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 | + |
215 | 241 | // Send thank you email, other post-import things |
216 | 242 | module_invoke_all( 'queue2civicrm_import', $contribution_info ); |
217 | 243 | |