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