Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
626f739f | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
626f739f | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | class CiviContributeProcessor { | |
34 | static $_paypalParamsMapper = array( | |
35 | //category => array(paypal_param => civicrm_field); | |
36 | 'contact' => array( | |
37 | 'salutation' => 'prefix_id', | |
38 | 'firstname' => 'first_name', | |
39 | 'lastname' => 'last_name', | |
40 | 'middlename' => 'middle_name', | |
41 | 'suffix' => 'suffix_id', | |
42 | 'email' => 'email', | |
43 | ), | |
44 | 'location' => array( | |
45 | 'shiptoname' => 'address_name', | |
46 | 'shiptostreet' => 'street_address', | |
47 | 'shiptostreet2' => 'supplemental_address_1', | |
48 | 'shiptocity' => 'city', | |
49 | 'shiptostate' => 'state_province', | |
50 | 'shiptozip' => 'postal_code', | |
51 | 'countrycode' => 'country', | |
52 | ), | |
53 | 'transaction' => array( | |
54 | 'amt' => 'total_amount', | |
55 | 'feeamt' => 'fee_amount', | |
56 | 'transactionid' => 'trxn_id', | |
57 | 'currencycode' => 'currency', | |
58 | 'l_name0' => 'source', | |
59 | 'ordertime' => 'receive_date', | |
60 | 'note' => 'note', | |
61 | 'custom' => 'note', | |
62 | 'l_number0' => 'note', | |
63 | 'is_test' => 'is_test', | |
64 | 'transactiontype' => 'trxn_type', | |
65 | 'recurrences' => 'installments', | |
66 | 'l_amt2' => 'amount', | |
67 | 'l_period2' => 'lol', | |
68 | 'invnum' => 'invoice_id', | |
69 | 'subscriptiondate' => 'start_date', | |
70 | 'subscriptionid' => 'processor_id', | |
71 | 'timestamp' => 'modified_date', | |
72 | ), | |
73 | ); | |
74 | ||
6a488035 TO |
75 | static $_csvParamsMapper = array( |
76 | // Note: if csv header is not present in the mapper, header itself | |
77 | // is considered as a civicrm field. | |
78 | //category => array(csv_header => civicrm_field); | |
79 | 'contact' => array( | |
80 | 'first_name' => 'first_name', | |
81 | 'last_name' => 'last_name', | |
82 | 'middle_name' => 'middle_name', | |
83 | 'email' => 'email', | |
84 | ), | |
85 | 'location' => array( | |
86 | 'street_address' => 'street_address', | |
87 | 'supplemental_address_1' => 'supplemental_address_1', | |
88 | 'city' => 'city', | |
89 | 'postal_code' => 'postal_code', | |
90 | 'country' => 'country', | |
91 | ), | |
92 | 'transaction' => array( | |
93 | 'total_amount' => 'total_amount', | |
94 | 'trxn_id' => 'trxn_id', | |
95 | 'currency' => 'currency', | |
96 | 'source' => 'source', | |
97 | 'receive_date' => 'receive_date', | |
98 | 'note' => 'note', | |
99 | 'is_test' => 'is_test', | |
100 | ), | |
101 | ); | |
102 | ||
4e87860d EM |
103 | /** |
104 | * @param $paymentProcessor | |
105 | * @param $paymentMode | |
106 | * @param $start | |
107 | * @param $end | |
108 | */ | |
3bdca100 | 109 | public static function paypal($paymentProcessor, $paymentMode, $start, $end) { |
6a488035 TO |
110 | $url = "{$paymentProcessor['url_api']}nvp"; |
111 | ||
112 | $keyArgs = array( | |
113 | 'user' => $paymentProcessor['user_name'], | |
114 | 'pwd' => $paymentProcessor['password'], | |
115 | 'signature' => $paymentProcessor['signature'], | |
116 | 'version' => 3.0, | |
117 | ); | |
118 | ||
119 | $args = $keyArgs; | |
120 | $args += array( | |
121 | 'method' => 'TransactionSearch', | |
122 | 'startdate' => $start, | |
123 | 'enddate' => $end, | |
124 | ); | |
125 | ||
126 | require_once 'CRM/Core/Payment/PayPalImpl.php'; | |
127 | ||
128 | // as invokeAPI fetch only last 100 transactions. | |
129 | // we should require recursive calls to process more than 100. | |
130 | // first fetch transactions w/ give date intervals. | |
131 | // if we get error code w/ result, which means we do have more than 100 | |
132 | // manipulate date interval accordingly and fetch again. | |
133 | ||
134 | do { | |
135 | $result = CRM_Core_Payment_PayPalImpl::invokeAPI($args, $url); | |
136 | require_once "CRM/Contribute/BAO/Contribution/Utils.php"; | |
137 | ||
138 | $keyArgs['method'] = 'GetTransactionDetails'; | |
139 | foreach ($result as $name => $value) { | |
140 | if (substr($name, 0, 15) == 'l_transactionid') { | |
141 | ||
142 | // We don't/can't process subscription notifications, which appear | |
143 | // to be identified by transaction ids beginning with S- | |
144 | if (substr($value, 0, 2) == 'S-') { | |
145 | continue; | |
146 | } | |
147 | ||
148 | // Before we bother making a remote API call to PayPal to lookup | |
149 | // details about a transaction, let's make sure that it doesn't | |
150 | // already exist in the database. | |
151 | require_once 'CRM/Contribute/DAO/Contribution.php'; | |
3bdca100 | 152 | $dao = new CRM_Contribute_DAO_Contribution(); |
6a488035 TO |
153 | $dao->trxn_id = $value; |
154 | if ($dao->find(TRUE)) { | |
155 | preg_match('/(\d+)$/', $name, $matches); | |
56fdfc52 | 156 | $seq = $matches[1]; |
6a488035 | 157 | $email = $result["l_email{$seq}"]; |
56fdfc52 | 158 | $amt = $result["l_amt{$seq}"]; |
6a488035 TO |
159 | CRM_Core_Error::debug_log_message("Skipped (already recorded) - $email, $amt, $value ..<p>", TRUE); |
160 | continue; | |
161 | } | |
162 | ||
163 | $keyArgs['transactionid'] = $value; | |
164 | $trxnDetails = CRM_Core_Payment_PayPalImpl::invokeAPI($keyArgs, $url); | |
165 | if (is_a($trxnDetails, 'CRM_Core_Error')) { | |
166 | echo "PAYPAL ERROR: Skipping transaction id: $value<p>"; | |
167 | continue; | |
168 | } | |
169 | ||
170 | // only process completed payments | |
171 | if (strtolower($trxnDetails['paymentstatus']) != 'completed') { | |
172 | continue; | |
173 | } | |
174 | ||
175 | // only process receipts, not payments | |
176 | if (strtolower($trxnDetails['transactiontype']) == 'sendmoney') { | |
177 | continue; | |
178 | } | |
179 | ||
3c536a11 | 180 | $params = self::formatAPIParams($trxnDetails, |
6a488035 TO |
181 | self::$_paypalParamsMapper, |
182 | 'paypal' | |
183 | ); | |
184 | if ($paymentMode == 'test') { | |
185 | $params['transaction']['is_test'] = 1; | |
186 | } | |
187 | else { | |
188 | $params['transaction']['is_test'] = 0; | |
189 | } | |
190 | ||
fedc3428 | 191 | try { |
192 | if (self::processAPIContribution($params)) { | |
193 | CRM_Core_Error::debug_log_message("Processed - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE); | |
194 | } | |
195 | else { | |
196 | CRM_Core_Error::debug_log_message("Skipped - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE); | |
197 | } | |
6a488035 | 198 | } |
fedc3428 | 199 | catch (CiviCRM_API3_Exception $e) { |
6a488035 TO |
200 | CRM_Core_Error::debug_log_message("Skipped - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE); |
201 | } | |
202 | } | |
203 | } | |
204 | if ($result['l_errorcode0'] == '11002') { | |
56fdfc52 TO |
205 | $end = $result['l_timestamp99']; |
206 | $end_time = strtotime("{$end}", time()); | |
207 | $end_date = date('Y-m-d\T00:00:00.00\Z', $end_time); | |
6a488035 TO |
208 | $args['enddate'] = $end_date; |
209 | } | |
210 | } while ($result['l_errorcode0'] == '11002'); | |
211 | } | |
212 | ||
3bdca100 | 213 | public static function csv() { |
56fdfc52 | 214 | $csvFile = '/home/deepak/Desktop/crm-4247.csv'; |
6a488035 | 215 | $delimiter = ";"; |
56fdfc52 | 216 | $row = 1; |
6a488035 TO |
217 | |
218 | $handle = fopen($csvFile, "r"); | |
219 | if (!$handle) { | |
220 | CRM_Core_Error::fatal("Can't locate csv file."); | |
221 | } | |
222 | ||
223 | require_once "CRM/Contribute/BAO/Contribution/Utils.php"; | |
224 | while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { | |
225 | if ($row !== 1) { | |
226 | $data['header'] = $header; | |
3c536a11 | 227 | $params = self::formatAPIParams($data, |
6a488035 TO |
228 | self::$_csvParamsMapper, |
229 | 'csv' | |
230 | ); | |
3c536a11 | 231 | if (self::processAPIContribution($params)) { |
6a488035 TO |
232 | CRM_Core_Error::debug_log_message("Processed - line $row of csv file .. {$params['email']}, {$params['transaction']['total_amount']}, {$params['transaction']['trxn_id']} ..<p>", TRUE); |
233 | } | |
234 | else { | |
235 | CRM_Core_Error::debug_log_message("Skipped - line $row of csv file .. {$params['email']}, {$params['transaction']['total_amount']}, {$params['transaction']['trxn_id']} ..<p>", TRUE); | |
236 | } | |
237 | ||
238 | // clean up memory from dao's | |
239 | CRM_Core_DAO::freeResult(); | |
240 | } | |
241 | else { | |
242 | // we assuming - first row is always the header line | |
243 | $header = $data; | |
244 | CRM_Core_Error::debug_log_message("Considering first row ( line $row ) as HEADER ..<p>", TRUE); | |
245 | ||
246 | if (empty($header)) { | |
247 | CRM_Core_Error::fatal("Header is empty."); | |
248 | } | |
249 | } | |
250 | $row++; | |
251 | } | |
252 | fclose($handle); | |
253 | } | |
254 | ||
3bdca100 | 255 | public static function process() { |
6a488035 TO |
256 | require_once 'CRM/Utils/Request.php'; |
257 | ||
258 | $type = CRM_Utils_Request::retrieve('type', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'csv', 'REQUEST'); | |
259 | $type = strtolower($type); | |
260 | ||
261 | switch ($type) { | |
262 | case 'paypal': | |
6a488035 TO |
263 | $start = CRM_Utils_Request::retrieve('start', 'String', |
264 | CRM_Core_DAO::$_nullObject, FALSE, 31, 'REQUEST' | |
265 | ); | |
266 | $end = CRM_Utils_Request::retrieve('end', 'String', | |
267 | CRM_Core_DAO::$_nullObject, FALSE, 0, 'REQUEST' | |
268 | ); | |
269 | if ($start < $end) { | |
270 | CRM_Core_Error::fatal("Start offset can't be less than End offset."); | |
271 | } | |
272 | ||
273 | $start = date('Y-m-d', time() - $start * 24 * 60 * 60) . 'T00:00:00.00Z'; | |
274 | $end = date('Y-m-d', time() - $end * 24 * 60 * 60) . 'T23:59:00.00Z'; | |
275 | ||
276 | $ppID = CRM_Utils_Request::retrieve('ppID', 'Integer', | |
277 | CRM_Core_DAO::$_nullObject, TRUE, NULL, 'REQUEST' | |
278 | ); | |
279 | $mode = CRM_Utils_Request::retrieve('ppMode', 'String', | |
280 | CRM_Core_DAO::$_nullObject, FALSE, 'live', 'REQUEST' | |
281 | ); | |
282 | ||
6a488035 TO |
283 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($ppID, $mode); |
284 | ||
285 | CRM_Core_Error::debug_log_message("Start Date=$start, End Date=$end, ppID=$ppID, mode=$mode <p>", TRUE); | |
286 | ||
287 | return self::$type($paymentProcessor, $mode, $start, $end); | |
288 | ||
289 | case 'csv': | |
290 | return self::csv(); | |
291 | } | |
292 | } | |
96025800 | 293 | |
3c536a11 EM |
294 | /** |
295 | * @param array $apiParams | |
296 | * @param $mapper | |
297 | * @param string $type | |
298 | * @param bool $category | |
299 | * | |
300 | * @return array | |
301 | */ | |
302 | public static function formatAPIParams($apiParams, $mapper, $type = 'paypal', $category = TRUE) { | |
303 | $type = strtolower($type); | |
304 | ||
305 | if (!in_array($type, array( | |
306 | 'paypal', | |
3c536a11 EM |
307 | 'csv', |
308 | )) | |
309 | ) { | |
310 | // return the params as is | |
311 | return $apiParams; | |
312 | } | |
313 | $params = $transaction = array(); | |
314 | ||
315 | if ($type == 'paypal') { | |
316 | foreach ($apiParams as $detail => $val) { | |
317 | if (isset($mapper['contact'][$detail])) { | |
318 | $params[$mapper['contact'][$detail]] = $val; | |
319 | } | |
320 | elseif (isset($mapper['location'][$detail])) { | |
321 | $params['address'][1][$mapper['location'][$detail]] = $val; | |
322 | } | |
323 | elseif (isset($mapper['transaction'][$detail])) { | |
324 | switch ($detail) { | |
325 | case 'l_period2': | |
326 | // Sadly, PayPal seems to send two distinct data elements in a single field, | |
327 | // so we break them out here. This is somewhat ugly and tragic. | |
328 | $freqUnits = array( | |
329 | 'D' => 'day', | |
330 | 'W' => 'week', | |
331 | 'M' => 'month', | |
332 | 'Y' => 'year', | |
333 | ); | |
334 | list($frequency_interval, $frequency_unit) = explode(' ', $val); | |
335 | $transaction['frequency_interval'] = $frequency_interval; | |
336 | $transaction['frequency_unit'] = $freqUnits[$frequency_unit]; | |
337 | break; | |
338 | ||
339 | case 'subscriptiondate': | |
340 | case 'timestamp': | |
341 | // PayPal dates are in ISO-8601 format. We need a format that | |
342 | // MySQL likes | |
343 | $unix_timestamp = strtotime($val); | |
344 | $transaction[$mapper['transaction'][$detail]] = date('YmdHis', $unix_timestamp); | |
345 | break; | |
346 | ||
347 | case 'note': | |
348 | case 'custom': | |
349 | case 'l_number0': | |
350 | if ($val) { | |
351 | $val = "[PayPal_field:{$detail}] {$val}"; | |
352 | $transaction[$mapper['transaction'][$detail]] = !empty($transaction[$mapper['transaction'][$detail]]) ? $transaction[$mapper['transaction'][$detail]] . " <br/> " . $val : $val; | |
353 | } | |
354 | break; | |
355 | ||
356 | default: | |
357 | $transaction[$mapper['transaction'][$detail]] = $val; | |
358 | } | |
359 | } | |
360 | } | |
361 | ||
362 | if (!empty($transaction) && $category) { | |
363 | $params['transaction'] = $transaction; | |
364 | } | |
365 | else { | |
366 | $params += $transaction; | |
367 | } | |
368 | ||
369 | CRM_Contribute_BAO_Contribution_Utils::_fillCommonParams($params, $type); | |
370 | ||
371 | return $params; | |
372 | } | |
373 | ||
374 | if ($type == 'csv') { | |
375 | $header = $apiParams['header']; | |
376 | unset($apiParams['header']); | |
377 | foreach ($apiParams as $key => $val) { | |
378 | if (isset($mapper['contact'][$header[$key]])) { | |
379 | $params[$mapper['contact'][$header[$key]]] = $val; | |
380 | } | |
381 | elseif (isset($mapper['location'][$header[$key]])) { | |
382 | $params['address'][1][$mapper['location'][$header[$key]]] = $val; | |
383 | } | |
384 | elseif (isset($mapper['transaction'][$header[$key]])) { | |
385 | $transaction[$mapper['transaction'][$header[$key]]] = $val; | |
386 | } | |
387 | else { | |
388 | $params[$header[$key]] = $val; | |
389 | } | |
390 | } | |
391 | ||
392 | if (!empty($transaction) && $category) { | |
393 | $params['transaction'] = $transaction; | |
394 | } | |
395 | else { | |
396 | $params += $transaction; | |
397 | } | |
398 | ||
399 | CRM_Contribute_BAO_Contribution_Utils::_fillCommonParams($params, $type); | |
400 | ||
401 | return $params; | |
402 | } | |
403 | ||
3c536a11 EM |
404 | } |
405 | ||
406 | /** | |
fedc3428 | 407 | * @deprecated function. |
408 | * | |
409 | * This function has probably been defunct for quite a long time. | |
410 | * | |
3c536a11 EM |
411 | * @param array $params |
412 | * | |
413 | * @return bool | |
414 | */ | |
415 | public static function processAPIContribution($params) { | |
416 | if (empty($params) || array_key_exists('error', $params)) { | |
417 | return FALSE; | |
418 | } | |
419 | ||
fedc3428 | 420 | $params['contact_id'] = CRM_Contact_BAO_Contact::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', array(), FALSE); |
421 | ||
422 | $contact = civicrm_api3('Contact', 'create', $params); | |
3c536a11 EM |
423 | |
424 | // only pass transaction params to contribution::create, if available | |
425 | if (array_key_exists('transaction', $params)) { | |
426 | $params = $params['transaction']; | |
fedc3428 | 427 | $params['contact_id'] = $contact['id']; |
3c536a11 EM |
428 | } |
429 | ||
3c536a11 EM |
430 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, |
431 | CRM_Utils_Array::value('id', $params, NULL), | |
432 | 'Contribution' | |
433 | ); | |
434 | // create contribution | |
435 | ||
436 | // if this is a recurring contribution then process it first | |
437 | if ($params['trxn_type'] == 'subscrpayment') { | |
438 | // see if a recurring record already exists | |
439 | $recurring = new CRM_Contribute_BAO_ContributionRecur(); | |
440 | $recurring->processor_id = $params['processor_id']; | |
441 | if (!$recurring->find(TRUE)) { | |
442 | $recurring = new CRM_Contribute_BAO_ContributionRecur(); | |
443 | $recurring->invoice_id = $params['invoice_id']; | |
444 | $recurring->find(TRUE); | |
445 | } | |
446 | ||
447 | // This is the same thing the CiviCRM IPN handler does to handle | |
448 | // subsequent recurring payments to avoid duplicate contribution | |
449 | // errors due to invoice ID. See: | |
450 | // ./CRM/Core/Payment/PayPalIPN.php:200 | |
451 | if ($recurring->id) { | |
452 | $params['invoice_id'] = md5(uniqid(rand(), TRUE)); | |
453 | } | |
454 | ||
455 | $recurring->copyValues($params); | |
456 | $recurring->save(); | |
457 | if (is_a($recurring, 'CRM_Core_Error')) { | |
458 | return FALSE; | |
459 | } | |
460 | else { | |
461 | $params['contribution_recur_id'] = $recurring->id; | |
462 | } | |
463 | } | |
464 | ||
33621c4f | 465 | $contribution = CRM_Contribute_BAO_Contribution::create($params); |
3c536a11 EM |
466 | if (!$contribution->id) { |
467 | return FALSE; | |
468 | } | |
469 | ||
470 | return TRUE; | |
471 | } | |
472 | ||
6a488035 TO |
473 | } |
474 | ||
475 | // bootstrap the environment and run the processor | |
476 | session_start(); | |
477 | require_once '../civicrm.config.php'; | |
478 | require_once 'CRM/Core/Config.php'; | |
479 | $config = CRM_Core_Config::singleton(); | |
480 | ||
481 | CRM_Utils_System::authenticateScript(TRUE); | |
482 | ||
483 | //log the execution of script | |
484 | CRM_Core_Error::debug_log_message('ContributionProcessor.php'); | |
485 | ||
83617886 | 486 | $lock = Civi::lockManager()->acquire('worker.contribute.CiviContributeProcessor'); |
6a488035 TO |
487 | |
488 | if ($lock->isAcquired()) { | |
489 | // try to unset any time limits | |
490 | if (!ini_get('safe_mode')) { | |
491 | set_time_limit(0); | |
492 | } | |
493 | ||
494 | CiviContributeProcessor::process(); | |
495 | } | |
496 | else { | |
33c5988b | 497 | throw new Exception('Could not acquire lock, another CiviContributeProcessor process is running'); |
6a488035 TO |
498 | } |
499 | ||
500 | $lock->release(); | |
501 | ||
502 | echo "Done processing<p>"; |