Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CiviContributeProcessor { | |
36 | static $_paypalParamsMapper = array( | |
37 | //category => array(paypal_param => civicrm_field); | |
38 | 'contact' => array( | |
39 | 'salutation' => 'prefix_id', | |
40 | 'firstname' => 'first_name', | |
41 | 'lastname' => 'last_name', | |
42 | 'middlename' => 'middle_name', | |
43 | 'suffix' => 'suffix_id', | |
44 | 'email' => 'email', | |
45 | ), | |
46 | 'location' => array( | |
47 | 'shiptoname' => 'address_name', | |
48 | 'shiptostreet' => 'street_address', | |
49 | 'shiptostreet2' => 'supplemental_address_1', | |
50 | 'shiptocity' => 'city', | |
51 | 'shiptostate' => 'state_province', | |
52 | 'shiptozip' => 'postal_code', | |
53 | 'countrycode' => 'country', | |
54 | ), | |
55 | 'transaction' => array( | |
56 | 'amt' => 'total_amount', | |
57 | 'feeamt' => 'fee_amount', | |
58 | 'transactionid' => 'trxn_id', | |
59 | 'currencycode' => 'currency', | |
60 | 'l_name0' => 'source', | |
61 | 'ordertime' => 'receive_date', | |
62 | 'note' => 'note', | |
63 | 'custom' => 'note', | |
64 | 'l_number0' => 'note', | |
65 | 'is_test' => 'is_test', | |
66 | 'transactiontype' => 'trxn_type', | |
67 | 'recurrences' => 'installments', | |
68 | 'l_amt2' => 'amount', | |
69 | 'l_period2' => 'lol', | |
70 | 'invnum' => 'invoice_id', | |
71 | 'subscriptiondate' => 'start_date', | |
72 | 'subscriptionid' => 'processor_id', | |
73 | 'timestamp' => 'modified_date', | |
74 | ), | |
75 | ); | |
76 | ||
77 | static $_googleParamsMapper = array( | |
78 | //category => array(google_param => civicrm_field); | |
79 | 'contact' => array( | |
80 | 'first-name' => 'first_name', | |
81 | 'last-name' => 'last_name', | |
82 | 'contact-name' => 'display_name', | |
83 | 'email' => 'email', | |
84 | ), | |
85 | 'location' => array( | |
86 | 'address1' => 'street_address', | |
87 | 'address2' => 'supplemental_address_1', | |
88 | 'city' => 'city', | |
89 | 'postal-code' => 'postal_code', | |
90 | 'country-code' => 'country', | |
91 | ), | |
92 | 'transaction' => array( | |
93 | 'total-charge-amount' => 'total_amount', | |
94 | 'google-order-number' => 'trxn_id', | |
95 | 'currency' => 'currency', | |
96 | 'item-name' => 'source', | |
97 | 'item-description' => 'note', | |
98 | 'timestamp' => 'receive_date', | |
99 | 'latest-charge-fee' => 'fee_amount', | |
100 | 'net-amount' => 'net_amount', | |
101 | 'times' => 'installments', | |
102 | 'period' => 'frequency_unit', | |
103 | 'frequency_interval' => 'frequency_interval', | |
104 | 'start_date' => 'start_date', | |
105 | 'modified_date' => 'modified_date', | |
106 | 'trxn_type' => 'trxn_type', | |
107 | 'amount' => 'amount', | |
108 | ), | |
109 | ); | |
110 | ||
111 | static $_csvParamsMapper = array( | |
112 | // Note: if csv header is not present in the mapper, header itself | |
113 | // is considered as a civicrm field. | |
114 | //category => array(csv_header => civicrm_field); | |
115 | 'contact' => array( | |
116 | 'first_name' => 'first_name', | |
117 | 'last_name' => 'last_name', | |
118 | 'middle_name' => 'middle_name', | |
119 | 'email' => 'email', | |
120 | ), | |
121 | 'location' => array( | |
122 | 'street_address' => 'street_address', | |
123 | 'supplemental_address_1' => 'supplemental_address_1', | |
124 | 'city' => 'city', | |
125 | 'postal_code' => 'postal_code', | |
126 | 'country' => 'country', | |
127 | ), | |
128 | 'transaction' => array( | |
129 | 'total_amount' => 'total_amount', | |
130 | 'trxn_id' => 'trxn_id', | |
131 | 'currency' => 'currency', | |
132 | 'source' => 'source', | |
133 | 'receive_date' => 'receive_date', | |
134 | 'note' => 'note', | |
135 | 'is_test' => 'is_test', | |
136 | ), | |
137 | ); | |
138 | ||
4e87860d EM |
139 | /** |
140 | * @param $paymentProcessor | |
141 | * @param $paymentMode | |
142 | * @param $start | |
143 | * @param $end | |
144 | */ | |
3bdca100 | 145 | public static function paypal($paymentProcessor, $paymentMode, $start, $end) { |
6a488035 TO |
146 | $url = "{$paymentProcessor['url_api']}nvp"; |
147 | ||
148 | $keyArgs = array( | |
149 | 'user' => $paymentProcessor['user_name'], | |
150 | 'pwd' => $paymentProcessor['password'], | |
151 | 'signature' => $paymentProcessor['signature'], | |
152 | 'version' => 3.0, | |
153 | ); | |
154 | ||
155 | $args = $keyArgs; | |
156 | $args += array( | |
157 | 'method' => 'TransactionSearch', | |
158 | 'startdate' => $start, | |
159 | 'enddate' => $end, | |
160 | ); | |
161 | ||
162 | require_once 'CRM/Core/Payment/PayPalImpl.php'; | |
163 | ||
164 | // as invokeAPI fetch only last 100 transactions. | |
165 | // we should require recursive calls to process more than 100. | |
166 | // first fetch transactions w/ give date intervals. | |
167 | // if we get error code w/ result, which means we do have more than 100 | |
168 | // manipulate date interval accordingly and fetch again. | |
169 | ||
170 | do { | |
171 | $result = CRM_Core_Payment_PayPalImpl::invokeAPI($args, $url); | |
172 | require_once "CRM/Contribute/BAO/Contribution/Utils.php"; | |
173 | ||
174 | $keyArgs['method'] = 'GetTransactionDetails'; | |
175 | foreach ($result as $name => $value) { | |
176 | if (substr($name, 0, 15) == 'l_transactionid') { | |
177 | ||
178 | // We don't/can't process subscription notifications, which appear | |
179 | // to be identified by transaction ids beginning with S- | |
180 | if (substr($value, 0, 2) == 'S-') { | |
181 | continue; | |
182 | } | |
183 | ||
184 | // Before we bother making a remote API call to PayPal to lookup | |
185 | // details about a transaction, let's make sure that it doesn't | |
186 | // already exist in the database. | |
187 | require_once 'CRM/Contribute/DAO/Contribution.php'; | |
3bdca100 | 188 | $dao = new CRM_Contribute_DAO_Contribution(); |
6a488035 TO |
189 | $dao->trxn_id = $value; |
190 | if ($dao->find(TRUE)) { | |
191 | preg_match('/(\d+)$/', $name, $matches); | |
56fdfc52 | 192 | $seq = $matches[1]; |
6a488035 | 193 | $email = $result["l_email{$seq}"]; |
56fdfc52 | 194 | $amt = $result["l_amt{$seq}"]; |
6a488035 TO |
195 | CRM_Core_Error::debug_log_message("Skipped (already recorded) - $email, $amt, $value ..<p>", TRUE); |
196 | continue; | |
197 | } | |
198 | ||
199 | $keyArgs['transactionid'] = $value; | |
200 | $trxnDetails = CRM_Core_Payment_PayPalImpl::invokeAPI($keyArgs, $url); | |
201 | if (is_a($trxnDetails, 'CRM_Core_Error')) { | |
202 | echo "PAYPAL ERROR: Skipping transaction id: $value<p>"; | |
203 | continue; | |
204 | } | |
205 | ||
206 | // only process completed payments | |
207 | if (strtolower($trxnDetails['paymentstatus']) != 'completed') { | |
208 | continue; | |
209 | } | |
210 | ||
211 | // only process receipts, not payments | |
212 | if (strtolower($trxnDetails['transactiontype']) == 'sendmoney') { | |
213 | continue; | |
214 | } | |
215 | ||
3c536a11 | 216 | $params = self::formatAPIParams($trxnDetails, |
6a488035 TO |
217 | self::$_paypalParamsMapper, |
218 | 'paypal' | |
219 | ); | |
220 | if ($paymentMode == 'test') { | |
221 | $params['transaction']['is_test'] = 1; | |
222 | } | |
223 | else { | |
224 | $params['transaction']['is_test'] = 0; | |
225 | } | |
226 | ||
3c536a11 | 227 | if (self::processAPIContribution($params)) { |
6a488035 TO |
228 | CRM_Core_Error::debug_log_message("Processed - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE); |
229 | } | |
230 | else { | |
231 | CRM_Core_Error::debug_log_message("Skipped - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE); | |
232 | } | |
233 | } | |
234 | } | |
235 | if ($result['l_errorcode0'] == '11002') { | |
56fdfc52 TO |
236 | $end = $result['l_timestamp99']; |
237 | $end_time = strtotime("{$end}", time()); | |
238 | $end_date = date('Y-m-d\T00:00:00.00\Z', $end_time); | |
6a488035 TO |
239 | $args['enddate'] = $end_date; |
240 | } | |
241 | } while ($result['l_errorcode0'] == '11002'); | |
242 | } | |
243 | ||
4e87860d EM |
244 | /** |
245 | * @param $paymentProcessor | |
246 | * @param $paymentMode | |
247 | * @param $start | |
248 | * @param $end | |
249 | */ | |
3bdca100 | 250 | public static function google($paymentProcessor, $paymentMode, $start, $end) { |
6a488035 TO |
251 | require_once "CRM/Contribute/BAO/Contribution/Utils.php"; |
252 | require_once 'CRM/Core/Payment/Google.php'; | |
253 | $nextPageToken = TRUE; | |
254 | $searchParams = array( | |
255 | 'start' => $start, | |
256 | 'end' => $end, | |
257 | 'notification-types' => array('charge-amount'), | |
258 | ); | |
259 | ||
260 | $response = CRM_Core_Payment_Google::invokeAPI($paymentProcessor, $searchParams); | |
261 | ||
262 | while ($nextPageToken) { | |
263 | if ($response[0] == 'error') { | |
264 | CRM_Core_Error::debug_log_message("GOOGLE ERROR: " . | |
265 | $response[1]['error']['error-message']['VALUE'], TRUE | |
266 | ); | |
267 | } | |
268 | $nextPageToken = isset($response[1][$response[0]]['next-page-token']['VALUE']) ? $response[1][$response[0]]['next-page-token']['VALUE'] : FALSE; | |
269 | ||
270 | if (is_array($response[1][$response[0]]['notifications']['charge-amount-notification'])) { | |
271 | ||
272 | if (array_key_exists('google-order-number', | |
56fdfc52 TO |
273 | $response[1][$response[0]]['notifications']['charge-amount-notification'] |
274 | )) { | |
6a488035 TO |
275 | // sometimes 'charge-amount-notification' itself is an absolute |
276 | // array and not array of arrays. This is the case when there is only one | |
277 | // charge-amount-notification. Hack for this special case - | |
278 | $chrgAmt = $response[1][$response[0]]['notifications']['charge-amount-notification']; | |
279 | unset($response[1][$response[0]]['notifications']['charge-amount-notification']); | |
280 | $response[1][$response[0]]['notifications']['charge-amount-notification'][] = $chrgAmt; | |
281 | } | |
282 | ||
3bdca100 | 283 | foreach ($response[1][$response[0]]['notifications']['charge-amount-notification'] as $amtData) { |
a9e80afc | 284 | $searchParams = array( |
56fdfc52 | 285 | 'order-numbers' => array($amtData['google-order-number']['VALUE']), |
6a488035 TO |
286 | 'notification-types' => array('risk-information', 'new-order', 'charge-amount'), |
287 | ); | |
288 | $response = CRM_Core_Payment_Google::invokeAPI($paymentProcessor, | |
289 | $searchParams | |
290 | ); | |
291 | // append amount information as well | |
292 | $response[] = $amtData; | |
293 | ||
3c536a11 | 294 | $params = self::formatAPIParams($response, |
6a488035 TO |
295 | self::$_googleParamsMapper, |
296 | 'google' | |
297 | ); | |
298 | if ($paymentMode == 'test') { | |
299 | $params['transaction']['is_test'] = 1; | |
300 | } | |
301 | else { | |
302 | $params['transaction']['is_test'] = 0; | |
303 | } | |
3c536a11 | 304 | if (self::processAPIContribution($params)) { |
6a488035 TO |
305 | CRM_Core_Error::debug_log_message("Processed - {$params['email']}, {$amtData['total-charge-amount']['VALUE']}, {$amtData['google-order-number']['VALUE']} ..<p>", TRUE); |
306 | } | |
307 | else { | |
308 | CRM_Core_Error::debug_log_message("Skipped - {$params['email']}, {$amtData['total-charge-amount']['VALUE']}, {$amtData['google-order-number']['VALUE']} ..<p>", TRUE); | |
309 | } | |
310 | } | |
311 | ||
312 | if ($nextPageToken) { | |
313 | $searchParams = array('next-page-token' => $nextPageToken); | |
314 | $response = CRM_Core_Payment_Google::invokeAPI($paymentProcessor, $searchParams); | |
315 | } | |
316 | } | |
317 | } | |
318 | } | |
319 | ||
3bdca100 | 320 | public static function csv() { |
56fdfc52 | 321 | $csvFile = '/home/deepak/Desktop/crm-4247.csv'; |
6a488035 | 322 | $delimiter = ";"; |
56fdfc52 | 323 | $row = 1; |
6a488035 TO |
324 | |
325 | $handle = fopen($csvFile, "r"); | |
326 | if (!$handle) { | |
327 | CRM_Core_Error::fatal("Can't locate csv file."); | |
328 | } | |
329 | ||
330 | require_once "CRM/Contribute/BAO/Contribution/Utils.php"; | |
331 | while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { | |
332 | if ($row !== 1) { | |
333 | $data['header'] = $header; | |
3c536a11 | 334 | $params = self::formatAPIParams($data, |
6a488035 TO |
335 | self::$_csvParamsMapper, |
336 | 'csv' | |
337 | ); | |
3c536a11 | 338 | if (self::processAPIContribution($params)) { |
6a488035 TO |
339 | CRM_Core_Error::debug_log_message("Processed - line $row of csv file .. {$params['email']}, {$params['transaction']['total_amount']}, {$params['transaction']['trxn_id']} ..<p>", TRUE); |
340 | } | |
341 | else { | |
342 | CRM_Core_Error::debug_log_message("Skipped - line $row of csv file .. {$params['email']}, {$params['transaction']['total_amount']}, {$params['transaction']['trxn_id']} ..<p>", TRUE); | |
343 | } | |
344 | ||
345 | // clean up memory from dao's | |
346 | CRM_Core_DAO::freeResult(); | |
347 | } | |
348 | else { | |
349 | // we assuming - first row is always the header line | |
350 | $header = $data; | |
351 | CRM_Core_Error::debug_log_message("Considering first row ( line $row ) as HEADER ..<p>", TRUE); | |
352 | ||
353 | if (empty($header)) { | |
354 | CRM_Core_Error::fatal("Header is empty."); | |
355 | } | |
356 | } | |
357 | $row++; | |
358 | } | |
359 | fclose($handle); | |
360 | } | |
361 | ||
3bdca100 | 362 | public static function process() { |
6a488035 TO |
363 | require_once 'CRM/Utils/Request.php'; |
364 | ||
365 | $type = CRM_Utils_Request::retrieve('type', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'csv', 'REQUEST'); | |
366 | $type = strtolower($type); | |
367 | ||
368 | switch ($type) { | |
369 | case 'paypal': | |
370 | case 'google': | |
371 | $start = CRM_Utils_Request::retrieve('start', 'String', | |
372 | CRM_Core_DAO::$_nullObject, FALSE, 31, 'REQUEST' | |
373 | ); | |
374 | $end = CRM_Utils_Request::retrieve('end', 'String', | |
375 | CRM_Core_DAO::$_nullObject, FALSE, 0, 'REQUEST' | |
376 | ); | |
377 | if ($start < $end) { | |
378 | CRM_Core_Error::fatal("Start offset can't be less than End offset."); | |
379 | } | |
380 | ||
381 | $start = date('Y-m-d', time() - $start * 24 * 60 * 60) . 'T00:00:00.00Z'; | |
382 | $end = date('Y-m-d', time() - $end * 24 * 60 * 60) . 'T23:59:00.00Z'; | |
383 | ||
384 | $ppID = CRM_Utils_Request::retrieve('ppID', 'Integer', | |
385 | CRM_Core_DAO::$_nullObject, TRUE, NULL, 'REQUEST' | |
386 | ); | |
387 | $mode = CRM_Utils_Request::retrieve('ppMode', 'String', | |
388 | CRM_Core_DAO::$_nullObject, FALSE, 'live', 'REQUEST' | |
389 | ); | |
390 | ||
6a488035 TO |
391 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($ppID, $mode); |
392 | ||
393 | CRM_Core_Error::debug_log_message("Start Date=$start, End Date=$end, ppID=$ppID, mode=$mode <p>", TRUE); | |
394 | ||
395 | return self::$type($paymentProcessor, $mode, $start, $end); | |
396 | ||
397 | case 'csv': | |
398 | return self::csv(); | |
399 | } | |
400 | } | |
96025800 | 401 | |
3c536a11 EM |
402 | /** |
403 | * @param array $apiParams | |
404 | * @param $mapper | |
405 | * @param string $type | |
406 | * @param bool $category | |
407 | * | |
408 | * @return array | |
409 | */ | |
410 | public static function formatAPIParams($apiParams, $mapper, $type = 'paypal', $category = TRUE) { | |
411 | $type = strtolower($type); | |
412 | ||
413 | if (!in_array($type, array( | |
414 | 'paypal', | |
415 | 'google', | |
416 | 'csv', | |
417 | )) | |
418 | ) { | |
419 | // return the params as is | |
420 | return $apiParams; | |
421 | } | |
422 | $params = $transaction = array(); | |
423 | ||
424 | if ($type == 'paypal') { | |
425 | foreach ($apiParams as $detail => $val) { | |
426 | if (isset($mapper['contact'][$detail])) { | |
427 | $params[$mapper['contact'][$detail]] = $val; | |
428 | } | |
429 | elseif (isset($mapper['location'][$detail])) { | |
430 | $params['address'][1][$mapper['location'][$detail]] = $val; | |
431 | } | |
432 | elseif (isset($mapper['transaction'][$detail])) { | |
433 | switch ($detail) { | |
434 | case 'l_period2': | |
435 | // Sadly, PayPal seems to send two distinct data elements in a single field, | |
436 | // so we break them out here. This is somewhat ugly and tragic. | |
437 | $freqUnits = array( | |
438 | 'D' => 'day', | |
439 | 'W' => 'week', | |
440 | 'M' => 'month', | |
441 | 'Y' => 'year', | |
442 | ); | |
443 | list($frequency_interval, $frequency_unit) = explode(' ', $val); | |
444 | $transaction['frequency_interval'] = $frequency_interval; | |
445 | $transaction['frequency_unit'] = $freqUnits[$frequency_unit]; | |
446 | break; | |
447 | ||
448 | case 'subscriptiondate': | |
449 | case 'timestamp': | |
450 | // PayPal dates are in ISO-8601 format. We need a format that | |
451 | // MySQL likes | |
452 | $unix_timestamp = strtotime($val); | |
453 | $transaction[$mapper['transaction'][$detail]] = date('YmdHis', $unix_timestamp); | |
454 | break; | |
455 | ||
456 | case 'note': | |
457 | case 'custom': | |
458 | case 'l_number0': | |
459 | if ($val) { | |
460 | $val = "[PayPal_field:{$detail}] {$val}"; | |
461 | $transaction[$mapper['transaction'][$detail]] = !empty($transaction[$mapper['transaction'][$detail]]) ? $transaction[$mapper['transaction'][$detail]] . " <br/> " . $val : $val; | |
462 | } | |
463 | break; | |
464 | ||
465 | default: | |
466 | $transaction[$mapper['transaction'][$detail]] = $val; | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
471 | if (!empty($transaction) && $category) { | |
472 | $params['transaction'] = $transaction; | |
473 | } | |
474 | else { | |
475 | $params += $transaction; | |
476 | } | |
477 | ||
478 | CRM_Contribute_BAO_Contribution_Utils::_fillCommonParams($params, $type); | |
479 | ||
480 | return $params; | |
481 | } | |
482 | ||
483 | if ($type == 'csv') { | |
484 | $header = $apiParams['header']; | |
485 | unset($apiParams['header']); | |
486 | foreach ($apiParams as $key => $val) { | |
487 | if (isset($mapper['contact'][$header[$key]])) { | |
488 | $params[$mapper['contact'][$header[$key]]] = $val; | |
489 | } | |
490 | elseif (isset($mapper['location'][$header[$key]])) { | |
491 | $params['address'][1][$mapper['location'][$header[$key]]] = $val; | |
492 | } | |
493 | elseif (isset($mapper['transaction'][$header[$key]])) { | |
494 | $transaction[$mapper['transaction'][$header[$key]]] = $val; | |
495 | } | |
496 | else { | |
497 | $params[$header[$key]] = $val; | |
498 | } | |
499 | } | |
500 | ||
501 | if (!empty($transaction) && $category) { | |
502 | $params['transaction'] = $transaction; | |
503 | } | |
504 | else { | |
505 | $params += $transaction; | |
506 | } | |
507 | ||
508 | CRM_Contribute_BAO_Contribution_Utils::_fillCommonParams($params, $type); | |
509 | ||
510 | return $params; | |
511 | } | |
512 | ||
513 | if ($type == 'google') { | |
514 | // return if response smell invalid | |
515 | if (!array_key_exists('risk-information-notification', $apiParams[1][$apiParams[0]]['notifications'])) { | |
516 | return FALSE; | |
517 | } | |
518 | $riskInfo = &$apiParams[1][$apiParams[0]]['notifications']['risk-information-notification']; | |
519 | ||
520 | if (array_key_exists('new-order-notification', $apiParams[1][$apiParams[0]]['notifications'])) { | |
521 | $newOrder = &$apiParams[1][$apiParams[0]]['notifications']['new-order-notification']; | |
522 | } | |
523 | ||
524 | if ($riskInfo['google-order-number']['VALUE'] == $apiParams[2]['google-order-number']['VALUE']) { | |
525 | foreach ($riskInfo['risk-information']['billing-address'] as $field => $info) { | |
526 | if (!empty($mapper['location'][$field])) { | |
527 | $params['address'][1][$mapper['location'][$field]] = $info['VALUE']; | |
528 | } | |
529 | elseif (!empty($mapper['contact'][$field])) { | |
530 | if ($newOrder && !empty($newOrder['buyer-billing-address']['structured-name'])) { | |
531 | foreach ($newOrder['buyer-billing-address']['structured-name'] as $namePart => $nameValue) { | |
532 | $params[$mapper['contact'][$namePart]] = $nameValue['VALUE']; | |
533 | } | |
534 | } | |
535 | else { | |
536 | $params[$mapper['contact'][$field]] = $info['VALUE']; | |
537 | } | |
538 | } | |
539 | elseif (!empty($mapper['transaction'][$field])) { | |
540 | $transaction[$mapper['transaction'][$field]] = $info['VALUE']; | |
541 | } | |
542 | } | |
543 | ||
544 | // Response is an huge array. Lets pickup only those which we ineterested in | |
545 | // using a local mapper, rather than traversing the entire array. | |
546 | $localMapper = array( | |
547 | 'google-order-number' => $riskInfo['google-order-number']['VALUE'], | |
548 | 'total-charge-amount' => $apiParams[2]['total-charge-amount']['VALUE'], | |
549 | 'currency' => $apiParams[2]['total-charge-amount']['currency'], | |
550 | 'item-name' => $newOrder['shopping-cart']['items']['item']['item-name']['VALUE'], | |
551 | 'timestamp' => $apiParams[2]['timestamp']['VALUE'], | |
552 | ); | |
553 | if (array_key_exists('latest-charge-fee', $apiParams[2])) { | |
554 | $localMapper['latest-charge-fee'] = $apiParams[2]['latest-charge-fee']['total']['VALUE']; | |
555 | $localMapper['net-amount'] = $localMapper['total-charge-amount'] - $localMapper['latest-charge-fee']; | |
556 | } | |
557 | ||
558 | // This is a subscription (recurring) donation. | |
559 | if (array_key_exists('subscription', $newOrder['shopping-cart']['items']['item'])) { | |
560 | $subscription = $newOrder['shopping-cart']['items']['item']['subscription']; | |
561 | $localMapper['amount'] = $newOrder['order-total']['VALUE']; | |
562 | $localMapper['times'] = $subscription['payments']['subscription-payment']['times']; | |
563 | // Convert Google's period to one compatible with the CiviCRM db field. | |
564 | $freqUnits = array( | |
565 | 'DAILY' => 'day', | |
566 | 'WEEKLY' => 'week', | |
567 | 'MONHTLY' => 'month', | |
568 | 'YEARLY' => 'year', | |
569 | ); | |
570 | $localMapper['period'] = $freqUnits[$subscription['period']]; | |
571 | // Unlike PayPal, Google has no concept of freq. interval, it is always 1. | |
572 | $localMapper['frequency_interval'] = '1'; | |
573 | // Google Checkout dates are in ISO-8601 format. We need a format that | |
574 | // MySQL likes | |
575 | $unix_timestamp = strtotime($localMapper['timestamp']); | |
576 | $mysql_date = date('YmdHis', $unix_timestamp); | |
577 | $localMapper['modified_date'] = $mysql_date; | |
578 | $localMapper['start_date'] = $mysql_date; | |
579 | // This is PayPal's nomenclature, but just use it for Google as well since | |
580 | // we act on the value of trxn_type in processAPIContribution(). | |
581 | $localMapper['trxn_type'] = 'subscrpayment'; | |
582 | } | |
583 | ||
584 | foreach ($localMapper as $localKey => $localVal) { | |
585 | if (!empty($mapper['transaction'][$localKey])) { | |
586 | $transaction[$mapper['transaction'][$localKey]] = $localVal; | |
587 | } | |
588 | } | |
589 | ||
590 | if (empty($params) && empty($transaction)) { | |
591 | continue; | |
592 | } | |
593 | ||
594 | if (!empty($transaction) && $category) { | |
595 | $params['transaction'] = $transaction; | |
596 | } | |
597 | else { | |
598 | $params += $transaction; | |
599 | } | |
600 | ||
601 | CRM_Contribute_BAO_Contribution_Utils::_fillCommonParams($params, $type); | |
602 | } | |
603 | return $params; | |
604 | } | |
605 | } | |
606 | ||
607 | /** | |
608 | * @param array $params | |
609 | * | |
610 | * @return bool | |
611 | */ | |
612 | public static function processAPIContribution($params) { | |
613 | if (empty($params) || array_key_exists('error', $params)) { | |
614 | return FALSE; | |
615 | } | |
616 | ||
617 | // add contact using dedupe rule | |
618 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual'); | |
619 | $dedupeParams['check_permission'] = FALSE; | |
620 | $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); | |
621 | // if we find more than one contact, use the first one | |
622 | if (!empty($dupeIds[0])) { | |
623 | $params['contact_id'] = $dupeIds[0]; | |
624 | } | |
625 | $contact = CRM_Contact_BAO_Contact::create($params); | |
626 | if (!$contact->id) { | |
627 | return FALSE; | |
628 | } | |
629 | ||
630 | // only pass transaction params to contribution::create, if available | |
631 | if (array_key_exists('transaction', $params)) { | |
632 | $params = $params['transaction']; | |
633 | $params['contact_id'] = $contact->id; | |
634 | } | |
635 | ||
636 | // handle contribution custom data | |
637 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', | |
638 | FALSE, | |
639 | FALSE, | |
640 | CRM_Utils_Array::value('financial_type_id', | |
641 | $params | |
642 | ) | |
643 | ); | |
644 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
645 | CRM_Utils_Array::value('id', $params, NULL), | |
646 | 'Contribution' | |
647 | ); | |
648 | // create contribution | |
649 | ||
650 | // if this is a recurring contribution then process it first | |
651 | if ($params['trxn_type'] == 'subscrpayment') { | |
652 | // see if a recurring record already exists | |
653 | $recurring = new CRM_Contribute_BAO_ContributionRecur(); | |
654 | $recurring->processor_id = $params['processor_id']; | |
655 | if (!$recurring->find(TRUE)) { | |
656 | $recurring = new CRM_Contribute_BAO_ContributionRecur(); | |
657 | $recurring->invoice_id = $params['invoice_id']; | |
658 | $recurring->find(TRUE); | |
659 | } | |
660 | ||
661 | // This is the same thing the CiviCRM IPN handler does to handle | |
662 | // subsequent recurring payments to avoid duplicate contribution | |
663 | // errors due to invoice ID. See: | |
664 | // ./CRM/Core/Payment/PayPalIPN.php:200 | |
665 | if ($recurring->id) { | |
666 | $params['invoice_id'] = md5(uniqid(rand(), TRUE)); | |
667 | } | |
668 | ||
669 | $recurring->copyValues($params); | |
670 | $recurring->save(); | |
671 | if (is_a($recurring, 'CRM_Core_Error')) { | |
672 | return FALSE; | |
673 | } | |
674 | else { | |
675 | $params['contribution_recur_id'] = $recurring->id; | |
676 | } | |
677 | } | |
678 | ||
679 | $contribution = &CRM_Contribute_BAO_Contribution::create($params, | |
680 | CRM_Core_DAO::$_nullArray | |
681 | ); | |
682 | if (!$contribution->id) { | |
683 | return FALSE; | |
684 | } | |
685 | ||
686 | return TRUE; | |
687 | } | |
688 | ||
6a488035 TO |
689 | } |
690 | ||
691 | // bootstrap the environment and run the processor | |
692 | session_start(); | |
693 | require_once '../civicrm.config.php'; | |
694 | require_once 'CRM/Core/Config.php'; | |
695 | $config = CRM_Core_Config::singleton(); | |
696 | ||
697 | CRM_Utils_System::authenticateScript(TRUE); | |
698 | ||
699 | //log the execution of script | |
700 | CRM_Core_Error::debug_log_message('ContributionProcessor.php'); | |
701 | ||
33c5988b | 702 | $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire('worker.contribute.CiviContributeProcessor'); |
6a488035 TO |
703 | |
704 | if ($lock->isAcquired()) { | |
705 | // try to unset any time limits | |
706 | if (!ini_get('safe_mode')) { | |
707 | set_time_limit(0); | |
708 | } | |
709 | ||
710 | CiviContributeProcessor::process(); | |
711 | } | |
712 | else { | |
33c5988b | 713 | throw new Exception('Could not acquire lock, another CiviContributeProcessor process is running'); |
6a488035 TO |
714 | } |
715 | ||
716 | $lock->release(); | |
717 | ||
718 | echo "Done processing<p>"; |