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