3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
34 class CRM_Contribute_BAO_Contribution_Utils
{
37 * Process payment after confirmation.
39 * @param CRM_Core_Form $form
41 * @param array $paymentParams
42 * Array with payment related key.
44 * @param int $contactID
46 * @param int $contributionTypeId
48 * @param int|string $component component id
51 * @throws CRM_Core_Exception
57 public static function processConfirm(
62 $component = 'contribution',
65 CRM_Core_Payment_Form
::mapParams($form->_bltID
, $form->_params
, $paymentParams, TRUE);
66 $lineItems = $form->_lineItem
;
67 $isPaymentTransaction = self
::isPaymentTransaction($form);
69 $financialType = new CRM_Financial_DAO_FinancialType();
70 $financialType->id
= $contributionTypeId;
71 $financialType->find(TRUE);
72 if ($financialType->is_deductible
) {
73 $form->assign('is_deductible', TRUE);
74 $form->set('is_deductible', TRUE);
77 // add some financial type details to the params list
78 // if folks need to use it
79 //CRM-15297 - contributionType is obsolete - pass financial type as well so people can deprecate it
80 $paymentParams['financialType_name'] = $paymentParams['contributionType_name'] = $form->_params
['contributionType_name'] = $financialType->name
;
82 $paymentParams['financialType_accounting_code'] = $paymentParams['contributionType_accounting_code'] = $form->_params
['contributionType_accounting_code'] = CRM_Financial_BAO_FinancialAccount
::getAccountingCode($contributionTypeId);
83 $paymentParams['contributionPageID'] = $form->_params
['contributionPageID'] = $form->_values
['id'];
84 $paymentParams['contactID'] = $form->_params
['contactID'] = $contactID;
87 $form->_params
['receive_date'] = date('YmdHis');
88 $form->assign('receive_date',
89 CRM_Utils_Date
::mysqlToIso($form->_params
['receive_date'])
91 if ($isPaymentTransaction) {
92 // Fix for CRM-14354. If the membership is recurring, don't create a
93 // civicrm_contribution_recur record for the additional contribution
94 // (i.e., the amount NOT associated with the membership). Temporarily
95 // cache the is_recur values so we can process the additional gift as a
97 if (!empty($form->_values
['is_recur'])) {
98 if ($form->_membershipBlock
['is_separate_payment'] && !empty($form->_params
['auto_renew'])) {
99 $cachedFormValue = CRM_Utils_Array
::value('is_recur', $form->_values
);
100 $cachedParamValue = CRM_Utils_Array
::value('is_recur', $paymentParams);
101 unset($form->_values
['is_recur']);
102 unset($paymentParams['is_recur']);
106 $contributionParams = array(
107 'contact_id' => $contactID,
108 'line_item' => $lineItems,
109 'is_test' => $isTest,
110 'campaign_id' => CRM_Utils_Array
::value('campaign_id', $paymentParams, CRM_Utils_Array
::value('campaign_id', $form->_values
)),
111 'contribution_page_id' => $form->_id
,
112 'source' => CRM_Utils_Array
::value('source', $paymentParams, CRM_Utils_Array
::value('description', $paymentParams)),
114 $isMonetary = !empty($form->_values
['is_monetary']);
116 if (empty($paymentParams['is_pay_later'])) {
117 // @todo look up payment_instrument_id on payment processor table.
118 $contributionParams['payment_instrument_id'] = 1;
121 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processFormContribution(
131 $paymentParams['contributionTypeID'] = $contributionTypeId;
132 $paymentParams['item_name'] = $form->_params
['description'];
134 if ($contribution && $form->_values
['is_recur'] && $contribution->contribution_recur_id
136 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
139 $paymentParams['qfKey'] = $form->controller
->_key
;
140 if ($component == 'membership') {
141 return array('contribution' => $contribution);
144 // restore cached values (part of fix for CRM-14354)
145 if (!empty($cachedFormValue)) {
146 $form->_values
['is_recur'] = $cachedFormValue;
147 $paymentParams['is_recur'] = $cachedParamValue;
150 $paymentParams['contributionID'] = $contribution->id
;
151 //CRM-15297 deprecate contributionTypeID
152 $paymentParams['financialTypeID'] = $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
153 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
154 if (isset($paymentParams['contribution_source'])) {
155 $paymentParams['source'] = $paymentParams['contribution_source'];
158 if ($form->_values
['is_recur'] && $contribution->contribution_recur_id
) {
159 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
162 if (!empty($form->_paymentProcessor
) && $form->_amount
> 0.0) {
164 $payment = Civi\Payment\System
::singleton()->getByProcessor($form->_paymentProcessor
);
165 if ($form->_contributeMode
== 'notify') {
166 // We want to get rid of this & make it generic - eg. by making payment processing the last thing
167 // and always calling it first.
168 $form->postProcessHook();
170 $result = $payment->doPayment($paymentParams);
171 $form->_params
= array_merge($form->_params
, $result);
172 $form->assign('trxn_id', CRM_Utils_Array
::value('trxn_id', $result));
173 if (!empty($result['trxn_id'])) {
174 $contribution->trxn_id
= $result['trxn_id'];
176 if (!empty($result['payment_status_id'])) {
177 $contribution->payment_status_id
= $result['payment_status_id'];
179 $result['contribution'] = $contribution;
182 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
183 // Clean up DB as appropriate.
184 if (!empty($paymentParams['contributionID'])) {
185 CRM_Contribute_BAO_Contribution
::failPayment($paymentParams['contributionID'],
186 $paymentParams['contactID'], $e->getMessage());
188 if (!empty($paymentParams['contributionRecurID'])) {
189 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
192 $result['is_payment_failure'] = TRUE;
193 $result['error'] = $e;
199 // Only pay later or unpaid should reach this point. The theory is that paylater should get a receipt now &
201 // transaction receipts should be outcome driven.
202 $form->set('params', $form->_params
);
203 if (isset($paymentParams['contribution_source'])) {
204 $form->_params
['source'] = $paymentParams['contribution_source'];
207 // get the price set values for receipt.
208 if ($form->_priceSetId
&& $form->_lineItem
) {
209 $form->_values
['lineItem'] = $form->_lineItem
;
210 $form->_values
['priceSetID'] = $form->_priceSetId
;
213 $form->_values
['contribution_id'] = $contribution->id
;
214 $form->_values
['contribution_page_id'] = $contribution->contribution_page_id
;
216 CRM_Contribute_BAO_ContributionPage
::sendMail($contactID,
218 $contribution->is_test
223 * Is a payment being made.
224 * Note that setting is_monetary on the form is somewhat legacy and the behaviour around this setting is confusing. It would be preferable
225 * to look for the amount only (assuming this cannot refer to payment in goats or other non-monetary currency
226 * @param CRM_Core_Form $form
230 static protected function isPaymentTransaction($form) {
231 if (!empty($form->_values
['is_monetary']) && $form->_amount
>= 0.0) {
239 * Get the contribution details by month of the year.
247 public static function contributionChartMonthly($param) {
249 $param = array(1 => array($param, 'Integer'));
253 $param = array(1 => array($param, 'Integer'));
257 SELECT sum(contrib.total_amount) AS ctAmt,
258 MONTH( contrib.receive_date) AS contribMonth
259 FROM civicrm_contribution AS contrib
260 INNER JOIN civicrm_contact AS contact ON ( contact.id = contrib.contact_id )
261 WHERE contrib.contact_id = contact.id
262 AND ( contrib.is_test = 0 OR contrib.is_test IS NULL )
263 AND contrib.contribution_status_id = 1
264 AND date_format(contrib.receive_date,'%Y') = %1
265 AND contact.is_deleted = 0
266 GROUP BY contribMonth
267 ORDER BY month(contrib.receive_date)";
269 $dao = CRM_Core_DAO
::executeQuery($query, $param);
272 while ($dao->fetch()) {
273 if ($dao->contribMonth
) {
274 $params['By Month'][$dao->contribMonth
] = $dao->ctAmt
;
281 * Get the contribution details by year.
286 public static function contributionChartYearly() {
287 $config = CRM_Core_Config
::singleton();
288 $yearClause = "year(contrib.receive_date) as contribYear";
289 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] != 1 ||
$config->fiscalYearStart
['d'] != 1)) {
291 WHEN (MONTH(contrib.receive_date)>= " . $config->fiscalYearStart
['M'] . "
292 && DAYOFMONTH(contrib.receive_date)>= " . $config->fiscalYearStart
['d'] . " )
294 concat(YEAR(contrib.receive_date), '-',YEAR(contrib.receive_date)+1)
296 concat(YEAR(contrib.receive_date)-1,'-', YEAR(contrib.receive_date))
301 SELECT sum(contrib.total_amount) AS ctAmt,
303 FROM civicrm_contribution AS contrib
304 INNER JOIN civicrm_contact contact ON ( contact.id = contrib.contact_id )
305 WHERE ( contrib.is_test = 0 OR contrib.is_test IS NULL )
306 AND contrib.contribution_status_id = 1
307 AND contact.is_deleted = 0
309 ORDER BY contribYear";
310 $dao = CRM_Core_DAO
::executeQuery($query);
313 while ($dao->fetch()) {
314 if (!empty($dao->contribYear
)) {
315 $params['By Year'][$dao->contribYear
] = $dao->ctAmt
;
322 * @param array $params
323 * @param int $contactID
326 public static function createCMSUser(&$params, $contactID, $mail) {
327 // lets ensure we only create one CMS user
328 static $created = FALSE;
335 if (!empty($params['cms_create_account'])) {
336 $params['contactID'] = $contactID;
337 if (!CRM_Core_BAO_CMSUser
::create($params, $mail)) {
338 CRM_Core_Error
::statusBounce(ts('Your profile is not saved and Account is not created.'));
344 * @param array $params
345 * @param string $type
349 public static function _fillCommonParams(&$params, $type = 'paypal') {
350 if (array_key_exists('transaction', $params)) {
351 $transaction = &$params['transaction'];
354 $transaction = &$params;
357 $params['contact_type'] = 'Individual';
359 $billingLocTypeId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_LocationType', 'Billing', 'id', 'name');
360 if (!$billingLocTypeId) {
361 $billingLocTypeId = 1;
363 if (!CRM_Utils_System
::isNull($params['address'])) {
364 $params['address'][1]['is_primary'] = 1;
365 $params['address'][1]['location_type_id'] = $billingLocTypeId;
367 if (!CRM_Utils_System
::isNull($params['email'])) {
368 $params['email'] = array(
370 'email' => $params['email'],
371 'location_type_id' => $billingLocTypeId,
376 if (isset($transaction['trxn_id'])) {
377 // set error message if transaction has already been processed.
378 $contribution = new CRM_Contribute_DAO_Contribution();
379 $contribution->trxn_id
= $transaction['trxn_id'];
380 if ($contribution->find(TRUE)) {
381 $params['error'][] = ts('transaction already processed.');
385 // generate a new transaction id, if not already exist
386 $transaction['trxn_id'] = md5(uniqid(rand(), TRUE));
389 if (!isset($transaction['financial_type_id'])) {
390 $contributionTypes = array_keys(CRM_Contribute_PseudoConstant
::financialType());
391 $transaction['financial_type_id'] = $contributionTypes[0];
394 if (($type == 'paypal') && (!isset($transaction['net_amount']))) {
395 $transaction['net_amount'] = $transaction['total_amount'] - CRM_Utils_Array
::value('fee_amount', $transaction, 0);
398 if (!isset($transaction['invoice_id'])) {
399 $transaction['invoice_id'] = $transaction['trxn_id'];
402 $source = ts('ContributionProcessor: %1 API',
403 array(1 => ucfirst($type))
405 if (isset($transaction['source'])) {
406 $transaction['source'] = $source . ':: ' . $transaction['source'];
409 $transaction['source'] = $source;
416 * @param int $contactID
420 public static function getFirstLastDetails($contactID) {
427 if (!isset($_cache[$contactID])) {
429 SELECT total_amount, receive_date
430 FROM civicrm_contribution c
431 WHERE contact_id = %1
432 ORDER BY receive_date ASC
435 $params = array(1 => array($contactID, 'Integer'));
437 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
443 $details['first'] = array(
444 'total_amount' => $dao->total_amount
,
445 'receive_date' => $dao->receive_date
,
449 // flip asc and desc to get the last query
450 $sql = str_replace('ASC', 'DESC', $sql);
451 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
453 $details['last'] = array(
454 'total_amount' => $dao->total_amount
,
455 'receive_date' => $dao->receive_date
,
459 $_cache[$contactID] = $details;
461 return $_cache[$contactID];
465 * Calculate the tax amount based on given tax rate.
467 * @param float $amount
469 * @param float $taxRate
470 * Tax rate of selected financial account for field.
473 * array of tax amount
476 public static function calculateTaxAmount($amount, $taxRate) {
477 $taxAmount = array();
478 $taxAmount['tax_amount'] = ($taxRate / 100) * CRM_Utils_Rule
::cleanMoney($amount);