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