Index: civicrm/trunk/sites/all/modules/wmf_owa/wmf_owa.module |
— | — | @@ -43,14 +43,16 @@ |
44 | 44 | /** |
45 | 45 | * Implementation of hook_civicrm_post |
46 | 46 | * |
| 47 | + * @fixme What about in the event of a contrib deletion? |
| 48 | + * |
47 | 49 | * @param unknown_type $op |
48 | 50 | * @param unknown_type $objectName |
49 | 51 | * @param unknown_type $objectId |
50 | 52 | * @param unknown_type $objectRef |
51 | 53 | */ |
52 | 54 | 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' )) ) { |
55 | 57 | return; |
56 | 58 | } |
57 | 59 | |
— | — | @@ -58,36 +60,98 @@ |
59 | 61 | $contrib_tracking_data = wmf_owa_get_contribution_tracking_data( $objectId ); |
60 | 62 | if ( !$contrib_tracking_data ) { |
61 | 63 | // 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 ); |
63 | 65 | return; |
64 | 66 | } |
65 | 67 | |
| 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 ) { |
66 | 113 | /** |
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. |
69 | 119 | */ |
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 | + } |
71 | 132 | |
| 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 | + |
72 | 136 | // format the contribution tracking message |
73 | 137 | $query_params = array( |
74 | 138 | "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, |
78 | 142 | "owa_ct_tax" => 0, |
79 | 143 | "owa_ct_shipping" => 0, |
80 | 144 | "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, |
83 | 147 | ); |
84 | 148 | |
85 | 149 | // 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 ); |
87 | 154 | |
88 | | - // sent the query to OWA |
89 | | - wmf_owa_send_tracking( $query_params ); |
90 | | - return; |
91 | | - |
| 155 | + return $query_params; |
92 | 156 | } |
93 | 157 | |
94 | 158 | /** |
— | — | @@ -102,8 +166,14 @@ |
103 | 167 | $result = db_fetch_object( db_query( $query, $contribution_id )); |
104 | 168 | |
105 | 169 | // 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 ); |
108 | 178 | |
109 | 179 | return $result; |
110 | 180 | } |
— | — | @@ -121,10 +191,13 @@ |
122 | 192 | |
123 | 193 | if ( !curl_exec( $ch )) { |
124 | 194 | // 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 ); |
126 | 199 | } 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 |
129 | 202 | } |
130 | 203 | |
131 | 204 | curl_close( $ch ); |