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