3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CiviContributeProcessor
{
36 static $_paypalParamsMapper = array(
37 //category => array(paypal_param => civicrm_field);
39 'salutation' => 'prefix_id',
40 'firstname' => 'first_name',
41 'lastname' => 'last_name',
42 'middlename' => 'middle_name',
43 'suffix' => 'suffix_id',
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',
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',
64 'l_number0' => 'note',
65 'is_test' => 'is_test',
66 'transactiontype' => 'trxn_type',
67 'recurrences' => 'installments',
70 'invnum' => 'invoice_id',
71 'subscriptiondate' => 'start_date',
72 'subscriptionid' => 'processor_id',
73 'timestamp' => 'modified_date',
77 static $_googleParamsMapper = array(
78 //category => array(google_param => civicrm_field);
80 'first-name' => 'first_name',
81 'last-name' => 'last_name',
82 'contact-name' => 'display_name',
86 'address1' => 'street_address',
87 'address2' => 'supplemental_address_1',
89 'postal-code' => 'postal_code',
90 'country-code' => 'country',
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',
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);
116 'first_name' => 'first_name',
117 'last_name' => 'last_name',
118 'middle_name' => 'middle_name',
122 'street_address' => 'street_address',
123 'supplemental_address_1' => 'supplemental_address_1',
125 'postal_code' => 'postal_code',
126 'country' => 'country',
128 'transaction' => array(
129 'total_amount' => 'total_amount',
130 'trxn_id' => 'trxn_id',
131 'currency' => 'currency',
132 'source' => 'source',
133 'receive_date' => 'receive_date',
135 'is_test' => 'is_test',
140 function paypal($paymentProcessor, $paymentMode, $start, $end) {
141 $url = "{$paymentProcessor['url_api']}nvp";
144 'user' => $paymentProcessor['user_name'],
145 'pwd' => $paymentProcessor['password'],
146 'signature' => $paymentProcessor['signature'],
152 'method' => 'TransactionSearch',
153 'startdate' => $start,
157 require_once 'CRM/Core/Payment/PayPalImpl.php';
159 // as invokeAPI fetch only last 100 transactions.
160 // we should require recursive calls to process more than 100.
161 // first fetch transactions w/ give date intervals.
162 // if we get error code w/ result, which means we do have more than 100
163 // manipulate date interval accordingly and fetch again.
166 $result = CRM_Core_Payment_PayPalImpl
::invokeAPI($args, $url);
167 require_once "CRM/Contribute/BAO/Contribution/Utils.php";
169 $keyArgs['method'] = 'GetTransactionDetails';
170 foreach ($result as $name => $value) {
171 if (substr($name, 0, 15) == 'l_transactionid') {
173 // We don't/can't process subscription notifications, which appear
174 // to be identified by transaction ids beginning with S-
175 if (substr($value, 0, 2) == 'S-') {
179 // Before we bother making a remote API call to PayPal to lookup
180 // details about a transaction, let's make sure that it doesn't
181 // already exist in the database.
182 require_once 'CRM/Contribute/DAO/Contribution.php';
183 $dao = new CRM_Contribute_DAO_Contribution
;
184 $dao->trxn_id
= $value;
185 if ($dao->find(TRUE)) {
186 preg_match('/(\d+)$/', $name, $matches);
188 $email = $result["l_email{$seq}"];
189 $amt = $result["l_amt{$seq}"];
190 CRM_Core_Error
::debug_log_message("Skipped (already recorded) - $email, $amt, $value ..<p>", TRUE);
194 $keyArgs['transactionid'] = $value;
195 $trxnDetails = CRM_Core_Payment_PayPalImpl
::invokeAPI($keyArgs, $url);
196 if (is_a($trxnDetails, 'CRM_Core_Error')) {
197 echo "PAYPAL ERROR: Skipping transaction id: $value<p>";
201 // only process completed payments
202 if (strtolower($trxnDetails['paymentstatus']) != 'completed') {
206 // only process receipts, not payments
207 if (strtolower($trxnDetails['transactiontype']) == 'sendmoney') {
211 $params = CRM_Contribute_BAO_Contribution_Utils
::formatAPIParams($trxnDetails,
212 self
::$_paypalParamsMapper,
215 if ($paymentMode == 'test') {
216 $params['transaction']['is_test'] = 1;
219 $params['transaction']['is_test'] = 0;
222 if (CRM_Contribute_BAO_Contribution_Utils
::processAPIContribution($params)) {
223 CRM_Core_Error
::debug_log_message("Processed - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE);
226 CRM_Core_Error
::debug_log_message("Skipped - {$trxnDetails['email']}, {$trxnDetails['amt']}, {$value} ..<p>", TRUE);
230 if ($result['l_errorcode0'] == '11002') {
231 $end = $result['l_timestamp99'];
232 $end_time = strtotime("{$end}", time());
233 $end_date = date('Y-m-d\T00:00:00.00\Z', $end_time);
234 $args['enddate'] = $end_date;
236 } while ($result['l_errorcode0'] == '11002');
240 function google($paymentProcessor, $paymentMode, $start, $end) {
241 require_once "CRM/Contribute/BAO/Contribution/Utils.php";
242 require_once 'CRM/Core/Payment/Google.php';
243 $nextPageToken = TRUE;
244 $searchParams = array(
247 'notification-types' => array('charge-amount'),
250 $response = CRM_Core_Payment_Google
::invokeAPI($paymentProcessor, $searchParams);
252 while ($nextPageToken) {
253 if ($response[0] == 'error') {
254 CRM_Core_Error
::debug_log_message("GOOGLE ERROR: " .
255 $response[1]['error']['error-message']['VALUE'], TRUE
258 $nextPageToken = isset($response[1][$response[0]]['next-page-token']['VALUE']) ?
$response[1][$response[0]]['next-page-token']['VALUE'] : FALSE;
260 if (is_array($response[1][$response[0]]['notifications']['charge-amount-notification'])) {
262 if (array_key_exists('google-order-number',
263 $response[1][$response[0]]['notifications']['charge-amount-notification']
265 // sometimes 'charge-amount-notification' itself is an absolute
266 // array and not array of arrays. This is the case when there is only one
267 // charge-amount-notification. Hack for this special case -
268 $chrgAmt = $response[1][$response[0]]['notifications']['charge-amount-notification'];
269 unset($response[1][$response[0]]['notifications']['charge-amount-notification']);
270 $response[1][$response[0]]['notifications']['charge-amount-notification'][] = $chrgAmt;
273 foreach ($response[1][$response[0]]['notifications']['charge-amount-notification']
276 $searchParams = array('order-numbers' => array($amtData['google-order-number']['VALUE']),
277 'notification-types' => array('risk-information', 'new-order', 'charge-amount'),
279 $response = CRM_Core_Payment_Google
::invokeAPI($paymentProcessor,
282 // append amount information as well
283 $response[] = $amtData;
285 $params = CRM_Contribute_BAO_Contribution_Utils
::formatAPIParams($response,
286 self
::$_googleParamsMapper,
289 if ($paymentMode == 'test') {
290 $params['transaction']['is_test'] = 1;
293 $params['transaction']['is_test'] = 0;
295 if (CRM_Contribute_BAO_Contribution_Utils
::processAPIContribution($params)) {
296 CRM_Core_Error
::debug_log_message("Processed - {$params['email']}, {$amtData['total-charge-amount']['VALUE']}, {$amtData['google-order-number']['VALUE']} ..<p>", TRUE);
299 CRM_Core_Error
::debug_log_message("Skipped - {$params['email']}, {$amtData['total-charge-amount']['VALUE']}, {$amtData['google-order-number']['VALUE']} ..<p>", TRUE);
303 if ($nextPageToken) {
304 $searchParams = array('next-page-token' => $nextPageToken);
305 $response = CRM_Core_Payment_Google
::invokeAPI($paymentProcessor, $searchParams);
313 $csvFile = '/home/deepak/Desktop/crm-4247.csv';
317 $handle = fopen($csvFile, "r");
319 CRM_Core_Error
::fatal("Can't locate csv file.");
322 require_once "CRM/Contribute/BAO/Contribution/Utils.php";
323 while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
325 $data['header'] = $header;
326 $params = CRM_Contribute_BAO_Contribution_Utils
::formatAPIParams($data,
327 self
::$_csvParamsMapper,
330 if (CRM_Contribute_BAO_Contribution_Utils
::processAPIContribution($params)) {
331 CRM_Core_Error
::debug_log_message("Processed - line $row of csv file .. {$params['email']}, {$params['transaction']['total_amount']}, {$params['transaction']['trxn_id']} ..<p>", TRUE);
334 CRM_Core_Error
::debug_log_message("Skipped - line $row of csv file .. {$params['email']}, {$params['transaction']['total_amount']}, {$params['transaction']['trxn_id']} ..<p>", TRUE);
337 // clean up memory from dao's
338 CRM_Core_DAO
::freeResult();
341 // we assuming - first row is always the header line
343 CRM_Core_Error
::debug_log_message("Considering first row ( line $row ) as HEADER ..<p>", TRUE);
345 if (empty($header)) {
346 CRM_Core_Error
::fatal("Header is empty.");
356 require_once 'CRM/Utils/Request.php';
358 $type = CRM_Utils_Request
::retrieve('type', 'String', CRM_Core_DAO
::$_nullObject, FALSE, 'csv', 'REQUEST');
359 $type = strtolower($type);
364 $start = CRM_Utils_Request
::retrieve('start', 'String',
365 CRM_Core_DAO
::$_nullObject, FALSE, 31, 'REQUEST'
367 $end = CRM_Utils_Request
::retrieve('end', 'String',
368 CRM_Core_DAO
::$_nullObject, FALSE, 0, 'REQUEST'
371 CRM_Core_Error
::fatal("Start offset can't be less than End offset.");
374 $start = date('Y-m-d', time() - $start * 24 * 60 * 60) . 'T00:00:00.00Z';
375 $end = date('Y-m-d', time() - $end * 24 * 60 * 60) . 'T23:59:00.00Z';
377 $ppID = CRM_Utils_Request
::retrieve('ppID', 'Integer',
378 CRM_Core_DAO
::$_nullObject, TRUE, NULL, 'REQUEST'
380 $mode = CRM_Utils_Request
::retrieve('ppMode', 'String',
381 CRM_Core_DAO
::$_nullObject, FALSE, 'live', 'REQUEST'
384 require_once 'CRM/Financial/BAO/PaymentProcessor.php';
385 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ppID, $mode);
387 CRM_Core_Error
::debug_log_message("Start Date=$start, End Date=$end, ppID=$ppID, mode=$mode <p>", TRUE);
389 return self
::$type($paymentProcessor, $mode, $start, $end);
397 // bootstrap the environment and run the processor
399 require_once '../civicrm.config.php';
400 require_once 'CRM/Core/Config.php';
401 $config = CRM_Core_Config
::singleton();
403 CRM_Utils_System
::authenticateScript(TRUE);
405 //log the execution of script
406 CRM_Core_Error
::debug_log_message('ContributionProcessor.php');
408 require_once 'CRM/Core/Lock.php';
409 $lock = new CRM_Core_Lock('CiviContributeProcessor');
411 if ($lock->isAcquired()) {
412 // try to unset any time limits
413 if (!ini_get('safe_mode')) {
417 CiviContributeProcessor
::process();
420 throw new Exception('Could not acquire lock, another CiviMailProcessor process is running');
425 echo "Done processing<p>";