r75494 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75493‎ | r75494 | r75495 >
Date:01:27, 27 October 2010
Author:awjrichards
Status:deferred
Tags:
Comment:
Updated to use variable substitution in watchdog calls; Updated to use queue2civicrm_import hook
Modified paths:
  • /civicrm/trunk/sites/all/modules/wmf_owa/wmf_owa.module (modified) (history)

Diff [purge]

Index: civicrm/trunk/sites/all/modules/wmf_owa/wmf_owa.module
@@ -43,14 +43,16 @@
4444 /**
4545 * Implementation of hook_civicrm_post
4646 *
 47+ * @fixme What about in the event of a contrib deletion?
 48+ *
4749 * @param unknown_type $op
4850 * @param unknown_type $objectName
4951 * @param unknown_type $objectId
5052 * @param unknown_type $objectRef
5153 */
5254 function wmf_owa_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
53 - // only continue if we're handling a new or updated contribution
54 - if ( $objectName != 'Contribution' && !in_array( $op, array( 'edit', 'create' )) ) {
 55+ // only continue if we're handling an updated contribution
 56+ if ( $objectName != 'Contribution' && !in_array( $op, array( 'edit' )) ) {
5557 return;
5658 }
5759
@@ -58,36 +60,98 @@
5961 $contrib_tracking_data = wmf_owa_get_contribution_tracking_data( $objectId );
6062 if ( !$contrib_tracking_data ) {
6163 // log, don't post to OWA
62 - watchdog( 'wmf_owa', 'Could not load tracking data for contribution_id ' . $objectId, WATCHDOG_WARNING );
 64+ watchdog( 'wmf_owa', 'Could not load tracking data for contribution_id %objectId', array( '%objectId' => $objectId ), WATCHDOG_WARNING );
6365 return;
6466 }
6567
 68+ $query_params = wmf_owa_prepare_owa_query( $contrib_tracking_data, $objectRef );
 69+
 70+ // send the query to OWA
 71+ wmf_owa_send_tracking( $query_params );
 72+ return;
 73+
 74+}
 75+
 76+/**
 77+ * Hook implementation of queue2civicrm_import
 78+ *
 79+ * This gets called when queue2civicrm has completed importing a trxn
 80+ * from the queueing service.
 81+ * @param $contribution_id
 82+ */
 83+function wmf_owa_queue2civicrm_import( $contribution_id ) {
 84+ // load the OWA-specific tracking data
 85+ $contrib_tracking_data = wmf_owa_get_contribution_tracking_data( $contribution_id );
 86+ if ( !$contrib_tracking_data ) {
 87+ // log, don't post to OWA
 88+ watchdog( 'wmf_owa', 'Could not load tracking data for contribution_id %objectId ', array( '%objectId' => $objectId ), WATCHDOG_WARNING );
 89+ return;
 90+ }
 91+
 92+ // load the contribution object
 93+ civicrm_initialize(true);
 94+ require_once 'api/v2/Contribute.php';
 95+ $contribution = civicrm_contribution_get( array( 'contribution_id' => $contribution_id ) );
 96+
 97+ // note that we cast the contribution as an object so wmf_owa_prepare_owa_query can work with it correctly
 98+ $query_params = wmf_owa_prepare_owa_query( $contrib_tacking_data, (object) $contribution );
 99+
 100+ // send the query to OWA
 101+ wmf_owa_send_tracking( $query_params );
 102+ return;
 103+}
 104+
 105+/**
 106+ * Format contribution/tracking data in the desired way for OWA
 107+ *
 108+ * @param object $contrib_tracking_obj
 109+ * @param object $contrib_obj
 110+ * @return array The associatve array of transaction data to post to OWA
 111+ */
 112+function wmf_owa_prepare_owa_query( $contrib_tracking_obj, $contrib_obj ) {
66113 /**
67 - * pull the gateway out fo the trxn_id string
68 - * it is the element prior to the first space in the str
 114+ * Set the amount and owa order source fields
 115+ *
 116+ * If we are processing a refund, we need to do some funky stuff - like append RFD to the order source
 117+ * and fetch the original total amount, so that we can send OWA the inverse of the original total amount.
 118+ * This allows the transaction to be canceled out for the same OWA session.
69119 */
70 - $gateway = substr( $objectRef->trxn_id, 0, strpos( $objectRef->trxn_id, "\s" ));
 120+ if ( $contrib_obj->source == 'RFD' ) {
 121+ // append RFD to the order source - this allows to send a unique trxn using the same OWA session
 122+ $owa_ct_order_source = $contrib_tracking_obj->contribution_tracing_id . '-RFD';
 123+
 124+ // we want to send a negative amount to OWA to cancel out the original trxn, which means we need to fetch the original amount
 125+ $query = "SELECT total_amount FROM civicrm_contribution WHERE id=%d";
 126+ $result = db_fetch_object( db_query( $query, $contrib_obj->id ));
 127+ $total_amount = '-' . $result->total_amount;
 128+ } else {
 129+ $owa_ct_order_source = $contrib_tracking_obj->contribution_tracking_id;
 130+ $total_amount = $contrib_obj->total_amount;
 131+ }
71132
 133+ //fetch the gateway from the transaction id - it's the first element in the trxn_id
 134+ $gateway = substr( $trxn_id, 0, strpos( $trxn_id, "\s" ) );
 135+
72136 // format the contribution tracking message
73137 $query_params = array(
74138 "owa_event_type" => "ecommerce.transaction",
75 - "owa_ct_order_id" => $contrib_tracking_data->contribution_tracking_id,
76 - "owa_ct_order_source" => $contrib_tracking_data->utm_campaign,
77 - "owa_ct_total" => $objectRef->total_amount,
 139+ "owa_ct_order_id" => $owa_ct_order_source,
 140+ "owa_ct_order_source" => $contrib_tracking_obj->utm_campaign,
 141+ "owa_ct_total" => $total_amount,
78142 "owa_ct_tax" => 0,
79143 "owa_ct_shipping" => 0,
80144 "owa_ct_gateway" => $gateway,
81 - "owa_page_url" => $contrib_tracking_data->url,
82 - "owa_session_id"=> $contrib_tracking_data->owa_session,
 145+ "owa_page_url" => $contrib_tracking_obj->url,
 146+ "owa_session_id"=> $contrib_tracking_obj->owa_session,
83147 );
84148
85149 // log the query params
86 - watchdog( 'wmf_owa', 'OWA tracking params for contribution_id ' . $objectId . ': ' . print_r( $query_params, true ), WATCHDOG_DEBUG );
 150+ watchdog( 'wmf_owa',
 151+ 'OWA tracking params for contribution_id %objectId: %query_params',
 152+ array( '%objectId' => $objectId, '%query_params' => print_r( $query_params, true ) ),
 153+ WATCHDOG_DEBUG );
87154
88 - // sent the query to OWA
89 - wmf_owa_send_tracking( $query_params );
90 - return;
91 -
 155+ return $query_params;
92156 }
93157
94158 /**
@@ -102,8 +166,14 @@
103167 $result = db_fetch_object( db_query( $query, $contribution_id ));
104168
105169 // log $query and $result
106 - watchdog( 'wmf_owa', 'OWA query to contribution_tracking for contribution_id ' . $contribution_id . ': ' . $query, WATCHDOG_DEBUG );
107 - watchdog( 'wmf_owa', 'OWA query result from contribution_tracking for contribution_id ' . $contribution_id . ': ' . print_r( $result, true ), WATCHDOG_DEBUG );
 170+ watchdog( 'wmf_owa',
 171+ 'OWA query to contribution_tracking for contribution_id %contribution_id: %query',
 172+ array( '%contribution_id' => $contribution_id, '%query' => $query ),
 173+ WATCHDOG_DEBUG );
 174+ watchdog( 'wmf_owa',
 175+ 'OWA query result from contribution_tracking for contribution_id %contribution_id: %result',
 176+ array( '%contribution_id' => $contribution_id, '%result' => print_r( $result, true ) ),
 177+ WATCHDOG_DEBUG );
108178
109179 return $result;
110180 }
@@ -121,10 +191,13 @@
122192
123193 if ( !curl_exec( $ch )) {
124194 // log curl error curl_error( $ch );
125 - watchdog( 'wmf_owa', 'Error posting to OWA with query params: ' . print_r( $query_params, true ), WATCHDOG_ERROR );
 195+ watchdog( 'wmf_owa',
 196+ 'Error posting to OWA with query params: %params',
 197+ array( '%params' => print_r( $query_params, true ) ),
 198+ WATCHDOG_ERROR );
126199 } else {
127 - watchdog( 'wmf_owa', 'Posted to OWA for contribution_id: ' . $query_params[ 'owa_ct_order_id' ] ); // notice
128 - watchdog( 'wmf_owa', 'Posted to OWA with query params: ' . print_r( $query_params, true ), WATCHDOG_DEBUG ); //debug
 200+ watchdog( 'wmf_owa', 'Posted to OWA for contribution_id: %contribution_id', array( '%contribution_id' => $query_params[ 'owa_ct_order_id' ] ) ); // notice
 201+ watchdog( 'wmf_owa', 'Posted to OWA with query params: %query_params', array( '%query_params' => print_r( $query_params, true ) ), WATCHDOG_DEBUG ); //debug
129202 }
130203
131204 curl_close( $ch );

Status & tagging log