Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contribute_BAO_Contribution_Utils { | |
36 | ||
37 | /** | |
38 | * Function to process payment after confirmation | |
39 | * | |
b8950542 EM |
40 | * @param object $form form object |
41 | * @param array $paymentParams array with payment related key | |
6a488035 | 42 | * value pairs |
b8950542 | 43 | * @param array $premiumParams array with premium related key |
6a488035 | 44 | * value pairs |
b8950542 EM |
45 | * @param int $contactID contact id |
46 | * @param int $contributionTypeId financial type id | |
47 | * @param int|string $component component id | |
48 | * | |
49 | * @param null $fieldTypes | |
6a488035 TO |
50 | * |
51 | * @return array associated array | |
52 | * | |
53 | * @static | |
54 | * @access public | |
55 | */ | |
56 | static function processConfirm(&$form, | |
57 | &$paymentParams, | |
58 | &$premiumParams, | |
59 | $contactID, | |
60 | $contributionTypeId, | |
61 | $component = 'contribution', | |
62 | $fieldTypes = NULL | |
63 | ) { | |
64 | CRM_Core_Payment_Form::mapParams($form->_bltID, $form->_params, $paymentParams, TRUE); | |
4bd318e0 EM |
65 | $isTest = ($form->_mode == 'test') ? 1 : 0; |
66 | $lineItems = $form->_lineItem; | |
6a488035 TO |
67 | |
68 | $contributionType = new CRM_Financial_DAO_FinancialType(); | |
69 | if (isset($paymentParams['financial_type'])) { | |
70 | $contributionType->id = $paymentParams['financial_type']; | |
71 | } | |
a7488080 | 72 | elseif (!empty($form->_values['pledge_id'])) { |
6a488035 TO |
73 | $contributionType->id = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', |
74 | $form->_values['pledge_id'], | |
75 | 'financial_type_id' | |
76 | ); | |
77 | } | |
78 | else { | |
79 | $contributionType->id = $contributionTypeId; | |
80 | } | |
81 | if (!$contributionType->find(TRUE)) { | |
82 | CRM_Core_Error::fatal('Could not find a system table'); | |
83 | } | |
84 | ||
85 | // add some financial type details to the params list | |
86 | // if folks need to use it | |
87 | $paymentParams['contributionType_name'] = $form->_params['contributionType_name'] = $contributionType->name; | |
88 | //CRM-11456 | |
89 | $paymentParams['contributionType_accounting_code'] = $form->_params['contributionType_accounting_code'] = CRM_Financial_BAO_FinancialAccount::getAccountingCode($contributionType->id); | |
90 | $paymentParams['contributionPageID'] = $form->_params['contributionPageID'] = $form->_values['id']; | |
91 | ||
92 | ||
93 | $payment = NULL; | |
94 | $paymentObjError = ts('The system did not record payment details for this payment and so could not process the transaction. Please report this error to the site administrator.'); | |
95 | if ($form->_values['is_monetary'] && $form->_amount > 0.0 && is_array($form->_paymentProcessor)) { | |
96 | $payment = CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form); | |
97 | } | |
98 | ||
99 | //fix for CRM-2062 | |
100 | $now = date('YmdHis'); | |
101 | ||
102 | $result = NULL; | |
103 | if ($form->_contributeMode == 'notify' || | |
104 | $form->_params['is_pay_later'] | |
105 | ) { | |
106 | // this is not going to come back, i.e. we fill in the other details | |
107 | // when we get a callback from the payment processor | |
108 | // also add the contact ID and contribution ID to the params list | |
109 | $paymentParams['contactID'] = $form->_params['contactID'] = $contactID; | |
110 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution( | |
111 | $form, | |
112 | $paymentParams, | |
113 | NULL, | |
114 | $contactID, | |
115 | $contributionType, | |
4bd318e0 EM |
116 | TRUE, TRUE, |
117 | $isTest, | |
118 | $lineItems | |
6a488035 TO |
119 | ); |
120 | ||
121 | if ($contribution) { | |
122 | $form->_params['contributionID'] = $contribution->id; | |
123 | } | |
124 | ||
125 | $form->_params['contributionTypeID'] = $contributionType->id; | |
126 | $form->_params['item_name'] = $form->_params['description']; | |
127 | $form->_params['receive_date'] = $now; | |
128 | ||
129 | if ($contribution && $form->_values['is_recur'] && | |
130 | $contribution->contribution_recur_id | |
131 | ) { | |
132 | $form->_params['contributionRecurID'] = $contribution->contribution_recur_id; | |
133 | } | |
134 | ||
135 | $form->set('params', $form->_params); | |
136 | $form->postProcessPremium($premiumParams, $contribution); | |
137 | ||
138 | if ($form->_values['is_monetary'] && $form->_amount > 0.0) { | |
139 | // add qfKey so we can send to paypal | |
140 | $form->_params['qfKey'] = $form->controller->_key; | |
141 | if ($component == 'membership') { | |
142 | $membershipResult = array(1 => $contribution); | |
143 | return $membershipResult; | |
144 | } | |
145 | else { | |
146 | if (!$form->_params['is_pay_later']) { | |
147 | if (is_object($payment)) { | |
148 | // call postprocess hook before leaving | |
149 | $form->postProcessHook(); | |
150 | // this does not return | |
151 | $result = &$payment->doTransferCheckout($form->_params, 'contribute'); | |
152 | } | |
153 | else{ | |
154 | CRM_Core_Error::fatal($paymentObjError); | |
155 | } | |
156 | } | |
157 | else { | |
158 | // follow similar flow as IPN | |
159 | // send the receipt mail | |
160 | $form->set('params', $form->_params); | |
161 | if ($contributionType->is_deductible) { | |
162 | $form->assign('is_deductible', TRUE); | |
163 | $form->set('is_deductible', TRUE); | |
164 | } | |
165 | if (isset($paymentParams['contribution_source'])) { | |
166 | $form->_params['source'] = $paymentParams['contribution_source']; | |
167 | } | |
168 | ||
169 | // get the price set values for receipt. | |
170 | if ($form->_priceSetId && $form->_lineItem) { | |
171 | $form->_values['lineItem'] = $form->_lineItem; | |
172 | $form->_values['priceSetID'] = $form->_priceSetId; | |
173 | } | |
174 | ||
175 | $form->_values['contribution_id'] = $contribution->id; | |
176 | $form->_values['contribution_page_id'] = $contribution->contribution_page_id; | |
177 | ||
178 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, | |
179 | $form->_values, | |
180 | $contribution->is_test | |
181 | ); | |
182 | return; | |
183 | } | |
184 | } | |
185 | } | |
186 | } | |
187 | elseif ($form->_contributeMode == 'express') { | |
188 | if ($form->_values['is_monetary'] && $form->_amount > 0.0) { | |
189 | // determine if express + recurring and direct accordingly | |
190 | if ($paymentParams['is_recur'] == 1) { | |
191 | if (is_object($payment)) { | |
361fb6c0 | 192 | $result = $payment->createRecurringPayments($paymentParams); |
6a488035 TO |
193 | } |
194 | else { | |
195 | CRM_Core_Error::fatal($paymentObjError); | |
196 | } | |
197 | } | |
198 | else { | |
199 | if (is_object($payment)) { | |
361fb6c0 | 200 | $result = $payment->doExpressCheckout($paymentParams); |
6a488035 TO |
201 | } |
202 | else { | |
203 | CRM_Core_Error::fatal($paymentObjError); | |
204 | } | |
205 | } | |
206 | } | |
207 | } | |
208 | elseif ($form->_values['is_monetary'] && $form->_amount > 0.0) { | |
a7488080 | 209 | if (!empty($paymentParams['is_recur']) && |
6a488035 TO |
210 | $form->_contributeMode == 'direct' |
211 | ) { | |
212 | ||
213 | // For recurring contribution, create Contribution Record first. | |
214 | // Contribution ID, Recurring ID and Contact ID needed | |
215 | // When we get a callback from the payment processor | |
216 | ||
217 | $paymentParams['contactID'] = $contactID; | |
cd125a40 FG |
218 | |
219 | // Fix for CRM-14354. If the membership is recurring, don't create a | |
220 | // civicrm_contribution_recur record for the additional contribution | |
221 | // (i.e., the amount NOT associated with the membership). Temporarily | |
222 | // cache the is_recur values so we can process the additional gift as a | |
223 | // one-off payment. | |
d8a20b17 FG |
224 | if ($form->_membershipBlock['is_separate_payment']) { |
225 | $cachedFormValue = CRM_Utils_Array::value('is_recur', $form->_values); | |
226 | unset($form->_values['is_recur']); | |
227 | $cachedParamValue = CRM_Utils_Array::value('is_recur', $paymentParams); | |
228 | unset($paymentParams['is_recur']); | |
229 | } | |
cd125a40 | 230 | |
6a488035 TO |
231 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution( |
232 | $form, | |
233 | $paymentParams, | |
234 | NULL, | |
235 | $contactID, | |
236 | $contributionType, | |
4bd318e0 EM |
237 | TRUE, TRUE, |
238 | $isTest, | |
239 | $lineItems | |
6a488035 TO |
240 | ); |
241 | ||
cd125a40 | 242 | // restore cached values (part of fix for CRM-14354) |
d8a20b17 FG |
243 | if ($form->_membershipBlock['is_separate_payment']) { |
244 | $form->_values['is_recur'] = $cachedFormValue; | |
245 | $paymentParams['is_recur'] = $cachedParamValue; | |
246 | } | |
cd125a40 | 247 | |
6a488035 TO |
248 | $paymentParams['contributionID'] = $contribution->id; |
249 | $paymentParams['contributionTypeID'] = $contribution->financial_type_id; | |
250 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; | |
251 | ||
252 | if ($form->_values['is_recur'] && $contribution->contribution_recur_id) { | |
253 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; | |
254 | } | |
255 | } | |
256 | if (is_object($payment)) { | |
361fb6c0 | 257 | $result = $payment->doDirectPayment($paymentParams); |
6a488035 TO |
258 | } |
259 | else { | |
260 | CRM_Core_Error::fatal($paymentObjError); | |
261 | } | |
262 | } | |
263 | ||
264 | if ($component == 'membership') { | |
265 | $membershipResult = array(); | |
266 | } | |
267 | ||
268 | if (is_a($result, 'CRM_Core_Error')) { | |
269 | //make sure to cleanup db for recurring case. | |
a7488080 | 270 | if (!empty($paymentParams['contributionID'])) { |
6a488035 TO |
271 | CRM_Contribute_BAO_Contribution::deleteContribution($paymentParams['contributionID']); |
272 | } | |
a7488080 | 273 | if (!empty($paymentParams['contributionRecurID'])) { |
6a488035 TO |
274 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']); |
275 | } | |
276 | ||
277 | if ($component !== 'membership') { | |
278 | CRM_Core_Error::displaySessionError($result); | |
279 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', | |
280 | "_qf_Main_display=true&qfKey={$form->_params['qfKey']}" | |
281 | )); | |
282 | } | |
283 | $membershipResult[1] = $result; | |
284 | } | |
92acf33d | 285 | elseif ($result || ($form->_amount == 0.0 && !$form->_params['is_pay_later'])) { |
6a488035 TO |
286 | if ($result) { |
287 | $form->_params = array_merge($form->_params, $result); | |
288 | } | |
289 | $form->_params['receive_date'] = $now; | |
290 | $form->set('params', $form->_params); | |
291 | $form->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $result)); | |
292 | $form->assign('receive_date', | |
293 | CRM_Utils_Date::mysqlToIso($form->_params['receive_date']) | |
294 | ); | |
295 | ||
296 | // result has all the stuff we need | |
297 | // lets archive it to a financial transaction | |
298 | if ($contributionType->is_deductible) { | |
299 | $form->assign('is_deductible', TRUE); | |
300 | $form->set('is_deductible', TRUE); | |
301 | } | |
302 | ||
303 | if (isset($paymentParams['contribution_source'])) { | |
304 | $form->_params['source'] = $paymentParams['contribution_source']; | |
305 | } | |
306 | ||
307 | // check if pending was set to true by payment processor | |
308 | $pending = FALSE; | |
8c096555 | 309 | if (!empty($form->_params['contribution_status_pending'])) { |
6a488035 TO |
310 | $pending = TRUE; |
311 | } | |
312 | if (!(!empty($paymentParams['is_recur']) && $form->_contributeMode == 'direct')) { | |
313 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($form, | |
314 | $form->_params, $result, | |
315 | $contactID, $contributionType, | |
4bd318e0 EM |
316 | $pending, TRUE, |
317 | $isTest, | |
318 | $lineItems | |
6a488035 TO |
319 | ); |
320 | } | |
321 | $form->postProcessPremium($premiumParams, $contribution); | |
322 | ||
323 | $membershipResult[1] = $contribution; | |
324 | } | |
325 | ||
326 | if ($component == 'membership') { | |
327 | return $membershipResult; | |
328 | } | |
329 | ||
330 | //Do not send an email if Recurring contribution is done via Direct Mode | |
331 | //We will send email once the IPN is received. | |
332 | if (!empty($paymentParams['is_recur']) && $form->_contributeMode == 'direct') { | |
333 | return TRUE; | |
334 | } | |
335 | ||
336 | // get the price set values for receipt. | |
337 | if ($form->_priceSetId && $form->_lineItem) { | |
338 | $form->_values['lineItem'] = $form->_lineItem; | |
339 | $form->_values['priceSetID'] = $form->_priceSetId; | |
340 | } | |
341 | ||
342 | // finally send an email receipt | |
343 | if ($contribution) { | |
344 | $form->_values['contribution_id'] = $contribution->id; | |
345 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, | |
346 | $form->_values, $contribution->is_test, | |
347 | FALSE, $fieldTypes | |
348 | ); | |
349 | } | |
350 | } | |
351 | ||
352 | /** | |
353 | * Function to get the contribution details by month | |
354 | * of the year | |
355 | * | |
356 | * @param int $param year | |
357 | * | |
358 | * @return array associated array | |
359 | * | |
360 | * @static | |
361 | * @access public | |
362 | */ | |
363 | static function contributionChartMonthly($param) { | |
364 | if ($param) { | |
365 | $param = array(1 => array($param, 'Integer')); | |
366 | } | |
367 | else { | |
368 | $param = date("Y"); | |
369 | $param = array(1 => array($param, 'Integer')); | |
370 | } | |
371 | ||
372 | $query = " | |
373 | SELECT sum(contrib.total_amount) AS ctAmt, | |
374 | MONTH( contrib.receive_date) AS contribMonth | |
375 | FROM civicrm_contribution AS contrib | |
376 | INNER JOIN civicrm_contact AS contact ON ( contact.id = contrib.contact_id ) | |
377 | WHERE contrib.contact_id = contact.id | |
378 | AND ( contrib.is_test = 0 OR contrib.is_test IS NULL ) | |
379 | AND contrib.contribution_status_id = 1 | |
380 | AND date_format(contrib.receive_date,'%Y') = %1 | |
381 | AND contact.is_deleted = 0 | |
382 | GROUP BY contribMonth | |
383 | ORDER BY month(contrib.receive_date)"; | |
384 | ||
385 | $dao = CRM_Core_DAO::executeQuery($query, $param); | |
386 | ||
387 | $params = NULL; | |
388 | while ($dao->fetch()) { | |
389 | if ($dao->contribMonth) { | |
390 | $params['By Month'][$dao->contribMonth] = $dao->ctAmt; | |
391 | } | |
392 | } | |
393 | return $params; | |
394 | } | |
395 | ||
396 | /** | |
397 | * Function to get the contribution details by year | |
398 | * | |
399 | * @return array associated array | |
400 | * | |
401 | * @static | |
402 | * @access public | |
403 | */ | |
404 | static function contributionChartYearly() { | |
405 | $config = CRM_Core_Config::singleton(); | |
406 | $yearClause = "year(contrib.receive_date) as contribYear"; | |
407 | if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] != 1 || $config->fiscalYearStart['d'] != 1)) { | |
a48278eb KJ |
408 | $yearClause = "CASE |
409 | WHEN (MONTH(contrib.receive_date)>= " . $config->fiscalYearStart['M'] . " | |
f813f78e | 410 | && DAYOFMONTH(contrib.receive_date)>= " . $config->fiscalYearStart['d'] . " ) |
a48278eb KJ |
411 | THEN |
412 | concat(YEAR(contrib.receive_date), '-',YEAR(contrib.receive_date)+1) | |
413 | ELSE | |
414 | concat(YEAR(contrib.receive_date)-1,'-', YEAR(contrib.receive_date)) | |
415 | END AS contribYear"; | |
6a488035 TO |
416 | } |
417 | ||
418 | $query = " | |
419 | SELECT sum(contrib.total_amount) AS ctAmt, | |
420 | {$yearClause} | |
421 | FROM civicrm_contribution AS contrib | |
422 | INNER JOIN civicrm_contact contact ON ( contact.id = contrib.contact_id ) | |
423 | WHERE ( contrib.is_test = 0 OR contrib.is_test IS NULL ) | |
424 | AND contrib.contribution_status_id = 1 | |
425 | AND contact.is_deleted = 0 | |
426 | GROUP BY contribYear | |
427 | ORDER BY contribYear"; | |
428 | $dao = CRM_Core_DAO::executeQuery($query); | |
429 | ||
430 | $params = NULL; | |
431 | while ($dao->fetch()) { | |
432 | if (!empty($dao->contribYear)) { | |
433 | $params['By Year'][$dao->contribYear] = $dao->ctAmt; | |
434 | } | |
435 | } | |
436 | return $params; | |
437 | } | |
438 | ||
186c9c17 EM |
439 | /** |
440 | * @param $params | |
441 | * @param $contactID | |
442 | * @param $mail | |
443 | */ | |
6a488035 TO |
444 | static function createCMSUser(&$params, $contactID, $mail) { |
445 | // lets ensure we only create one CMS user | |
446 | static $created = FALSE; | |
447 | ||
448 | if ($created) { | |
449 | return; | |
450 | } | |
451 | $created = TRUE; | |
452 | ||
a7488080 | 453 | if (!empty($params['cms_create_account'])) { |
6a488035 TO |
454 | $params['contactID'] = $contactID; |
455 | if (!CRM_Core_BAO_CMSUser::create($params, $mail)) { | |
456 | CRM_Core_Error::statusBounce(ts('Your profile is not saved and Account is not created.')); | |
457 | } | |
458 | } | |
459 | } | |
460 | ||
186c9c17 EM |
461 | /** |
462 | * @param $params | |
463 | * @param string $type | |
464 | * | |
465 | * @return bool | |
466 | */ | |
6a488035 TO |
467 | static function _fillCommonParams(&$params, $type = 'paypal') { |
468 | if (array_key_exists('transaction', $params)) { | |
469 | $transaction = &$params['transaction']; | |
470 | } | |
471 | else { | |
472 | $transaction = &$params; | |
473 | } | |
474 | ||
475 | $params['contact_type'] = 'Individual'; | |
476 | ||
477 | $billingLocTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', 'Billing', 'id', 'name'); | |
478 | if (!$billingLocTypeId) { | |
479 | $billingLocTypeId = 1; | |
480 | } | |
481 | if (!CRM_Utils_System::isNull($params['address'])) { | |
482 | $params['address'][1]['is_primary'] = 1; | |
483 | $params['address'][1]['location_type_id'] = $billingLocTypeId; | |
484 | } | |
485 | if (!CRM_Utils_System::isNull($params['email'])) { | |
486 | $params['email'] = array( | |
487 | 1 => array('email' => $params['email'], | |
488 | 'location_type_id' => $billingLocTypeId, | |
489 | )); | |
490 | } | |
491 | ||
492 | if (isset($transaction['trxn_id'])) { | |
493 | // set error message if transaction has already been processed. | |
494 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
495 | $contribution->trxn_id = $transaction['trxn_id']; | |
496 | if ($contribution->find(TRUE)) { | |
497 | $params['error'][] = ts('transaction already processed.'); | |
498 | } | |
499 | } | |
500 | else { | |
501 | // generate a new transaction id, if not already exist | |
502 | $transaction['trxn_id'] = md5(uniqid(rand(), TRUE)); | |
503 | } | |
504 | ||
505 | if (!isset( $transaction['financial_type_id'])) { | |
506 | $contributionTypes = array_keys(CRM_Contribute_PseudoConstant::financialType()); | |
507 | $transaction['financial_type_id'] = $contributionTypes[0]; | |
508 | } | |
509 | ||
510 | if (($type == 'paypal') && (!isset($transaction['net_amount']))) { | |
511 | $transaction['net_amount'] = $transaction['total_amount'] - CRM_Utils_Array::value('fee_amount', $transaction, 0); | |
512 | } | |
513 | ||
514 | if (!isset($transaction['invoice_id'])) { | |
515 | $transaction['invoice_id'] = $transaction['trxn_id']; | |
516 | } | |
517 | ||
518 | $source = ts('ContributionProcessor: %1 API', | |
519 | array(1 => ucfirst($type)) | |
520 | ); | |
521 | if (isset($transaction['source'])) { | |
522 | $transaction['source'] = $source . ':: ' . $transaction['source']; | |
523 | } | |
524 | else { | |
525 | $transaction['source'] = $source; | |
526 | } | |
527 | ||
528 | return TRUE; | |
529 | } | |
530 | ||
186c9c17 EM |
531 | /** |
532 | * @param $apiParams | |
533 | * @param $mapper | |
534 | * @param string $type | |
535 | * @param bool $category | |
536 | * | |
537 | * @return array | |
538 | */ | |
6a488035 TO |
539 | static function formatAPIParams($apiParams, $mapper, $type = 'paypal', $category = TRUE) { |
540 | $type = strtolower($type); | |
541 | ||
542 | if (!in_array($type, array( | |
543 | 'paypal', 'google', 'csv'))) { | |
544 | // return the params as is | |
545 | return $apiParams; | |
546 | } | |
547 | $params = $transaction = array(); | |
548 | ||
549 | if ($type == 'paypal') { | |
550 | foreach ($apiParams as $detail => $val) { | |
551 | if (isset($mapper['contact'][$detail])) { | |
552 | $params[$mapper['contact'][$detail]] = $val; | |
553 | } | |
554 | elseif (isset($mapper['location'][$detail])) { | |
555 | $params['address'][1][$mapper['location'][$detail]] = $val; | |
556 | } | |
557 | elseif (isset($mapper['transaction'][$detail])) { | |
558 | switch ($detail) { | |
559 | case 'l_period2': | |
560 | // Sadly, PayPal seems to send two distinct data elements in a single field, | |
561 | // so we break them out here. This is somewhat ugly and tragic. | |
562 | $freqUnits = array( | |
563 | 'D' => 'day', | |
564 | 'W' => 'week', | |
565 | 'M' => 'month', | |
566 | 'Y' => 'year', | |
567 | ); | |
568 | list($frequency_interval, $frequency_unit) = explode(' ', $val); | |
569 | $transaction['frequency_interval'] = $frequency_interval; | |
570 | $transaction['frequency_unit'] = $freqUnits[$frequency_unit]; | |
571 | break; | |
572 | ||
573 | case 'subscriptiondate': | |
574 | case 'timestamp': | |
575 | // PayPal dates are in ISO-8601 format. We need a format that | |
576 | // MySQL likes | |
577 | $unix_timestamp = strtotime($val); | |
578 | $transaction[$mapper['transaction'][$detail]] = date('YmdHis', $unix_timestamp); | |
579 | break; | |
580 | ||
581 | case 'note': | |
582 | case 'custom': | |
583 | case 'l_number0': | |
584 | if ($val) { | |
585 | $val = "[PayPal_field:{$detail}] {$val}"; | |
586 | $transaction[$mapper['transaction'][$detail]] = !empty($transaction[$mapper['transaction'][$detail]]) ? $transaction[$mapper['transaction'][$detail]] . " <br/> " . $val : $val; | |
587 | } | |
588 | break; | |
589 | ||
590 | default: | |
591 | $transaction[$mapper['transaction'][$detail]] = $val; | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | if (!empty($transaction) && $category) { | |
597 | $params['transaction'] = $transaction; | |
598 | } | |
599 | else { | |
600 | $params += $transaction; | |
601 | } | |
602 | ||
603 | self::_fillCommonParams($params, $type); | |
604 | ||
605 | return $params; | |
606 | } | |
607 | ||
608 | if ($type == 'csv') { | |
609 | $header = $apiParams['header']; | |
610 | unset($apiParams['header']); | |
611 | foreach ($apiParams as $key => $val) { | |
612 | if (isset($mapper['contact'][$header[$key]])) { | |
613 | $params[$mapper['contact'][$header[$key]]] = $val; | |
614 | } | |
615 | elseif (isset($mapper['location'][$header[$key]])) { | |
616 | $params['address'][1][$mapper['location'][$header[$key]]] = $val; | |
617 | } | |
618 | elseif (isset($mapper['transaction'][$header[$key]])) { | |
619 | $transaction[$mapper['transaction'][$header[$key]]] = $val; | |
620 | } | |
621 | else { | |
622 | $params[$header[$key]] = $val; | |
623 | } | |
624 | } | |
625 | ||
626 | if (!empty($transaction) && $category) { | |
627 | $params['transaction'] = $transaction; | |
628 | } | |
629 | else { | |
630 | $params += $transaction; | |
631 | } | |
632 | ||
633 | self::_fillCommonParams($params, $type); | |
634 | ||
635 | return $params; | |
636 | } | |
637 | ||
638 | if ($type == 'google') { | |
639 | // return if response smell invalid | |
640 | if (!array_key_exists('risk-information-notification', $apiParams[1][$apiParams[0]]['notifications'])) { | |
641 | return FALSE; | |
642 | } | |
643 | $riskInfo = &$apiParams[1][$apiParams[0]]['notifications']['risk-information-notification']; | |
644 | ||
645 | if (array_key_exists('new-order-notification', $apiParams[1][$apiParams[0]]['notifications'])) { | |
646 | $newOrder = &$apiParams[1][$apiParams[0]]['notifications']['new-order-notification']; | |
647 | } | |
648 | ||
649 | if ($riskInfo['google-order-number']['VALUE'] == $apiParams[2]['google-order-number']['VALUE']) { | |
650 | foreach ($riskInfo['risk-information']['billing-address'] as $field => $info) { | |
a7488080 | 651 | if (!empty($mapper['location'][$field])) { |
6a488035 TO |
652 | $params['address'][1][$mapper['location'][$field]] = $info['VALUE']; |
653 | } | |
a7488080 | 654 | elseif (!empty($mapper['contact'][$field])) { |
8cc574cf | 655 | if ($newOrder && !empty($newOrder['buyer-billing-address']['structured-name'])) { |
6a488035 TO |
656 | foreach ($newOrder['buyer-billing-address']['structured-name'] as $namePart => $nameValue) { |
657 | $params[$mapper['contact'][$namePart]] = $nameValue['VALUE']; | |
658 | } | |
659 | } | |
660 | else { | |
661 | $params[$mapper['contact'][$field]] = $info['VALUE']; | |
662 | } | |
663 | } | |
a7488080 | 664 | elseif (!empty($mapper['transaction'][$field])) { |
6a488035 TO |
665 | $transaction[$mapper['transaction'][$field]] = $info['VALUE']; |
666 | } | |
667 | } | |
668 | ||
669 | // Response is an huge array. Lets pickup only those which we ineterested in | |
670 | // using a local mapper, rather than traversing the entire array. | |
671 | $localMapper = array( | |
672 | 'google-order-number' => $riskInfo['google-order-number']['VALUE'], | |
673 | 'total-charge-amount' => $apiParams[2]['total-charge-amount']['VALUE'], | |
674 | 'currency' => $apiParams[2]['total-charge-amount']['currency'], | |
675 | 'item-name' => $newOrder['shopping-cart']['items']['item']['item-name']['VALUE'], | |
676 | 'timestamp' => $apiParams[2]['timestamp']['VALUE'], | |
677 | ); | |
678 | if (array_key_exists('latest-charge-fee', $apiParams[2])) { | |
679 | $localMapper['latest-charge-fee'] = $apiParams[2]['latest-charge-fee']['total']['VALUE']; | |
680 | $localMapper['net-amount'] = $localMapper['total-charge-amount'] - $localMapper['latest-charge-fee']; | |
681 | } | |
682 | ||
683 | // This is a subscription (recurring) donation. | |
684 | if (array_key_exists('subscription', $newOrder['shopping-cart']['items']['item'])) { | |
685 | $subscription = $newOrder['shopping-cart']['items']['item']['subscription']; | |
686 | $localMapper['amount'] = $newOrder['order-total']['VALUE']; | |
687 | $localMapper['times'] = $subscription['payments']['subscription-payment']['times']; | |
688 | // Convert Google's period to one compatible with the CiviCRM db field. | |
689 | $freqUnits = array( | |
690 | 'DAILY' => 'day', | |
691 | 'WEEKLY' => 'week', | |
692 | 'MONHTLY' => 'month', | |
693 | 'YEARLY' => 'year', | |
694 | ); | |
695 | $localMapper['period'] = $freqUnits[$subscription['period']]; | |
696 | // Unlike PayPal, Google has no concept of freq. interval, it is always 1. | |
697 | $localMapper['frequency_interval'] = '1'; | |
698 | // Google Checkout dates are in ISO-8601 format. We need a format that | |
699 | // MySQL likes | |
700 | $unix_timestamp = strtotime($localMapper['timestamp']); | |
701 | $mysql_date = date('YmdHis', $unix_timestamp); | |
702 | $localMapper['modified_date'] = $mysql_date; | |
703 | $localMapper['start_date'] = $mysql_date; | |
704 | // This is PayPal's nomenclature, but just use it for Google as well since | |
705 | // we act on the value of trxn_type in processAPIContribution(). | |
706 | $localMapper['trxn_type'] = 'subscrpayment'; | |
707 | } | |
708 | ||
709 | foreach ($localMapper as $localKey => $localVal) { | |
a7488080 | 710 | if (!empty($mapper['transaction'][$localKey])) { |
6a488035 TO |
711 | $transaction[$mapper['transaction'][$localKey]] = $localVal; |
712 | } | |
713 | } | |
714 | ||
715 | if (empty($params) && empty($transaction)) { | |
716 | continue; | |
717 | } | |
718 | ||
719 | if (!empty($transaction) && $category) { | |
720 | $params['transaction'] = $transaction; | |
721 | } | |
722 | else { | |
723 | $params += $transaction; | |
724 | } | |
725 | ||
726 | self::_fillCommonParams($params, $type); | |
727 | } | |
728 | return $params; | |
729 | } | |
730 | } | |
731 | ||
186c9c17 EM |
732 | /** |
733 | * @param $params | |
734 | * | |
735 | * @return bool | |
736 | */ | |
6a488035 TO |
737 | static function processAPIContribution($params) { |
738 | if (empty($params) || array_key_exists('error', $params)) { | |
739 | return FALSE; | |
740 | } | |
741 | ||
742 | // add contact using dedupe rule | |
743 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual'); | |
744 | $dedupeParams['check_permission'] = FALSE; | |
745 | $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); | |
746 | // if we find more than one contact, use the first one | |
a7488080 | 747 | if (!empty($dupeIds[0])) { |
6a488035 TO |
748 | $params['contact_id'] = $dupeIds[0]; |
749 | } | |
750 | $contact = CRM_Contact_BAO_Contact::create($params); | |
751 | if (!$contact->id) { | |
752 | return FALSE; | |
753 | } | |
754 | ||
755 | // only pass transaction params to contribution::create, if available | |
756 | if (array_key_exists('transaction', $params)) { | |
757 | $params = $params['transaction']; | |
758 | $params['contact_id'] = $contact->id; | |
759 | } | |
760 | ||
761 | // handle contribution custom data | |
762 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', | |
763 | FALSE, | |
764 | FALSE, | |
765 | CRM_Utils_Array::value('financial_type_id', | |
766 | $params | |
767 | ) | |
768 | ); | |
769 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
770 | $customFields, | |
771 | CRM_Utils_Array::value('id', $params, NULL), | |
772 | 'Contribution' | |
773 | ); | |
774 | // create contribution | |
775 | ||
776 | // if this is a recurring contribution then process it first | |
777 | if ($params['trxn_type'] == 'subscrpayment') { | |
778 | // see if a recurring record already exists | |
779 | $recurring = new CRM_Contribute_BAO_ContributionRecur; | |
780 | $recurring->processor_id = $params['processor_id']; | |
781 | if (!$recurring->find(TRUE)) { | |
782 | $recurring = new CRM_Contribute_BAO_ContributionRecur; | |
783 | $recurring->invoice_id = $params['invoice_id']; | |
784 | $recurring->find(TRUE); | |
785 | } | |
786 | ||
787 | // This is the same thing the CiviCRM IPN handler does to handle | |
788 | // subsequent recurring payments to avoid duplicate contribution | |
789 | // errors due to invoice ID. See: | |
790 | // ./CRM/Core/Payment/PayPalIPN.php:200 | |
791 | if ($recurring->id) { | |
792 | $params['invoice_id'] = md5(uniqid(rand(), TRUE)); | |
793 | } | |
794 | ||
795 | $recurring->copyValues($params); | |
796 | $recurring->save(); | |
797 | if (is_a($recurring, 'CRM_Core_Error')) { | |
798 | return FALSE; | |
799 | } | |
800 | else { | |
801 | $params['contribution_recur_id'] = $recurring->id; | |
802 | } | |
803 | } | |
804 | ||
805 | $contribution = &CRM_Contribute_BAO_Contribution::create($params, | |
806 | CRM_Core_DAO::$_nullArray | |
807 | ); | |
808 | if (!$contribution->id) { | |
809 | return FALSE; | |
810 | } | |
811 | ||
812 | return TRUE; | |
813 | } | |
814 | ||
186c9c17 EM |
815 | /** |
816 | * @param $contactID | |
817 | * | |
818 | * @return mixed | |
819 | */ | |
6a488035 TO |
820 | static function getFirstLastDetails($contactID) { |
821 | static $_cache; | |
822 | ||
823 | if (!$_cache) { | |
824 | $_cache = array(); | |
825 | } | |
826 | ||
827 | if (!isset($_cache[$contactID])) { | |
828 | $sql = " | |
829 | SELECT total_amount, receive_date | |
830 | FROM civicrm_contribution c | |
831 | WHERE contact_id = %1 | |
832 | ORDER BY receive_date ASC | |
833 | LIMIT 1 | |
834 | "; | |
835 | $params = array(1 => array($contactID, 'Integer')); | |
836 | ||
837 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
838 | $details = array( | |
839 | 'first' => NULL, | |
840 | 'last' => NULL, | |
841 | ); | |
842 | if ($dao->fetch()) { | |
843 | $details['first'] = array( | |
844 | 'total_amount' => $dao->total_amount, | |
845 | 'receive_date' => $dao->receive_date, | |
846 | ); | |
847 | } | |
848 | ||
849 | // flip asc and desc to get the last query | |
850 | $sql = str_replace('ASC', 'DESC', $sql); | |
851 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
852 | if ($dao->fetch()) { | |
853 | $details['last'] = array( | |
854 | 'total_amount' => $dao->total_amount, | |
855 | 'receive_date' => $dao->receive_date, | |
856 | ); | |
857 | } | |
858 | ||
859 | $_cache[$contactID] = $details; | |
860 | } | |
861 | return $_cache[$contactID]; | |
862 | } | |
863 | } | |
864 |