3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
33 class CiviContributeProcessor
{
34 static $_paypalParamsMapper = array(
35 //category => array(paypal_param => civicrm_field);
37 'salutation' => 'prefix_id',
38 'firstname' => 'first_name',
39 'lastname' => 'last_name',
40 'middlename' => 'middle_name',
41 'suffix' => 'suffix_id',
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',
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',
62 'l_number0' => 'note',
63 'is_test' => 'is_test',
64 'transactiontype' => 'trxn_type',
65 'recurrences' => 'installments',
68 'invnum' => 'invoice_id',
69 'subscriptiondate' => 'start_date',
70 'subscriptionid' => 'processor_id',
71 'timestamp' => 'modified_date',
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);
80 'first_name' => 'first_name',
81 'last_name' => 'last_name',
82 'middle_name' => 'middle_name',
86 'street_address' => 'street_address',
87 'supplemental_address_1' => 'supplemental_address_1',
89 'postal_code' => 'postal_code',
90 'country' => 'country',
92 'transaction' => array(
93 'total_amount' => 'total_amount',
94 'trxn_id' => 'trxn_id',
95 'currency' => 'currency',
97 'receive_date' => 'receive_date',
99 'is_test' => 'is_test',
104 * @param $paymentProcessor
105 * @param $paymentMode
109 public static function paypal($paymentProcessor, $paymentMode, $start, $end) {
110 $url = "{$paymentProcessor['url_api']}nvp";
113 'user' => $paymentProcessor['user_name'],
114 'pwd' => $paymentProcessor['password'],
115 'signature' => $paymentProcessor['signature'],
121 'method' => 'TransactionSearch',
122 'startdate' => $start,
126 require_once 'CRM/Core/Payment/PayPalImpl.php';
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.
135 $result = CRM_Core_Payment_PayPalImpl
::invokeAPI($args, $url);
136 require_once "CRM/Contribute/BAO/Contribution/Utils.php";
138 $keyArgs['method'] = 'GetTransactionDetails';
139 foreach ($result as $name => $value) {
140 if (substr($name, 0, 15) == 'l_transactionid') {
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-') {
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';
152 $dao = new CRM_Contribute_DAO_Contribution();
153 $dao->trxn_id
= $value;
154 if ($dao->find(TRUE)) {
155 preg_match('/(\d+)$/', $name, $matches);
157 $email = $result["l_email{$seq}"];
158 $amt = $result["l_amt{$seq}"];
159 CRM_Core_Error
::debug_log_message("Skipped (already recorded) - $email, $amt, $value ..<p>", TRUE);
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>";
170 // only process completed payments
171 if (strtolower($trxnDetails['paymentstatus']) != 'completed') {
175 // only process receipts, not payments
176 if (strtolower($trxnDetails['transactiontype']) == 'sendmoney') {
180 $params = self
::formatAPIParams($trxnDetails,
181 self
::$_paypalParamsMapper,
184 if ($paymentMode == 'test') {
185 $params['transaction']['is_test'] = 1;
188 $params['transaction']['is_test'] = 0;
191 if (self
::processAPIContribution($params)) {
192 CRM_Core_Error
::debug_log_message("Processed - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE);
195 CRM_Core_Error
::debug_log_message("Skipped - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE);
199 if ($result['l_errorcode0'] == '11002') {
200 $end = $result['l_timestamp99'];
201 $end_time = strtotime("{$end}", time());
202 $end_date = date('Y-m-d\T00:00:00.00\Z', $end_time);
203 $args['enddate'] = $end_date;
205 } while ($result['l_errorcode0'] == '11002');
208 public static function csv() {
209 $csvFile = '/home/deepak/Desktop/crm-4247.csv';
213 $handle = fopen($csvFile, "r");
215 CRM_Core_Error
::fatal("Can't locate csv file.");
218 require_once "CRM/Contribute/BAO/Contribution/Utils.php";
219 while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
221 $data['header'] = $header;
222 $params = self
::formatAPIParams($data,
223 self
::$_csvParamsMapper,
226 if (self
::processAPIContribution($params)) {
227 CRM_Core_Error
::debug_log_message("Processed - line $row of csv file .. {$params['email']}, {$params['transaction']['total_amount']}, {$params['transaction']['trxn_id']} ..<p>", TRUE);
230 CRM_Core_Error
::debug_log_message("Skipped - line $row of csv file .. {$params['email']}, {$params['transaction']['total_amount']}, {$params['transaction']['trxn_id']} ..<p>", TRUE);
233 // clean up memory from dao's
234 CRM_Core_DAO
::freeResult();
237 // we assuming - first row is always the header line
239 CRM_Core_Error
::debug_log_message("Considering first row ( line $row ) as HEADER ..<p>", TRUE);
241 if (empty($header)) {
242 CRM_Core_Error
::fatal("Header is empty.");
250 public static function process() {
251 require_once 'CRM/Utils/Request.php';
253 $type = CRM_Utils_Request
::retrieve('type', 'String', CRM_Core_DAO
::$_nullObject, FALSE, 'csv', 'REQUEST');
254 $type = strtolower($type);
258 $start = CRM_Utils_Request
::retrieve('start', 'String',
259 CRM_Core_DAO
::$_nullObject, FALSE, 31, 'REQUEST'
261 $end = CRM_Utils_Request
::retrieve('end', 'String',
262 CRM_Core_DAO
::$_nullObject, FALSE, 0, 'REQUEST'
265 CRM_Core_Error
::fatal("Start offset can't be less than End offset.");
268 $start = date('Y-m-d', time() - $start * 24 * 60 * 60) . 'T00:00:00.00Z';
269 $end = date('Y-m-d', time() - $end * 24 * 60 * 60) . 'T23:59:00.00Z';
271 $ppID = CRM_Utils_Request
::retrieve('ppID', 'Integer',
272 CRM_Core_DAO
::$_nullObject, TRUE, NULL, 'REQUEST'
274 $mode = CRM_Utils_Request
::retrieve('ppMode', 'String',
275 CRM_Core_DAO
::$_nullObject, FALSE, 'live', 'REQUEST'
278 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ppID, $mode);
280 CRM_Core_Error
::debug_log_message("Start Date=$start, End Date=$end, ppID=$ppID, mode=$mode <p>", TRUE);
282 return self
::$type($paymentProcessor, $mode, $start, $end);
290 * @param array $apiParams
292 * @param string $type
293 * @param bool $category
297 public static function formatAPIParams($apiParams, $mapper, $type = 'paypal', $category = TRUE) {
298 $type = strtolower($type);
300 if (!in_array($type, array(
305 // return the params as is
308 $params = $transaction = array();
310 if ($type == 'paypal') {
311 foreach ($apiParams as $detail => $val) {
312 if (isset($mapper['contact'][$detail])) {
313 $params[$mapper['contact'][$detail]] = $val;
315 elseif (isset($mapper['location'][$detail])) {
316 $params['address'][1][$mapper['location'][$detail]] = $val;
318 elseif (isset($mapper['transaction'][$detail])) {
321 // Sadly, PayPal seems to send two distinct data elements in a single field,
322 // so we break them out here. This is somewhat ugly and tragic.
329 list($frequency_interval, $frequency_unit) = explode(' ', $val);
330 $transaction['frequency_interval'] = $frequency_interval;
331 $transaction['frequency_unit'] = $freqUnits[$frequency_unit];
334 case 'subscriptiondate':
336 // PayPal dates are in ISO-8601 format. We need a format that
338 $unix_timestamp = strtotime($val);
339 $transaction[$mapper['transaction'][$detail]] = date('YmdHis', $unix_timestamp);
346 $val = "[PayPal_field:{$detail}] {$val}";
347 $transaction[$mapper['transaction'][$detail]] = !empty($transaction[$mapper['transaction'][$detail]]) ?
$transaction[$mapper['transaction'][$detail]] . " <br/> " . $val : $val;
352 $transaction[$mapper['transaction'][$detail]] = $val;
357 if (!empty($transaction) && $category) {
358 $params['transaction'] = $transaction;
361 $params +
= $transaction;
364 CRM_Contribute_BAO_Contribution_Utils
::_fillCommonParams($params, $type);
369 if ($type == 'csv') {
370 $header = $apiParams['header'];
371 unset($apiParams['header']);
372 foreach ($apiParams as $key => $val) {
373 if (isset($mapper['contact'][$header[$key]])) {
374 $params[$mapper['contact'][$header[$key]]] = $val;
376 elseif (isset($mapper['location'][$header[$key]])) {
377 $params['address'][1][$mapper['location'][$header[$key]]] = $val;
379 elseif (isset($mapper['transaction'][$header[$key]])) {
380 $transaction[$mapper['transaction'][$header[$key]]] = $val;
383 $params[$header[$key]] = $val;
387 if (!empty($transaction) && $category) {
388 $params['transaction'] = $transaction;
391 $params +
= $transaction;
394 CRM_Contribute_BAO_Contribution_Utils
::_fillCommonParams($params, $type);
402 * @param array $params
406 public static function processAPIContribution($params) {
407 if (empty($params) ||
array_key_exists('error', $params)) {
411 // add contact using dedupe rule
412 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
413 $dedupeParams['check_permission'] = FALSE;
414 $dupeIds = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual');
415 // if we find more than one contact, use the first one
416 if (!empty($dupeIds[0])) {
417 $params['contact_id'] = $dupeIds[0];
419 $contact = CRM_Contact_BAO_Contact
::create($params);
424 // only pass transaction params to contribution::create, if available
425 if (array_key_exists('transaction', $params)) {
426 $params = $params['transaction'];
427 $params['contact_id'] = $contact->id
;
430 // handle contribution custom data
431 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution',
434 CRM_Utils_Array
::value('financial_type_id',
438 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
439 CRM_Utils_Array
::value('id', $params, NULL),
442 // create contribution
444 // if this is a recurring contribution then process it first
445 if ($params['trxn_type'] == 'subscrpayment') {
446 // see if a recurring record already exists
447 $recurring = new CRM_Contribute_BAO_ContributionRecur();
448 $recurring->processor_id
= $params['processor_id'];
449 if (!$recurring->find(TRUE)) {
450 $recurring = new CRM_Contribute_BAO_ContributionRecur();
451 $recurring->invoice_id
= $params['invoice_id'];
452 $recurring->find(TRUE);
455 // This is the same thing the CiviCRM IPN handler does to handle
456 // subsequent recurring payments to avoid duplicate contribution
457 // errors due to invoice ID. See:
458 // ./CRM/Core/Payment/PayPalIPN.php:200
459 if ($recurring->id
) {
460 $params['invoice_id'] = md5(uniqid(rand(), TRUE));
463 $recurring->copyValues($params);
465 if (is_a($recurring, 'CRM_Core_Error')) {
469 $params['contribution_recur_id'] = $recurring->id
;
473 $contribution = &CRM_Contribute_BAO_Contribution
::create($params,
474 CRM_Core_DAO
::$_nullArray
476 if (!$contribution->id
) {
485 // bootstrap the environment and run the processor
487 require_once '../civicrm.config.php';
488 require_once 'CRM/Core/Config.php';
489 $config = CRM_Core_Config
::singleton();
491 CRM_Utils_System
::authenticateScript(TRUE);
493 //log the execution of script
494 CRM_Core_Error
::debug_log_message('ContributionProcessor.php');
496 $lock = Civi
::lockManager()->acquire('worker.contribute.CiviContributeProcessor');
498 if ($lock->isAcquired()) {
499 // try to unset any time limits
500 if (!ini_get('safe_mode')) {
504 CiviContributeProcessor
::process();
507 throw new Exception('Could not acquire lock, another CiviContributeProcessor process is running');
512 echo "Done processing<p>";