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