Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
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 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * form to process actions on the group aspect of Custom Data | |
36 | */ | |
37 | class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_ContributionBase { | |
38 | ||
39 | /** | |
fe482240 | 40 | * The id of the contact associated with this contribution. |
6a488035 TO |
41 | * |
42 | * @var int | |
6a488035 TO |
43 | */ |
44 | public $_contactID; | |
45 | ||
1f1740fe DL |
46 | |
47 | /** | |
fe482240 | 48 | * The id of the contribution object that is created when the form is submitted. |
1f1740fe DL |
49 | * |
50 | * @var int | |
1f1740fe DL |
51 | */ |
52 | public $_contributionID; | |
53 | ||
c8bde7ea | 54 | /** |
fe482240 | 55 | * Set the parameters to be passed to contribution create function. |
c8bde7ea EM |
56 | * |
57 | * @param array $params | |
100fef9d | 58 | * @param int $financialTypeID |
ceb9fecb | 59 | * @param float $nonDeductibleAmount |
ceb9fecb | 60 | * @param bool $pending |
ab8fe4ef | 61 | * @param array $paymentProcessorOutcome |
62 | * @param string $receiptDate | |
100fef9d | 63 | * @param int $recurringContributionID |
c8bde7ea | 64 | * |
c8bde7ea EM |
65 | * @return array |
66 | */ | |
a13f3d8c | 67 | public static function getContributionParams( |
3febe800 | 68 | $params, $financialTypeID, $nonDeductibleAmount, $pending, |
f6261e9d | 69 | $paymentProcessorOutcome, $receiptDate, $recurringContributionID) { |
c8bde7ea | 70 | $contributionParams = array( |
c8bde7ea | 71 | 'financial_type_id' => $financialTypeID, |
c8bde7ea EM |
72 | 'receive_date' => (CRM_Utils_Array::value('receive_date', $params)) ? CRM_Utils_Date::processDate($params['receive_date']) : date('YmdHis'), |
73 | 'non_deductible_amount' => $nonDeductibleAmount, | |
74 | 'total_amount' => $params['amount'], | |
17ab42dd | 75 | 'tax_amount' => CRM_Utils_Array::value('tax_amount', $params), |
c8bde7ea EM |
76 | 'amount_level' => CRM_Utils_Array::value('amount_level', $params), |
77 | 'invoice_id' => $params['invoiceID'], | |
78 | 'currency' => $params['currencyID'], | |
c8bde7ea EM |
79 | 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), |
80 | //configure cancel reason, cancel date and thankyou date | |
81 | //from 'contribution' type profile if included | |
82 | 'cancel_reason' => CRM_Utils_Array::value('cancel_reason', $params, 0), | |
a1a2a83d TO |
83 | 'cancel_date' => isset($params['cancel_date']) ? CRM_Utils_Date::format($params['cancel_date']) : NULL, |
84 | 'thankyou_date' => isset($params['thankyou_date']) ? CRM_Utils_Date::format($params['thankyou_date']) : NULL, | |
c8bde7ea | 85 | //setting to make available to hook - although seems wrong to set on form for BAO hook availability |
21dfd5f5 | 86 | 'skipLineItem' => CRM_Utils_Array::value('skipLineItem', $params, 0), |
c8bde7ea | 87 | ); |
3febe800 | 88 | |
a7a33651 EM |
89 | if ($paymentProcessorOutcome) { |
90 | $contributionParams['payment_processor'] = CRM_Utils_Array::value('payment_processor', $paymentProcessorOutcome); | |
91 | } | |
c8bde7ea EM |
92 | if (!$pending && $paymentProcessorOutcome) { |
93 | $contributionParams += array( | |
94 | 'fee_amount' => CRM_Utils_Array::value('fee_amount', $paymentProcessorOutcome), | |
95 | 'net_amount' => CRM_Utils_Array::value('net_amount', $paymentProcessorOutcome, $params['amount']), | |
96 | 'trxn_id' => $paymentProcessorOutcome['trxn_id'], | |
97 | 'receipt_date' => $receiptDate, | |
98 | // also add financial_trxn details as part of fix for CRM-4724 | |
99 | 'trxn_result_code' => CRM_Utils_Array::value('trxn_result_code', $paymentProcessorOutcome), | |
c8bde7ea EM |
100 | ); |
101 | } | |
102 | ||
103 | // CRM-4038: for non-en_US locales, CRM_Contribute_BAO_Contribution::add() expects localised amounts | |
104 | $contributionParams['non_deductible_amount'] = trim(CRM_Utils_Money::format($contributionParams['non_deductible_amount'], ' ')); | |
105 | $contributionParams['total_amount'] = trim(CRM_Utils_Money::format($contributionParams['total_amount'], ' ')); | |
106 | ||
107 | if ($recurringContributionID) { | |
108 | $contributionParams['contribution_recur_id'] = $recurringContributionID; | |
109 | } | |
110 | ||
8387fd96 | 111 | $contributionParams['contribution_status_id'] = ($pending && $contributionParams['total_amount'] != 0) ? 2 : 1; |
c8bde7ea EM |
112 | if (isset($contributionParams['invoice_id'])) { |
113 | $contributionParams['id'] = CRM_Core_DAO::getFieldValue( | |
114 | 'CRM_Contribute_DAO_Contribution', | |
115 | $contributionParams['invoice_id'], | |
116 | 'id', | |
117 | 'invoice_id' | |
118 | ); | |
119 | } | |
120 | ||
121 | return $contributionParams; | |
122 | } | |
123 | ||
fda99f2f EM |
124 | /** |
125 | * Get non-deductible amount. | |
126 | * | |
127 | * This is a bit too much about wierd form interpretation to be this deep. | |
128 | * | |
129 | * CRM-11885 | |
130 | * if non_deductible_amount exists i.e. Additional Details fieldset was opened [and staff typed something] -> keep | |
131 | * it. | |
ceb9fecb EM |
132 | * |
133 | * @param array $params | |
134 | * @param CRM_Financial_BAO_FinancialType $financialType | |
135 | * @param bool $online | |
fda99f2f EM |
136 | * |
137 | * @return array | |
138 | */ | |
139 | protected static function getNonDeductibleAmount($params, $financialType, $online) { | |
140 | if (isset($params['non_deductible_amount']) && (!empty($params['non_deductible_amount']))) { | |
141 | return $params['non_deductible_amount']; | |
142 | } | |
143 | else { | |
144 | if ($financialType->is_deductible) { | |
145 | if ($online && isset($params['selectProduct'])) { | |
146 | $selectProduct = CRM_Utils_Array::value('selectProduct', $params); | |
147 | } | |
148 | if (!$online && isset($params['product_name'][0])) { | |
149 | $selectProduct = $params['product_name'][0]; | |
150 | } | |
151 | // if there is a product - compare the value to the contribution amount | |
152 | if (isset($selectProduct) && | |
153 | $selectProduct != 'no_thanks' | |
154 | ) { | |
155 | $productDAO = new CRM_Contribute_DAO_Product(); | |
156 | $productDAO->id = $selectProduct; | |
157 | $productDAO->find(TRUE); | |
158 | // product value exceeds contribution amount | |
159 | if ($params['amount'] < $productDAO->price) { | |
160 | $nonDeductibleAmount = $params['amount']; | |
161 | return $nonDeductibleAmount; | |
162 | } | |
163 | // product value does NOT exceed contribution amount | |
164 | else { | |
165 | return $productDAO->price; | |
166 | } | |
167 | } | |
168 | // contribution is deductible - but there is no product | |
169 | else { | |
170 | return '0.00'; | |
171 | } | |
172 | } | |
173 | // contribution is NOT deductible | |
174 | else { | |
175 | return $params['amount']; | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
6a488035 | 180 | /** |
fe482240 | 181 | * Set variables up before form is built. |
6a488035 TO |
182 | */ |
183 | public function preProcess() { | |
184 | $config = CRM_Core_Config::singleton(); | |
185 | parent::preProcess(); | |
cde484fd | 186 | |
6a488035 TO |
187 | // lineItem isn't set until Register postProcess |
188 | $this->_lineItem = $this->get('lineItem'); | |
189 | $this->_paymentProcessor = $this->get('paymentProcessor'); | |
a9768188 EM |
190 | $this->_params = $this->controller->exportValues('Main'); |
191 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); | |
192 | $this->_params['amount'] = $this->get('amount'); | |
193 | if (isset($this->_params['amount'])) { | |
194 | $this->setFormAmountFields($this->_params['priceSetId']); | |
6a488035 | 195 | } |
6a488035 | 196 | |
a9768188 EM |
197 | $this->_params['tax_amount'] = $this->get('tax_amount'); |
198 | $this->_useForMember = $this->get('useForMember'); | |
6a488035 | 199 | |
a9768188 EM |
200 | if (isset($this->_params['credit_card_exp_date'])) { |
201 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
202 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
203 | } | |
5df36634 | 204 | |
a9768188 | 205 | $this->_params['currencyID'] = $config->defaultCurrency; |
6a488035 | 206 | |
a9768188 EM |
207 | if (!empty($this->_membershipBlock)) { |
208 | $this->_params['selectMembership'] = $this->get('selectMembership'); | |
209 | } | |
210 | if (!empty($this->_paymentProcessor) && $this->_paymentProcessor['object']->supports('preApproval')) { | |
211 | $preApprovalParams = $this->_paymentProcessor['object']->getPreApprovalDetails($this->get('pre_approval_parameters')); | |
212 | $this->_params = array_merge($this->_params, $preApprovalParams); | |
213 | } | |
cde484fd | 214 | |
a9768188 EM |
215 | // We may have fetched some billing details from the getPreApprovalDetails function so we |
216 | // want to ensure we set this after that function has been called. | |
217 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $this->_params, FALSE); | |
6a488035 TO |
218 | |
219 | $this->_params['is_pay_later'] = $this->get('is_pay_later'); | |
220 | $this->assign('is_pay_later', $this->_params['is_pay_later']); | |
221 | if ($this->_params['is_pay_later']) { | |
222 | $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']); | |
223 | } | |
224 | // if onbehalf-of-organization | |
4779abb3 | 225 | if (!empty($this->_values['onbehalf_profile_id']) && !empty($this->_params['onbehalf']['organization_name'])) { |
c8fa0a31 | 226 | // CRM-15182 |
6cc679d2 | 227 | $this->_params['organization_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_params['onbehalf']['organization_name'], 'id', 'display_name'); |
6a488035 TO |
228 | |
229 | $this->_params['organization_name'] = $this->_params['onbehalf']['organization_name']; | |
230 | $addressBlocks = array( | |
a13f3d8c TO |
231 | 'street_address', |
232 | 'city', | |
233 | 'state_province', | |
234 | 'postal_code', | |
235 | 'country', | |
236 | 'supplemental_address_1', | |
237 | 'supplemental_address_2', | |
238 | 'supplemental_address_3', | |
239 | 'postal_code_suffix', | |
240 | 'geo_code_1', | |
241 | 'geo_code_2', | |
242 | 'address_name', | |
6a488035 TO |
243 | ); |
244 | ||
245 | $blocks = array('email', 'phone', 'im', 'url', 'openid'); | |
246 | foreach ($this->_params['onbehalf'] as $loc => $value) { | |
247 | $field = $typeId = NULL; | |
248 | if (strstr($loc, '-')) { | |
249 | list($field, $locType) = explode('-', $loc); | |
250 | } | |
251 | ||
252 | if (in_array($field, $addressBlocks)) { | |
253 | if ($locType == 'Primary') { | |
254 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
255 | $locType = $defaultLocationType->id; | |
cde484fd DL |
256 | } |
257 | ||
6a488035 TO |
258 | if ($field == 'country') { |
259 | $value = CRM_Core_PseudoConstant::countryIsoCode($value); | |
260 | } | |
261 | elseif ($field == 'state_province') { | |
262 | $value = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); | |
263 | } | |
cde484fd | 264 | |
6a488035 TO |
265 | $isPrimary = 1; |
266 | if (isset($this->_params['onbehalf_location']['address']) | |
a13f3d8c TO |
267 | && count($this->_params['onbehalf_location']['address']) > 0 |
268 | ) { | |
6a488035 TO |
269 | $isPrimary = 0; |
270 | } | |
cde484fd | 271 | |
6a488035 | 272 | $this->_params['onbehalf_location']['address'][$locType][$field] = $value; |
a7488080 | 273 | if (empty($this->_params['onbehalf_location']['address'][$locType]['is_primary'])) { |
6a488035 | 274 | $this->_params['onbehalf_location']['address'][$locType]['is_primary'] = $isPrimary; |
a13f3d8c | 275 | } |
6a488035 TO |
276 | $this->_params['onbehalf_location']['address'][$locType]['location_type_id'] = $locType; |
277 | } | |
278 | elseif (in_array($field, $blocks)) { | |
279 | if (!$typeId || is_numeric($typeId)) { | |
a13f3d8c TO |
280 | $blockName = $fieldName = $field; |
281 | $locationType = 'location_type_id'; | |
282 | if ($locType == 'Primary') { | |
6a488035 TO |
283 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
284 | $locationValue = $defaultLocationType->id; | |
285 | } | |
286 | else { | |
287 | $locationValue = $locType; | |
288 | } | |
a13f3d8c | 289 | $locTypeId = ''; |
6a488035 TO |
290 | $phoneExtField = array(); |
291 | ||
292 | if ($field == 'url') { | |
a13f3d8c | 293 | $blockName = 'website'; |
887e764d PN |
294 | $locationType = 'website_type_id'; |
295 | list($field, $locationValue) = explode('-', $loc); | |
6a488035 TO |
296 | } |
297 | elseif ($field == 'im') { | |
298 | $fieldName = 'name'; | |
299 | $locTypeId = 'provider_id'; | |
a13f3d8c | 300 | $typeId = $this->_params['onbehalf']["{$loc}-provider_id"]; |
6a488035 TO |
301 | } |
302 | elseif ($field == 'phone') { | |
303 | list($field, $locType, $typeId) = explode('-', $loc); | |
304 | $locTypeId = 'phone_type_id'; | |
305 | ||
306 | //check if extension field exists | |
a13f3d8c | 307 | $extField = str_replace('phone', 'phone_ext', $loc); |
6a488035 TO |
308 | if (isset($this->_params['onbehalf'][$extField])) { |
309 | $phoneExtField = array('phone_ext' => $this->_params['onbehalf'][$extField]); | |
310 | } | |
311 | } | |
312 | ||
313 | $isPrimary = 1; | |
a13f3d8c TO |
314 | if (isset ($this->_params['onbehalf_location'][$blockName]) |
315 | && count($this->_params['onbehalf_location'][$blockName]) > 0 | |
316 | ) { | |
317 | $isPrimary = 0; | |
6a488035 TO |
318 | } |
319 | if ($locationValue) { | |
320 | $blockValues = array( | |
a13f3d8c | 321 | $fieldName => $value, |
6a488035 | 322 | $locationType => $locationValue, |
a13f3d8c | 323 | 'is_primary' => $isPrimary, |
6a488035 TO |
324 | ); |
325 | ||
326 | if ($locTypeId) { | |
a13f3d8c | 327 | $blockValues = array_merge($blockValues, array($locTypeId => $typeId)); |
6a488035 TO |
328 | } |
329 | if (!empty($phoneExtField)) { | |
330 | $blockValues = array_merge($blockValues, $phoneExtField); | |
331 | } | |
332 | ||
333 | $this->_params['onbehalf_location'][$blockName][] = $blockValues; | |
334 | } | |
335 | } | |
336 | } | |
337 | elseif (strstr($loc, 'custom')) { | |
338 | if ($value && isset($this->_params['onbehalf']["{$loc}_id"])) { | |
339 | $value = $this->_params['onbehalf']["{$loc}_id"]; | |
340 | } | |
341 | $this->_params['onbehalf_location']["{$loc}"] = $value; | |
342 | } | |
343 | else { | |
344 | if ($loc == 'contact_sub_type') { | |
345 | $this->_params['onbehalf_location'][$loc] = $value; | |
346 | } | |
347 | else { | |
348 | $this->_params['onbehalf_location'][$field] = $value; | |
349 | } | |
350 | } | |
351 | } | |
352 | } | |
a7488080 | 353 | elseif (!empty($this->_values['is_for_organization'])) { |
6a488035 TO |
354 | // no on behalf of an organization, CRM-5519 |
355 | // so reset loc blocks from main params. | |
356 | foreach (array( | |
a13f3d8c TO |
357 | 'phone', |
358 | 'email', | |
21dfd5f5 | 359 | 'address', |
a13f3d8c | 360 | ) as $blk) { |
6a488035 TO |
361 | if (isset($this->_params[$blk])) { |
362 | unset($this->_params[$blk]); | |
363 | } | |
364 | } | |
365 | } | |
90102a32 | 366 | $this->setRecurringMembershipParams(); |
6a488035 TO |
367 | |
368 | if ($this->_pcpId) { | |
369 | $params = $this->processPcp($this, $this->_params); | |
370 | $this->_params = $params; | |
371 | } | |
372 | $this->_params['invoiceID'] = $this->get('invoiceID'); | |
373 | ||
374 | //carry campaign from profile. | |
375 | if (array_key_exists('contribution_campaign_id', $this->_params)) { | |
376 | $this->_params['campaign_id'] = $this->_params['contribution_campaign_id']; | |
377 | } | |
378 | ||
569df8ec DG |
379 | // assign contribution page id to the template so we can add css class for it |
380 | $this->assign('contributionPageID', $this->_id); | |
381 | ||
6a488035 TO |
382 | $this->set('params', $this->_params); |
383 | } | |
384 | ||
385 | /** | |
fe482240 | 386 | * Build the form object. |
6a488035 TO |
387 | */ |
388 | public function buildQuickForm() { | |
389 | $this->assignToTemplate(); | |
390 | ||
391 | $params = $this->_params; | |
6a488035 | 392 | // make sure we have values for it |
4779abb3 | 393 | if (!empty($this->_values['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) { |
a13f3d8c | 394 | $honorName = NULL; |
133e2c99 | 395 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); |
396 | ||
133e2c99 | 397 | $this->assign('soft_credit_type', $softCreditTypes[$params['soft_credit_type_id']]); |
4779abb3 | 398 | CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields($this, $params['honor']); |
6a488035 | 399 | |
133e2c99 | 400 | $fieldTypes = array('Contact'); |
4779abb3 | 401 | $fieldTypes[] = CRM_Core_BAO_UFGroup::getContactType($this->_values['honoree_profile_id']); |
402 | $this->buildCustom($this->_values['honoree_profile_id'], 'honoreeProfileFields', TRUE, 'honor', $fieldTypes); | |
6a488035 TO |
403 | } |
404 | $this->assign('receiptFromEmail', CRM_Utils_Array::value('receipt_from_email', $this->_values)); | |
405 | $amount_block_is_active = $this->get('amount_block_is_active'); | |
406 | $this->assign('amount_block_is_active', $amount_block_is_active); | |
407 | ||
a13f3d8c | 408 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
03b412ae PB |
409 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); |
410 | if ($invoicing) { | |
411 | $getTaxDetails = FALSE; | |
412 | $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); | |
413 | foreach ($this->_lineItem as $key => $value) { | |
414 | foreach ($value as $v) { | |
415 | if (isset($v['tax_rate'])) { | |
416 | if ($v['tax_rate'] != '') { | |
417 | $getTaxDetails = TRUE; | |
418 | } | |
419 | } | |
420 | } | |
421 | } | |
422 | $this->assign('getTaxDetails', $getTaxDetails); | |
423 | $this->assign('taxTerm', $taxTerm); | |
424 | $this->assign('totalTaxAmount', $params['tax_amount']); | |
425 | } | |
a7488080 | 426 | if (!empty($params['selectProduct']) && $params['selectProduct'] != 'no_thanks') { |
6a488035 TO |
427 | $option = CRM_Utils_Array::value('options_' . $params['selectProduct'], $params); |
428 | $productID = $params['selectProduct']; | |
429 | CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, FALSE, | |
430 | $productID, $option | |
431 | ); | |
432 | $this->set('productID', $productID); | |
433 | $this->set('option', $option); | |
434 | } | |
435 | $config = CRM_Core_Config::singleton(); | |
436 | if (in_array('CiviMember', $config->enableComponents)) { | |
437 | if (isset($params['selectMembership']) && | |
438 | $params['selectMembership'] != 'no_thanks' | |
439 | ) { | |
42e3a033 | 440 | $this->buildMembershipBlock( |
5b757295 | 441 | $this->_membershipContactID, |
6a488035 TO |
442 | FALSE, |
443 | $params['selectMembership'], | |
5b757295 | 444 | FALSE |
6a488035 | 445 | ); |
9cc96227 | 446 | if (!empty($params['auto_renew'])) { |
447 | $this->assign('auto_renew', TRUE); | |
448 | } | |
6a488035 TO |
449 | } |
450 | else { | |
451 | $this->assign('membershipBlock', FALSE); | |
452 | } | |
453 | } | |
454 | $this->buildCustom($this->_values['custom_pre_id'], 'customPre', TRUE); | |
455 | $this->buildCustom($this->_values['custom_post_id'], 'customPost', TRUE); | |
456 | ||
4779abb3 | 457 | if (!empty($this->_values['onbehalf_profile_id']) && !empty($params['onbehalf'])) { |
a13f3d8c | 458 | $fieldTypes = array('Contact', 'Organization'); |
6a488035 | 459 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); |
a13f3d8c | 460 | $fieldTypes = array_merge($fieldTypes, $contactSubType); |
6a488035 TO |
461 | if (is_array($this->_membershipBlock) && !empty($this->_membershipBlock)) { |
462 | $fieldTypes = array_merge($fieldTypes, array('Membership')); | |
463 | } | |
464 | else { | |
465 | $fieldTypes = array_merge($fieldTypes, array('Contribution')); | |
466 | } | |
467 | ||
4779abb3 | 468 | $this->buildCustom($this->_values['onbehalf_profile_id'], 'onbehalfProfile', TRUE, 'onbehalf', $fieldTypes); |
6a488035 TO |
469 | } |
470 | ||
471 | $this->_separateMembershipPayment = $this->get('separateMembershipPayment'); | |
472 | $this->assign('is_separate_payment', $this->_separateMembershipPayment); | |
9da8dc8c | 473 | if ($this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
6a488035 | 474 | $this->assign('lineItem', $this->_lineItem); |
a13f3d8c TO |
475 | } |
476 | else { | |
6a488035 TO |
477 | $this->assign('is_quick_config', 1); |
478 | $this->_params['is_quick_config'] = 1; | |
479 | } | |
480 | $this->assign('priceSetID', $this->_priceSetId); | |
a13f3d8c | 481 | $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, NULL, 'name'); |
f92fc7eb CW |
482 | if ($this->_paymentProcessor && |
483 | $this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('Google_Checkout', $paymentProcessorType) | |
6a488035 TO |
484 | && !$this->_params['is_pay_later'] && !($this->_amount == 0) |
485 | ) { | |
486 | $this->_checkoutButtonName = $this->getButtonName('next', 'checkout'); | |
487 | $this->add('image', | |
488 | $this->_checkoutButtonName, | |
489 | $this->_paymentProcessor['url_button'], | |
97e557d7 | 490 | array('class' => 'crm-form-submit') |
6a488035 TO |
491 | ); |
492 | ||
493 | $this->addButtons(array( | |
494 | array( | |
495 | 'type' => 'back', | |
f212d37d | 496 | 'name' => ts('Go Back'), |
6a488035 TO |
497 | ), |
498 | ) | |
499 | ); | |
500 | } | |
501 | else { | |
0f2b049e | 502 | // The concept of contributeMode is deprecated. |
503 | // the is_monetary concept probably should be too as it can be calculated from | |
504 | // the existence of 'amount' & seems fragile. | |
6a488035 TO |
505 | if ($this->_contributeMode == 'notify' || !$this->_values['is_monetary'] || |
506 | $this->_amount <= 0.0 || $this->_params['is_pay_later'] || | |
507 | ($this->_separateMembershipPayment && $this->_amount <= 0.0) | |
508 | ) { | |
f212d37d | 509 | $contribButton = ts('Continue'); |
6a488035 TO |
510 | $this->assign('button', ts('Continue')); |
511 | } | |
512 | else { | |
513 | $contribButton = ts('Make Contribution'); | |
514 | $this->assign('button', ts('Make Contribution')); | |
515 | } | |
516 | $this->addButtons(array( | |
517 | array( | |
518 | 'type' => 'next', | |
519 | 'name' => $contribButton, | |
520 | 'spacing' => ' ', | |
521 | 'isDefault' => TRUE, | |
c6edd786 | 522 | 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"), |
6a488035 TO |
523 | ), |
524 | array( | |
525 | 'type' => 'back', | |
526 | 'name' => ts('Go Back'), | |
527 | ), | |
528 | ) | |
529 | ); | |
530 | } | |
531 | ||
532 | $defaults = array(); | |
9c80939e | 533 | $fields = array_fill_keys(array_keys($this->_fields), 1); |
6a488035 TO |
534 | $fields["billing_state_province-{$this->_bltID}"] = $fields["billing_country-{$this->_bltID}"] = $fields["email-{$this->_bltID}"] = 1; |
535 | ||
536 | $contact = $this->_params; | |
537 | foreach ($fields as $name => $dontCare) { | |
9c80939e CW |
538 | // Recursively set defaults for nested fields |
539 | if (isset($contact[$name]) && is_array($contact[$name]) && ($name == 'onbehalf' || $name == 'honor')) { | |
540 | foreach ($contact[$name] as $fieldName => $fieldValue) { | |
a13f3d8c TO |
541 | if (is_array($fieldValue) && !in_array($this->_fields[$name][$fieldName]['html_type'], array( |
542 | 'Multi-Select', | |
21dfd5f5 | 543 | 'AdvMulti-Select', |
a13f3d8c TO |
544 | )) |
545 | ) { | |
38b95e0f | 546 | foreach ($fieldValue as $key => $value) { |
547 | $defaults["{$name}[{$fieldName}][{$key}]"] = $value; | |
548 | } | |
549 | } | |
550 | else { | |
551 | $defaults["{$name}[{$fieldName}]"] = $fieldValue; | |
552 | } | |
9c80939e CW |
553 | } |
554 | } | |
555 | elseif (isset($contact[$name])) { | |
6a488035 TO |
556 | $defaults[$name] = $contact[$name]; |
557 | if (substr($name, 0, 7) == 'custom_') { | |
558 | $timeField = "{$name}_time"; | |
559 | if (isset($contact[$timeField])) { | |
560 | $defaults[$timeField] = $contact[$timeField]; | |
561 | } | |
562 | if (isset($contact["{$name}_id"])) { | |
563 | $defaults["{$name}_id"] = $contact["{$name}_id"]; | |
564 | } | |
565 | } | |
a13f3d8c TO |
566 | elseif (in_array($name, array( |
567 | 'addressee', | |
568 | 'email_greeting', | |
21dfd5f5 | 569 | 'postal_greeting', |
a13f3d8c TO |
570 | )) && !empty($contact[$name . '_custom']) |
571 | ) { | |
6a488035 TO |
572 | $defaults[$name . '_custom'] = $contact[$name . '_custom']; |
573 | } | |
574 | } | |
575 | } | |
576 | ||
577 | $this->assign('useForMember', $this->get('useForMember')); | |
578 | ||
6a488035 TO |
579 | $this->setDefaults($defaults); |
580 | ||
581 | $this->freeze(); | |
582 | } | |
583 | ||
584 | /** | |
ab8fe4ef | 585 | * Overwrite action. |
586 | * | |
587 | * Since we are only showing elements in frozen mode no help display needed. | |
6a488035 TO |
588 | * |
589 | * @return int | |
6a488035 | 590 | */ |
00be9182 | 591 | public function getAction() { |
6a488035 TO |
592 | if ($this->_action & CRM_Core_Action::PREVIEW) { |
593 | return CRM_Core_Action::VIEW | CRM_Core_Action::PREVIEW; | |
594 | } | |
595 | else { | |
596 | return CRM_Core_Action::VIEW; | |
597 | } | |
598 | } | |
599 | ||
600 | /** | |
5e4f7f74 | 601 | * Set default values for the form. |
6a488035 | 602 | * |
5e4f7f74 EM |
603 | * Note that in edit/view mode |
604 | * the default values are retrieved from the database | |
6a488035 | 605 | */ |
a13f3d8c TO |
606 | public function setDefaultValues() { |
607 | } | |
6a488035 TO |
608 | |
609 | /** | |
fe482240 | 610 | * Process the form. |
6a488035 TO |
611 | */ |
612 | public function postProcess() { | |
da8d9879 | 613 | $contactID = $this->getContactID(); |
75637f22 EM |
614 | $result = $this->processFormSubmission($contactID); |
615 | if (is_array($result) && !empty($result['is_payment_failure'])) { | |
73baf02a EM |
616 | // We will probably have the function that gets this error throw an exception on the next round of refactoring. |
617 | CRM_Core_Session::singleton()->setStatus(ts("Payment Processor Error message :") . | |
618 | $result['error']->getMessage()); | |
75637f22 EM |
619 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', |
620 | "_qf_Main_display=true&qfKey={$this->_params['qfKey']}" | |
621 | )); | |
cc789d46 | 622 | } |
5fb28746 EM |
623 | // Presumably this is for hooks to access? Not quite clear & perhaps not required. |
624 | $this->set('params', $this->_params); | |
75637f22 | 625 | } |
6a488035 | 626 | |
75637f22 EM |
627 | /** |
628 | * Wrangle financial type ID. | |
629 | * | |
630 | * This wrangling of the financialType ID was happening in a shared function rather than in the form it relates to & hence has been moved to that form | |
631 | * Pledges are not relevant to the membership code so that portion will not go onto the membership form. | |
632 | * | |
633 | * Comments from previous refactor indicate doubt as to what was going on. | |
634 | * | |
635 | * @param int $contributionTypeId | |
636 | * | |
637 | * @return null|string | |
638 | */ | |
639 | public function wrangleFinancialTypeID($contributionTypeId) { | |
640 | if (isset($paymentParams['financial_type'])) { | |
641 | $contributionTypeId = $paymentParams['financial_type']; | |
6a488035 | 642 | } |
75637f22 EM |
643 | elseif (!empty($this->_values['pledge_id'])) { |
644 | $contributionTypeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', | |
645 | $this->_values['pledge_id'], | |
646 | 'financial_type_id' | |
647 | ); | |
6a488035 | 648 | } |
75637f22 EM |
649 | return $contributionTypeId; |
650 | } | |
6a488035 | 651 | |
75637f22 EM |
652 | /** |
653 | * Process the form. | |
654 | * | |
655 | * @param array $premiumParams | |
656 | * @param CRM_Contribute_BAO_Contribution $contribution | |
657 | */ | |
5fb28746 | 658 | protected function postProcessPremium($premiumParams, $contribution) { |
75637f22 EM |
659 | $hour = $minute = $second = 0; |
660 | // assigning Premium information to receipt tpl | |
661 | $selectProduct = CRM_Utils_Array::value('selectProduct', $premiumParams); | |
662 | if ($selectProduct && | |
663 | $selectProduct != 'no_thanks' | |
664 | ) { | |
665 | $startDate = $endDate = ""; | |
666 | $this->assign('selectPremium', TRUE); | |
667 | $productDAO = new CRM_Contribute_DAO_Product(); | |
668 | $productDAO->id = $selectProduct; | |
669 | $productDAO->find(TRUE); | |
670 | $this->assign('product_name', $productDAO->name); | |
671 | $this->assign('price', $productDAO->price); | |
672 | $this->assign('sku', $productDAO->sku); | |
673 | $this->assign('option', CRM_Utils_Array::value('options_' . $premiumParams['selectProduct'], $premiumParams)); | |
6a488035 | 674 | |
75637f22 | 675 | $periodType = $productDAO->period_type; |
6a488035 | 676 | |
75637f22 EM |
677 | if ($periodType) { |
678 | $fixed_period_start_day = $productDAO->fixed_period_start_day; | |
679 | $duration_unit = $productDAO->duration_unit; | |
680 | $duration_interval = $productDAO->duration_interval; | |
681 | if ($periodType == 'rolling') { | |
682 | $startDate = date('Y-m-d'); | |
683 | } | |
684 | elseif ($periodType == 'fixed') { | |
685 | if ($fixed_period_start_day) { | |
686 | $date = explode('-', date('Y-m-d')); | |
687 | $month = substr($fixed_period_start_day, 0, strlen($fixed_period_start_day) - 2); | |
688 | $day = substr($fixed_period_start_day, -2) . "<br/>"; | |
689 | $year = $date[0]; | |
690 | $startDate = $year . '-' . $month . '-' . $day; | |
691 | } | |
692 | else { | |
693 | $startDate = date('Y-m-d'); | |
694 | } | |
6a488035 | 695 | } |
6a488035 | 696 | |
75637f22 EM |
697 | $date = explode('-', $startDate); |
698 | $year = $date[0]; | |
699 | $month = $date[1]; | |
700 | $day = $date[2]; | |
6a488035 | 701 | |
75637f22 EM |
702 | switch ($duration_unit) { |
703 | case 'year': | |
704 | $year = $year + $duration_interval; | |
705 | break; | |
6a488035 | 706 | |
75637f22 EM |
707 | case 'month': |
708 | $month = $month + $duration_interval; | |
709 | break; | |
6a488035 | 710 | |
75637f22 EM |
711 | case 'day': |
712 | $day = $day + $duration_interval; | |
713 | break; | |
6a488035 | 714 | |
75637f22 EM |
715 | case 'week': |
716 | $day = $day + ($duration_interval * 7); | |
6a488035 | 717 | } |
75637f22 EM |
718 | $endDate = date('Y-m-d H:i:s', mktime($hour, $minute, $second, $month, $day, $year)); |
719 | $this->assign('start_date', $startDate); | |
720 | $this->assign('end_date', $endDate); | |
6a488035 TO |
721 | } |
722 | ||
75637f22 EM |
723 | $dao = new CRM_Contribute_DAO_Premium(); |
724 | $dao->entity_table = 'civicrm_contribution_page'; | |
725 | $dao->entity_id = $this->_id; | |
726 | $dao->find(TRUE); | |
727 | $this->assign('contact_phone', $dao->premiums_contact_phone); | |
728 | $this->assign('contact_email', $dao->premiums_contact_email); | |
729 | ||
730 | //create Premium record | |
731 | $params = array( | |
732 | 'product_id' => $premiumParams['selectProduct'], | |
733 | 'contribution_id' => $contribution->id, | |
734 | 'product_option' => CRM_Utils_Array::value('options_' . $premiumParams['selectProduct'], $premiumParams), | |
735 | 'quantity' => 1, | |
736 | 'start_date' => CRM_Utils_Date::customFormat($startDate, '%Y%m%d'), | |
737 | 'end_date' => CRM_Utils_Date::customFormat($endDate, '%Y%m%d'), | |
738 | ); | |
739 | if (!empty($premiumParams['selectProduct'])) { | |
740 | $daoPremiumsProduct = new CRM_Contribute_DAO_PremiumsProduct(); | |
741 | $daoPremiumsProduct->product_id = $premiumParams['selectProduct']; | |
742 | $daoPremiumsProduct->premiums_id = $dao->id; | |
743 | $daoPremiumsProduct->find(TRUE); | |
744 | $params['financial_type_id'] = $daoPremiumsProduct->financial_type_id; | |
6a488035 | 745 | } |
75637f22 EM |
746 | //Fixed For CRM-3901 |
747 | $daoContrProd = new CRM_Contribute_DAO_ContributionProduct(); | |
748 | $daoContrProd->contribution_id = $contribution->id; | |
749 | if ($daoContrProd->find(TRUE)) { | |
750 | $params['id'] = $daoContrProd->id; | |
6a488035 | 751 | } |
6a488035 | 752 | |
75637f22 EM |
753 | CRM_Contribute_BAO_Contribution::addPremium($params); |
754 | if ($productDAO->cost && !empty($params['financial_type_id'])) { | |
755 | $trxnParams = array( | |
756 | 'cost' => $productDAO->cost, | |
757 | 'currency' => $productDAO->currency, | |
758 | 'financial_type_id' => $params['financial_type_id'], | |
759 | 'contributionId' => $contribution->id, | |
760 | ); | |
761 | CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($trxnParams); | |
6a488035 TO |
762 | } |
763 | } | |
75637f22 EM |
764 | elseif ($selectProduct == 'no_thanks') { |
765 | //Fixed For CRM-3901 | |
766 | $daoContrProd = new CRM_Contribute_DAO_ContributionProduct(); | |
767 | $daoContrProd->contribution_id = $contribution->id; | |
768 | if ($daoContrProd->find(TRUE)) { | |
769 | $daoContrProd->delete(); | |
6a488035 TO |
770 | } |
771 | } | |
75637f22 | 772 | } |
6a488035 | 773 | |
75637f22 EM |
774 | /** |
775 | * Process the contribution. | |
776 | * | |
777 | * @param CRM_Core_Form $form | |
778 | * @param array $params | |
779 | * @param array $result | |
f6261e9d | 780 | * @param array $contributionParams |
781 | * Parameters to be passed to contribution create action. | |
3febe800 | 782 | * This differs from params in that we are currently adding params to it and 1) ensuring they are being |
783 | * passed consistently & 2) documenting them here. | |
784 | * - contact_id | |
785 | * - line_item | |
786 | * - is_test | |
787 | * - campaign_id | |
788 | * - contribution_page_id | |
789 | * - source | |
790 | * - payment_type_id | |
791 | * - thankyou_date (not all forms will set this) | |
792 | * | |
75637f22 | 793 | * @param CRM_Financial_DAO_FinancialType $financialType |
75637f22 | 794 | * @param bool $online |
3febe800 | 795 | * Is the form a front end form? If so set a bunch of unpredictable things that should be passed in from the form. |
75637f22 | 796 | * |
75637f22 EM |
797 | * @param int $billingLocationID |
798 | * ID of billing location type. | |
799 | * | |
800 | * @return \CRM_Contribute_DAO_Contribution | |
801 | * @throws \Exception | |
802 | */ | |
803 | public static function processFormContribution( | |
804 | &$form, | |
805 | $params, | |
806 | $result, | |
f6261e9d | 807 | $contributionParams, |
75637f22 | 808 | $financialType, |
75637f22 | 809 | $online, |
75637f22 EM |
810 | $billingLocationID |
811 | ) { | |
812 | $transaction = new CRM_Core_Transaction(); | |
f6261e9d | 813 | $contactID = $contributionParams['contact_id']; |
814 | ||
75637f22 | 815 | $isEmailReceipt = !empty($form->_values['is_email_receipt']); |
09108d7d | 816 | $isSeparateMembershipPayment = empty($params['separate_membership_payment']) ? FALSE : TRUE; |
817 | $pledgeID = empty($params['pledge_id']) ? NULL : $params['pledge_id']; | |
75637f22 | 818 | if (!$isSeparateMembershipPayment && !empty($form->_values['pledge_block_id']) && |
09108d7d | 819 | (!empty($params['is_pledge']) || $pledgeID)) { |
75637f22 | 820 | $isPledge = TRUE; |
6a488035 TO |
821 | } |
822 | else { | |
75637f22 | 823 | $isPledge = FALSE; |
6a488035 TO |
824 | } |
825 | ||
75637f22 EM |
826 | // add these values for the recurringContrib function ,CRM-10188 |
827 | $params['financial_type_id'] = $financialType->id; | |
6a488035 | 828 | |
3febe800 | 829 | $contributionParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $billingLocationID); |
6a488035 | 830 | |
75637f22 EM |
831 | //@todo - this is being set from the form to resolve CRM-10188 - an |
832 | // eNotice caused by it not being set @ the front end | |
833 | // however, we then get it being over-written with null for backend contributions | |
834 | // a better fix would be to set the values in the respective forms rather than require | |
835 | // a function being shared by two forms to deal with their respective values | |
836 | // moving it to the BAO & not taking the $form as a param would make sense here. | |
837 | if (!isset($params['is_email_receipt']) && $isEmailReceipt) { | |
838 | $params['is_email_receipt'] = $isEmailReceipt; | |
51e89def | 839 | } |
75637f22 EM |
840 | $recurringContributionID = self::processRecurringContribution($form, $params, $contactID, $financialType, $online); |
841 | $nonDeductibleAmount = self::getNonDeductibleAmount($params, $financialType, $online); | |
51e89def | 842 | |
75637f22 EM |
843 | $now = date('YmdHis'); |
844 | $receiptDate = CRM_Utils_Array::value('receipt_date', $params); | |
845 | if ($isEmailReceipt) { | |
846 | $receiptDate = $now; | |
6a488035 TO |
847 | } |
848 | ||
75637f22 | 849 | if (isset($params['amount'])) { |
f6261e9d | 850 | $contributionParams = array_merge(self::getContributionParams( |
f7248cb7 | 851 | $params, $financialType->id, $nonDeductibleAmount, TRUE, |
f6261e9d | 852 | $result, $receiptDate, |
853 | $recurringContributionID), $contributionParams | |
75637f22 | 854 | ); |
f6261e9d | 855 | $contribution = CRM_Contribute_BAO_Contribution::add($contributionParams); |
6a488035 | 856 | |
75637f22 EM |
857 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
858 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); | |
859 | if ($invoicing) { | |
860 | $dataArray = array(); | |
f6261e9d | 861 | // @todo - interrogate the line items passed in on the params array. |
862 | // No reason to assume line items will be set on the form. | |
75637f22 EM |
863 | foreach ($form->_lineItem as $lineItemKey => $lineItemValue) { |
864 | foreach ($lineItemValue as $key => $value) { | |
865 | if (isset($value['tax_amount']) && isset($value['tax_rate'])) { | |
866 | if (isset($dataArray[$value['tax_rate']])) { | |
867 | $dataArray[$value['tax_rate']] = $dataArray[$value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value); | |
868 | } | |
869 | else { | |
870 | $dataArray[$value['tax_rate']] = CRM_Utils_Array::value('tax_amount', $value); | |
871 | } | |
872 | } | |
6a488035 TO |
873 | } |
874 | } | |
75637f22 EM |
875 | $smarty = CRM_Core_Smarty::singleton(); |
876 | $smarty->assign('dataArray', $dataArray); | |
877 | $smarty->assign('totalTaxAmount', $params['tax_amount']); | |
878 | } | |
879 | if (is_a($contribution, 'CRM_Core_Error')) { | |
880 | $message = CRM_Core_Error::getMessages($contribution); | |
881 | CRM_Core_Error::fatal($message); | |
6a488035 TO |
882 | } |
883 | ||
75637f22 EM |
884 | // lets store it in the form variable so postProcess hook can get to this and use it |
885 | $form->_contributionID = $contribution->id; | |
886 | } | |
6a488035 | 887 | |
7a13735b | 888 | // process soft credit / pcp params first |
889 | CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($params, $form); | |
75637f22 | 890 | |
7a13735b | 891 | //CRM-13981, processing honor contact into soft-credit contribution |
892 | CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); | |
75637f22 EM |
893 | |
894 | //handle pledge stuff. | |
895 | if ($isPledge) { | |
896 | if ($pledgeID) { | |
897 | //when user doing pledge payments. | |
898 | //update the schedule when payment(s) are made | |
09108d7d | 899 | foreach ($params['pledge_amount'] as $paymentId => $dontCare) { |
75637f22 EM |
900 | $scheduledAmount = CRM_Core_DAO::getFieldValue( |
901 | 'CRM_Pledge_DAO_PledgePayment', | |
902 | $paymentId, | |
903 | 'scheduled_amount', | |
904 | 'id' | |
905 | ); | |
906 | ||
907 | $pledgePaymentParams = array( | |
908 | 'id' => $paymentId, | |
909 | 'contribution_id' => $contribution->id, | |
910 | 'status_id' => $contribution->contribution_status_id, | |
911 | 'actual_amount' => $scheduledAmount, | |
912 | ); | |
913 | ||
914 | CRM_Pledge_BAO_PledgePayment::add($pledgePaymentParams); | |
6a488035 | 915 | } |
75637f22 EM |
916 | |
917 | //update pledge status according to the new payment statuses | |
918 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID); | |
6a488035 | 919 | } |
75637f22 EM |
920 | else { |
921 | //when user creating pledge record. | |
922 | $pledgeParams = array(); | |
923 | $pledgeParams['contact_id'] = $contribution->contact_id; | |
924 | $pledgeParams['installment_amount'] = $pledgeParams['actual_amount'] = $contribution->total_amount; | |
925 | $pledgeParams['contribution_id'] = $contribution->id; | |
926 | $pledgeParams['contribution_page_id'] = $contribution->contribution_page_id; | |
927 | $pledgeParams['financial_type_id'] = $contribution->financial_type_id; | |
928 | $pledgeParams['frequency_interval'] = $params['pledge_frequency_interval']; | |
929 | $pledgeParams['installments'] = $params['pledge_installments']; | |
930 | $pledgeParams['frequency_unit'] = $params['pledge_frequency_unit']; | |
931 | if ($pledgeParams['frequency_unit'] == 'month') { | |
932 | $pledgeParams['frequency_day'] = intval(date("d")); | |
9ce8bcfc | 933 | } |
75637f22 EM |
934 | else { |
935 | $pledgeParams['frequency_day'] = 1; | |
936 | } | |
937 | $pledgeParams['create_date'] = $pledgeParams['start_date'] = $pledgeParams['scheduled_date'] = date("Ymd"); | |
938 | $pledgeParams['status_id'] = $contribution->contribution_status_id; | |
939 | $pledgeParams['max_reminders'] = $form->_values['max_reminders']; | |
940 | $pledgeParams['initial_reminder_day'] = $form->_values['initial_reminder_day']; | |
941 | $pledgeParams['additional_reminder_day'] = $form->_values['additional_reminder_day']; | |
942 | $pledgeParams['is_test'] = $contribution->is_test; | |
943 | $pledgeParams['acknowledge_date'] = date('Ymd'); | |
944 | $pledgeParams['original_installment_amount'] = $pledgeParams['installment_amount']; | |
945 | ||
946 | //inherit campaign from contirb page. | |
3febe800 | 947 | $pledgeParams['campaign_id'] = CRM_Utils_Array::value('campaign_id', $contributionParams); |
75637f22 EM |
948 | |
949 | $pledge = CRM_Pledge_BAO_Pledge::create($pledgeParams); | |
950 | ||
951 | $form->_params['pledge_id'] = $pledge->id; | |
952 | ||
953 | //send acknowledgment email. only when pledge is created | |
954 | if ($pledge->id) { | |
955 | //build params to send acknowledgment. | |
956 | $pledgeParams['id'] = $pledge->id; | |
957 | $pledgeParams['receipt_from_name'] = $form->_values['receipt_from_name']; | |
958 | $pledgeParams['receipt_from_email'] = $form->_values['receipt_from_email']; | |
959 | ||
960 | //scheduled amount will be same as installment_amount. | |
961 | $pledgeParams['scheduled_amount'] = $pledgeParams['installment_amount']; | |
962 | ||
963 | //get total pledge amount. | |
964 | $pledgeParams['total_pledge_amount'] = $pledge->amount; | |
965 | ||
966 | CRM_Pledge_BAO_Pledge::sendAcknowledgment($form, $pledgeParams); | |
fa9d0451 | 967 | } |
9ce8bcfc | 968 | } |
6a488035 | 969 | } |
6a488035 | 970 | |
75637f22 | 971 | if ($online && $contribution) { |
09108d7d | 972 | CRM_Core_BAO_CustomValueTable::postProcess($params, |
75637f22 EM |
973 | 'civicrm_contribution', |
974 | $contribution->id, | |
975 | 'Contribution' | |
976 | ); | |
977 | } | |
978 | elseif ($contribution) { | |
979 | //handle custom data. | |
980 | $params['contribution_id'] = $contribution->id; | |
981 | if (!empty($params['custom']) && | |
982 | is_array($params['custom']) && | |
983 | !is_a($contribution, 'CRM_Core_Error') | |
6a488035 | 984 | ) { |
75637f22 | 985 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contribution', $contribution->id); |
6a488035 | 986 | } |
75637f22 EM |
987 | } |
988 | // Save note | |
989 | if ($contribution && !empty($params['contribution_note'])) { | |
990 | $noteParams = array( | |
991 | 'entity_table' => 'civicrm_contribution', | |
992 | 'note' => $params['contribution_note'], | |
993 | 'entity_id' => $contribution->id, | |
994 | 'contact_id' => $contribution->contact_id, | |
995 | 'modified_date' => date('Ymd'), | |
cc789d46 | 996 | ); |
75637f22 EM |
997 | |
998 | CRM_Core_BAO_Note::add($noteParams, array()); | |
cc789d46 | 999 | } |
cc789d46 | 1000 | |
75637f22 EM |
1001 | if (isset($params['related_contact'])) { |
1002 | $contactID = $params['related_contact']; | |
cc789d46 | 1003 | } |
75637f22 EM |
1004 | elseif (isset($params['cms_contactID'])) { |
1005 | $contactID = $params['cms_contactID']; | |
6a488035 | 1006 | } |
75637f22 EM |
1007 | |
1008 | //create contribution activity w/ individual and target | |
1009 | //activity w/ organisation contact id when onbelf, CRM-4027 | |
1010 | $targetContactID = NULL; | |
1011 | if (!empty($params['hidden_onbehalf_profile'])) { | |
1012 | $targetContactID = $contribution->contact_id; | |
1013 | $contribution->contact_id = $contactID; | |
1014 | } | |
1015 | ||
1016 | // create an activity record | |
1017 | if ($contribution) { | |
1018 | CRM_Activity_BAO_Activity::addActivity($contribution, NULL, $targetContactID); | |
1019 | } | |
1020 | ||
1021 | $transaction->commit(); | |
1022 | // CRM-13074 - create the CMSUser after the transaction is completed as it | |
1023 | // is not appropriate to delete a valid contribution if a user create problem occurs | |
1024 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, | |
1025 | $contactID, | |
1026 | 'email-' . $billingLocationID | |
1027 | ); | |
1028 | return $contribution; | |
6a488035 TO |
1029 | } |
1030 | ||
1031 | /** | |
75637f22 | 1032 | * Create the recurring contribution record. |
6a488035 | 1033 | * |
75637f22 EM |
1034 | * @param CRM_Core_Form $form |
1035 | * @param array $params | |
1036 | * @param int $contactID | |
1037 | * @param string $contributionType | |
1038 | * @param bool $online | |
1039 | * | |
1040 | * @return mixed | |
6a488035 | 1041 | */ |
75637f22 EM |
1042 | public static function processRecurringContribution(&$form, &$params, $contactID, $contributionType, $online = TRUE) { |
1043 | // return if this page is not set for recurring | |
1044 | // or the user has not chosen the recurring option | |
cde484fd | 1045 | |
75637f22 EM |
1046 | //this is online case validation. |
1047 | if ((empty($form->_values['is_recur']) && $online) || empty($params['is_recur'])) { | |
1048 | return NULL; | |
1049 | } | |
6a488035 | 1050 | |
75637f22 EM |
1051 | $recurParams = array('contact_id' => $contactID); |
1052 | $recurParams['amount'] = CRM_Utils_Array::value('amount', $params); | |
1053 | $recurParams['auto_renew'] = CRM_Utils_Array::value('auto_renew', $params); | |
1054 | $recurParams['frequency_unit'] = CRM_Utils_Array::value('frequency_unit', $params); | |
1055 | $recurParams['frequency_interval'] = CRM_Utils_Array::value('frequency_interval', $params); | |
1056 | $recurParams['installments'] = CRM_Utils_Array::value('installments', $params); | |
1057 | $recurParams['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params); | |
1058 | $recurParams['currency'] = CRM_Utils_Array::value('currency', $params); | |
6a488035 | 1059 | |
75637f22 EM |
1060 | // CRM-14354: For an auto-renewing membership with an additional contribution, |
1061 | // if separate payments is not enabled, make sure only the membership fee recurs | |
1062 | if (!empty($form->_membershipBlock) | |
1063 | && $form->_membershipBlock['is_separate_payment'] === '0' | |
1064 | && isset($params['selectMembership']) | |
1065 | && $form->_values['is_allow_other_amount'] == '1' | |
1066 | // CRM-16331 | |
1067 | && !empty($form->_membershipTypeValues) | |
1068 | && !empty($form->_membershipTypeValues[$params['selectMembership']]['minimum_fee']) | |
1069 | ) { | |
1070 | $recurParams['amount'] = $form->_membershipTypeValues[$params['selectMembership']]['minimum_fee']; | |
1071 | } | |
6a488035 | 1072 | |
75637f22 EM |
1073 | $recurParams['is_test'] = 0; |
1074 | if (($form->_action & CRM_Core_Action::PREVIEW) || | |
1075 | (isset($form->_mode) && ($form->_mode == 'test')) | |
1076 | ) { | |
1077 | $recurParams['is_test'] = 1; | |
1078 | } | |
6a488035 | 1079 | |
75637f22 EM |
1080 | $recurParams['start_date'] = $recurParams['create_date'] = $recurParams['modified_date'] = date('YmdHis'); |
1081 | if (!empty($params['receive_date'])) { | |
1082 | $recurParams['start_date'] = $params['receive_date']; | |
1083 | } | |
1084 | $recurParams['invoice_id'] = CRM_Utils_Array::value('invoiceID', $params); | |
1085 | $recurParams['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); | |
1086 | $recurParams['payment_processor_id'] = CRM_Utils_Array::value('payment_processor_id', $params); | |
1087 | $recurParams['is_email_receipt'] = CRM_Utils_Array::value('is_email_receipt', $params); | |
1088 | // we need to add a unique trxn_id to avoid a unique key error | |
1089 | // in paypal IPN we reset this when paypal sends us the real trxn id, CRM-2991 | |
1090 | $recurParams['trxn_id'] = CRM_Utils_Array::value('trxn_id', $params, $params['invoiceID']); | |
1091 | $recurParams['financial_type_id'] = $contributionType->id; | |
6a488035 | 1092 | |
75637f22 EM |
1093 | if (!$online || $form->_values['is_monetary']) { |
1094 | $recurParams['payment_instrument_id'] = 1; | |
1095 | } | |
cde484fd | 1096 | |
75637f22 EM |
1097 | $campaignId = CRM_Utils_Array::value('campaign_id', $params); |
1098 | if ($online) { | |
1099 | if (!array_key_exists('campaign_id', $params)) { | |
1100 | $campaignId = CRM_Utils_Array::value('campaign_id', $form->_values); | |
6a488035 TO |
1101 | } |
1102 | } | |
75637f22 EM |
1103 | $recurParams['campaign_id'] = $campaignId; |
1104 | ||
1105 | $recurring = CRM_Contribute_BAO_ContributionRecur::add($recurParams); | |
1106 | if (is_a($recurring, 'CRM_Core_Error')) { | |
1107 | CRM_Core_Error::displaySessionError($recurring); | |
1108 | $urlString = 'civicrm/contribute/transact'; | |
1109 | $urlParams = '_qf_Main_display=true'; | |
1110 | if (get_class($form) == 'CRM_Contribute_Form_Contribution') { | |
1111 | $urlString = 'civicrm/contact/view/contribution'; | |
1112 | $urlParams = "action=add&cid={$form->_contactID}"; | |
1113 | if ($form->_mode) { | |
1114 | $urlParams .= "&mode={$form->_mode}"; | |
1115 | } | |
6a488035 | 1116 | } |
75637f22 | 1117 | CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams)); |
6a488035 | 1118 | } |
75637f22 EM |
1119 | |
1120 | return $recurring->id; | |
6a488035 TO |
1121 | } |
1122 | ||
1123 | /** | |
75637f22 | 1124 | * Add on behalf of organization and it's location. |
cd6994fc | 1125 | * |
75637f22 EM |
1126 | * This situation occurs when on behalf of is enabled for the contribution page and the person |
1127 | * signing up does so on behalf of an organization. | |
a1a94e61 | 1128 | * |
75637f22 EM |
1129 | * @param array $behalfOrganization |
1130 | * array of organization info. | |
1131 | * @param int $contactID | |
1132 | * individual contact id. One. | |
1133 | * who is doing the process of signup / contribution. | |
9fc4e1d9 | 1134 | * |
75637f22 EM |
1135 | * @param array $values |
1136 | * form values array. | |
1137 | * @param array $params | |
1138 | * @param array $fields | |
1139 | * Array of fields from the onbehalf profile relevant to the organization. | |
6a488035 | 1140 | */ |
75637f22 | 1141 | public static function processOnBehalfOrganization(&$behalfOrganization, &$contactID, &$values, &$params, $fields = NULL) { |
33260076 | 1142 | $isNotCurrentEmployer = FALSE; |
75637f22 EM |
1143 | $dupeIDs = array(); |
1144 | $orgID = NULL; | |
6cc679d2 | 1145 | if (!empty($behalfOrganization['organization_id'])) { |
75637f22 EM |
1146 | $orgID = $behalfOrganization['organization_id']; |
1147 | unset($behalfOrganization['organization_id']); | |
33260076 | 1148 | } |
1149 | // create employer relationship with $contactID only when new organization is there | |
1150 | // else retain the existing relationship | |
1151 | else { | |
1152 | // get the Employee relationship type id | |
1153 | $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b'); | |
b9dc56f5 | 1154 | |
33260076 | 1155 | // keep relationship params ready |
1156 | $relParams['relationship_type_id'] = $relTypeId . '_a_b'; | |
1157 | $relParams['is_permission_a_b'] = 1; | |
1158 | $relParams['is_active'] = 1; | |
1159 | $isNotCurrentEmployer = TRUE; | |
bb06e9ed | 1160 | } |
6a488035 | 1161 | |
75637f22 EM |
1162 | // formalities for creating / editing organization. |
1163 | $behalfOrganization['contact_type'] = 'Organization'; | |
c8bde7ea | 1164 | |
75637f22 EM |
1165 | if (!$orgID) { |
1166 | // check if matching organization contact exists | |
1167 | $dedupeParams = CRM_Dedupe_Finder::formatParams($behalfOrganization, 'Organization'); | |
1168 | $dedupeParams['check_permission'] = FALSE; | |
1169 | $dupeIDs = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Organization', 'Unsupervised'); | |
6a488035 | 1170 | |
75637f22 EM |
1171 | // CRM-6243 says to pick the first org even if more than one match |
1172 | if (count($dupeIDs) >= 1) { | |
1173 | $behalfOrganization['contact_id'] = $orgID = $dupeIDs[0]; | |
1174 | // don't allow name edit | |
1175 | unset($behalfOrganization['organization_name']); | |
6a488035 TO |
1176 | } |
1177 | } | |
1178 | else { | |
75637f22 EM |
1179 | // if found permissioned related organization, allow location edit |
1180 | $behalfOrganization['contact_id'] = $orgID; | |
1181 | // don't allow name edit | |
1182 | unset($behalfOrganization['organization_name']); | |
6a488035 TO |
1183 | } |
1184 | ||
75637f22 EM |
1185 | // handling for image url |
1186 | if (!empty($behalfOrganization['image_URL'])) { | |
1187 | CRM_Contact_BAO_Contact::processImageParams($behalfOrganization); | |
6a488035 TO |
1188 | } |
1189 | ||
75637f22 EM |
1190 | // create organization, add location |
1191 | $orgID = CRM_Contact_BAO_Contact::createProfileContact($behalfOrganization, $fields, $orgID, | |
1192 | NULL, NULL, 'Organization' | |
1193 | ); | |
1194 | // create relationship | |
33260076 | 1195 | if ($isNotCurrentEmployer) { |
1196 | $relParams['contact_check'][$orgID] = 1; | |
1197 | $cid = array('contact' => $contactID); | |
1198 | CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid); | |
1199 | } | |
1f1740fe | 1200 | |
75637f22 EM |
1201 | // if multiple match - send a duplicate alert |
1202 | if ($dupeIDs && (count($dupeIDs) > 1)) { | |
1203 | $values['onbehalf_dupe_alert'] = 1; | |
1204 | // required for IPN | |
1205 | $params['onbehalf_dupe_alert'] = 1; | |
6a488035 | 1206 | } |
cde484fd | 1207 | |
75637f22 EM |
1208 | // make sure organization-contact-id is considered for recording |
1209 | // contribution/membership etc.. | |
1210 | if ($contactID != $orgID) { | |
1211 | // take a note of contact-id, so we can send the | |
1212 | // receipt to individual contact as well. | |
6a488035 | 1213 | |
75637f22 EM |
1214 | // required for mailing/template display ..etc |
1215 | $values['related_contact'] = $contactID; | |
6a488035 | 1216 | |
75637f22 EM |
1217 | //make this employee of relationship as current |
1218 | //employer / employee relationship, CRM-3532 | |
33260076 | 1219 | if ($isNotCurrentEmployer && |
75637f22 EM |
1220 | ($orgID != CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id')) |
1221 | ) { | |
33260076 | 1222 | $isNotCurrentEmployer = FALSE; |
6a488035 | 1223 | } |
6a488035 | 1224 | |
33260076 | 1225 | if (!$isNotCurrentEmployer && $orgID) { |
75637f22 EM |
1226 | //build current employer params |
1227 | $currentEmpParams[$contactID] = $orgID; | |
1228 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); | |
6a488035 | 1229 | } |
6a488035 | 1230 | |
75637f22 EM |
1231 | // contribution / signup will be done using this |
1232 | // organization id. | |
1233 | $contactID = $orgID; | |
6a488035 | 1234 | } |
75637f22 EM |
1235 | } |
1236 | ||
6a488035 | 1237 | /** |
75637f22 | 1238 | * Function used to send notification mail to pcp owner. |
6a488035 | 1239 | * |
75637f22 | 1240 | * This is used by contribution and also event PCPs. |
03110609 | 1241 | * |
75637f22 EM |
1242 | * @param object $contribution |
1243 | * @param object $contributionSoft | |
1244 | * Contribution object. | |
12f92dbd | 1245 | */ |
12f92dbd | 1246 | public static function pcpNotifyOwner($contribution, $contributionSoft) { |
5fe87df6 N |
1247 | $params = array('id' => $contributionSoft->pcp_id); |
1248 | CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo); | |
1249 | $ownerNotifyID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCPBlock', $pcpInfo['pcp_block_id'], 'owner_notify_id'); | |
1250 | ||
1251 | if ($ownerNotifyID != CRM_Core_OptionGroup::getValue('pcp_owner_notify', 'no_notifications', 'name') && | |
1252 | (($ownerNotifyID == CRM_Core_OptionGroup::getValue('pcp_owner_notify', 'owner_chooses', 'name') && | |
1253 | CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $contributionSoft->pcp_id, 'is_notify')) || | |
1254 | $ownerNotifyID == CRM_Core_OptionGroup::getValue('pcp_owner_notify', 'all_owners', 'name'))) { | |
1255 | $pcpInfoURL = CRM_Utils_System::url('civicrm/pcp/info', | |
1256 | "reset=1&id={$contributionSoft->pcp_id}", | |
1257 | TRUE, NULL, FALSE, TRUE | |
1258 | ); | |
12f92dbd | 1259 | // set email in the template here |
5fe87df6 | 1260 | |
b576d770 | 1261 | if (CRM_Core_BAO_LocationType::getBilling()) { |
1262 | list($donorName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contribution->contact_id, | |
1263 | FALSE, CRM_Core_BAO_LocationType::getBilling()); | |
5fe87df6 N |
1264 | } |
1265 | // get primary location email if no email exist( for billing location). | |
1266 | if (!$email) { | |
1267 | list($donorName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contribution->contact_id); | |
1268 | } | |
1269 | list($ownerName, $ownerEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contributionSoft->contact_id); | |
12f92dbd | 1270 | $tplParams = array( |
5fe87df6 | 1271 | 'page_title' => $pcpInfo['title'], |
12f92dbd N |
1272 | 'receive_date' => $contribution->receive_date, |
1273 | 'total_amount' => $contributionSoft->amount, | |
1274 | 'donors_display_name' => $donorName, | |
5fe87df6 N |
1275 | 'donors_email' => $email, |
1276 | 'pcpInfoURL' => $pcpInfoURL, | |
12f92dbd | 1277 | 'is_honor_roll_enabled' => $contributionSoft->pcp_display_in_roll, |
dd55005c | 1278 | 'currency' => $contributionSoft->currency, |
12f92dbd N |
1279 | ); |
1280 | $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); | |
1281 | $sendTemplateParams = array( | |
1282 | 'groupName' => 'msg_tpl_workflow_contribution', | |
1283 | 'valueName' => 'pcp_owner_notify', | |
1284 | 'contactId' => $contributionSoft->contact_id, | |
5fe87df6 N |
1285 | 'toEmail' => $ownerEmail, |
1286 | 'toName' => $ownerName, | |
12f92dbd N |
1287 | 'from' => "$domainValues[0] <$domainValues[1]>", |
1288 | 'tplParams' => $tplParams, | |
1289 | 'PDFFilename' => 'receipt.pdf', | |
1290 | ); | |
1291 | CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); | |
6a488035 TO |
1292 | } |
1293 | } | |
1294 | ||
1295 | /** | |
5e4f7f74 EM |
1296 | * Function used to se pcp related defaults / params. |
1297 | * | |
1298 | * This is used by contribution and also event PCPs | |
6a488035 | 1299 | * |
014c4014 TO |
1300 | * @param CRM_Core_Form $page |
1301 | * Form object. | |
1302 | * @param array $params | |
6a488035 | 1303 | * |
cd6994fc | 1304 | * @return array |
6a488035 | 1305 | */ |
00be9182 | 1306 | public static function processPcp(&$page, $params) { |
547206bc | 1307 | $params['pcp_made_through_id'] = $page->_pcpId; |
6a488035 | 1308 | $page->assign('pcpBlock', TRUE); |
8cc574cf | 1309 | if (!empty($params['pcp_display_in_roll']) && empty($params['pcp_roll_nickname'])) { |
6a488035 TO |
1310 | $params['pcp_roll_nickname'] = ts('Anonymous'); |
1311 | $params['pcp_is_anonymous'] = 1; | |
1312 | } | |
1313 | else { | |
1314 | $params['pcp_is_anonymous'] = 0; | |
1315 | } | |
1316 | foreach (array( | |
547206bc | 1317 | 'pcp_display_in_roll', |
1318 | 'pcp_is_anonymous', | |
1319 | 'pcp_roll_nickname', | |
21dfd5f5 | 1320 | 'pcp_personal_note', |
547206bc | 1321 | ) as $val) { |
a7488080 | 1322 | if (!empty($params[$val])) { |
6a488035 TO |
1323 | $page->assign($val, $params[$val]); |
1324 | } | |
1325 | } | |
1326 | ||
1327 | return $params; | |
1328 | } | |
705b4205 EM |
1329 | |
1330 | /** | |
5e4f7f74 EM |
1331 | * Process membership. |
1332 | * | |
5624f515 | 1333 | * @param array $membershipParams |
014c4014 | 1334 | * @param int $contactID |
5624f515 EM |
1335 | * @param array $customFieldsFormatted |
1336 | * @param array $fieldTypes | |
1337 | * @param array $premiumParams | |
014c4014 TO |
1338 | * @param array $membershipLineItems |
1339 | * Line items specifically relating to memberships. | |
ab8fe4ef | 1340 | * @param bool $isPayLater |
705b4205 | 1341 | */ |
4b6c8c1e | 1342 | protected function processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, |
1343 | $membershipLineItems, $isPayLater) { | |
8bc79dfc | 1344 | |
4b6c8c1e | 1345 | $membershipTypeIDs = (array) $membershipParams['selectMembership']; |
1346 | $membershipTypes = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIDs); | |
1347 | $membershipType = empty($membershipTypes) ? array() : reset($membershipTypes); | |
1348 | $isPending = $this->getIsPending(); | |
5624f515 | 1349 | |
4b6c8c1e | 1350 | $this->assign('membership_name', CRM_Utils_Array::value('name', $membershipType)); |
7c113627 | 1351 | |
4b6c8c1e | 1352 | $isPaidMembership = FALSE; |
1353 | if ($this->_amount >= 0.0 && isset($membershipParams['amount'])) { | |
1354 | //amount must be greater than zero for | |
1355 | //adding contribution record to contribution table. | |
1356 | //this condition arises when separate membership payment is | |
1357 | //enabled and contribution amount is not selected. fix for CRM-3010 | |
1358 | $isPaidMembership = TRUE; | |
1359 | } | |
1360 | $isProcessSeparateMembershipTransaction = $this->isSeparateMembershipTransaction($this->_id, $this->_values['amount_block_is_active']); | |
7c113627 | 1361 | |
4b6c8c1e | 1362 | if ($this->_values['amount_block_is_active']) { |
1363 | $financialTypeID = $this->_values['financial_type_id']; | |
705b4205 | 1364 | } |
4b6c8c1e | 1365 | else { |
1366 | $financialTypeID = CRM_Utils_Array::value('financial_type_id', $membershipType, CRM_Utils_Array::value('financial_type_id', $membershipParams)); | |
705b4205 | 1367 | } |
4b6c8c1e | 1368 | |
1369 | if (CRM_Utils_Array::value('membership_source', $this->_params)) { | |
1370 | $membershipParams['contribution_source'] = $this->_params['membership_source']; | |
1371 | } | |
1372 | ||
1373 | $this->postProcessMembership($membershipParams, $contactID, | |
1374 | $this, $premiumParams, $customFieldsFormatted, $fieldTypes, $membershipType, $membershipTypeIDs, $isPaidMembership, $this->_membershipId, $isProcessSeparateMembershipTransaction, $financialTypeID, | |
1375 | $membershipLineItems, $isPayLater, $isPending); | |
1376 | ||
1377 | $this->assign('membership_assign', TRUE); | |
1378 | $this->set('membershipTypeID', $membershipParams['selectMembership']); | |
705b4205 | 1379 | } |
fb49fa48 | 1380 | |
8bc79dfc | 1381 | /** |
668ddfc2 EM |
1382 | * Process the Memberships. |
1383 | * | |
1384 | * @param array $membershipParams | |
1385 | * Array of membership fields. | |
1386 | * @param int $contactID | |
1387 | * Contact id. | |
1388 | * @param CRM_Contribute_Form_Contribution_Confirm $form | |
1389 | * Confirmation form object. | |
1390 | * | |
1391 | * @param array $premiumParams | |
1392 | * @param null $customFieldsFormatted | |
1393 | * @param null $includeFieldTypes | |
1394 | * | |
1395 | * @param array $membershipDetails | |
1396 | * | |
1397 | * @param array $membershipTypeIDs | |
1398 | * | |
1399 | * @param bool $isPaidMembership | |
1400 | * @param array $membershipID | |
1401 | * | |
1402 | * @param bool $isProcessSeparateMembershipTransaction | |
1403 | * | |
1404 | * @param int $financialTypeID | |
1405 | * @param array $membershipLineItems | |
1406 | * Line items specific to membership payment that is separate to contribution. | |
1407 | * @param bool $isPayLater | |
1408 | * @param bool $isPending | |
1409 | * | |
1410 | * @throws \CRM_Core_Exception | |
1411 | */ | |
1412 | protected function postProcessMembership( | |
1413 | $membershipParams, $contactID, &$form, $premiumParams, | |
1414 | $customFieldsFormatted = NULL, $includeFieldTypes = NULL, $membershipDetails, $membershipTypeIDs, $isPaidMembership, $membershipID, | |
1415 | $isProcessSeparateMembershipTransaction, $financialTypeID, $membershipLineItems, $isPayLater, $isPending) { | |
82583880 | 1416 | $membershipContribution = NULL; |
668ddfc2 | 1417 | $isTest = CRM_Utils_Array::value('is_test', $membershipParams, FALSE); |
63137f3a | 1418 | $errors = $createdMemberships = $paymentResults = array(); |
668ddfc2 EM |
1419 | |
1420 | if ($isPaidMembership) { | |
1421 | if ($isProcessSeparateMembershipTransaction) { | |
1422 | // If we have 2 transactions only one can use the invoice id. | |
1423 | $membershipParams['invoiceID'] .= '-2'; | |
1424 | } | |
1425 | ||
1426 | $paymentResult = CRM_Contribute_BAO_Contribution_Utils::processConfirm($form, $membershipParams, | |
5fb28746 | 1427 | $contactID, |
668ddfc2 EM |
1428 | $financialTypeID, |
1429 | 'membership', | |
46763692 | 1430 | $isTest |
668ddfc2 | 1431 | ); |
668ddfc2 | 1432 | |
82583880 | 1433 | if (!empty($paymentResult['contribution'])) { |
5fb28746 | 1434 | $this->postProcessPremium($premiumParams, $paymentResult['contribution']); |
668ddfc2 EM |
1435 | //note that this will be over-written if we are using a separate membership transaction. Otherwise there is only one |
1436 | $membershipContribution = $paymentResult['contribution']; | |
1437 | // Save the contribution ID so that I can be used in email receipts | |
1438 | // For example, if you need to generate a tax receipt for the donation only. | |
1439 | $form->_values['contribution_other_id'] = $membershipContribution->id; | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | if ($isProcessSeparateMembershipTransaction) { | |
1444 | try { | |
1445 | $form->_lineItem = $membershipLineItems; | |
1446 | if (empty($form->_params['auto_renew']) && !empty($membershipParams['is_recur'])) { | |
1447 | unset($membershipParams['is_recur']); | |
1448 | } | |
63137f3a | 1449 | list($membershipContribution, $secondPaymentResult) = $this->processSecondaryFinancialTransaction($contactID, $form, $membershipParams, |
668ddfc2 | 1450 | $isTest, $membershipLineItems, CRM_Utils_Array::value('minimum_fee', $membershipDetails, 0), CRM_Utils_Array::value('financial_type_id', $membershipDetails)); |
63137f3a | 1451 | $paymentResults[] = array('contribution_id' => $membershipContribution->id, 'result' => $secondPaymentResult); |
668ddfc2 EM |
1452 | } |
1453 | catch (CRM_Core_Exception $e) { | |
1454 | $errors[2] = $e->getMessage(); | |
1455 | $membershipContribution = NULL; | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | $membership = NULL; | |
1460 | if (!empty($membershipContribution) && !is_a($membershipContribution, 'CRM_Core_Error')) { | |
1461 | $membershipContributionID = $membershipContribution->id; | |
1462 | } | |
1463 | ||
1464 | //@todo - why is this nested so deep? it seems like it could be just set on the calling function on the form layer | |
1465 | if (isset($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) { | |
1466 | $form->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id']; | |
1467 | } | |
1468 | //@todo it should no longer be possible for it to get to this point & membership to not be an array | |
1469 | if (is_array($membershipTypeIDs) && !empty($membershipContributionID)) { | |
1470 | $typesTerms = CRM_Utils_Array::value('types_terms', $membershipParams, array()); | |
1471 | foreach ($membershipTypeIDs as $memType) { | |
1472 | $numTerms = CRM_Utils_Array::value($memType, $typesTerms, 1); | |
1473 | if (!empty($membershipContribution)) { | |
1474 | $pendingStatus = CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name'); | |
1475 | $pending = ($membershipContribution->contribution_status_id == $pendingStatus) ? TRUE : FALSE; | |
1476 | } | |
1477 | else { | |
1478 | $pending = $isPending; | |
1479 | } | |
1480 | $contributionRecurID = isset($form->_params['contributionRecurID']) ? $form->_params['contributionRecurID'] : NULL; | |
1481 | ||
1482 | $membershipSource = NULL; | |
1483 | if (!empty($form->_params['membership_source'])) { | |
1484 | $membershipSource = $form->_params['membership_source']; | |
1485 | } | |
1486 | elseif (isset($form->_values['title']) && !empty($form->_values['title'])) { | |
1487 | $membershipSource = ts('Online Contribution:') . ' ' . $form->_values['title']; | |
1488 | } | |
1489 | $isPayLater = NULL; | |
1490 | if (isset($form->_params)) { | |
1491 | $isPayLater = CRM_Utils_Array::value('is_pay_later', $form->_params); | |
1492 | } | |
1493 | $campaignId = NULL; | |
1494 | if (isset($form->_values) && is_array($form->_values) && !empty($form->_values)) { | |
1495 | $campaignId = CRM_Utils_Array::value('campaign_id', $form->_params); | |
1496 | if (!array_key_exists('campaign_id', $form->_params)) { | |
1497 | $campaignId = CRM_Utils_Array::value('campaign_id', $form->_values); | |
1498 | } | |
1499 | } | |
1500 | ||
1501 | list($membership, $renewalMode, $dates) = CRM_Member_BAO_Membership::renewMembership( | |
1502 | $contactID, $memType, $isTest, | |
1503 | date('YmdHis'), CRM_Utils_Array::value('cms_contactID', $membershipParams), | |
1504 | $customFieldsFormatted, | |
1505 | $numTerms, $membershipID, $pending, | |
1506 | $contributionRecurID, $membershipSource, $isPayLater, $campaignId | |
1507 | ); | |
1508 | $form->set('renewal_mode', $renewalMode); | |
1509 | if (!empty($dates)) { | |
1510 | $form->assign('mem_start_date', | |
1511 | CRM_Utils_Date::customFormat($dates['start_date'], '%Y%m%d') | |
1512 | ); | |
1513 | $form->assign('mem_end_date', | |
1514 | CRM_Utils_Date::customFormat($dates['end_date'], '%Y%m%d') | |
1515 | ); | |
1516 | } | |
1517 | ||
1518 | if (!empty($membershipContribution)) { | |
1519 | // update recurring id for membership record | |
1520 | CRM_Member_BAO_Membership::updateRecurMembership($membership, $membershipContribution); | |
1521 | CRM_Member_BAO_Membership::linkMembershipPayment($membership, $membershipContribution); | |
1522 | } | |
1523 | } | |
1524 | if ($form->_priceSetId && !empty($form->_useForMember) && !empty($form->_lineItem)) { | |
1525 | foreach ($form->_lineItem[$form->_priceSetId] as & $priceFieldOp) { | |
1526 | if (!empty($priceFieldOp['membership_type_id']) && | |
1527 | isset($createdMemberships[$priceFieldOp['membership_type_id']]) | |
1528 | ) { | |
1529 | $membershipOb = $createdMemberships[$priceFieldOp['membership_type_id']]; | |
1530 | $priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::customFormat($membershipOb->start_date, '%B %E%f, %Y') : '-'; | |
1531 | $priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::customFormat($membershipOb->end_date, '%B %E%f, %Y') : '-'; | |
1532 | } | |
1533 | else { | |
1534 | $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A'; | |
1535 | } | |
1536 | } | |
1537 | $form->_values['lineItem'] = $form->_lineItem; | |
1538 | $form->assign('lineItem', $form->_lineItem); | |
1539 | } | |
1540 | } | |
1541 | ||
1542 | if (!empty($errors)) { | |
1543 | $message = $this->compileErrorMessage($errors); | |
1544 | throw new CRM_Core_Exception($message); | |
1545 | } | |
1546 | $form->_params['createdMembershipIDs'] = array(); | |
1547 | ||
1548 | // CRM-7851 - Moved after processing Payment Errors | |
1549 | //@todo - the reasoning for this being here seems a little outdated | |
1550 | foreach ($createdMemberships as $createdMembership) { | |
1551 | CRM_Core_BAO_CustomValueTable::postProcess( | |
1552 | $form->_params, | |
1553 | 'civicrm_membership', | |
1554 | $createdMembership->id, | |
1555 | 'Membership' | |
1556 | ); | |
1557 | $form->_params['createdMembershipIDs'][] = $createdMembership->id; | |
1558 | } | |
1559 | if (count($createdMemberships) == 1) { | |
1560 | //presumably this is only relevant for exactly 1 membership | |
1561 | $form->_params['membershipID'] = $createdMembership->id; | |
1562 | } | |
1563 | ||
1564 | //CRM-15232: Check if membership is created and on the basis of it use | |
1565 | //membership receipt template to send payment receipt | |
1566 | if (count($createdMemberships)) { | |
1567 | $form->_values['isMembership'] = TRUE; | |
1568 | } | |
82583880 EM |
1569 | if (isset($membershipContributionID)) { |
1570 | $form->_values['contribution_id'] = $membershipContributionID; | |
1571 | } | |
a506739a | 1572 | |
1573 | if ($form->_paymentProcessor) { | |
1574 | // the is_monetary concept probably should be deprecated as it can be calculated from | |
1575 | // the existence of 'amount' & seems fragile. | |
668ddfc2 EM |
1576 | if ($form->_values['is_monetary'] && $form->_amount > 0.0 && !$form->_params['is_pay_later']) { |
1577 | // call postProcess hook before leaving | |
1578 | $form->postProcessHook(); | |
668ddfc2 | 1579 | } |
a506739a | 1580 | |
82583880 | 1581 | $payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor); |
0739d6cf | 1582 | $paymentActionResult = $payment->doPayment($form->_params, 'contribute'); |
63137f3a | 1583 | $paymentResults[] = array('contribution_id' => $paymentResult['contribution']->id, 'result' => $paymentActionResult); |
668ddfc2 EM |
1584 | // Do not send an email if Recurring transaction is done via Direct Mode |
1585 | // Email will we sent when the IPN is received. | |
63137f3a | 1586 | foreach ($paymentResults as $result) { |
1587 | $this->completeTransaction($result['result'], $result['contribution_id']); | |
1588 | } | |
668ddfc2 EM |
1589 | return; |
1590 | } | |
1591 | ||
1592 | //finally send an email receipt | |
1593 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, | |
1594 | $form->_values, | |
1595 | $isTest, FALSE, | |
1596 | $includeFieldTypes | |
1597 | ); | |
1598 | } | |
1599 | ||
1600 | /** | |
1601 | * Turn array of errors into message string. | |
1602 | * | |
1603 | * @param array $errors | |
1604 | * | |
1605 | * @return string | |
1606 | */ | |
1607 | protected function compileErrorMessage($errors) { | |
1608 | foreach ($errors as $error) { | |
1609 | if (is_string($error)) { | |
1610 | $message[] = $error; | |
1611 | } | |
1612 | } | |
1613 | return ts('Payment Processor Error message') . ': ' . implode('<br/>', $message); | |
1614 | } | |
1615 | ||
1616 | /** | |
1617 | * Where a second separate financial transaction is supported we will process it here. | |
1618 | * | |
1619 | * @param int $contactID | |
1620 | * @param CRM_Contribute_Form_Contribution_Confirm $form | |
1621 | * @param array $tempParams | |
1622 | * @param bool $isTest | |
1623 | * @param array $lineItems | |
1624 | * @param $minimumFee | |
1625 | * @param int $financialTypeID | |
1626 | * | |
1627 | * @throws CRM_Core_Exception | |
1628 | * @throws Exception | |
1629 | * @return CRM_Contribute_BAO_Contribution | |
1630 | */ | |
1631 | protected function processSecondaryFinancialTransaction($contactID, &$form, $tempParams, $isTest, $lineItems, $minimumFee, | |
1632 | $financialTypeID) { | |
1633 | $financialType = new CRM_Financial_DAO_FinancialType(); | |
1634 | $financialType->id = $financialTypeID; | |
1635 | $financialType->find(TRUE); | |
1636 | $tempParams['amount'] = $minimumFee; | |
1637 | $tempParams['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1638 | ||
668ddfc2 EM |
1639 | //assign receive date when separate membership payment |
1640 | //and contribution amount not selected. | |
1641 | if ($form->_amount == 0) { | |
1642 | $now = date('YmdHis'); | |
1643 | $form->_params['receive_date'] = $now; | |
1644 | $receiveDate = CRM_Utils_Date::mysqlToIso($now); | |
1645 | $form->set('params', $form->_params); | |
1646 | $form->assign('receive_date', $receiveDate); | |
1647 | } | |
1648 | ||
668ddfc2 | 1649 | $form->set('membership_amount', $minimumFee); |
668ddfc2 EM |
1650 | $form->assign('membership_amount', $minimumFee); |
1651 | ||
1652 | // we don't need to create the user twice, so lets disable cms_create_account | |
1653 | // irrespective of the value, CRM-2888 | |
1654 | $tempParams['cms_create_account'] = 0; | |
1655 | ||
668ddfc2 EM |
1656 | //set this variable as we are not creating pledge for |
1657 | //separate membership payment contribution. | |
1658 | //so for differentiating membership contribution from | |
1659 | //main contribution. | |
1660 | $form->_params['separate_membership_payment'] = 1; | |
3febe800 | 1661 | $contributionParams = array( |
1662 | 'contact_id' => $contactID, | |
1663 | 'line_item' => $lineItems, | |
1664 | 'is_test' => $isTest, | |
1665 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $tempParams, CRM_Utils_Array::value('campaign_id', | |
1666 | $form->_values)), | |
1667 | 'contribution_page_id' => $form->_id, | |
1668 | 'source' => CRM_Utils_Array::value('source', $tempParams, CRM_Utils_Array::value('description', $tempParams)), | |
1669 | ); | |
1670 | $isMonetary = !empty($form->_values['is_monetary']); | |
1671 | if ($isMonetary) { | |
1672 | if (empty($paymentParams['is_pay_later'])) { | |
f3a63d1e | 1673 | $contributionParams['payment_instrument_id'] = $form->_paymentProcessor['payment_instrument_id']; |
3febe800 | 1674 | } |
1675 | } | |
668ddfc2 EM |
1676 | $membershipContribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($form, |
1677 | $tempParams, | |
0193ebdb | 1678 | $tempParams, |
3febe800 | 1679 | $contributionParams, |
668ddfc2 | 1680 | $financialType, |
668ddfc2 | 1681 | TRUE, |
668ddfc2 EM |
1682 | $form->_bltID |
1683 | ); | |
0193ebdb | 1684 | |
1685 | if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) { | |
1686 | // At the moment our tests are calling this form in a way that leaves 'object' empty. For | |
1687 | // now we compensate here. | |
1688 | if (empty($form->_paymentProcessor['object'])) { | |
1689 | $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); | |
1690 | } | |
1691 | else { | |
1692 | $payment = $form->_paymentProcessor['object']; | |
1693 | } | |
1694 | $result = $payment->doPayment($tempParams, 'contribute'); | |
1695 | $form->set('membership_trx_id', $result['trxn_id']); | |
1696 | $form->assign('membership_trx_id', $result['trxn_id']); | |
0193ebdb | 1697 | } |
1698 | ||
63137f3a | 1699 | return array($membershipContribution, $result); |
668ddfc2 EM |
1700 | } |
1701 | ||
1702 | /** | |
8bc79dfc EM |
1703 | * Is the payment a pending payment. |
1704 | * | |
1705 | * We are moving towards always creating as pending and updating at the end (based on payment), so this should be | |
1706 | * an interim refactoring. It was shared with another unrelated form & some parameters may not apply to this form. | |
1707 | * | |
8bc79dfc EM |
1708 | * @return bool |
1709 | */ | |
1710 | protected function getIsPending() { | |
0f2b049e | 1711 | // The concept of contributeMode is deprecated. |
1712 | // the is_monetary concept probably should be too as it can be calculated from | |
1713 | // the existence of 'amount' & seems fragile. | |
8bc79dfc EM |
1714 | if (((isset($this->_contributeMode)) || !empty |
1715 | ($this->_params['is_pay_later']) | |
1716 | ) && | |
1717 | (($this->_values['is_monetary'] && $this->_amount > 0.0)) | |
1718 | ) { | |
1719 | return TRUE; | |
1720 | } | |
1721 | return FALSE; | |
1722 | } | |
1723 | ||
fb49fa48 | 1724 | /** |
5e4f7f74 | 1725 | * Are we going to do 2 financial transactions. |
fb49fa48 | 1726 | * |
5e4f7f74 EM |
1727 | * Ie the membership block supports a separate transactions AND the contribution form has been configured for a |
1728 | * contribution | |
1729 | * transaction AND a membership transaction AND the payment processor supports double financial transactions (ie. NOT doTransferPayment style) | |
fb49fa48 | 1730 | * |
014c4014 | 1731 | * @param int $formID |
fb49fa48 EM |
1732 | * @param bool $amountBlockActiveOnForm |
1733 | * | |
1734 | * @return bool | |
1735 | */ | |
1736 | public function isSeparateMembershipTransaction($formID, $amountBlockActiveOnForm) { | |
1737 | $memBlockDetails = CRM_Member_BAO_Membership::getMembershipBlock($formID); | |
1738 | if (!empty($memBlockDetails['is_separate_payment']) && $amountBlockActiveOnForm) { | |
1739 | return TRUE; | |
1740 | } | |
1741 | return FALSE; | |
1742 | } | |
df6c4f28 EM |
1743 | |
1744 | /** | |
5e4f7f74 EM |
1745 | * This function sets the fields. |
1746 | * | |
df6c4f28 EM |
1747 | * - $this->_params['amount_level'] |
1748 | * - $this->_params['selectMembership'] | |
1749 | * And under certain circumstances sets | |
1750 | * $this->_params['amount'] = null; | |
1751 | * | |
100fef9d | 1752 | * @param int $priceSetID |
df6c4f28 EM |
1753 | */ |
1754 | public function setFormAmountFields($priceSetID) { | |
1755 | $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config'); | |
1756 | $priceField = new CRM_Price_DAO_PriceField(); | |
1757 | $priceField->price_set_id = $priceSetID; | |
1758 | $priceField->orderBy('weight'); | |
1759 | $priceField->find(); | |
06b4ee3e | 1760 | $paramWeDoNotUnderstand = NULL; |
df6c4f28 EM |
1761 | |
1762 | while ($priceField->fetch()) { | |
df6c4f28 EM |
1763 | if ($priceField->name == "contribution_amount") { |
1764 | $paramWeDoNotUnderstand = $priceField->id; | |
1765 | } | |
1766 | if ($isQuickConfig && !empty($this->_params["price_{$priceField->id}"])) { | |
1767 | if ($this->_values['fee'][$priceField->id]['html_type'] != 'Text') { | |
c039f658 | 1768 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
1769 | // function to get correct amount level consistently. Remove setting of the amount level in | |
1770 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest | |
1771 | // to cover all variants. | |
df6c4f28 EM |
1772 | $this->_params['amount_level'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', |
1773 | $this->_params["price_{$priceField->id}"], 'label'); | |
1774 | } | |
1775 | if ($priceField->name == "membership_amount") { | |
1776 | $this->_params['selectMembership'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', | |
1777 | $this->_params["price_{$priceField->id}"], 'membership_type_id'); | |
1778 | } | |
5e4f7f74 EM |
1779 | } |
1780 | // If separate payment we set contribution amount to be null, so that it will not show contribution amount same | |
1781 | // as membership amount. | |
df6c4f28 EM |
1782 | // @todo - this needs more documentation - it appears the setting to null is tied up with separate membership payments |
1783 | // but the circumstances are very confusing. Many of these conditions are repeated in the next conditional | |
1784 | // so we should merge them together | |
1785 | // the quick config seems like a red-herring - if this is about a separate membership payment then there | |
f64a217a | 1786 | // are 2 types of line items - membership ones & non-membership ones - regardless of whether quick config is set |
df6c4f28 EM |
1787 | elseif ( |
1788 | CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) | |
1789 | && !empty($this->_values['fee'][$priceField->id]) | |
1790 | && ($this->_values['fee'][$priceField->id]['name'] == "other_amount") | |
1791 | && CRM_Utils_Array::value("price_{$paramWeDoNotUnderstand}", $this->_params) < 1 | |
1792 | && empty($this->_params["price_{$priceField->id}"]) | |
1793 | ) { | |
a13f3d8c | 1794 | $this->_params['amount'] = NULL; |
df6c4f28 EM |
1795 | } |
1796 | ||
1797 | // Fix for CRM-14375 - If we are using separate payments and "no | |
1798 | // thank you" is selected for the additional contribution, set | |
1799 | // contribution amount to be null, so that it will not show | |
1800 | // contribution amount same as membership amount. | |
1801 | //@todo - merge with section above | |
1802 | if ($this->_membershipBlock['is_separate_payment'] | |
3c0201c9 | 1803 | && !empty($this->_values['fee'][$priceField->id]) |
df6c4f28 EM |
1804 | && CRM_Utils_Array::value('name', $this->_values['fee'][$priceField->id]) == 'contribution_amount' |
1805 | && CRM_Utils_Array::value("price_{$priceField->id}", $this->_params) == '-1' | |
1806 | ) { | |
a13f3d8c | 1807 | $this->_params['amount'] = NULL; |
df6c4f28 EM |
1808 | } |
1809 | } | |
1810 | } | |
be26f3e0 | 1811 | |
03110609 | 1812 | /** |
5e4f7f74 EM |
1813 | * Submit function. |
1814 | * | |
03110609 EM |
1815 | * @param array $params |
1816 | * | |
1817 | * @throws CiviCRM_API3_Exception | |
1818 | */ | |
00be9182 | 1819 | public static function submit($params) { |
be26f3e0 EM |
1820 | $form = new CRM_Contribute_Form_Contribution_Confirm(); |
1821 | $form->_id = $params['id']; | |
75637f22 | 1822 | |
f64a217a EM |
1823 | CRM_Contribute_BAO_ContributionPage::setValues($form->_id, $form->_values); |
1824 | $form->_separateMembershipPayment = CRM_Contribute_BAO_ContributionPage::getIsMembershipPayment($form->_id); | |
be26f3e0 EM |
1825 | //this way the mocked up controller ignores the session stuff |
1826 | $_SERVER['REQUEST_METHOD'] = 'GET'; | |
1827 | $form->controller = new CRM_Contribute_Controller_Contribution(); | |
1828 | $params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1829 | $paramsProcessedForForm = $form->_params = self::getFormParams($params['id'], $params); | |
f64a217a | 1830 | $form->_amount = $params['amount']; |
870156d6 | 1831 | // hack these in for test support. |
1832 | $form->_fields['billing_first_name'] = 1; | |
1833 | $form->_fields['billing_last_name'] = 1; | |
be26f3e0 EM |
1834 | $priceSetID = $form->_params['priceSetId'] = $paramsProcessedForForm['price_set_id']; |
1835 | $priceFields = CRM_Price_BAO_PriceSet::getSetDetail($priceSetID); | |
f64a217a EM |
1836 | $priceSetFields = reset($priceFields); |
1837 | $form->_values['fee'] = $priceSetFields['fields']; | |
5624f515 | 1838 | $form->_priceSetId = $priceSetID; |
be26f3e0 | 1839 | $form->setFormAmountFields($priceSetID); |
e02d7e96 EM |
1840 | if (!empty($params['payment_processor_id'])) { |
1841 | $form->_paymentProcessor = civicrm_api3('payment_processor', 'getsingle', array( | |
1842 | 'id' => $params['payment_processor_id'], | |
1843 | )); | |
0f2b049e | 1844 | // The concept of contributeMode is deprecated as is the billing_mode concept. |
a13f3d8c | 1845 | if ($form->_paymentProcessor['billing_mode'] == 1) { |
e6fa4056 EM |
1846 | $form->_contributeMode = 'direct'; |
1847 | } | |
1848 | else { | |
1849 | $form->_contributeMode = 'notify'; | |
1850 | } | |
d25e4224 | 1851 | } |
07102c2c | 1852 | else { |
e02d7e96 | 1853 | $form->_params['payment_processor_id'] = 0; |
07102c2c | 1854 | } |
be26f3e0 EM |
1855 | $priceFields = $priceFields[$priceSetID]['fields']; |
1856 | CRM_Price_BAO_PriceSet::processAmount($priceFields, $paramsProcessedForForm, $lineItems, 'civicrm_contribution'); | |
1857 | $form->_lineItem = array($priceSetID => $lineItems); | |
75637f22 | 1858 | $form->processFormSubmission(CRM_Utils_Array::value('contact_id', $params)); |
be26f3e0 EM |
1859 | } |
1860 | ||
1861 | /** | |
ceb9fecb EM |
1862 | * Helper function for static submit function. |
1863 | * | |
1864 | * Set relevant params - help us to build up an array that we can pass in. | |
5e4f7f74 | 1865 | * |
100fef9d | 1866 | * @param int $id |
be26f3e0 EM |
1867 | * @param array $params |
1868 | * | |
1869 | * @return array | |
1870 | * @throws CiviCRM_API3_Exception | |
1871 | */ | |
00be9182 | 1872 | public static function getFormParams($id, array $params) { |
a13f3d8c | 1873 | if (!isset($params['is_pay_later'])) { |
e02d7e96 | 1874 | if (!empty($params['payment_processor_id'])) { |
d25e4224 DG |
1875 | $params['is_pay_later'] = 0; |
1876 | } | |
1877 | else { | |
1878 | $params['is_pay_later'] = civicrm_api3('contribution_page', 'getvalue', array( | |
a13f3d8c | 1879 | 'id' => $id, |
21dfd5f5 | 1880 | 'return' => 'is_pay_later', |
a13f3d8c | 1881 | )); |
d25e4224 | 1882 | } |
be26f3e0 | 1883 | } |
a13f3d8c | 1884 | if (empty($params['price_set_id'])) { |
be26f3e0 EM |
1885 | $params['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id']); |
1886 | } | |
1887 | return $params; | |
1888 | } | |
96025800 | 1889 | |
75637f22 EM |
1890 | /** |
1891 | * Post form submission handling. | |
1892 | * | |
1893 | * This is also called from the test suite. | |
1894 | * | |
1895 | * @param int $contactID | |
1896 | * | |
1897 | * @return array | |
1898 | */ | |
1899 | protected function processFormSubmission($contactID) { | |
1900 | $isPayLater = $this->_params['is_pay_later']; | |
1901 | if (isset($this->_params['payment_processor_id']) && $this->_params['payment_processor_id'] == 0) { | |
1902 | $this->_params['is_pay_later'] = $isPayLater = TRUE; | |
1903 | } | |
1904 | // add a description field at the very beginning | |
1905 | $this->_params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']); | |
1906 | ||
1907 | $this->_params['accountingCode'] = CRM_Utils_Array::value('accountingCode', $this->_values); | |
1908 | ||
1909 | // fix currency ID | |
1910 | $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency; | |
1911 | ||
1912 | //carry payment processor id. | |
1913 | if (CRM_Utils_Array::value('id', $this->_paymentProcessor)) { | |
1914 | $this->_params['payment_processor_id'] = $this->_paymentProcessor['id']; | |
1915 | } | |
f85b8063 KJ |
1916 | |
1917 | $premiumParams = $membershipParams = $params = $this->_params; | |
75637f22 EM |
1918 | if (!empty($params['image_URL'])) { |
1919 | CRM_Contact_BAO_Contact::processImageParams($params); | |
1920 | } | |
f85b8063 | 1921 | |
75637f22 EM |
1922 | $fields = array('email-Primary' => 1); |
1923 | ||
1924 | // get the add to groups | |
1925 | $addToGroups = array(); | |
1926 | ||
1927 | // now set the values for the billing location. | |
1928 | foreach ($this->_fields as $name => $value) { | |
1929 | $fields[$name] = 1; | |
1930 | ||
1931 | // get the add to groups for uf fields | |
1932 | if (!empty($value['add_to_group_id'])) { | |
1933 | $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id']; | |
1934 | } | |
1935 | } | |
1936 | ||
bddc8a28 | 1937 | $fields = $this->formatParamsForPaymentProcessor($fields); |
75637f22 EM |
1938 | |
1939 | // billing email address | |
1940 | $fields["email-{$this->_bltID}"] = 1; | |
1941 | ||
75637f22 EM |
1942 | // if onbehalf-of-organization contribution, take out |
1943 | // organization params in a separate variable, to make sure | |
1944 | // normal behavior is continued. And use that variable to | |
1945 | // process on-behalf-of functionality. | |
4779abb3 | 1946 | if (!empty($this->_values['onbehalf_profile_id'])) { |
75637f22 EM |
1947 | $behalfOrganization = array(); |
1948 | $orgFields = array('organization_name', 'organization_id', 'org_option'); | |
1949 | foreach ($orgFields as $fld) { | |
1950 | if (array_key_exists($fld, $params)) { | |
1951 | $behalfOrganization[$fld] = $params[$fld]; | |
1952 | unset($params[$fld]); | |
1953 | } | |
1954 | } | |
1955 | ||
1956 | if (is_array($params['onbehalf']) && !empty($params['onbehalf'])) { | |
1957 | foreach ($params['onbehalf'] as $fld => $values) { | |
1958 | if (strstr($fld, 'custom_')) { | |
1959 | $behalfOrganization[$fld] = $values; | |
1960 | } | |
1961 | elseif (!(strstr($fld, '-'))) { | |
1962 | if (in_array($fld, array( | |
1963 | 'contribution_campaign_id', | |
1964 | 'member_campaign_id', | |
1965 | ))) { | |
1966 | $fld = 'campaign_id'; | |
1967 | } | |
1968 | else { | |
1969 | $behalfOrganization[$fld] = $values; | |
1970 | } | |
1971 | $this->_params[$fld] = $values; | |
1972 | } | |
1973 | } | |
1974 | } | |
1975 | ||
1976 | if (array_key_exists('onbehalf_location', $params) && is_array($params['onbehalf_location'])) { | |
1977 | foreach ($params['onbehalf_location'] as $block => $vals) { | |
1978 | //fix for custom data (of type checkbox, multi-select) | |
1979 | if (substr($block, 0, 7) == 'custom_') { | |
1980 | continue; | |
1981 | } | |
1982 | // fix the index of block elements | |
1983 | if (is_array($vals)) { | |
1984 | foreach ($vals as $key => $val) { | |
1985 | //dont adjust the index of address block as | |
1986 | //it's index is WRT to location type | |
1987 | $newKey = ($block == 'address') ? $key : ++$key; | |
1988 | $behalfOrganization[$block][$newKey] = $val; | |
1989 | } | |
1990 | } | |
1991 | } | |
1992 | unset($params['onbehalf_location']); | |
1993 | } | |
1994 | if (!empty($params['onbehalf[image_URL]'])) { | |
1995 | $behalfOrganization['image_URL'] = $params['onbehalf[image_URL]']; | |
1996 | } | |
1997 | } | |
1998 | ||
1999 | // check for profile double opt-in and get groups to be subscribed | |
2000 | $subscribeGroupIds = CRM_Core_BAO_UFGroup::getDoubleOptInGroupIds($params, $contactID); | |
2001 | ||
2002 | // since we are directly adding contact to group lets unset it from mailing | |
2003 | if (!empty($addToGroups)) { | |
2004 | foreach ($addToGroups as $groupId) { | |
2005 | if (isset($subscribeGroupIds[$groupId])) { | |
2006 | unset($subscribeGroupIds[$groupId]); | |
2007 | } | |
2008 | } | |
2009 | } | |
2010 | ||
2011 | foreach ($addToGroups as $k) { | |
2012 | if (array_key_exists($k, $subscribeGroupIds)) { | |
2013 | unset($addToGroups[$k]); | |
2014 | } | |
2015 | } | |
2016 | ||
2017 | if (empty($contactID)) { | |
2018 | $dupeParams = $params; | |
2019 | if (!empty($dupeParams['onbehalf'])) { | |
2020 | unset($dupeParams['onbehalf']); | |
2021 | } | |
2022 | if (!empty($dupeParams['honor'])) { | |
2023 | unset($dupeParams['honor']); | |
2024 | } | |
2025 | ||
2026 | $dedupeParams = CRM_Dedupe_Finder::formatParams($dupeParams, 'Individual'); | |
2027 | $dedupeParams['check_permission'] = FALSE; | |
2028 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); | |
2029 | ||
2030 | // if we find more than one contact, use the first one | |
2031 | $contactID = CRM_Utils_Array::value(0, $ids); | |
2032 | ||
2033 | // Fetch default greeting id's if creating a contact | |
2034 | if (!$contactID) { | |
2035 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { | |
2036 | if (!isset($params[$greeting])) { | |
2037 | $params[$greeting] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting); | |
2038 | } | |
2039 | } | |
2040 | } | |
2041 | $contactType = NULL; | |
2042 | } | |
2043 | else { | |
2044 | $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'contact_type'); | |
2045 | } | |
2046 | $contactID = CRM_Contact_BAO_Contact::createProfileContact( | |
2047 | $params, | |
2048 | $fields, | |
2049 | $contactID, | |
2050 | $addToGroups, | |
2051 | NULL, | |
2052 | $contactType, | |
2053 | TRUE | |
2054 | ); | |
2055 | ||
2056 | // Make the contact ID associated with the contribution available at the Class level. | |
2057 | // Also make available to the session. | |
2058 | //@todo consider handling this in $this->getContactID(); | |
2059 | $this->set('contactID', $contactID); | |
2060 | $this->_contactID = $contactID; | |
2061 | ||
2062 | //get email primary first if exist | |
2063 | $subscriptionEmail = array('email' => CRM_Utils_Array::value('email-Primary', $params)); | |
2064 | if (!$subscriptionEmail['email']) { | |
2065 | $subscriptionEmail['email'] = CRM_Utils_Array::value("email-{$this->_bltID}", $params); | |
2066 | } | |
2067 | // subscribing contact to groups | |
2068 | if (!empty($subscribeGroupIds) && $subscriptionEmail['email']) { | |
2069 | CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($subscribeGroupIds, $subscriptionEmail, $contactID); | |
2070 | } | |
2071 | ||
2072 | // If onbehalf-of-organization contribution / signup, add organization | |
2073 | // and it's location. | |
4779abb3 | 2074 | if (isset($this->_values['onbehalf_profile_id']) && isset($behalfOrganization['organization_name'])) { |
75637f22 EM |
2075 | $ufFields = array(); |
2076 | foreach ($this->_fields['onbehalf'] as $name => $value) { | |
2077 | $ufFields[$name] = 1; | |
2078 | } | |
2079 | self::processOnBehalfOrganization($behalfOrganization, $contactID, $this->_values, | |
2080 | $this->_params, $ufFields | |
2081 | ); | |
2082 | } | |
2083 | elseif (!empty($this->_membershipContactID) && $contactID != $this->_membershipContactID) { | |
2084 | // this is an onbehalf renew case for inherited membership. For e.g a permissioned member of household, | |
2085 | // store current user id as related contact for later use for mailing / activity.. | |
2086 | $this->_values['related_contact'] = $contactID; | |
2087 | $this->_params['related_contact'] = $contactID; | |
2088 | // swap contact like we do for on-behalf-org case, so parent/primary membership is affected | |
2089 | $contactID = $this->_membershipContactID; | |
2090 | } | |
2091 | ||
2092 | // lets store the contactID in the session | |
2093 | // for things like tell a friend | |
2094 | $session = CRM_Core_Session::singleton(); | |
2095 | if (!$session->get('userID')) { | |
2096 | $session->set('transaction.userID', $contactID); | |
2097 | } | |
2098 | else { | |
2099 | $session->set('transaction.userID', NULL); | |
2100 | } | |
2101 | ||
2102 | $this->_useForMember = $this->get('useForMember'); | |
2103 | ||
2104 | // store the fact that this is a membership and membership type is selected | |
75637f22 EM |
2105 | if ((!empty($membershipParams['selectMembership']) && |
2106 | $membershipParams['selectMembership'] != 'no_thanks' | |
2107 | ) || | |
2108 | $this->_useForMember | |
2109 | ) { | |
75637f22 EM |
2110 | if (!$this->_useForMember) { |
2111 | $this->assign('membership_assign', TRUE); | |
2112 | $this->set('membershipTypeID', $this->_params['selectMembership']); | |
2113 | } | |
2114 | ||
2115 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
2116 | $membershipParams['is_test'] = 1; | |
2117 | } | |
2118 | if ($this->_params['is_pay_later']) { | |
2119 | $membershipParams['is_pay_later'] = 1; | |
2120 | } | |
75637f22 EM |
2121 | |
2122 | //inherit campaign from contribution page. | |
2123 | if (!array_key_exists('campaign_id', $membershipParams)) { | |
2124 | $membershipParams['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values); | |
2125 | } | |
2126 | ||
7dd9ca37 EM |
2127 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $membershipParams, TRUE); |
2128 | $this->doMembershipProcessing($contactID, $membershipParams, $premiumParams, $isPayLater); | |
75637f22 EM |
2129 | } |
2130 | else { | |
2131 | // at this point we've created a contact and stored its address etc | |
2132 | // all the payment processors expect the name and address to be in the | |
2133 | // so we copy stuff over to first_name etc. | |
2134 | $paymentParams = $this->_params; | |
75637f22 | 2135 | |
75637f22 EM |
2136 | if (!empty($paymentParams['onbehalf']) && |
2137 | is_array($paymentParams['onbehalf']) | |
2138 | ) { | |
2139 | foreach ($paymentParams['onbehalf'] as $key => $value) { | |
2140 | if (strstr($key, 'custom_')) { | |
2141 | $this->_params[$key] = $value; | |
2142 | } | |
2143 | } | |
75637f22 | 2144 | } |
75637f22 | 2145 | |
a8a4259e | 2146 | $result = CRM_Contribute_BAO_Contribution_Utils::processConfirm($this, $paymentParams, |
5fb28746 | 2147 | $contactID, |
46763692 | 2148 | $this->wrangleFinancialTypeID($this->_values['financial_type_id']), |
75637f22 | 2149 | 'contribution', |
46763692 | 2150 | ($this->_mode == 'test') ? 1 : 0 |
75637f22 | 2151 | ); |
82583880 | 2152 | |
0193ebdb | 2153 | if (empty($result['is_payment_failure'])) { |
2154 | // @todo move premium processing to complete transaction if it truly is an 'after' action. | |
2155 | $this->postProcessPremium($premiumParams, $result['contribution']); | |
2156 | } | |
2157 | if (!empty($result['contribution'])) { | |
2158 | // Not quite sure why it would be empty at this stage but tests show it can be ... at least in tests. | |
2159 | $this->completeTransaction($result, $result['contribution']->id); | |
a8a4259e EM |
2160 | } |
2161 | return $result; | |
75637f22 EM |
2162 | } |
2163 | } | |
2164 | ||
7dd9ca37 EM |
2165 | /** |
2166 | * Membership processing section. | |
2167 | * | |
2168 | * This is in a separate function as part of a move towards refactoring. | |
2169 | * | |
2170 | * @param int $contactID | |
2171 | * @param array $membershipParams | |
2172 | * @param array $premiumParams | |
2173 | * @param bool $isPayLater | |
2174 | */ | |
2175 | protected function doMembershipProcessing($contactID, $membershipParams, $premiumParams, $isPayLater) { | |
0d9ff2d4 | 2176 | // This could be set by a hook. |
2177 | if (!empty($this->_params['installments'])) { | |
2178 | $membershipParams['installments'] = $this->_params['installments']; | |
2179 | } | |
7dd9ca37 EM |
2180 | // added new parameter for cms user contact id, needed to distinguish behaviour for on behalf of sign-ups |
2181 | if (isset($this->_params['related_contact'])) { | |
2182 | $membershipParams['cms_contactID'] = $this->_params['related_contact']; | |
2183 | } | |
2184 | else { | |
2185 | $membershipParams['cms_contactID'] = $contactID; | |
2186 | } | |
2187 | ||
2188 | if (!empty($membershipParams['onbehalf']) && | |
2189 | is_array($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id']) | |
2190 | ) { | |
2191 | $this->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id']; | |
2192 | } | |
2193 | ||
2194 | $customFieldsFormatted = $fieldTypes = array(); | |
2195 | if (!empty($membershipParams['onbehalf']) && | |
2196 | is_array($membershipParams['onbehalf']) | |
2197 | ) { | |
2198 | foreach ($membershipParams['onbehalf'] as $key => $value) { | |
2199 | if (strstr($key, 'custom_')) { | |
2200 | $customFieldId = explode('_', $key); | |
2201 | CRM_Core_BAO_CustomField::formatCustomField( | |
2202 | $customFieldId[1], | |
2203 | $customFieldsFormatted, | |
2204 | $value, | |
2205 | 'Membership', | |
2206 | NULL, | |
2207 | $contactID | |
2208 | ); | |
2209 | } | |
2210 | } | |
2211 | $fieldTypes = array('Contact', 'Organization', 'Membership'); | |
2212 | } | |
2213 | ||
2214 | $priceFieldIds = $this->get('memberPriceFieldIDS'); | |
2215 | ||
2216 | if (!empty($priceFieldIds)) { | |
2217 | $contributionTypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceFieldIds['id'], 'financial_type_id'); | |
2218 | unset($priceFieldIds['id']); | |
2219 | $membershipTypeIds = array(); | |
2220 | $membershipTypeTerms = array(); | |
2221 | foreach ($priceFieldIds as $priceFieldId) { | |
2222 | if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) { | |
2223 | $membershipTypeIds[] = $id; | |
2224 | //@todo the value for $term is immediately overwritten. It is unclear from the code whether it was intentional to | |
2225 | // do this or a double = was intended (this ambiguity is the reason many IDEs complain about 'assignment in condition' | |
2226 | $term = 1; | |
2227 | if ($term = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_num_terms')) { | |
2228 | $membershipTypeTerms[$id] = ($term > 1) ? $term : 1; | |
2229 | } | |
2230 | else { | |
2231 | $membershipTypeTerms[$id] = 1; | |
2232 | } | |
2233 | } | |
2234 | } | |
2235 | $membershipParams['selectMembership'] = $membershipTypeIds; | |
2236 | $membershipParams['financial_type_id'] = $contributionTypeID; | |
2237 | $membershipParams['types_terms'] = $membershipTypeTerms; | |
2238 | } | |
2239 | if (!empty($membershipParams['selectMembership'])) { | |
2240 | // CRM-12233 | |
2241 | $membershipLineItems = array(); | |
2242 | if ($this->_separateMembershipPayment && $this->_values['amount_block_is_active']) { | |
2243 | foreach ($this->_values['fee'] as $key => $feeValues) { | |
2244 | if ($feeValues['name'] == 'membership_amount') { | |
2245 | $fieldId = $this->_params['price_' . $key]; | |
2246 | $membershipLineItems[$this->_priceSetId][$fieldId] = $this->_lineItem[$this->_priceSetId][$fieldId]; | |
2247 | unset($this->_lineItem[$this->_priceSetId][$fieldId]); | |
2248 | break; | |
2249 | } | |
2250 | } | |
2251 | } | |
4b6c8c1e | 2252 | try { |
2253 | $this->processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, $membershipLineItems, $isPayLater); | |
2254 | } | |
2255 | catch (CRM_Core_Exception $e) { | |
2256 | CRM_Core_Session::singleton()->setStatus($e->getMessage()); | |
2257 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=true&qfKey={$this->_params['qfKey']}")); | |
2258 | } | |
7dd9ca37 EM |
2259 | if (!$this->_amount > 0.0 || !$membershipParams['amount']) { |
2260 | // we need to explicitly create a CMS user in case of free memberships | |
2261 | // since it is done under processConfirm for paid memberships | |
2262 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($membershipParams, | |
2263 | $membershipParams['cms_contactID'], | |
2264 | 'email-' . $this->_bltID | |
2265 | ); | |
2266 | } | |
2267 | } | |
2268 | } | |
2269 | ||
0193ebdb | 2270 | /** |
2271 | * Complete transaction if payment has been processed. | |
2272 | * | |
2273 | * Check the result for a success outcome & if paid then complete the transaction. | |
2274 | * | |
2275 | * Completing will trigger update of related entities and emails. | |
2276 | * | |
2277 | * @param array $result | |
2278 | * @param int $contributionID | |
2279 | * | |
2280 | * @throws \CRM_Core_Exception | |
2281 | */ | |
2282 | protected function completeTransaction($result, $contributionID) { | |
2283 | if (CRM_Utils_Array::value('payment_status_id', $result) == 1) { | |
2284 | try { | |
2285 | civicrm_api3('contribution', 'completetransaction', array( | |
2286 | 'id' => $contributionID, | |
2287 | 'trxn_id' => CRM_Utils_Array::value('trxn_id', $result), | |
2288 | 'payment_processor_id' => $this->_paymentProcessor['id'], | |
2289 | 'is_transactional' => FALSE, | |
2290 | 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), | |
2291 | ) | |
2292 | ); | |
2293 | } | |
2294 | catch (CiviCRM_API3_Exception $e) { | |
2295 | if ($e->getErrorCode() != 'contribution_completed') { | |
2296 | throw new CRM_Core_Exception('Failed to update contribution in database'); | |
2297 | } | |
2298 | } | |
2299 | } | |
2300 | } | |
2301 | ||
6a488035 | 2302 | } |