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']; | |
24c1b3d8 | 96 | if ($pledgeParams['frequency_unit'] === 'month') { |
356bfcaf | 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']))); |
cbbbf0ef | 105 | $pledgeParams['start_date'] = $pledgeParams['scheduled_date'] = date('Ymd', strtotime($params['start_date'])); |
356bfcaf | 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 | 151 | * @return array |
24c1b3d8 | 152 | * @throws \CRM_Core_Exception |
c8bde7ea | 153 | */ |
a13f3d8c | 154 | public static function getContributionParams( |
0ede7391 | 155 | $params, $financialTypeID, |
f6261e9d | 156 | $paymentProcessorOutcome, $receiptDate, $recurringContributionID) { |
be2fb01f | 157 | $contributionParams = [ |
c8bde7ea | 158 | 'financial_type_id' => $financialTypeID, |
9126e134 | 159 | 'receive_date' => !empty($params['receive_date']) ? CRM_Utils_Date::processDate($params['receive_date']) : date('YmdHis'), |
6b409353 CW |
160 | 'tax_amount' => $params['tax_amount'] ?? NULL, |
161 | 'amount_level' => $params['amount_level'] ?? NULL, | |
c8bde7ea EM |
162 | 'invoice_id' => $params['invoiceID'], |
163 | 'currency' => $params['currencyID'], | |
cbbbf0ef | 164 | 'is_pay_later' => $params['is_pay_later'] ?? 0, |
c8bde7ea EM |
165 | //configure cancel reason, cancel date and thankyou date |
166 | //from 'contribution' type profile if included | |
cbbbf0ef | 167 | 'cancel_reason' => $params['cancel_reason'] ?? 0, |
a1a2a83d TO |
168 | 'cancel_date' => isset($params['cancel_date']) ? CRM_Utils_Date::format($params['cancel_date']) : NULL, |
169 | 'thankyou_date' => isset($params['thankyou_date']) ? CRM_Utils_Date::format($params['thankyou_date']) : NULL, | |
c8bde7ea | 170 | //setting to make available to hook - although seems wrong to set on form for BAO hook availability |
cbbbf0ef | 171 | 'skipLineItem' => $params['skipLineItem'] ?? 0, |
be2fb01f | 172 | ]; |
3febe800 | 173 | |
a7a33651 | 174 | if ($paymentProcessorOutcome) { |
9c1bc317 | 175 | $contributionParams['payment_processor'] = $paymentProcessorOutcome['payment_processor'] ?? NULL; |
a7a33651 | 176 | } |
7127b69c | 177 | if (!empty($params["is_email_receipt"])) { |
be2fb01f | 178 | $contributionParams += [ |
7127b69c | 179 | 'receipt_date' => $receiptDate, |
be2fb01f | 180 | ]; |
7127b69c | 181 | } |
c8bde7ea | 182 | |
c8bde7ea EM |
183 | if ($recurringContributionID) { |
184 | $contributionParams['contribution_recur_id'] = $recurringContributionID; | |
185 | } | |
186 | ||
0ede7391 | 187 | $contributionParams['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); |
c8bde7ea EM |
188 | if (isset($contributionParams['invoice_id'])) { |
189 | $contributionParams['id'] = CRM_Core_DAO::getFieldValue( | |
190 | 'CRM_Contribute_DAO_Contribution', | |
191 | $contributionParams['invoice_id'], | |
192 | 'id', | |
193 | 'invoice_id' | |
194 | ); | |
195 | } | |
196 | ||
197 | return $contributionParams; | |
198 | } | |
199 | ||
fda99f2f EM |
200 | /** |
201 | * Get non-deductible amount. | |
202 | * | |
203 | * This is a bit too much about wierd form interpretation to be this deep. | |
204 | * | |
0e480632 | 205 | * @see https://issues.civicrm.org/jira/browse/CRM-11885 |
fda99f2f EM |
206 | * if non_deductible_amount exists i.e. Additional Details fieldset was opened [and staff typed something] -> keep |
207 | * it. | |
ceb9fecb EM |
208 | * |
209 | * @param array $params | |
210 | * @param CRM_Financial_BAO_FinancialType $financialType | |
211 | * @param bool $online | |
5afce5ad | 212 | * @param CRM_Contribute_Form_Contribution_Confirm $form |
fda99f2f EM |
213 | * |
214 | * @return array | |
215 | */ | |
8a594d37 | 216 | public static function getNonDeductibleAmount($params, $financialType, $online, $form) { |
fda99f2f EM |
217 | if (isset($params['non_deductible_amount']) && (!empty($params['non_deductible_amount']))) { |
218 | return $params['non_deductible_amount']; | |
219 | } | |
9c1bc317 | 220 | $priceSetId = $params['priceSetId'] ?? NULL; |
5afce5ad | 221 | // return non-deductible amount if it is set at the price field option level |
05465712 | 222 | if ($priceSetId && !empty($form->_lineItem)) { |
223 | $nonDeductibleAmount = CRM_Price_BAO_PriceSet::getNonDeductibleAmountFromPriceSet($priceSetId, $form->_lineItem); | |
224 | } | |
225 | ||
226 | if (!empty($nonDeductibleAmount)) { | |
227 | return $nonDeductibleAmount; | |
5afce5ad | 228 | } |
fda99f2f EM |
229 | else { |
230 | if ($financialType->is_deductible) { | |
231 | if ($online && isset($params['selectProduct'])) { | |
9c1bc317 | 232 | $selectProduct = $params['selectProduct'] ?? NULL; |
fda99f2f EM |
233 | } |
234 | if (!$online && isset($params['product_name'][0])) { | |
235 | $selectProduct = $params['product_name'][0]; | |
236 | } | |
237 | // if there is a product - compare the value to the contribution amount | |
238 | if (isset($selectProduct) && | |
24c1b3d8 | 239 | $selectProduct !== 'no_thanks' |
fda99f2f EM |
240 | ) { |
241 | $productDAO = new CRM_Contribute_DAO_Product(); | |
242 | $productDAO->id = $selectProduct; | |
243 | $productDAO->find(TRUE); | |
244 | // product value exceeds contribution amount | |
245 | if ($params['amount'] < $productDAO->price) { | |
246 | $nonDeductibleAmount = $params['amount']; | |
247 | return $nonDeductibleAmount; | |
248 | } | |
249 | // product value does NOT exceed contribution amount | |
250 | else { | |
251 | return $productDAO->price; | |
252 | } | |
253 | } | |
254 | // contribution is deductible - but there is no product | |
255 | else { | |
256 | return '0.00'; | |
257 | } | |
258 | } | |
259 | // contribution is NOT deductible | |
260 | else { | |
261 | return $params['amount']; | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
6a488035 | 266 | /** |
fe482240 | 267 | * Set variables up before form is built. |
6a488035 TO |
268 | */ |
269 | public function preProcess() { | |
6a488035 | 270 | parent::preProcess(); |
cde484fd | 271 | |
6a488035 TO |
272 | // lineItem isn't set until Register postProcess |
273 | $this->_lineItem = $this->get('lineItem'); | |
58466af1 | 274 | $this->_ccid = $this->get('ccid'); |
9a7ba24d | 275 | |
a9768188 EM |
276 | $this->_params = $this->controller->exportValues('Main'); |
277 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); | |
278 | $this->_params['amount'] = $this->get('amount'); | |
279 | if (isset($this->_params['amount'])) { | |
280 | $this->setFormAmountFields($this->_params['priceSetId']); | |
6a488035 | 281 | } |
6a488035 | 282 | |
a9768188 EM |
283 | $this->_params['tax_amount'] = $this->get('tax_amount'); |
284 | $this->_useForMember = $this->get('useForMember'); | |
6a488035 | 285 | |
2e09448c | 286 | CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($this->_params); |
5df36634 | 287 | |
358b59a5 | 288 | $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency; |
6a488035 | 289 | |
a9768188 EM |
290 | if (!empty($this->_membershipBlock)) { |
291 | $this->_params['selectMembership'] = $this->get('selectMembership'); | |
292 | } | |
9c86599c | 293 | if (!empty($this->_paymentProcessor) && $this->_paymentProcessor['object']->supports('preApproval')) { |
294 | $preApprovalParams = $this->_paymentProcessor['object']->getPreApprovalDetails($this->get('pre_approval_parameters')); | |
295 | $this->_params = array_merge($this->_params, $preApprovalParams); | |
dd22a911 | 296 | |
9c86599c | 297 | // We may have fetched some billing details from the getPreApprovalDetails function so we |
298 | // want to ensure we set this after that function has been called. | |
299 | CRM_Core_Payment_Form::mapParams($this->_bltID, $preApprovalParams, $this->_params, FALSE); | |
2db8bc3f | 300 | } |
6a488035 TO |
301 | |
302 | $this->_params['is_pay_later'] = $this->get('is_pay_later'); | |
303 | $this->assign('is_pay_later', $this->_params['is_pay_later']); | |
304 | if ($this->_params['is_pay_later']) { | |
cbbbf0ef | 305 | $this->assign('pay_later_receipt', $this->_values['pay_later_receipt'] ?? NULL); |
6a488035 TO |
306 | } |
307 | // if onbehalf-of-organization | |
4779abb3 | 308 | if (!empty($this->_values['onbehalf_profile_id']) && !empty($this->_params['onbehalf']['organization_name'])) { |
f85a1a75 | 309 | if (empty($this->_params['org_option']) && empty($this->_params['organization_id'])) { |
9c1bc317 | 310 | $this->_params['organization_id'] = $this->_params['onbehalfof_id'] ?? NULL; |
f85a1a75 | 311 | } |
6a488035 | 312 | $this->_params['organization_name'] = $this->_params['onbehalf']['organization_name']; |
be2fb01f | 313 | $addressBlocks = [ |
a13f3d8c TO |
314 | 'street_address', |
315 | 'city', | |
316 | 'state_province', | |
317 | 'postal_code', | |
318 | 'country', | |
319 | 'supplemental_address_1', | |
320 | 'supplemental_address_2', | |
321 | 'supplemental_address_3', | |
322 | 'postal_code_suffix', | |
323 | 'geo_code_1', | |
324 | 'geo_code_2', | |
325 | 'address_name', | |
be2fb01f | 326 | ]; |
6a488035 | 327 | |
be2fb01f | 328 | $blocks = ['email', 'phone', 'im', 'url', 'openid']; |
6a488035 TO |
329 | foreach ($this->_params['onbehalf'] as $loc => $value) { |
330 | $field = $typeId = NULL; | |
331 | if (strstr($loc, '-')) { | |
fb8d25da | 332 | [$field, $locType] = explode('-', $loc); |
6a488035 TO |
333 | } |
334 | ||
335 | if (in_array($field, $addressBlocks)) { | |
24c1b3d8 | 336 | if ($locType === 'Primary') { |
6a488035 TO |
337 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
338 | $locType = $defaultLocationType->id; | |
cde484fd DL |
339 | } |
340 | ||
24c1b3d8 | 341 | if ($field === 'country') { |
6a488035 TO |
342 | $value = CRM_Core_PseudoConstant::countryIsoCode($value); |
343 | } | |
24c1b3d8 | 344 | elseif ($field === 'state_province') { |
6a488035 TO |
345 | $value = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); |
346 | } | |
cde484fd | 347 | |
6a488035 TO |
348 | $isPrimary = 1; |
349 | if (isset($this->_params['onbehalf_location']['address']) | |
a13f3d8c TO |
350 | && count($this->_params['onbehalf_location']['address']) > 0 |
351 | ) { | |
6a488035 TO |
352 | $isPrimary = 0; |
353 | } | |
cde484fd | 354 | |
6a488035 | 355 | $this->_params['onbehalf_location']['address'][$locType][$field] = $value; |
a7488080 | 356 | if (empty($this->_params['onbehalf_location']['address'][$locType]['is_primary'])) { |
6a488035 | 357 | $this->_params['onbehalf_location']['address'][$locType]['is_primary'] = $isPrimary; |
a13f3d8c | 358 | } |
6a488035 TO |
359 | $this->_params['onbehalf_location']['address'][$locType]['location_type_id'] = $locType; |
360 | } | |
361 | elseif (in_array($field, $blocks)) { | |
362 | if (!$typeId || is_numeric($typeId)) { | |
a13f3d8c TO |
363 | $blockName = $fieldName = $field; |
364 | $locationType = 'location_type_id'; | |
24c1b3d8 | 365 | if ($locType === 'Primary') { |
6a488035 TO |
366 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
367 | $locationValue = $defaultLocationType->id; | |
368 | } | |
369 | else { | |
370 | $locationValue = $locType; | |
371 | } | |
a13f3d8c | 372 | $locTypeId = ''; |
be2fb01f | 373 | $phoneExtField = []; |
6a488035 | 374 | |
fb8d25da | 375 | if ($field === 'url') { |
a13f3d8c | 376 | $blockName = 'website'; |
887e764d | 377 | $locationType = 'website_type_id'; |
fb8d25da | 378 | [$field, $locationValue] = explode('-', $loc); |
6a488035 | 379 | } |
fb8d25da | 380 | elseif ($field === 'im') { |
6a488035 TO |
381 | $fieldName = 'name'; |
382 | $locTypeId = 'provider_id'; | |
a13f3d8c | 383 | $typeId = $this->_params['onbehalf']["{$loc}-provider_id"]; |
6a488035 TO |
384 | } |
385 | elseif ($field == 'phone') { | |
fb8d25da | 386 | [$field, $locType, $typeId] = explode('-', $loc); |
6a488035 TO |
387 | $locTypeId = 'phone_type_id'; |
388 | ||
389 | //check if extension field exists | |
a13f3d8c | 390 | $extField = str_replace('phone', 'phone_ext', $loc); |
6a488035 | 391 | if (isset($this->_params['onbehalf'][$extField])) { |
be2fb01f | 392 | $phoneExtField = ['phone_ext' => $this->_params['onbehalf'][$extField]]; |
6a488035 TO |
393 | } |
394 | } | |
395 | ||
396 | $isPrimary = 1; | |
f1b56c48 | 397 | if (isset($this->_params['onbehalf_location'][$blockName]) |
a13f3d8c TO |
398 | && count($this->_params['onbehalf_location'][$blockName]) > 0 |
399 | ) { | |
400 | $isPrimary = 0; | |
6a488035 TO |
401 | } |
402 | if ($locationValue) { | |
be2fb01f | 403 | $blockValues = [ |
a13f3d8c | 404 | $fieldName => $value, |
6a488035 | 405 | $locationType => $locationValue, |
a13f3d8c | 406 | 'is_primary' => $isPrimary, |
be2fb01f | 407 | ]; |
6a488035 TO |
408 | |
409 | if ($locTypeId) { | |
be2fb01f | 410 | $blockValues = array_merge($blockValues, [$locTypeId => $typeId]); |
6a488035 TO |
411 | } |
412 | if (!empty($phoneExtField)) { | |
413 | $blockValues = array_merge($blockValues, $phoneExtField); | |
414 | } | |
415 | ||
416 | $this->_params['onbehalf_location'][$blockName][] = $blockValues; | |
417 | } | |
418 | } | |
419 | } | |
420 | elseif (strstr($loc, 'custom')) { | |
421 | if ($value && isset($this->_params['onbehalf']["{$loc}_id"])) { | |
422 | $value = $this->_params['onbehalf']["{$loc}_id"]; | |
423 | } | |
424 | $this->_params['onbehalf_location']["{$loc}"] = $value; | |
425 | } | |
426 | else { | |
24c1b3d8 | 427 | if ($loc === 'contact_sub_type') { |
6a488035 TO |
428 | $this->_params['onbehalf_location'][$loc] = $value; |
429 | } | |
430 | else { | |
431 | $this->_params['onbehalf_location'][$field] = $value; | |
432 | } | |
433 | } | |
434 | } | |
435 | } | |
a7488080 | 436 | elseif (!empty($this->_values['is_for_organization'])) { |
6a488035 TO |
437 | // no on behalf of an organization, CRM-5519 |
438 | // so reset loc blocks from main params. | |
be2fb01f | 439 | foreach ([ |
1330f57a SL |
440 | 'phone', |
441 | 'email', | |
442 | 'address', | |
443 | ] as $blk) { | |
6a488035 TO |
444 | if (isset($this->_params[$blk])) { |
445 | unset($this->_params[$blk]); | |
446 | } | |
447 | } | |
448 | } | |
90102a32 | 449 | $this->setRecurringMembershipParams(); |
6a488035 TO |
450 | |
451 | if ($this->_pcpId) { | |
452 | $params = $this->processPcp($this, $this->_params); | |
453 | $this->_params = $params; | |
454 | } | |
455 | $this->_params['invoiceID'] = $this->get('invoiceID'); | |
456 | ||
457 | //carry campaign from profile. | |
458 | if (array_key_exists('contribution_campaign_id', $this->_params)) { | |
459 | $this->_params['campaign_id'] = $this->_params['contribution_campaign_id']; | |
460 | } | |
461 | ||
569df8ec DG |
462 | // assign contribution page id to the template so we can add css class for it |
463 | $this->assign('contributionPageID', $this->_id); | |
cbbbf0ef | 464 | $this->assign('is_for_organization', $this->_params['is_for_organization'] ?? NULL); |
569df8ec | 465 | |
6a488035 TO |
466 | $this->set('params', $this->_params); |
467 | } | |
468 | ||
469 | /** | |
fe482240 | 470 | * Build the form object. |
6a488035 TO |
471 | */ |
472 | public function buildQuickForm() { | |
8684f612 | 473 | // FIXME: Some of this code is identical to Thankyou.php and should be broken out into a shared function |
6a488035 TO |
474 | $this->assignToTemplate(); |
475 | ||
476 | $params = $this->_params; | |
6a488035 | 477 | // make sure we have values for it |
2f44fc96 | 478 | if (!empty($this->_values['honoree_profile_id']) && !empty($params['soft_credit_type_id']) && empty($this->_ccid)) { |
a13f3d8c | 479 | $honorName = NULL; |
133e2c99 | 480 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); |
481 | ||
133e2c99 | 482 | $this->assign('soft_credit_type', $softCreditTypes[$params['soft_credit_type_id']]); |
4779abb3 | 483 | CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields($this, $params['honor']); |
6a488035 | 484 | |
be2fb01f | 485 | $fieldTypes = ['Contact']; |
4779abb3 | 486 | $fieldTypes[] = CRM_Core_BAO_UFGroup::getContactType($this->_values['honoree_profile_id']); |
487 | $this->buildCustom($this->_values['honoree_profile_id'], 'honoreeProfileFields', TRUE, 'honor', $fieldTypes); | |
6a488035 | 488 | } |
2627f4ba | 489 | $this->assign('receiptFromEmail', $this->_values['receipt_from_email'] ?? NULL); |
6a488035 TO |
490 | $amount_block_is_active = $this->get('amount_block_is_active'); |
491 | $this->assign('amount_block_is_active', $amount_block_is_active); | |
492 | ||
147b56dc MW |
493 | // Make a copy of line items array to use for display only |
494 | $tplLineItems = $this->_lineItem; | |
97288cdc | 495 | if (CRM_Invoicing_Utils::isInvoicingEnabled()) { |
f1b56c48 | 496 | // @todo $params seems like exactly the wrong place to get totalTaxAmount from |
497 | // this is a calculated variable so we it should be transparent how we | |
498 | // calculated it rather than coming from 'params' | |
03b412ae PB |
499 | $this->assign('totalTaxAmount', $params['tax_amount']); |
500 | } | |
f1b56c48 | 501 | $this->assignLineItemsToTemplate($tplLineItems); |
502 | ||
503 | $isDisplayLineItems = $this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); | |
504 | ||
505 | $this->assign('isDisplayLineItems', $isDisplayLineItems); | |
506 | ||
507 | if (!$isDisplayLineItems) { | |
508 | // quickConfig is deprecated in favour of isDisplayLineItems. Lots of logic has been harnessed to quick config | |
509 | // whereas isDisplayLineItems is specific & clear. | |
97288cdc | 510 | $this->assign('is_quick_config', 1); |
511 | $this->_params['is_quick_config'] = 1; | |
512 | } | |
513 | ||
24c1b3d8 | 514 | if (!empty($params['selectProduct']) && $params['selectProduct'] !== 'no_thanks') { |
9c1bc317 | 515 | $option = $params['options_' . $params['selectProduct']] ?? NULL; |
6a488035 TO |
516 | $productID = $params['selectProduct']; |
517 | CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, FALSE, | |
518 | $productID, $option | |
519 | ); | |
520 | $this->set('productID', $productID); | |
521 | $this->set('option', $option); | |
522 | } | |
523 | $config = CRM_Core_Config::singleton(); | |
2f44fc96 | 524 | if (in_array('CiviMember', $config->enableComponents) && empty($this->_ccid)) { |
6a488035 | 525 | if (isset($params['selectMembership']) && |
24c1b3d8 | 526 | $params['selectMembership'] !== 'no_thanks' |
6a488035 | 527 | ) { |
42e3a033 | 528 | $this->buildMembershipBlock( |
5b757295 | 529 | $this->_membershipContactID, |
e622655c | 530 | $params['selectMembership'] |
6a488035 | 531 | ); |
9cc96227 | 532 | if (!empty($params['auto_renew'])) { |
533 | $this->assign('auto_renew', TRUE); | |
534 | } | |
6a488035 TO |
535 | } |
536 | else { | |
537 | $this->assign('membershipBlock', FALSE); | |
538 | } | |
539 | } | |
2f44fc96 | 540 | if (empty($this->_ccid)) { |
541 | $this->buildCustom($this->_values['custom_pre_id'], 'customPre', TRUE); | |
542 | $this->buildCustom($this->_values['custom_post_id'], 'customPost', TRUE); | |
543 | } | |
6a488035 | 544 | |
a9653117 | 545 | if (!empty($this->_values['onbehalf_profile_id']) && |
546 | !empty($params['onbehalf']) && | |
547 | ($this->_values['is_for_organization'] == 2 || | |
548 | !empty($params['is_for_organization']) | |
2f44fc96 | 549 | ) && empty($this->_ccid) |
a9653117 | 550 | ) { |
be2fb01f | 551 | $fieldTypes = ['Contact', 'Organization']; |
6a488035 | 552 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); |
a13f3d8c | 553 | $fieldTypes = array_merge($fieldTypes, $contactSubType); |
6a488035 | 554 | if (is_array($this->_membershipBlock) && !empty($this->_membershipBlock)) { |
be2fb01f | 555 | $fieldTypes = array_merge($fieldTypes, ['Membership']); |
6a488035 TO |
556 | } |
557 | else { | |
be2fb01f | 558 | $fieldTypes = array_merge($fieldTypes, ['Contribution']); |
6a488035 TO |
559 | } |
560 | ||
4779abb3 | 561 | $this->buildCustom($this->_values['onbehalf_profile_id'], 'onbehalfProfile', TRUE, 'onbehalf', $fieldTypes); |
6a488035 TO |
562 | } |
563 | ||
564 | $this->_separateMembershipPayment = $this->get('separateMembershipPayment'); | |
565 | $this->assign('is_separate_payment', $this->_separateMembershipPayment); | |
97288cdc | 566 | |
6a488035 | 567 | $this->assign('priceSetID', $this->_priceSetId); |
6a488035 | 568 | |
229159bf CW |
569 | // The concept of contributeMode is deprecated. |
570 | // the is_monetary concept probably should be too as it can be calculated from | |
571 | // the existence of 'amount' & seems fragile. | |
fb8d25da | 572 | if ($this->_contributeMode === 'notify' || |
a4859929 | 573 | $this->_amount <= 0.0 || $this->_params['is_pay_later'] |
229159bf CW |
574 | ) { |
575 | $contribButton = ts('Continue'); | |
6a488035 | 576 | } |
fe552de9 | 577 | elseif (!empty($this->_ccid)) { |
578 | $contribButton = ts('Make Payment'); | |
fe552de9 | 579 | } |
6a488035 | 580 | else { |
229159bf | 581 | $contribButton = ts('Make Contribution'); |
6a488035 | 582 | } |
a4859929 | 583 | $this->assign('button', $contribButton); |
e364c762 | 584 | |
585 | $this->assign('continueText', | |
586 | $this->getPaymentProcessorObject()->getText('contributionPageContinueText', [ | |
587 | 'is_payment_to_existing' => !empty($this->_ccid), | |
588 | 'amount' => $this->_amount, | |
1330f57a | 589 | ]) |
e364c762 | 590 | ); |
591 | ||
be2fb01f | 592 | $this->addButtons([ |
1330f57a SL |
593 | [ |
594 | 'type' => 'next', | |
595 | 'name' => $contribButton, | |
596 | 'spacing' => ' ', | |
597 | 'isDefault' => TRUE, | |
1330f57a SL |
598 | ], |
599 | [ | |
600 | 'type' => 'back', | |
601 | 'name' => ts('Go Back'), | |
602 | ], | |
603 | ]); | |
6a488035 | 604 | |
be2fb01f | 605 | $defaults = []; |
9c80939e | 606 | $fields = array_fill_keys(array_keys($this->_fields), 1); |
6a488035 TO |
607 | $fields["billing_state_province-{$this->_bltID}"] = $fields["billing_country-{$this->_bltID}"] = $fields["email-{$this->_bltID}"] = 1; |
608 | ||
609 | $contact = $this->_params; | |
610 | foreach ($fields as $name => $dontCare) { | |
9c80939e | 611 | // Recursively set defaults for nested fields |
fb8d25da | 612 | if (isset($contact[$name]) && is_array($contact[$name]) && ($name === 'onbehalf' || $name === 'honor')) { |
9c80939e | 613 | foreach ($contact[$name] as $fieldName => $fieldValue) { |
fb8d25da | 614 | if (is_array($fieldValue) && $this->_fields[$name][$fieldName]['html_type'] === 'CheckBox') { |
38b95e0f | 615 | foreach ($fieldValue as $key => $value) { |
616 | $defaults["{$name}[{$fieldName}][{$key}]"] = $value; | |
617 | } | |
618 | } | |
619 | else { | |
620 | $defaults["{$name}[{$fieldName}]"] = $fieldValue; | |
621 | } | |
9c80939e CW |
622 | } |
623 | } | |
624 | elseif (isset($contact[$name])) { | |
6a488035 | 625 | $defaults[$name] = $contact[$name]; |
fb8d25da | 626 | if (substr($name, 0, 7) === 'custom_') { |
6a488035 TO |
627 | $timeField = "{$name}_time"; |
628 | if (isset($contact[$timeField])) { | |
629 | $defaults[$timeField] = $contact[$timeField]; | |
630 | } | |
631 | if (isset($contact["{$name}_id"])) { | |
632 | $defaults["{$name}_id"] = $contact["{$name}_id"]; | |
633 | } | |
634 | } | |
be2fb01f | 635 | elseif (in_array($name, [ |
1330f57a SL |
636 | 'addressee', |
637 | 'email_greeting', | |
638 | 'postal_greeting', | |
639 | ]) && !empty($contact[$name . '_custom']) | |
a13f3d8c | 640 | ) { |
6a488035 TO |
641 | $defaults[$name . '_custom'] = $contact[$name . '_custom']; |
642 | } | |
643 | } | |
644 | } | |
645 | ||
646 | $this->assign('useForMember', $this->get('useForMember')); | |
647 | ||
6a488035 TO |
648 | $this->setDefaults($defaults); |
649 | ||
650 | $this->freeze(); | |
651 | } | |
652 | ||
4b238552 MW |
653 | /** |
654 | * Build Membership Block in Contribution Pages. | |
655 | * @todo this was shared on CRM_Contribute_Form_ContributionBase but we are refactoring and simplifying for each | |
656 | * step (main/confirm/thankyou) | |
657 | * | |
658 | * @param int $cid | |
659 | * Contact checked for having a current membership for a particular membership. | |
4b238552 MW |
660 | * @param int|array $selectedMembershipTypeID |
661 | * Selected membership id. | |
4b238552 MW |
662 | * @param null $isTest |
663 | * | |
664 | * @return bool | |
665 | * Is this a separate membership payment | |
666 | * | |
667 | * @throws \CiviCRM_API3_Exception | |
668 | * @throws \CRM_Core_Exception | |
669 | */ | |
e622655c | 670 | private function buildMembershipBlock($cid, $selectedMembershipTypeID = NULL, $isTest = NULL) { |
4b238552 MW |
671 | $separateMembershipPayment = FALSE; |
672 | if ($this->_membershipBlock) { | |
673 | $this->_currentMemberships = []; | |
674 | ||
675 | $membershipTypeIds = $membershipTypes = $radio = $radioOptAttrs = []; | |
676 | $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember); | |
677 | ||
4b238552 MW |
678 | $autoRenewMembershipTypeOptions = []; |
679 | ||
680 | $separateMembershipPayment = $this->_membershipBlock['is_separate_payment'] ?? NULL; | |
681 | ||
682 | if ($membershipPriceset) { | |
683 | foreach ($this->_priceSet['fields'] as $pField) { | |
684 | if (empty($pField['options'])) { | |
685 | continue; | |
686 | } | |
687 | foreach ($pField['options'] as $opId => $opValues) { | |
688 | if (empty($opValues['membership_type_id'])) { | |
689 | continue; | |
690 | } | |
691 | $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; | |
692 | } | |
693 | } | |
694 | } | |
695 | elseif (!empty($this->_membershipBlock['membership_types'])) { | |
696 | $membershipTypeIds = explode(',', $this->_membershipBlock['membership_types']); | |
697 | } | |
698 | ||
699 | if (!empty($membershipTypeIds)) { | |
700 | //set status message if wrong membershipType is included in membershipBlock | |
701 | if (isset($this->_mid) && !$membershipPriceset) { | |
702 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
703 | $this->_mid, | |
704 | 'membership_type_id' | |
705 | ); | |
706 | if (!in_array($membershipTypeID, $membershipTypeIds)) { | |
707 | CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Invalid Membership'), 'error'); | |
708 | } | |
709 | } | |
710 | ||
711 | $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIds); | |
712 | $this->_membershipTypeValues = $membershipTypeValues; | |
713 | $endDate = NULL; | |
714 | ||
715 | // Check if we support auto-renew on this contribution page | |
716 | // FIXME: If any of the payment processors do NOT support recurring you cannot setup an | |
717 | // auto-renew payment even if that processor is not selected. | |
718 | $allowAutoRenewOpt = TRUE; | |
719 | if (is_array($this->_paymentProcessors)) { | |
720 | foreach ($this->_paymentProcessors as $id => $val) { | |
721 | if ($id && !$val['is_recur']) { | |
722 | $allowAutoRenewOpt = FALSE; | |
723 | } | |
724 | } | |
725 | } | |
726 | foreach ($membershipTypeIds as $value) { | |
727 | $memType = $membershipTypeValues[$value]; | |
728 | if ($selectedMembershipTypeID != NULL) { | |
729 | if ($memType['id'] == $selectedMembershipTypeID) { | |
730 | $this->assign('minimum_fee', $memType['minimum_fee'] ?? NULL); | |
731 | $this->assign('membership_name', $memType['name']); | |
e622655c | 732 | if ($cid) { |
4b238552 MW |
733 | $membership = new CRM_Member_DAO_Membership(); |
734 | $membership->contact_id = $cid; | |
735 | $membership->membership_type_id = $memType['id']; | |
736 | if ($membership->find(TRUE)) { | |
737 | $this->assign('renewal_mode', TRUE); | |
738 | $memType['current_membership'] = $membership->end_date; | |
739 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; | |
740 | } | |
741 | } | |
742 | $membershipTypes[] = $memType; | |
743 | } | |
744 | } | |
745 | elseif ($memType['is_active']) { | |
746 | ||
747 | if ($allowAutoRenewOpt) { | |
748 | $javascriptMethod = ['onclick' => "return showHideAutoRenew( this.value );"]; | |
749 | $isAvailableAutoRenew = $this->_membershipBlock['auto_renew'][$value] ?? 1; | |
750 | $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $memType['auto_renew'] * $isAvailableAutoRenew; | |
751 | $allowAutoRenewMembership = TRUE; | |
752 | } | |
753 | else { | |
754 | $javascriptMethod = NULL; | |
755 | $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = 0; | |
756 | } | |
757 | ||
758 | //add membership type. | |
759 | $radio[$memType['id']] = NULL; | |
760 | $radioOptAttrs[$memType['id']] = $javascriptMethod; | |
761 | if ($cid) { | |
762 | $membership = new CRM_Member_DAO_Membership(); | |
763 | $membership->contact_id = $cid; | |
764 | $membership->membership_type_id = $memType['id']; | |
765 | ||
766 | //show current membership, skip pending and cancelled membership records, | |
767 | //because we take first membership record id for renewal | |
768 | $membership->whereAdd('status_id != 5 AND status_id !=6'); | |
769 | ||
770 | if (!is_null($isTest)) { | |
771 | $membership->is_test = $isTest; | |
772 | } | |
773 | ||
774 | //CRM-4297 | |
775 | $membership->orderBy('end_date DESC'); | |
776 | ||
777 | if ($membership->find(TRUE)) { | |
778 | if (!$membership->end_date) { | |
779 | unset($radio[$memType['id']]); | |
780 | unset($radioOptAttrs[$memType['id']]); | |
781 | $this->assign('islifetime', TRUE); | |
782 | continue; | |
783 | } | |
784 | $this->assign('renewal_mode', TRUE); | |
785 | $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; | |
786 | $memType['current_membership'] = $membership->end_date; | |
787 | if (!$endDate) { | |
788 | $endDate = $memType['current_membership']; | |
789 | $this->_defaultMemTypeId = $memType['id']; | |
790 | } | |
791 | if ($memType['current_membership'] < $endDate) { | |
792 | $endDate = $memType['current_membership']; | |
793 | $this->_defaultMemTypeId = $memType['id']; | |
794 | } | |
795 | } | |
796 | } | |
797 | $membershipTypes[] = $memType; | |
798 | } | |
799 | } | |
800 | } | |
801 | ||
802 | $this->assign('membershipBlock', $this->_membershipBlock); | |
e622655c | 803 | $this->assign('showRadio', FALSE); |
4b238552 | 804 | $this->assign('membershipTypes', $membershipTypes); |
4b238552 MW |
805 | $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions)); |
806 | //give preference to user submitted auto_renew value. | |
807 | $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()); | |
808 | $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew); | |
809 | ||
810 | // Assign autorenew option (0:hide,1:optional,2:required) so we can use it in confirmation etc. | |
811 | $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId); | |
812 | //$selectedMembershipTypeID is retrieved as an array for membership priceset if multiple | |
813 | //options for different organisation is selected on the contribution page. | |
814 | if (is_numeric($selectedMembershipTypeID) && isset($membershipTypeValues[$selectedMembershipTypeID]['auto_renew'])) { | |
815 | $this->assign('autoRenewOption', $membershipTypeValues[$selectedMembershipTypeID]['auto_renew']); | |
816 | } | |
817 | else { | |
818 | $this->assign('autoRenewOption', $autoRenewOption); | |
819 | } | |
4b238552 MW |
820 | } |
821 | ||
822 | return $separateMembershipPayment; | |
823 | } | |
824 | ||
6a488035 | 825 | /** |
ab8fe4ef | 826 | * Overwrite action. |
827 | * | |
828 | * Since we are only showing elements in frozen mode no help display needed. | |
6a488035 TO |
829 | * |
830 | * @return int | |
6a488035 | 831 | */ |
00be9182 | 832 | public function getAction() { |
6a488035 TO |
833 | if ($this->_action & CRM_Core_Action::PREVIEW) { |
834 | return CRM_Core_Action::VIEW | CRM_Core_Action::PREVIEW; | |
835 | } | |
836 | else { | |
837 | return CRM_Core_Action::VIEW; | |
838 | } | |
839 | } | |
840 | ||
841 | /** | |
5e4f7f74 | 842 | * Set default values for the form. |
6a488035 | 843 | * |
5e4f7f74 EM |
844 | * Note that in edit/view mode |
845 | * the default values are retrieved from the database | |
6a488035 | 846 | */ |
a13f3d8c TO |
847 | public function setDefaultValues() { |
848 | } | |
6a488035 TO |
849 | |
850 | /** | |
fe482240 | 851 | * Process the form. |
6a488035 TO |
852 | */ |
853 | public function postProcess() { | |
da8d9879 | 854 | $contactID = $this->getContactID(); |
77beddbe | 855 | try { |
856 | $result = $this->processFormSubmission($contactID); | |
857 | } | |
858 | catch (CRM_Core_Exception $e) { | |
859 | $this->bounceOnError($e->getMessage()); | |
860 | } | |
861 | ||
75637f22 | 862 | if (is_array($result) && !empty($result['is_payment_failure'])) { |
77beddbe | 863 | $this->bounceOnError($result['error']->getMessage()); |
cc789d46 | 864 | } |
5fb28746 EM |
865 | // Presumably this is for hooks to access? Not quite clear & perhaps not required. |
866 | $this->set('params', $this->_params); | |
75637f22 | 867 | } |
6a488035 | 868 | |
75637f22 EM |
869 | /** |
870 | * Wrangle financial type ID. | |
871 | * | |
872 | * 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 | |
873 | * Pledges are not relevant to the membership code so that portion will not go onto the membership form. | |
874 | * | |
875 | * Comments from previous refactor indicate doubt as to what was going on. | |
876 | * | |
d1aed9dc | 877 | * @param int $financialTypeID |
75637f22 EM |
878 | * |
879 | * @return null|string | |
880 | */ | |
d1aed9dc MW |
881 | public function wrangleFinancialTypeID($financialTypeID) { |
882 | if (empty($financialTypeID) && !empty($this->_values['pledge_id'])) { | |
883 | $financialTypeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', | |
75637f22 EM |
884 | $this->_values['pledge_id'], |
885 | 'financial_type_id' | |
886 | ); | |
6a488035 | 887 | } |
d1aed9dc | 888 | return $financialTypeID; |
75637f22 | 889 | } |
6a488035 | 890 | |
75637f22 EM |
891 | /** |
892 | * Process the form. | |
893 | * | |
894 | * @param array $premiumParams | |
895 | * @param CRM_Contribute_BAO_Contribution $contribution | |
896 | */ | |
5fb28746 | 897 | protected function postProcessPremium($premiumParams, $contribution) { |
75637f22 EM |
898 | $hour = $minute = $second = 0; |
899 | // assigning Premium information to receipt tpl | |
9c1bc317 | 900 | $selectProduct = $premiumParams['selectProduct'] ?? NULL; |
75637f22 EM |
901 | if ($selectProduct && |
902 | $selectProduct != 'no_thanks' | |
903 | ) { | |
904 | $startDate = $endDate = ""; | |
905 | $this->assign('selectPremium', TRUE); | |
906 | $productDAO = new CRM_Contribute_DAO_Product(); | |
907 | $productDAO->id = $selectProduct; | |
908 | $productDAO->find(TRUE); | |
909 | $this->assign('product_name', $productDAO->name); | |
910 | $this->assign('price', $productDAO->price); | |
911 | $this->assign('sku', $productDAO->sku); | |
cbbbf0ef | 912 | $this->assign('option', $premiumParams['options_' . $premiumParams['selectProduct']] ?? NULL); |
6a488035 | 913 | |
75637f22 | 914 | $periodType = $productDAO->period_type; |
6a488035 | 915 | |
75637f22 EM |
916 | if ($periodType) { |
917 | $fixed_period_start_day = $productDAO->fixed_period_start_day; | |
918 | $duration_unit = $productDAO->duration_unit; | |
919 | $duration_interval = $productDAO->duration_interval; | |
fb8d25da | 920 | if ($periodType === 'rolling') { |
75637f22 EM |
921 | $startDate = date('Y-m-d'); |
922 | } | |
fb8d25da | 923 | elseif ($periodType === 'fixed') { |
75637f22 EM |
924 | if ($fixed_period_start_day) { |
925 | $date = explode('-', date('Y-m-d')); | |
926 | $month = substr($fixed_period_start_day, 0, strlen($fixed_period_start_day) - 2); | |
927 | $day = substr($fixed_period_start_day, -2) . "<br/>"; | |
928 | $year = $date[0]; | |
929 | $startDate = $year . '-' . $month . '-' . $day; | |
930 | } | |
931 | else { | |
932 | $startDate = date('Y-m-d'); | |
933 | } | |
6a488035 | 934 | } |
6a488035 | 935 | |
75637f22 EM |
936 | $date = explode('-', $startDate); |
937 | $year = $date[0]; | |
938 | $month = $date[1]; | |
939 | $day = $date[2]; | |
6a488035 | 940 | |
75637f22 EM |
941 | switch ($duration_unit) { |
942 | case 'year': | |
fb8d25da | 943 | $year += $duration_interval; |
75637f22 | 944 | break; |
6a488035 | 945 | |
75637f22 | 946 | case 'month': |
fb8d25da | 947 | $month += $duration_interval; |
75637f22 | 948 | break; |
6a488035 | 949 | |
75637f22 | 950 | case 'day': |
fb8d25da | 951 | $day += $duration_interval; |
75637f22 | 952 | break; |
6a488035 | 953 | |
75637f22 | 954 | case 'week': |
fb8d25da | 955 | $day += ($duration_interval * 7); |
6a488035 | 956 | } |
75637f22 EM |
957 | $endDate = date('Y-m-d H:i:s', mktime($hour, $minute, $second, $month, $day, $year)); |
958 | $this->assign('start_date', $startDate); | |
959 | $this->assign('end_date', $endDate); | |
6a488035 TO |
960 | } |
961 | ||
75637f22 EM |
962 | $dao = new CRM_Contribute_DAO_Premium(); |
963 | $dao->entity_table = 'civicrm_contribution_page'; | |
964 | $dao->entity_id = $this->_id; | |
965 | $dao->find(TRUE); | |
966 | $this->assign('contact_phone', $dao->premiums_contact_phone); | |
967 | $this->assign('contact_email', $dao->premiums_contact_email); | |
968 | ||
969 | //create Premium record | |
be2fb01f | 970 | $params = [ |
75637f22 EM |
971 | 'product_id' => $premiumParams['selectProduct'], |
972 | 'contribution_id' => $contribution->id, | |
6b409353 | 973 | 'product_option' => $premiumParams['options_' . $premiumParams['selectProduct']] ?? NULL, |
75637f22 EM |
974 | 'quantity' => 1, |
975 | 'start_date' => CRM_Utils_Date::customFormat($startDate, '%Y%m%d'), | |
976 | 'end_date' => CRM_Utils_Date::customFormat($endDate, '%Y%m%d'), | |
be2fb01f | 977 | ]; |
75637f22 EM |
978 | if (!empty($premiumParams['selectProduct'])) { |
979 | $daoPremiumsProduct = new CRM_Contribute_DAO_PremiumsProduct(); | |
980 | $daoPremiumsProduct->product_id = $premiumParams['selectProduct']; | |
981 | $daoPremiumsProduct->premiums_id = $dao->id; | |
982 | $daoPremiumsProduct->find(TRUE); | |
983 | $params['financial_type_id'] = $daoPremiumsProduct->financial_type_id; | |
6a488035 | 984 | } |
75637f22 EM |
985 | //Fixed For CRM-3901 |
986 | $daoContrProd = new CRM_Contribute_DAO_ContributionProduct(); | |
987 | $daoContrProd->contribution_id = $contribution->id; | |
988 | if ($daoContrProd->find(TRUE)) { | |
989 | $params['id'] = $daoContrProd->id; | |
6a488035 | 990 | } |
6a488035 | 991 | |
75637f22 EM |
992 | CRM_Contribute_BAO_Contribution::addPremium($params); |
993 | if ($productDAO->cost && !empty($params['financial_type_id'])) { | |
be2fb01f | 994 | $trxnParams = [ |
75637f22 EM |
995 | 'cost' => $productDAO->cost, |
996 | 'currency' => $productDAO->currency, | |
997 | 'financial_type_id' => $params['financial_type_id'], | |
998 | 'contributionId' => $contribution->id, | |
be2fb01f | 999 | ]; |
75637f22 | 1000 | CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($trxnParams); |
6a488035 TO |
1001 | } |
1002 | } | |
fb8d25da | 1003 | elseif ($selectProduct === 'no_thanks') { |
75637f22 EM |
1004 | //Fixed For CRM-3901 |
1005 | $daoContrProd = new CRM_Contribute_DAO_ContributionProduct(); | |
1006 | $daoContrProd->contribution_id = $contribution->id; | |
1007 | if ($daoContrProd->find(TRUE)) { | |
1008 | $daoContrProd->delete(); | |
6a488035 TO |
1009 | } |
1010 | } | |
75637f22 | 1011 | } |
6a488035 | 1012 | |
75637f22 EM |
1013 | /** |
1014 | * Process the contribution. | |
1015 | * | |
75637f22 EM |
1016 | * @param array $params |
1017 | * @param array $result | |
f6261e9d | 1018 | * @param array $contributionParams |
1019 | * Parameters to be passed to contribution create action. | |
3febe800 | 1020 | * This differs from params in that we are currently adding params to it and 1) ensuring they are being |
1021 | * passed consistently & 2) documenting them here. | |
1022 | * - contact_id | |
1023 | * - line_item | |
1024 | * - is_test | |
1025 | * - campaign_id | |
1026 | * - contribution_page_id | |
1027 | * - source | |
1028 | * - payment_type_id | |
1029 | * - thankyou_date (not all forms will set this) | |
1030 | * | |
75637f22 | 1031 | * @param CRM_Financial_DAO_FinancialType $financialType |
75637f22 EM |
1032 | * @param int $billingLocationID |
1033 | * ID of billing location type. | |
10490499 | 1034 | * @param bool $isRecur |
1035 | * Is this recurring? | |
75637f22 EM |
1036 | * |
1037 | * @return \CRM_Contribute_DAO_Contribution | |
12fde6ae | 1038 | * |
1039 | * @throws \CRM_Core_Exception | |
1040 | * @throws \CiviCRM_API3_Exception | |
a281a8ea EM |
1041 | * @todo - this code was previously shared with the backoffice form - some parts of this |
1042 | * function may relate to that form, not this one. | |
1043 | * | |
75637f22 | 1044 | */ |
a281a8ea | 1045 | protected function processFormContribution( |
75637f22 EM |
1046 | $params, |
1047 | $result, | |
f6261e9d | 1048 | $contributionParams, |
75637f22 | 1049 | $financialType, |
449f4c90 | 1050 | $billingLocationID, |
1051 | $isRecur | |
75637f22 | 1052 | ) { |
a281a8ea | 1053 | $form = $this; |
75637f22 | 1054 | $transaction = new CRM_Core_Transaction(); |
f6261e9d | 1055 | $contactID = $contributionParams['contact_id']; |
1056 | ||
75637f22 | 1057 | $isEmailReceipt = !empty($form->_values['is_email_receipt']); |
91768280 | 1058 | $isSeparateMembershipPayment = !empty($params['separate_membership_payment']); |
cbbbf0ef | 1059 | $pledgeID = !empty($params['pledge_id']) ? $params['pledge_id'] : $form->_values['pledge_id'] ?? NULL; |
75637f22 | 1060 | if (!$isSeparateMembershipPayment && !empty($form->_values['pledge_block_id']) && |
09108d7d | 1061 | (!empty($params['is_pledge']) || $pledgeID)) { |
75637f22 | 1062 | $isPledge = TRUE; |
6a488035 TO |
1063 | } |
1064 | else { | |
75637f22 | 1065 | $isPledge = FALSE; |
6a488035 TO |
1066 | } |
1067 | ||
75637f22 EM |
1068 | // add these values for the recurringContrib function ,CRM-10188 |
1069 | $params['financial_type_id'] = $financialType->id; | |
6a488035 | 1070 | |
3febe800 | 1071 | $contributionParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $billingLocationID); |
6a488035 | 1072 | |
75637f22 EM |
1073 | //@todo - this is being set from the form to resolve CRM-10188 - an |
1074 | // eNotice caused by it not being set @ the front end | |
1075 | // however, we then get it being over-written with null for backend contributions | |
1076 | // a better fix would be to set the values in the respective forms rather than require | |
1077 | // a function being shared by two forms to deal with their respective values | |
1078 | // moving it to the BAO & not taking the $form as a param would make sense here. | |
1079 | if (!isset($params['is_email_receipt']) && $isEmailReceipt) { | |
1080 | $params['is_email_receipt'] = $isEmailReceipt; | |
51e89def | 1081 | } |
449f4c90 | 1082 | $params['is_recur'] = $isRecur; |
9c1bc317 | 1083 | $params['payment_instrument_id'] = $contributionParams['payment_instrument_id'] ?? NULL; |
449f4c90 | 1084 | $recurringContributionID = self::processRecurringContribution($form, $params, $contactID, $financialType); |
51e89def | 1085 | |
75637f22 | 1086 | $now = date('YmdHis'); |
9c1bc317 | 1087 | $receiptDate = $params['receipt_date'] ?? NULL; |
75637f22 EM |
1088 | if ($isEmailReceipt) { |
1089 | $receiptDate = $now; | |
6a488035 TO |
1090 | } |
1091 | ||
75637f22 | 1092 | if (isset($params['amount'])) { |
f6261e9d | 1093 | $contributionParams = array_merge(self::getContributionParams( |
0ede7391 | 1094 | $params, $financialType->id, |
f6261e9d | 1095 | $result, $receiptDate, |
1096 | $recurringContributionID), $contributionParams | |
75637f22 | 1097 | ); |
a281a8ea | 1098 | $contributionParams['non_deductible_amount'] = self::getNonDeductibleAmount($params, $financialType, TRUE, $form); |
83644f47 | 1099 | $contributionParams['skipCleanMoney'] = TRUE; |
1100 | // @todo this is the wrong place for this - it should be done as close to form submission | |
1101 | // as possible | |
1102 | $contributionParams['total_amount'] = $params['amount']; | |
77beddbe | 1103 | |
f6261e9d | 1104 | $contribution = CRM_Contribute_BAO_Contribution::add($contributionParams); |
6a488035 | 1105 | |
fe4d1285 | 1106 | if (Civi::settings()->get('invoicing')) { |
be2fb01f | 1107 | $dataArray = []; |
f6261e9d | 1108 | // @todo - interrogate the line items passed in on the params array. |
1109 | // No reason to assume line items will be set on the form. | |
75637f22 EM |
1110 | foreach ($form->_lineItem as $lineItemKey => $lineItemValue) { |
1111 | foreach ($lineItemValue as $key => $value) { | |
1112 | if (isset($value['tax_amount']) && isset($value['tax_rate'])) { | |
1113 | if (isset($dataArray[$value['tax_rate']])) { | |
2cfe2cf4 | 1114 | $dataArray[$value['tax_rate']] = $dataArray[$value['tax_rate']] + $value['tax_amount']; |
75637f22 EM |
1115 | } |
1116 | else { | |
2cfe2cf4 | 1117 | $dataArray[$value['tax_rate']] = $value['tax_amount']; |
75637f22 EM |
1118 | } |
1119 | } | |
6a488035 TO |
1120 | } |
1121 | } | |
75637f22 EM |
1122 | $smarty = CRM_Core_Smarty::singleton(); |
1123 | $smarty->assign('dataArray', $dataArray); | |
79528215 | 1124 | $smarty->assign('totalTaxAmount', $params['tax_amount'] ?? NULL); |
75637f22 | 1125 | } |
6a488035 | 1126 | |
75637f22 EM |
1127 | // lets store it in the form variable so postProcess hook can get to this and use it |
1128 | $form->_contributionID = $contribution->id; | |
1129 | } | |
6a488035 | 1130 | |
7a13735b | 1131 | // process soft credit / pcp params first |
1132 | CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($params, $form); | |
75637f22 | 1133 | |
7a13735b | 1134 | //CRM-13981, processing honor contact into soft-credit contribution |
1135 | CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); | |
75637f22 | 1136 | |
75637f22 | 1137 | if ($isPledge) { |
356bfcaf | 1138 | $form = self::handlePledge($form, $params, $contributionParams, $pledgeID, $contribution, $isEmailReceipt); |
6a488035 | 1139 | } |
6a488035 | 1140 | |
a281a8ea | 1141 | if ($contribution) { |
09108d7d | 1142 | CRM_Core_BAO_CustomValueTable::postProcess($params, |
75637f22 EM |
1143 | 'civicrm_contribution', |
1144 | $contribution->id, | |
1145 | 'Contribution' | |
1146 | ); | |
1147 | } | |
75637f22 EM |
1148 | // Save note |
1149 | if ($contribution && !empty($params['contribution_note'])) { | |
be2fb01f | 1150 | $noteParams = [ |
75637f22 EM |
1151 | 'entity_table' => 'civicrm_contribution', |
1152 | 'note' => $params['contribution_note'], | |
1153 | 'entity_id' => $contribution->id, | |
1154 | 'contact_id' => $contribution->contact_id, | |
be2fb01f | 1155 | ]; |
75637f22 | 1156 | |
be2fb01f | 1157 | CRM_Core_BAO_Note::add($noteParams, []); |
cc789d46 | 1158 | } |
cc789d46 | 1159 | |
75637f22 EM |
1160 | //create contribution activity w/ individual and target |
1161 | //activity w/ organisation contact id when onbelf, CRM-4027 | |
d33f8fc4 | 1162 | $actParams = []; |
75637f22 | 1163 | $targetContactID = NULL; |
d33f8fc4 JP |
1164 | if (!empty($params['onbehalf_contact_id'])) { |
1165 | $actParams = [ | |
1166 | 'source_contact_id' => $params['onbehalf_contact_id'], | |
1167 | 'on_behalf' => TRUE, | |
1168 | ]; | |
75637f22 | 1169 | $targetContactID = $contribution->contact_id; |
75637f22 EM |
1170 | } |
1171 | ||
1172 | // create an activity record | |
1173 | if ($contribution) { | |
bf302610 | 1174 | CRM_Activity_BAO_Activity::addActivity($contribution, 'Contribution', $targetContactID, $actParams); |
75637f22 EM |
1175 | } |
1176 | ||
1177 | $transaction->commit(); | |
75637f22 | 1178 | return $contribution; |
6a488035 TO |
1179 | } |
1180 | ||
1181 | /** | |
75637f22 | 1182 | * Create the recurring contribution record. |
6a488035 | 1183 | * |
75637f22 EM |
1184 | * @param CRM_Core_Form $form |
1185 | * @param array $params | |
1186 | * @param int $contactID | |
1187 | * @param string $contributionType | |
75637f22 | 1188 | * |
449f4c90 | 1189 | * @return int|null |
6a488035 | 1190 | */ |
9b1f9df3 | 1191 | public static function processRecurringContribution($form, $params, $contactID, $contributionType) { |
cde484fd | 1192 | |
449f4c90 | 1193 | if (empty($params['is_recur'])) { |
75637f22 EM |
1194 | return NULL; |
1195 | } | |
6a488035 | 1196 | |
be2fb01f | 1197 | $recurParams = ['contact_id' => $contactID]; |
9c1bc317 CW |
1198 | $recurParams['amount'] = $params['amount'] ?? NULL; |
1199 | $recurParams['auto_renew'] = $params['auto_renew'] ?? NULL; | |
1200 | $recurParams['frequency_unit'] = $params['frequency_unit'] ?? NULL; | |
1201 | $recurParams['frequency_interval'] = $params['frequency_interval'] ?? NULL; | |
1202 | $recurParams['installments'] = $params['installments'] ?? NULL; | |
1203 | $recurParams['financial_type_id'] = $params['financial_type_id'] ?? NULL; | |
1204 | $recurParams['currency'] = $params['currency'] ?? NULL; | |
f69a9ac3 | 1205 | $recurParams['payment_instrument_id'] = $params['payment_instrument_id']; |
6a488035 | 1206 | |
75637f22 EM |
1207 | // CRM-14354: For an auto-renewing membership with an additional contribution, |
1208 | // if separate payments is not enabled, make sure only the membership fee recurs | |
1209 | if (!empty($form->_membershipBlock) | |
1210 | && $form->_membershipBlock['is_separate_payment'] === '0' | |
1211 | && isset($params['selectMembership']) | |
1212 | && $form->_values['is_allow_other_amount'] == '1' | |
1213 | // CRM-16331 | |
1214 | && !empty($form->_membershipTypeValues) | |
1215 | && !empty($form->_membershipTypeValues[$params['selectMembership']]['minimum_fee']) | |
1216 | ) { | |
1217 | $recurParams['amount'] = $form->_membershipTypeValues[$params['selectMembership']]['minimum_fee']; | |
1218 | } | |
6a488035 | 1219 | |
75637f22 EM |
1220 | $recurParams['is_test'] = 0; |
1221 | if (($form->_action & CRM_Core_Action::PREVIEW) || | |
1222 | (isset($form->_mode) && ($form->_mode == 'test')) | |
1223 | ) { | |
1224 | $recurParams['is_test'] = 1; | |
1225 | } | |
6a488035 | 1226 | |
75637f22 EM |
1227 | $recurParams['start_date'] = $recurParams['create_date'] = $recurParams['modified_date'] = date('YmdHis'); |
1228 | if (!empty($params['receive_date'])) { | |
12e53254 | 1229 | $recurParams['start_date'] = date('YmdHis', strtotime($params['receive_date'])); |
75637f22 | 1230 | } |
9c1bc317 | 1231 | $recurParams['invoice_id'] = $params['invoiceID'] ?? NULL; |
75637f22 | 1232 | $recurParams['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); |
9c1bc317 | 1233 | $recurParams['payment_processor_id'] = $params['payment_processor_id'] ?? NULL; |
eba1a1d7 | 1234 | $recurParams['is_email_receipt'] = (bool) ($params['is_email_receipt'] ?? FALSE); |
75637f22 EM |
1235 | // we need to add a unique trxn_id to avoid a unique key error |
1236 | // in paypal IPN we reset this when paypal sends us the real trxn id, CRM-2991 | |
cbbbf0ef | 1237 | $recurParams['trxn_id'] = $params['trxn_id'] ?? $params['invoiceID']; |
75637f22 | 1238 | $recurParams['financial_type_id'] = $contributionType->id; |
6a488035 | 1239 | |
cbbbf0ef | 1240 | $campaignId = $params['campaign_id'] ?? $form->_values['campaign_id'] ?? NULL; |
75637f22 | 1241 | $recurParams['campaign_id'] = $campaignId; |
75637f22 EM |
1242 | $recurring = CRM_Contribute_BAO_ContributionRecur::add($recurParams); |
1243 | if (is_a($recurring, 'CRM_Core_Error')) { | |
1244 | CRM_Core_Error::displaySessionError($recurring); | |
1245 | $urlString = 'civicrm/contribute/transact'; | |
1246 | $urlParams = '_qf_Main_display=true'; | |
1247 | if (get_class($form) == 'CRM_Contribute_Form_Contribution') { | |
1248 | $urlString = 'civicrm/contact/view/contribution'; | |
1249 | $urlParams = "action=add&cid={$form->_contactID}"; | |
1250 | if ($form->_mode) { | |
1251 | $urlParams .= "&mode={$form->_mode}"; | |
1252 | } | |
6a488035 | 1253 | } |
75637f22 | 1254 | CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams)); |
6a488035 | 1255 | } |
66c8ea62 | 1256 | $form->_params['contributionRecurID'] = $recurring->id; |
75637f22 EM |
1257 | |
1258 | return $recurring->id; | |
6a488035 TO |
1259 | } |
1260 | ||
1261 | /** | |
75637f22 | 1262 | * Add on behalf of organization and it's location. |
cd6994fc | 1263 | * |
75637f22 EM |
1264 | * This situation occurs when on behalf of is enabled for the contribution page and the person |
1265 | * signing up does so on behalf of an organization. | |
a1a94e61 | 1266 | * |
75637f22 EM |
1267 | * @param array $behalfOrganization |
1268 | * array of organization info. | |
1269 | * @param int $contactID | |
1270 | * individual contact id. One. | |
1271 | * who is doing the process of signup / contribution. | |
9fc4e1d9 | 1272 | * |
75637f22 EM |
1273 | * @param array $values |
1274 | * form values array. | |
1275 | * @param array $params | |
1276 | * @param array $fields | |
1277 | * Array of fields from the onbehalf profile relevant to the organization. | |
6a488035 | 1278 | */ |
75637f22 | 1279 | public static function processOnBehalfOrganization(&$behalfOrganization, &$contactID, &$values, &$params, $fields = NULL) { |
33260076 | 1280 | $isNotCurrentEmployer = FALSE; |
be2fb01f | 1281 | $dupeIDs = []; |
75637f22 | 1282 | $orgID = NULL; |
6cc679d2 | 1283 | if (!empty($behalfOrganization['organization_id'])) { |
75637f22 EM |
1284 | $orgID = $behalfOrganization['organization_id']; |
1285 | unset($behalfOrganization['organization_id']); | |
33260076 | 1286 | } |
1287 | // create employer relationship with $contactID only when new organization is there | |
1288 | // else retain the existing relationship | |
1289 | else { | |
33260076 | 1290 | $isNotCurrentEmployer = TRUE; |
bb06e9ed | 1291 | } |
6a488035 | 1292 | |
75637f22 EM |
1293 | if (!$orgID) { |
1294 | // check if matching organization contact exists | |
be2fb01f | 1295 | $dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts($behalfOrganization, 'Organization', 'Unsupervised', [], FALSE); |
6a488035 | 1296 | |
75637f22 EM |
1297 | // CRM-6243 says to pick the first org even if more than one match |
1298 | if (count($dupeIDs) >= 1) { | |
75c07fde | 1299 | $behalfOrganization['contact_id'] = $orgID = $dupeIDs[0]; |
75637f22 EM |
1300 | // don't allow name edit |
1301 | unset($behalfOrganization['organization_name']); | |
6a488035 TO |
1302 | } |
1303 | } | |
1304 | else { | |
75637f22 EM |
1305 | // if found permissioned related organization, allow location edit |
1306 | $behalfOrganization['contact_id'] = $orgID; | |
1307 | // don't allow name edit | |
1308 | unset($behalfOrganization['organization_name']); | |
6a488035 TO |
1309 | } |
1310 | ||
75637f22 EM |
1311 | // handling for image url |
1312 | if (!empty($behalfOrganization['image_URL'])) { | |
1313 | CRM_Contact_BAO_Contact::processImageParams($behalfOrganization); | |
6a488035 TO |
1314 | } |
1315 | ||
75637f22 | 1316 | // create organization, add location |
e4addeb2 | 1317 | $behalfOrganization['contact_type'] = 'Organization'; |
75637f22 EM |
1318 | $orgID = CRM_Contact_BAO_Contact::createProfileContact($behalfOrganization, $fields, $orgID, |
1319 | NULL, NULL, 'Organization' | |
1320 | ); | |
1321 | // create relationship | |
33260076 | 1322 | if ($isNotCurrentEmployer) { |
e4addeb2 JG |
1323 | try { |
1324 | \Civi\Api4\Relationship::create(FALSE) | |
1325 | ->addValue('contact_id_a', $contactID) | |
1326 | ->addValue('contact_id_b', $orgID) | |
1327 | ->addValue('relationship_type_id', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b')) | |
1328 | ->addValue('is_permission_a_b:name', 'View and update') | |
1329 | ->execute(); | |
1330 | } | |
1331 | catch (CRM_Core_Exception $e) { | |
1332 | // Ignore if duplicate relationship. | |
1333 | if ($e->getMessage() !== 'Duplicate Relationship') { | |
1334 | throw $e; | |
1335 | } | |
1336 | } | |
33260076 | 1337 | } |
1f1740fe | 1338 | |
75637f22 EM |
1339 | // if multiple match - send a duplicate alert |
1340 | if ($dupeIDs && (count($dupeIDs) > 1)) { | |
1341 | $values['onbehalf_dupe_alert'] = 1; | |
1342 | // required for IPN | |
1343 | $params['onbehalf_dupe_alert'] = 1; | |
6a488035 | 1344 | } |
cde484fd | 1345 | |
75637f22 EM |
1346 | // make sure organization-contact-id is considered for recording |
1347 | // contribution/membership etc.. | |
1348 | if ($contactID != $orgID) { | |
1349 | // take a note of contact-id, so we can send the | |
1350 | // receipt to individual contact as well. | |
6a488035 | 1351 | |
75637f22 EM |
1352 | // required for mailing/template display ..etc |
1353 | $values['related_contact'] = $contactID; | |
6a488035 | 1354 | |
0462a5b2 | 1355 | //CRM-19172: Create CMS user for individual on whose behalf organization is doing contribution |
1356 | $params['onbehalf_contact_id'] = $contactID; | |
1357 | ||
75637f22 EM |
1358 | //make this employee of relationship as current |
1359 | //employer / employee relationship, CRM-3532 | |
33260076 | 1360 | if ($isNotCurrentEmployer && |
75637f22 EM |
1361 | ($orgID != CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id')) |
1362 | ) { | |
33260076 | 1363 | $isNotCurrentEmployer = FALSE; |
6a488035 | 1364 | } |
6a488035 | 1365 | |
33260076 | 1366 | if (!$isNotCurrentEmployer && $orgID) { |
75637f22 EM |
1367 | //build current employer params |
1368 | $currentEmpParams[$contactID] = $orgID; | |
1369 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); | |
6a488035 | 1370 | } |
6a488035 | 1371 | |
75637f22 EM |
1372 | // contribution / signup will be done using this |
1373 | // organization id. | |
1374 | $contactID = $orgID; | |
6a488035 | 1375 | } |
75637f22 EM |
1376 | } |
1377 | ||
6a488035 | 1378 | /** |
5e4f7f74 EM |
1379 | * Function used to se pcp related defaults / params. |
1380 | * | |
1381 | * This is used by contribution and also event PCPs | |
6a488035 | 1382 | * |
014c4014 TO |
1383 | * @param CRM_Core_Form $page |
1384 | * Form object. | |
1385 | * @param array $params | |
6a488035 | 1386 | * |
cd6994fc | 1387 | * @return array |
6a488035 | 1388 | */ |
26f76772 | 1389 | public static function processPcp(&$page, $params): array { |
547206bc | 1390 | $params['pcp_made_through_id'] = $page->_pcpId; |
6a488035 | 1391 | $page->assign('pcpBlock', TRUE); |
8cc574cf | 1392 | if (!empty($params['pcp_display_in_roll']) && empty($params['pcp_roll_nickname'])) { |
6a488035 TO |
1393 | $params['pcp_roll_nickname'] = ts('Anonymous'); |
1394 | $params['pcp_is_anonymous'] = 1; | |
1395 | } | |
1396 | else { | |
1397 | $params['pcp_is_anonymous'] = 0; | |
1398 | } | |
be2fb01f | 1399 | foreach ([ |
1330f57a SL |
1400 | 'pcp_display_in_roll', |
1401 | 'pcp_is_anonymous', | |
1402 | 'pcp_roll_nickname', | |
1403 | 'pcp_personal_note', | |
1404 | ] as $val) { | |
a7488080 | 1405 | if (!empty($params[$val])) { |
6a488035 TO |
1406 | $page->assign($val, $params[$val]); |
1407 | } | |
1408 | } | |
1409 | ||
1410 | return $params; | |
1411 | } | |
705b4205 EM |
1412 | |
1413 | /** | |
5e4f7f74 EM |
1414 | * Process membership. |
1415 | * | |
5624f515 | 1416 | * @param array $membershipParams |
014c4014 | 1417 | * @param int $contactID |
5624f515 EM |
1418 | * @param array $customFieldsFormatted |
1419 | * @param array $fieldTypes | |
1420 | * @param array $premiumParams | |
014c4014 TO |
1421 | * @param array $membershipLineItems |
1422 | * Line items specifically relating to memberships. | |
705b4205 | 1423 | */ |
4b6c8c1e | 1424 | protected function processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, |
26f76772 | 1425 | $membershipLineItems): void { |
8bc79dfc | 1426 | |
4b6c8c1e | 1427 | $membershipTypeIDs = (array) $membershipParams['selectMembership']; |
1428 | $membershipTypes = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIDs); | |
be2fb01f | 1429 | $membershipType = empty($membershipTypes) ? [] : reset($membershipTypes); |
5624f515 | 1430 | |
cbbbf0ef | 1431 | $this->assign('membership_name', $membershipType['name']); |
9c1bc317 | 1432 | $this->_values['membership_name'] = $membershipType['name'] ?? NULL; |
7c113627 | 1433 | |
4b6c8c1e | 1434 | $isPaidMembership = FALSE; |
1435 | if ($this->_amount >= 0.0 && isset($membershipParams['amount'])) { | |
1436 | //amount must be greater than zero for | |
1437 | //adding contribution record to contribution table. | |
1438 | //this condition arises when separate membership payment is | |
1439 | //enabled and contribution amount is not selected. fix for CRM-3010 | |
1440 | $isPaidMembership = TRUE; | |
1441 | } | |
1442 | $isProcessSeparateMembershipTransaction = $this->isSeparateMembershipTransaction($this->_id, $this->_values['amount_block_is_active']); | |
7c113627 | 1443 | |
4b6c8c1e | 1444 | if ($this->_values['amount_block_is_active']) { |
1445 | $financialTypeID = $this->_values['financial_type_id']; | |
705b4205 | 1446 | } |
4b6c8c1e | 1447 | else { |
cbbbf0ef | 1448 | $financialTypeID = $membershipType['financial_type_id'] ?? $membershipParams['financial_type_id'] ?? NULL; |
705b4205 | 1449 | } |
4b6c8c1e | 1450 | |
f3acfdd9 | 1451 | if (!empty($this->_params['membership_source'])) { |
4b6c8c1e | 1452 | $membershipParams['contribution_source'] = $this->_params['membership_source']; |
1453 | } | |
1454 | ||
1455 | $this->postProcessMembership($membershipParams, $contactID, | |
1456 | $this, $premiumParams, $customFieldsFormatted, $fieldTypes, $membershipType, $membershipTypeIDs, $isPaidMembership, $this->_membershipId, $isProcessSeparateMembershipTransaction, $financialTypeID, | |
5b9d3ce8 | 1457 | $membershipLineItems); |
4b6c8c1e | 1458 | |
1459 | $this->assign('membership_assign', TRUE); | |
1460 | $this->set('membershipTypeID', $membershipParams['selectMembership']); | |
705b4205 | 1461 | } |
fb49fa48 | 1462 | |
8bc79dfc | 1463 | /** |
668ddfc2 EM |
1464 | * Process the Memberships. |
1465 | * | |
1466 | * @param array $membershipParams | |
1467 | * Array of membership fields. | |
1468 | * @param int $contactID | |
1469 | * Contact id. | |
1470 | * @param CRM_Contribute_Form_Contribution_Confirm $form | |
1471 | * Confirmation form object. | |
1472 | * | |
1473 | * @param array $premiumParams | |
1474 | * @param null $customFieldsFormatted | |
1475 | * @param null $includeFieldTypes | |
1476 | * | |
1477 | * @param array $membershipDetails | |
1478 | * | |
1479 | * @param array $membershipTypeIDs | |
1480 | * | |
1481 | * @param bool $isPaidMembership | |
1482 | * @param array $membershipID | |
1483 | * | |
1484 | * @param bool $isProcessSeparateMembershipTransaction | |
1485 | * | |
1486 | * @param int $financialTypeID | |
f2bca231 | 1487 | * @param array $unprocessedLineItems |
1488 | * Line items for payment options chosen on the form. | |
668ddfc2 EM |
1489 | * |
1490 | * @throws \CRM_Core_Exception | |
5b9d3ce8 MWMC |
1491 | * @throws \CiviCRM_API3_Exception |
1492 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
668ddfc2 EM |
1493 | */ |
1494 | protected function postProcessMembership( | |
1495 | $membershipParams, $contactID, &$form, $premiumParams, | |
ff708ff0 | 1496 | $customFieldsFormatted, $includeFieldTypes, $membershipDetails, $membershipTypeIDs, $isPaidMembership, $membershipID, |
5b9d3ce8 | 1497 | $isProcessSeparateMembershipTransaction, $financialTypeID, $unprocessedLineItems) { |
f2bca231 | 1498 | |
82583880 | 1499 | $membershipContribution = NULL; |
cbbbf0ef | 1500 | $isTest = $membershipParams['is_test'] ?? FALSE; |
be2fb01f | 1501 | $errors = $paymentResults = []; |
858f7096 | 1502 | $form->_values['isMembership'] = TRUE; |
cbbbf0ef | 1503 | $isRecurForFirstTransaction = $form->_params['is_recur'] ?? $membershipParams['is_recur'] ?? NULL; |
f2bca231 | 1504 | |
858f7096 | 1505 | $totalAmount = $membershipParams['amount']; |
449f4c90 | 1506 | |
668ddfc2 EM |
1507 | if ($isPaidMembership) { |
1508 | if ($isProcessSeparateMembershipTransaction) { | |
1509 | // If we have 2 transactions only one can use the invoice id. | |
1510 | $membershipParams['invoiceID'] .= '-2'; | |
449f4c90 | 1511 | if (!empty($membershipParams['auto_renew'])) { |
1512 | $isRecurForFirstTransaction = FALSE; | |
1513 | } | |
668ddfc2 EM |
1514 | } |
1515 | ||
417c6834 | 1516 | if (!$isProcessSeparateMembershipTransaction) { |
f2bca231 | 1517 | // Skip line items in the contribution processing transaction. |
1518 | // We will create them with the membership for proper linking. | |
417c6834 | 1519 | $membershipParams['skipLineItem'] = 1; |
1520 | } | |
8e8d287f | 1521 | else { |
1522 | $membershipParams['total_amount'] = $totalAmount; | |
f2bca231 | 1523 | $membershipParams['skipLineItem'] = 0; |
8e8d287f | 1524 | CRM_Price_BAO_LineItem::getLineItemArray($membershipParams); |
1525 | ||
1526 | } | |
1a3545fa | 1527 | $paymentResult = $form->processConfirm( |
f2bca231 | 1528 | $membershipParams, |
5fb28746 | 1529 | $contactID, |
668ddfc2 | 1530 | $financialTypeID, |
449f4c90 | 1531 | $isTest, |
1532 | $isRecurForFirstTransaction | |
668ddfc2 | 1533 | ); |
82583880 | 1534 | if (!empty($paymentResult['contribution'])) { |
be2fb01f | 1535 | $paymentResults[] = ['contribution_id' => $paymentResult['contribution']->id, 'result' => $paymentResult]; |
5fb28746 | 1536 | $this->postProcessPremium($premiumParams, $paymentResult['contribution']); |
668ddfc2 EM |
1537 | //note that this will be over-written if we are using a separate membership transaction. Otherwise there is only one |
1538 | $membershipContribution = $paymentResult['contribution']; | |
1539 | // Save the contribution ID so that I can be used in email receipts | |
1540 | // For example, if you need to generate a tax receipt for the donation only. | |
1541 | $form->_values['contribution_other_id'] = $membershipContribution->id; | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | if ($isProcessSeparateMembershipTransaction) { | |
1546 | try { | |
f2bca231 | 1547 | $form->_lineItem = $unprocessedLineItems; |
668ddfc2 EM |
1548 | if (empty($form->_params['auto_renew']) && !empty($membershipParams['is_recur'])) { |
1549 | unset($membershipParams['is_recur']); | |
1550 | } | |
fb8d25da | 1551 | [$membershipContribution, $secondPaymentResult] = $this->processSecondaryFinancialTransaction($contactID, $form, array_merge($membershipParams, ['skipLineItem' => 1]), |
cbbbf0ef | 1552 | $isTest, $unprocessedLineItems, $membershipDetails['minimum_fee'] ?? 0, $membershipDetails['financial_type_id'] ?? NULL); |
be2fb01f | 1553 | $paymentResults[] = ['contribution_id' => $membershipContribution->id, 'result' => $secondPaymentResult]; |
d275cdb2 | 1554 | $totalAmount = $membershipContribution->total_amount; |
668ddfc2 EM |
1555 | } |
1556 | catch (CRM_Core_Exception $e) { | |
1557 | $errors[2] = $e->getMessage(); | |
1558 | $membershipContribution = NULL; | |
1559 | } | |
1560 | } | |
1561 | ||
1562 | $membership = NULL; | |
1563 | if (!empty($membershipContribution) && !is_a($membershipContribution, 'CRM_Core_Error')) { | |
1564 | $membershipContributionID = $membershipContribution->id; | |
1565 | } | |
1566 | ||
1567 | //@todo - why is this nested so deep? it seems like it could be just set on the calling function on the form layer | |
1568 | if (isset($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) { | |
1569 | $form->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id']; | |
1570 | } | |
1571 | //@todo it should no longer be possible for it to get to this point & membership to not be an array | |
1572 | if (is_array($membershipTypeIDs) && !empty($membershipContributionID)) { | |
cbbbf0ef | 1573 | $typesTerms = $membershipParams['types_terms'] ?? []; |
65e172a3 | 1574 | |
be2fb01f | 1575 | $membershipLines = $nonMembershipLines = []; |
65e172a3 | 1576 | foreach ($unprocessedLineItems as $priceSetID => $lines) { |
1577 | foreach ($lines as $line) { | |
1578 | if (!empty($line['membership_type_id'])) { | |
1579 | $membershipLines[$line['membership_type_id']] = $line['price_field_value_id']; | |
1580 | } | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | $i = 1; | |
be2fb01f | 1585 | $form->_params['createdMembershipIDs'] = []; |
668ddfc2 | 1586 | foreach ($membershipTypeIDs as $memType) { |
be2fb01f | 1587 | $membershipLineItems = []; |
65e172a3 | 1588 | if ($i < count($membershipTypeIDs)) { |
1589 | $membershipLineItems[$priceSetID][$membershipLines[$memType]] = $unprocessedLineItems[$priceSetID][$membershipLines[$memType]]; | |
1590 | unset($unprocessedLineItems[$priceSetID][$membershipLines[$memType]]); | |
1591 | } | |
1592 | else { | |
1593 | $membershipLineItems = $unprocessedLineItems; | |
1594 | } | |
1595 | $i++; | |
cbbbf0ef | 1596 | $numTerms = $typesTerms[$memType] ?? 1; |
77c21b32 | 1597 | $contributionRecurID = $form->_params['contributionRecurID'] ?? NULL; |
668ddfc2 EM |
1598 | |
1599 | $membershipSource = NULL; | |
1600 | if (!empty($form->_params['membership_source'])) { | |
1601 | $membershipSource = $form->_params['membership_source']; | |
1602 | } | |
6489e3de SL |
1603 | elseif ((isset($form->_values['title']) && !empty($form->_values['title'])) || (isset($form->_values['frontend_title']) && !empty($form->_values['frontend_title']))) { |
1604 | $title = !empty($form->_values['frontend_title']) ? $form->_values['frontend_title'] : $form->_values['title']; | |
1605 | $membershipSource = ts('Online Contribution:') . ' ' . $title; | |
668ddfc2 EM |
1606 | } |
1607 | $isPayLater = NULL; | |
1608 | if (isset($form->_params)) { | |
9c1bc317 | 1609 | $isPayLater = $form->_params['is_pay_later'] ?? NULL; |
668ddfc2 | 1610 | } |
5fbbde1f | 1611 | $memParams = [ |
1612 | 'campaign_id' => $form->_params['campaign_id'] ?? ($form->_values['campaign_id'] ?? NULL), | |
1613 | ]; | |
668ddfc2 | 1614 | |
5b9d3ce8 MWMC |
1615 | // @todo Move this into CRM_Member_BAO_Membership::processMembership |
1616 | if (!empty($membershipContribution)) { | |
3ec3dcbe | 1617 | $pending = $membershipContribution->contribution_status_id == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); |
5b9d3ce8 MWMC |
1618 | } |
1619 | else { | |
cbbbf0ef MW |
1620 | // The concept of contributeMode is deprecated. |
1621 | // the is_monetary concept probably should be too as it can be calculated from | |
1622 | // the existence of 'amount' & seems fragile. | |
1623 | if (((isset($this->_contributeMode)) || !empty($this->_params['is_pay_later']) | |
1624 | ) && | |
1625 | (($this->_values['is_monetary'] && $this->_amount > 0.0)) | |
1626 | ) { | |
1627 | $pending = TRUE; | |
1628 | } | |
1629 | $pending = FALSE; | |
5b9d3ce8 | 1630 | } |
cbbbf0ef | 1631 | |
ddb56033 | 1632 | [$membership, $renewalMode, $dates] = self::legacyProcessMembership( |
668ddfc2 | 1633 | $contactID, $memType, $isTest, |
cbbbf0ef | 1634 | date('YmdHis'), $membershipParams['cms_contactID'] ?? NULL, |
668ddfc2 EM |
1635 | $customFieldsFormatted, |
1636 | $numTerms, $membershipID, $pending, | |
05f80619 | 1637 | $contributionRecurID, $membershipSource, $isPayLater, $memParams, $membershipContribution, |
65e172a3 | 1638 | $membershipLineItems |
668ddfc2 | 1639 | ); |
bd53b1b9 | 1640 | |
668ddfc2 EM |
1641 | $form->set('renewal_mode', $renewalMode); |
1642 | if (!empty($dates)) { | |
1fc22ffa | 1643 | $form->assign('mem_start_date', CRM_Utils_Date::customFormat($dates['start_date'], '%Y%m%d')); |
1644 | $form->assign('mem_end_date', CRM_Utils_Date::customFormat($dates['end_date'], '%Y%m%d')); | |
668ddfc2 EM |
1645 | } |
1646 | ||
1647 | if (!empty($membershipContribution)) { | |
c9cc6e3a MWMC |
1648 | // Next line is probably redundant. Checks prevent it happening twice. |
1649 | $membershipPaymentParams = [ | |
1650 | 'membership_id' => $membership->id, | |
1651 | 'membership_type_id' => $membership->membership_type_id, | |
1652 | 'contribution_id' => $membershipContribution->id, | |
1653 | ]; | |
1654 | civicrm_api3('MembershipPayment', 'create', $membershipPaymentParams); | |
668ddfc2 | 1655 | } |
040728f5 AF |
1656 | if ($membership) { |
1657 | CRM_Core_BAO_CustomValueTable::postProcess($form->_params, 'civicrm_membership', $membership->id, 'Membership'); | |
1658 | $form->_params['createdMembershipIDs'][] = $membership->id; | |
1659 | $form->_params['membershipID'] = $membership->id; | |
1660 | ||
1661 | //CRM-15232: Check if membership is created and on the basis of it use | |
1662 | //membership receipt template to send payment receipt | |
1663 | $form->_values['isMembership'] = TRUE; | |
1664 | } | |
668ddfc2 EM |
1665 | } |
1666 | if ($form->_priceSetId && !empty($form->_useForMember) && !empty($form->_lineItem)) { | |
1667 | foreach ($form->_lineItem[$form->_priceSetId] as & $priceFieldOp) { | |
e894942a WA |
1668 | if (!empty($priceFieldOp['membership_type_id']) && $membership->membership_type_id == $priceFieldOp['membership_type_id']) { |
1669 | $membershipOb = $membership; | |
56f54f02 | 1670 | $priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::formatDateOnlyLong($membershipOb->start_date) : '-'; |
1671 | $priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::formatDateOnlyLong($membershipOb->end_date) : '-'; | |
668ddfc2 EM |
1672 | } |
1673 | else { | |
1674 | $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A'; | |
1675 | } | |
1676 | } | |
1677 | $form->_values['lineItem'] = $form->_lineItem; | |
1678 | $form->assign('lineItem', $form->_lineItem); | |
1679 | } | |
1680 | } | |
1681 | ||
1682 | if (!empty($errors)) { | |
1683 | $message = $this->compileErrorMessage($errors); | |
1684 | throw new CRM_Core_Exception($message); | |
1685 | } | |
28da8ecd | 1686 | |
82583880 EM |
1687 | if (isset($membershipContributionID)) { |
1688 | $form->_values['contribution_id'] = $membershipContributionID; | |
1689 | } | |
a506739a | 1690 | |
1fc22ffa | 1691 | if (empty($form->_params['is_pay_later']) && $form->_paymentProcessor) { |
a506739a | 1692 | // the is_monetary concept probably should be deprecated as it can be calculated from |
1693 | // the existence of 'amount' & seems fragile. | |
668ddfc2 EM |
1694 | if ($form->_values['is_monetary'] && $form->_amount > 0.0 && !$form->_params['is_pay_later']) { |
1695 | // call postProcess hook before leaving | |
1696 | $form->postProcessHook(); | |
668ddfc2 | 1697 | } |
a506739a | 1698 | |
82583880 | 1699 | $payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor); |
fe3dbce1 | 1700 | // The contribution_other_id is effectively the ID for the only contribution or the non-membership contribution. |
1701 | // Since we have called the membership contribution (in a 2 contribution scenario) this is out | |
1702 | // primary-contribution compared to that - but let's face it - it's all just too hard & confusing at the moment! | |
be2fb01f | 1703 | $paymentParams = array_merge($form->_params, ['contributionID' => $form->_values['contribution_other_id']]); |
b43ed44b PN |
1704 | |
1705 | // CRM-19792 : set necessary fields for payment processor | |
1706 | CRM_Core_Payment_Form::mapParams($form->_bltID, $paymentParams, $paymentParams, TRUE); | |
1707 | ||
15816cb4 AS |
1708 | // If this is a single membership-related contribution, it won't have |
1709 | // be performed yet, so do it now. | |
1710 | if ($isPaidMembership && !$isProcessSeparateMembershipTransaction) { | |
1711 | $paymentActionResult = $payment->doPayment($paymentParams, 'contribute'); | |
be2fb01f | 1712 | $paymentResults[] = ['contribution_id' => $paymentResult['contribution']->id, 'result' => $paymentActionResult]; |
15816cb4 | 1713 | } |
668ddfc2 EM |
1714 | // Do not send an email if Recurring transaction is done via Direct Mode |
1715 | // Email will we sent when the IPN is received. | |
63137f3a | 1716 | foreach ($paymentResults as $result) { |
7c864699 TL |
1717 | //CRM-18211: Fix situation where second contribution doesn't exist because it is optional. |
1718 | if ($result['contribution_id']) { | |
1719 | $this->completeTransaction($result['result'], $result['contribution_id']); | |
1720 | } | |
63137f3a | 1721 | } |
668ddfc2 EM |
1722 | return; |
1723 | } | |
1724 | ||
1fc22ffa | 1725 | $emailValues = array_merge($membershipParams, $form->_values); |
1726 | $emailValues['membership_assign'] = 1; | |
05d09dc4 JP |
1727 | $emailValues['useForMember'] = !empty($form->_useForMember); |
1728 | ||
858f7096 | 1729 | // Finally send an email receipt for pay-later scenario (although it might sometimes be caught above!) |
1730 | if ($totalAmount == 0) { | |
1731 | // This feels like a bizarre hack as the variable name doesn't seem to be directly connected to it's use in the template. | |
1732 | $emailValues['useForMember'] = 0; | |
858f7096 | 1733 | $emailValues['amount'] = 0; |
036c6315 | 1734 | |
1735 | //CRM-18071, where on selecting $0 free membership payment section got hidden and | |
1736 | // also it reset any payment processor selection result into pending free membership | |
1737 | // so its a kind of hack to complete free membership at this point since there is no $form->_paymentProcessor info | |
bd53b1b9 | 1738 | if (!empty($membershipContribution) && !is_a($membershipContribution, 'CRM_Core_Error')) { |
18135422 | 1739 | if (empty($form->_paymentProcessor)) { |
1740 | // @todo this can maybe go now we are setting payment_processor_id = 0 more reliably. | |
cbbbf0ef | 1741 | $paymentProcessorIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_values['payment_processor'] ?? NULL); |
036c6315 | 1742 | $this->_paymentProcessor['id'] = $paymentProcessorIDs[0]; |
1743 | } | |
be2fb01f | 1744 | $result = ['payment_status_id' => 1, 'contribution' => $membershipContribution]; |
036c6315 | 1745 | $this->completeTransaction($result, $result['contribution']->id); |
1746 | } | |
1e584dec JP |
1747 | // return as completeTransaction() already sends the receipt mail. |
1748 | return; | |
858f7096 | 1749 | } |
1fc22ffa | 1750 | |
668ddfc2 | 1751 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, |
858f7096 | 1752 | $emailValues, |
668ddfc2 EM |
1753 | $isTest, FALSE, |
1754 | $includeFieldTypes | |
1755 | ); | |
1756 | } | |
1757 | ||
1758 | /** | |
1759 | * Turn array of errors into message string. | |
1760 | * | |
1761 | * @param array $errors | |
1762 | * | |
1763 | * @return string | |
1764 | */ | |
1765 | protected function compileErrorMessage($errors) { | |
1766 | foreach ($errors as $error) { | |
1767 | if (is_string($error)) { | |
1768 | $message[] = $error; | |
1769 | } | |
1770 | } | |
1771 | return ts('Payment Processor Error message') . ': ' . implode('<br/>', $message); | |
1772 | } | |
1773 | ||
1774 | /** | |
1775 | * Where a second separate financial transaction is supported we will process it here. | |
1776 | * | |
1777 | * @param int $contactID | |
1778 | * @param CRM_Contribute_Form_Contribution_Confirm $form | |
1779 | * @param array $tempParams | |
1780 | * @param bool $isTest | |
1781 | * @param array $lineItems | |
1782 | * @param $minimumFee | |
1783 | * @param int $financialTypeID | |
1784 | * | |
1785 | * @throws CRM_Core_Exception | |
1786 | * @throws Exception | |
1787 | * @return CRM_Contribute_BAO_Contribution | |
1788 | */ | |
1789 | protected function processSecondaryFinancialTransaction($contactID, &$form, $tempParams, $isTest, $lineItems, $minimumFee, | |
1790 | $financialTypeID) { | |
1791 | $financialType = new CRM_Financial_DAO_FinancialType(); | |
1792 | $financialType->id = $financialTypeID; | |
1793 | $financialType->find(TRUE); | |
1794 | $tempParams['amount'] = $minimumFee; | |
1795 | $tempParams['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
9c1bc317 | 1796 | $isRecur = $tempParams['is_recur'] ?? NULL; |
668ddfc2 | 1797 | |
668ddfc2 EM |
1798 | //assign receive date when separate membership payment |
1799 | //and contribution amount not selected. | |
1800 | if ($form->_amount == 0) { | |
1801 | $now = date('YmdHis'); | |
1802 | $form->_params['receive_date'] = $now; | |
1803 | $receiveDate = CRM_Utils_Date::mysqlToIso($now); | |
1804 | $form->set('params', $form->_params); | |
1805 | $form->assign('receive_date', $receiveDate); | |
1806 | } | |
1807 | ||
668ddfc2 | 1808 | $form->set('membership_amount', $minimumFee); |
668ddfc2 EM |
1809 | $form->assign('membership_amount', $minimumFee); |
1810 | ||
668ddfc2 EM |
1811 | //set this variable as we are not creating pledge for |
1812 | //separate membership payment contribution. | |
1813 | //so for differentiating membership contribution from | |
1814 | //main contribution. | |
1815 | $form->_params['separate_membership_payment'] = 1; | |
be2fb01f | 1816 | $contributionParams = [ |
3febe800 | 1817 | 'contact_id' => $contactID, |
1818 | 'line_item' => $lineItems, | |
1819 | 'is_test' => $isTest, | |
cbbbf0ef | 1820 | 'campaign_id' => $tempParams['campaign_id'] ?? $form->_values['campaign_id'] ?? NULL, |
3febe800 | 1821 | 'contribution_page_id' => $form->_id, |
cbbbf0ef | 1822 | 'source' => $tempParams['source'] ?? $tempParams['description'] ?? NULL, |
be2fb01f | 1823 | ]; |
3febe800 | 1824 | $isMonetary = !empty($form->_values['is_monetary']); |
1825 | if ($isMonetary) { | |
1826 | if (empty($paymentParams['is_pay_later'])) { | |
f3a63d1e | 1827 | $contributionParams['payment_instrument_id'] = $form->_paymentProcessor['payment_instrument_id']; |
3febe800 | 1828 | } |
1829 | } | |
b43ed44b PN |
1830 | |
1831 | // CRM-19792 : set necessary fields for payment processor | |
1832 | CRM_Core_Payment_Form::mapParams($form->_bltID, $form->_params, $tempParams, TRUE); | |
1833 | ||
a281a8ea | 1834 | $membershipContribution = $this->processFormContribution( |
668ddfc2 | 1835 | $tempParams, |
0193ebdb | 1836 | $tempParams, |
3febe800 | 1837 | $contributionParams, |
668ddfc2 | 1838 | $financialType, |
449f4c90 | 1839 | $form->_bltID, |
1840 | $isRecur | |
668ddfc2 | 1841 | ); |
0193ebdb | 1842 | |
be2fb01f | 1843 | $result = []; |
15816cb4 | 1844 | |
7f5ae3a0 AS |
1845 | // We're not processing the line item here because we are processing a membership. |
1846 | // To ensure processing of the correct parameters, replace relevant parameters | |
1847 | // in $tempParams with those in $membershipContribution. | |
1848 | $tempParams['amount_level'] = $membershipContribution->amount_level; | |
1849 | $tempParams['total_amount'] = $membershipContribution->total_amount; | |
1850 | $tempParams['tax_amount'] = $membershipContribution->tax_amount; | |
1851 | $tempParams['contactID'] = $membershipContribution->contact_id; | |
1852 | $tempParams['financialTypeID'] = $membershipContribution->financial_type_id; | |
1853 | $tempParams['invoiceID'] = $membershipContribution->invoice_id; | |
1854 | $tempParams['trxn_id'] = $membershipContribution->trxn_id; | |
1855 | $tempParams['contributionID'] = $membershipContribution->id; | |
15816cb4 | 1856 | |
0193ebdb | 1857 | if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) { |
1858 | // At the moment our tests are calling this form in a way that leaves 'object' empty. For | |
1859 | // now we compensate here. | |
1860 | if (empty($form->_paymentProcessor['object'])) { | |
1861 | $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); | |
1862 | } | |
1863 | else { | |
1864 | $payment = $form->_paymentProcessor['object']; | |
1865 | } | |
1866 | $result = $payment->doPayment($tempParams, 'contribute'); | |
1867 | $form->set('membership_trx_id', $result['trxn_id']); | |
1868 | $form->assign('membership_trx_id', $result['trxn_id']); | |
0193ebdb | 1869 | } |
1870 | ||
be2fb01f | 1871 | return [$membershipContribution, $result]; |
668ddfc2 EM |
1872 | } |
1873 | ||
fb49fa48 | 1874 | /** |
5e4f7f74 | 1875 | * Are we going to do 2 financial transactions. |
fb49fa48 | 1876 | * |
5e4f7f74 EM |
1877 | * Ie the membership block supports a separate transactions AND the contribution form has been configured for a |
1878 | * contribution | |
81355da3 | 1879 | * transaction AND a membership transaction AND the payment processor supports double financial transactions (ie. NOT doTransferCheckout style) |
fb49fa48 | 1880 | * |
014c4014 | 1881 | * @param int $formID |
fb49fa48 EM |
1882 | * @param bool $amountBlockActiveOnForm |
1883 | * | |
1884 | * @return bool | |
1885 | */ | |
1886 | public function isSeparateMembershipTransaction($formID, $amountBlockActiveOnForm) { | |
1887 | $memBlockDetails = CRM_Member_BAO_Membership::getMembershipBlock($formID); | |
1888 | if (!empty($memBlockDetails['is_separate_payment']) && $amountBlockActiveOnForm) { | |
1889 | return TRUE; | |
1890 | } | |
1891 | return FALSE; | |
1892 | } | |
df6c4f28 EM |
1893 | |
1894 | /** | |
5e4f7f74 EM |
1895 | * This function sets the fields. |
1896 | * | |
df6c4f28 EM |
1897 | * - $this->_params['amount_level'] |
1898 | * - $this->_params['selectMembership'] | |
1899 | * And under certain circumstances sets | |
1900 | * $this->_params['amount'] = null; | |
1901 | * | |
100fef9d | 1902 | * @param int $priceSetID |
df6c4f28 EM |
1903 | */ |
1904 | public function setFormAmountFields($priceSetID) { | |
1905 | $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config'); | |
1906 | $priceField = new CRM_Price_DAO_PriceField(); | |
1907 | $priceField->price_set_id = $priceSetID; | |
1908 | $priceField->orderBy('weight'); | |
1909 | $priceField->find(); | |
06b4ee3e | 1910 | $paramWeDoNotUnderstand = NULL; |
df6c4f28 EM |
1911 | |
1912 | while ($priceField->fetch()) { | |
df6c4f28 EM |
1913 | if ($priceField->name == "contribution_amount") { |
1914 | $paramWeDoNotUnderstand = $priceField->id; | |
1915 | } | |
1916 | if ($isQuickConfig && !empty($this->_params["price_{$priceField->id}"])) { | |
1917 | if ($this->_values['fee'][$priceField->id]['html_type'] != 'Text') { | |
c039f658 | 1918 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
1919 | // function to get correct amount level consistently. Remove setting of the amount level in | |
1920 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest | |
1921 | // to cover all variants. | |
df6c4f28 EM |
1922 | $this->_params['amount_level'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', |
1923 | $this->_params["price_{$priceField->id}"], 'label'); | |
1924 | } | |
1925 | if ($priceField->name == "membership_amount") { | |
1926 | $this->_params['selectMembership'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', | |
1927 | $this->_params["price_{$priceField->id}"], 'membership_type_id'); | |
1928 | } | |
5e4f7f74 EM |
1929 | } |
1930 | // If separate payment we set contribution amount to be null, so that it will not show contribution amount same | |
1931 | // as membership amount. | |
df6c4f28 EM |
1932 | // @todo - this needs more documentation - it appears the setting to null is tied up with separate membership payments |
1933 | // but the circumstances are very confusing. Many of these conditions are repeated in the next conditional | |
1934 | // so we should merge them together | |
1935 | // the quick config seems like a red-herring - if this is about a separate membership payment then there | |
f64a217a | 1936 | // are 2 types of line items - membership ones & non-membership ones - regardless of whether quick config is set |
df6c4f28 | 1937 | elseif ( |
b99f3e96 | 1938 | !empty($this->_membershipBlock['is_separate_payment']) |
df6c4f28 EM |
1939 | && !empty($this->_values['fee'][$priceField->id]) |
1940 | && ($this->_values['fee'][$priceField->id]['name'] == "other_amount") | |
cbbbf0ef | 1941 | && ($this->_params["price_{$paramWeDoNotUnderstand}"] ?? NULL) < 1 |
df6c4f28 EM |
1942 | && empty($this->_params["price_{$priceField->id}"]) |
1943 | ) { | |
a13f3d8c | 1944 | $this->_params['amount'] = NULL; |
df6c4f28 EM |
1945 | } |
1946 | ||
1947 | // Fix for CRM-14375 - If we are using separate payments and "no | |
1948 | // thank you" is selected for the additional contribution, set | |
1949 | // contribution amount to be null, so that it will not show | |
1950 | // contribution amount same as membership amount. | |
1951 | //@todo - merge with section above | |
32c3b33f | 1952 | if (!empty($this->_membershipBlock['is_separate_payment']) |
3c0201c9 | 1953 | && !empty($this->_values['fee'][$priceField->id]) |
cbbbf0ef MW |
1954 | && ($this->_values['fee'][$priceField->id]['name'] ?? NULL) == 'contribution_amount' |
1955 | && ($this->_params["price_{$priceField->id}"] ?? NULL) == '-1' | |
df6c4f28 | 1956 | ) { |
a13f3d8c | 1957 | $this->_params['amount'] = NULL; |
df6c4f28 EM |
1958 | } |
1959 | } | |
1960 | } | |
be26f3e0 | 1961 | |
03110609 | 1962 | /** |
5e4f7f74 EM |
1963 | * Submit function. |
1964 | * | |
03110609 EM |
1965 | * @param array $params |
1966 | * | |
d6c86ce5 EM |
1967 | * @throws \CRM_Core_Exception |
1968 | * @throws \CiviCRM_API3_Exception | |
1969 | * @throws \Civi\API\Exception\UnauthorizedException | |
03110609 | 1970 | */ |
00be9182 | 1971 | public static function submit($params) { |
be26f3e0 EM |
1972 | $form = new CRM_Contribute_Form_Contribution_Confirm(); |
1973 | $form->_id = $params['id']; | |
75637f22 | 1974 | |
f64a217a EM |
1975 | CRM_Contribute_BAO_ContributionPage::setValues($form->_id, $form->_values); |
1976 | $form->_separateMembershipPayment = CRM_Contribute_BAO_ContributionPage::getIsMembershipPayment($form->_id); | |
be26f3e0 EM |
1977 | //this way the mocked up controller ignores the session stuff |
1978 | $_SERVER['REQUEST_METHOD'] = 'GET'; | |
1979 | $form->controller = new CRM_Contribute_Controller_Contribution(); | |
1980 | $params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
ec57bf87 | 1981 | |
be26f3e0 | 1982 | $paramsProcessedForForm = $form->_params = self::getFormParams($params['id'], $params); |
58315149 | 1983 | |
1984 | $order = new CRM_Financial_BAO_Order(); | |
d6c86ce5 | 1985 | $order->setPriceSetIDByContributionPageID($params['id']); |
58315149 | 1986 | $order->setPriceSelectionFromUnfilteredInput($params); |
d6c86ce5 | 1987 | if (isset($params['amount']) && !CRM_Contribute_BAO_ContributionPage::getIsMembershipPayment($form->_id)) { |
58315149 | 1988 | // @todo deprecate receiving amount, calculate on the form. |
2df49c85 | 1989 | $order->setOverrideTotalAmount((float) $params['amount']); |
58315149 | 1990 | } |
1991 | $amount = $order->getTotalAmount(); | |
765d02a3 EM |
1992 | if ($form->_separateMembershipPayment) { |
1993 | $amount -= $order->getMembershipTotalAmount(); | |
1994 | } | |
1995 | $form->_amount = $params['amount'] = $form->_params['amount'] = $amount; | |
870156d6 | 1996 | // hack these in for test support. |
1997 | $form->_fields['billing_first_name'] = 1; | |
1998 | $form->_fields['billing_last_name'] = 1; | |
dccd9f4f | 1999 | // CRM-18854 - Set form values to allow pledge to be created for api test. |
de6c59ca | 2000 | if (!empty($params['pledge_block_id'])) { |
9c1bc317 | 2001 | $form->_values['pledge_id'] = $params['pledge_id'] ?? NULL; |
dccd9f4f ERL |
2002 | $form->_values['pledge_block_id'] = $params['pledge_block_id']; |
2003 | $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($params['id']); | |
2004 | $form->_values['max_reminders'] = $pledgeBlock['max_reminders']; | |
2005 | $form->_values['initial_reminder_day'] = $pledgeBlock['initial_reminder_day']; | |
2006 | $form->_values['additional_reminder_day'] = $pledgeBlock['additional_reminder_day']; | |
2007 | $form->_values['is_email_receipt'] = FALSE; | |
2008 | } | |
be26f3e0 EM |
2009 | $priceSetID = $form->_params['priceSetId'] = $paramsProcessedForForm['price_set_id']; |
2010 | $priceFields = CRM_Price_BAO_PriceSet::getSetDetail($priceSetID); | |
f64a217a EM |
2011 | $priceSetFields = reset($priceFields); |
2012 | $form->_values['fee'] = $priceSetFields['fields']; | |
5624f515 | 2013 | $form->_priceSetId = $priceSetID; |
be26f3e0 | 2014 | $form->setFormAmountFields($priceSetID); |
be2fb01f | 2015 | $capabilities = []; |
18135422 | 2016 | if ($form->_mode) { |
2017 | $capabilities[] = (ucfirst($form->_mode) . 'Mode'); | |
2018 | } | |
2019 | $form->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors($capabilities); | |
77c21b32 | 2020 | $form->_params['payment_processor_id'] = $params['payment_processor_id'] ?? 0; |
04b24bb5 | 2021 | if ($form->_params['payment_processor_id'] !== '') { |
2022 | // It can be blank with a $0 transaction - then no processor needs to be selected | |
2023 | $form->_paymentProcessor = $form->_paymentProcessors[$form->_params['payment_processor_id']]; | |
2024 | } | |
e02d7e96 | 2025 | if (!empty($params['payment_processor_id'])) { |
0f2b049e | 2026 | // The concept of contributeMode is deprecated as is the billing_mode concept. |
a13f3d8c | 2027 | if ($form->_paymentProcessor['billing_mode'] == 1) { |
e6fa4056 EM |
2028 | $form->_contributeMode = 'direct'; |
2029 | } | |
2030 | else { | |
2031 | $form->_contributeMode = 'notify'; | |
2032 | } | |
d25e4224 | 2033 | } |
65e172a3 | 2034 | |
ed781038 JP |
2035 | if (!empty($params['useForMember'])) { |
2036 | $form->set('useForMember', 1); | |
2037 | $form->_useForMember = 1; | |
2038 | } | |
be26f3e0 | 2039 | $priceFields = $priceFields[$priceSetID]['fields']; |
d6c86ce5 EM |
2040 | |
2041 | $form->_lineItem = [$priceSetID => $order->getLineItems()]; | |
be2fb01f | 2042 | $membershipPriceFieldIDs = []; |
d6c86ce5 | 2043 | foreach ($order->getLineItems() as $lineItem) { |
65e172a3 | 2044 | if (!empty($lineItem['membership_type_id'])) { |
2045 | $form->set('useForMember', 1); | |
2046 | $form->_useForMember = 1; | |
2047 | $membershipPriceFieldIDs['id'] = $priceSetID; | |
2048 | $membershipPriceFieldIDs[] = $lineItem['price_field_value_id']; | |
2049 | } | |
2050 | } | |
2051 | $form->set('memberPriceFieldIDS', $membershipPriceFieldIDs); | |
35bdddf0 | 2052 | $form->setRecurringMembershipParams(); |
cbbbf0ef | 2053 | $form->processFormSubmission($params['contact_id'] ?? NULL); |
be26f3e0 EM |
2054 | } |
2055 | ||
2056 | /** | |
ceb9fecb EM |
2057 | * Helper function for static submit function. |
2058 | * | |
2059 | * Set relevant params - help us to build up an array that we can pass in. | |
5e4f7f74 | 2060 | * |
100fef9d | 2061 | * @param int $id |
be26f3e0 EM |
2062 | * @param array $params |
2063 | * | |
2064 | * @return array | |
2065 | * @throws CiviCRM_API3_Exception | |
2066 | */ | |
00be9182 | 2067 | public static function getFormParams($id, array $params) { |
a13f3d8c | 2068 | if (!isset($params['is_pay_later'])) { |
e02d7e96 | 2069 | if (!empty($params['payment_processor_id'])) { |
d25e4224 DG |
2070 | $params['is_pay_later'] = 0; |
2071 | } | |
62e432ac | 2072 | elseif (($params['amount'] ?? 0) !== 0) { |
be2fb01f | 2073 | $params['is_pay_later'] = civicrm_api3('contribution_page', 'getvalue', [ |
a13f3d8c | 2074 | 'id' => $id, |
21dfd5f5 | 2075 | 'return' => 'is_pay_later', |
be2fb01f | 2076 | ]); |
d25e4224 | 2077 | } |
be26f3e0 | 2078 | } |
a13f3d8c | 2079 | if (empty($params['price_set_id'])) { |
be26f3e0 EM |
2080 | $params['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id']); |
2081 | } | |
2082 | return $params; | |
2083 | } | |
96025800 | 2084 | |
75637f22 EM |
2085 | /** |
2086 | * Post form submission handling. | |
2087 | * | |
2088 | * This is also called from the test suite. | |
2089 | * | |
2090 | * @param int $contactID | |
2091 | * | |
2092 | * @return array | |
fb8d25da | 2093 | * |
62c41ada | 2094 | * @throws \CRM_Core_Exception |
fb8d25da | 2095 | * @throws \CiviCRM_API3_Exception |
2096 | * @throws \Civi\API\Exception\UnauthorizedException | |
75637f22 EM |
2097 | */ |
2098 | protected function processFormSubmission($contactID) { | |
858f7096 | 2099 | if (!isset($this->_params['payment_processor_id'])) { |
2100 | // If there is no processor we are using the pay-later manual pseudo-processor. | |
2101 | // (note it might make sense to make this a row in the processor table in the db). | |
2102 | $this->_params['payment_processor_id'] = 0; | |
2103 | } | |
036c6315 | 2104 | if (isset($this->_params['payment_processor_id']) && $this->_params['payment_processor_id'] === 0) { |
75637f22 EM |
2105 | $this->_params['is_pay_later'] = $isPayLater = TRUE; |
2106 | } | |
58466af1 | 2107 | |
2108 | if (!empty($this->_ccid)) { | |
2109 | $this->_params['contribution_id'] = $this->_ccid; | |
2110 | } | |
67c998f4 JP |
2111 | //Set email-bltID if pre/post profile contains an email. |
2112 | if ($this->_emailExists == TRUE) { | |
2113 | foreach ($this->_params as $key => $val) { | |
2114 | if (substr($key, 0, 6) == 'email-' && empty($this->_params["email-{$this->_bltID}"])) { | |
2115 | $this->_params["email-{$this->_bltID}"] = $this->_params[$key]; | |
2116 | } | |
2117 | } | |
2118 | } | |
75637f22 | 2119 | // add a description field at the very beginning |
6489e3de SL |
2120 | $title = !empty($this->_values['frontend_title']) ? $this->_values['frontend_title'] : $this->_values['title']; |
2121 | $this->_params['description'] = ts('Online Contribution') . ': ' . (!empty($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $title); | |
75637f22 | 2122 | |
9c1bc317 | 2123 | $this->_params['accountingCode'] = $this->_values['accountingCode'] ?? NULL; |
75637f22 EM |
2124 | |
2125 | // fix currency ID | |
2126 | $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency; | |
2127 | ||
a55e39e9 | 2128 | CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($this->_params); |
2129 | ||
dccd9f4f | 2130 | // CRM-18854 |
f3acfdd9 | 2131 | if (!empty($this->_params['is_pledge']) && empty($this->_values['pledge_id']) && !empty($this->_values['adjust_recur_start_date'])) { |
dccd9f4f | 2132 | $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id); |
b99f3e96 CW |
2133 | if (!empty($this->_params['start_date']) || empty($pledgeBlock['is_pledge_start_date_visible']) |
2134 | || empty($pledgeBlock['is_pledge_start_date_editable'])) { | |
9c1bc317 | 2135 | $pledgeStartDate = $this->_params['start_date'] ?? NULL; |
dccd9f4f ERL |
2136 | $this->_params['receive_date'] = CRM_Pledge_BAO_Pledge::getPledgeStartDate($pledgeStartDate, $pledgeBlock); |
2137 | $recurParams = CRM_Pledge_BAO_Pledge::buildRecurParams($this->_params); | |
2138 | $this->_params = array_merge($this->_params, $recurParams); | |
2139 | } | |
2140 | } | |
2141 | ||
75637f22 | 2142 | //carry payment processor id. |
f3acfdd9 | 2143 | if (!empty($this->_paymentProcessor['id'])) { |
75637f22 EM |
2144 | $this->_params['payment_processor_id'] = $this->_paymentProcessor['id']; |
2145 | } | |
f85b8063 KJ |
2146 | |
2147 | $premiumParams = $membershipParams = $params = $this->_params; | |
75637f22 EM |
2148 | if (!empty($params['image_URL'])) { |
2149 | CRM_Contact_BAO_Contact::processImageParams($params); | |
2150 | } | |
f85b8063 | 2151 | |
be2fb01f | 2152 | $fields = ['email-Primary' => 1]; |
75637f22 EM |
2153 | |
2154 | // get the add to groups | |
be2fb01f | 2155 | $addToGroups = []; |
75637f22 EM |
2156 | |
2157 | // now set the values for the billing location. | |
2158 | foreach ($this->_fields as $name => $value) { | |
2159 | $fields[$name] = 1; | |
2160 | ||
2161 | // get the add to groups for uf fields | |
2162 | if (!empty($value['add_to_group_id'])) { | |
2163 | $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id']; | |
2164 | } | |
2165 | } | |
2166 | ||
bddc8a28 | 2167 | $fields = $this->formatParamsForPaymentProcessor($fields); |
75637f22 EM |
2168 | |
2169 | // billing email address | |
2170 | $fields["email-{$this->_bltID}"] = 1; | |
2171 | ||
75637f22 EM |
2172 | // if onbehalf-of-organization contribution, take out |
2173 | // organization params in a separate variable, to make sure | |
2174 | // normal behavior is continued. And use that variable to | |
2175 | // process on-behalf-of functionality. | |
2f44fc96 | 2176 | if (!empty($this->_values['onbehalf_profile_id']) && empty($this->_ccid)) { |
be2fb01f CW |
2177 | $behalfOrganization = []; |
2178 | $orgFields = ['organization_name', 'organization_id', 'org_option']; | |
75637f22 EM |
2179 | foreach ($orgFields as $fld) { |
2180 | if (array_key_exists($fld, $params)) { | |
2181 | $behalfOrganization[$fld] = $params[$fld]; | |
2182 | unset($params[$fld]); | |
2183 | } | |
2184 | } | |
2185 | ||
2186 | if (is_array($params['onbehalf']) && !empty($params['onbehalf'])) { | |
2187 | foreach ($params['onbehalf'] as $fld => $values) { | |
2188 | if (strstr($fld, 'custom_')) { | |
2189 | $behalfOrganization[$fld] = $values; | |
2190 | } | |
2191 | elseif (!(strstr($fld, '-'))) { | |
be2fb01f | 2192 | if (in_array($fld, [ |
75637f22 EM |
2193 | 'contribution_campaign_id', |
2194 | 'member_campaign_id', | |
be2fb01f | 2195 | ])) { |
75637f22 EM |
2196 | $fld = 'campaign_id'; |
2197 | } | |
2198 | else { | |
2199 | $behalfOrganization[$fld] = $values; | |
2200 | } | |
2201 | $this->_params[$fld] = $values; | |
2202 | } | |
2203 | } | |
2204 | } | |
2205 | ||
2206 | if (array_key_exists('onbehalf_location', $params) && is_array($params['onbehalf_location'])) { | |
2207 | foreach ($params['onbehalf_location'] as $block => $vals) { | |
2208 | //fix for custom data (of type checkbox, multi-select) | |
2209 | if (substr($block, 0, 7) == 'custom_') { | |
2210 | continue; | |
2211 | } | |
2212 | // fix the index of block elements | |
2213 | if (is_array($vals)) { | |
2214 | foreach ($vals as $key => $val) { | |
2215 | //dont adjust the index of address block as | |
2216 | //it's index is WRT to location type | |
2217 | $newKey = ($block == 'address') ? $key : ++$key; | |
2218 | $behalfOrganization[$block][$newKey] = $val; | |
2219 | } | |
2220 | } | |
2221 | } | |
2222 | unset($params['onbehalf_location']); | |
2223 | } | |
2224 | if (!empty($params['onbehalf[image_URL]'])) { | |
2225 | $behalfOrganization['image_URL'] = $params['onbehalf[image_URL]']; | |
2226 | } | |
2227 | } | |
2228 | ||
2229 | // check for profile double opt-in and get groups to be subscribed | |
2230 | $subscribeGroupIds = CRM_Core_BAO_UFGroup::getDoubleOptInGroupIds($params, $contactID); | |
2231 | ||
2232 | // since we are directly adding contact to group lets unset it from mailing | |
2233 | if (!empty($addToGroups)) { | |
2234 | foreach ($addToGroups as $groupId) { | |
2235 | if (isset($subscribeGroupIds[$groupId])) { | |
2236 | unset($subscribeGroupIds[$groupId]); | |
2237 | } | |
2238 | } | |
2239 | } | |
2240 | ||
2241 | foreach ($addToGroups as $k) { | |
2242 | if (array_key_exists($k, $subscribeGroupIds)) { | |
2243 | unset($addToGroups[$k]); | |
2244 | } | |
2245 | } | |
2246 | ||
2247 | if (empty($contactID)) { | |
2248 | $dupeParams = $params; | |
2249 | if (!empty($dupeParams['onbehalf'])) { | |
2250 | unset($dupeParams['onbehalf']); | |
2251 | } | |
2252 | if (!empty($dupeParams['honor'])) { | |
2253 | unset($dupeParams['honor']); | |
2254 | } | |
2255 | ||
be2fb01f | 2256 | $contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($dupeParams, 'Individual', 'Unsupervised', [], FALSE); |
75637f22 EM |
2257 | |
2258 | // Fetch default greeting id's if creating a contact | |
2259 | if (!$contactID) { | |
2260 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { | |
2261 | if (!isset($params[$greeting])) { | |
2262 | $params[$greeting] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting); | |
2263 | } | |
2264 | } | |
2265 | } | |
2266 | $contactType = NULL; | |
2267 | } | |
2268 | else { | |
2269 | $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'contact_type'); | |
2270 | } | |
2271 | $contactID = CRM_Contact_BAO_Contact::createProfileContact( | |
2272 | $params, | |
2273 | $fields, | |
2274 | $contactID, | |
2275 | $addToGroups, | |
2276 | NULL, | |
2277 | $contactType, | |
2278 | TRUE | |
2279 | ); | |
2280 | ||
2281 | // Make the contact ID associated with the contribution available at the Class level. | |
2282 | // Also make available to the session. | |
2283 | //@todo consider handling this in $this->getContactID(); | |
2284 | $this->set('contactID', $contactID); | |
2285 | $this->_contactID = $contactID; | |
2286 | ||
2287 | //get email primary first if exist | |
cbbbf0ef | 2288 | $subscriptionEmail = ['email' => $params['email-Primary'] ?? NULL]; |
75637f22 | 2289 | if (!$subscriptionEmail['email']) { |
9c1bc317 | 2290 | $subscriptionEmail['email'] = $params["email-{$this->_bltID}"] ?? NULL; |
75637f22 EM |
2291 | } |
2292 | // subscribing contact to groups | |
2293 | if (!empty($subscribeGroupIds) && $subscriptionEmail['email']) { | |
2294 | CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($subscribeGroupIds, $subscriptionEmail, $contactID); | |
2295 | } | |
2296 | ||
2297 | // If onbehalf-of-organization contribution / signup, add organization | |
2298 | // and it's location. | |
a9653117 | 2299 | if (isset($this->_values['onbehalf_profile_id']) && |
2300 | isset($behalfOrganization['organization_name']) && | |
2301 | ($this->_values['is_for_organization'] == 2 || | |
2302 | !empty($this->_params['is_for_organization']) | |
2303 | ) | |
2304 | ) { | |
be2fb01f | 2305 | $ufFields = []; |
75637f22 EM |
2306 | foreach ($this->_fields['onbehalf'] as $name => $value) { |
2307 | $ufFields[$name] = 1; | |
2308 | } | |
2309 | self::processOnBehalfOrganization($behalfOrganization, $contactID, $this->_values, | |
2310 | $this->_params, $ufFields | |
2311 | ); | |
2312 | } | |
2313 | elseif (!empty($this->_membershipContactID) && $contactID != $this->_membershipContactID) { | |
2314 | // this is an onbehalf renew case for inherited membership. For e.g a permissioned member of household, | |
2315 | // store current user id as related contact for later use for mailing / activity.. | |
2316 | $this->_values['related_contact'] = $contactID; | |
2317 | $this->_params['related_contact'] = $contactID; | |
2318 | // swap contact like we do for on-behalf-org case, so parent/primary membership is affected | |
2319 | $contactID = $this->_membershipContactID; | |
2320 | } | |
2321 | ||
2322 | // lets store the contactID in the session | |
2323 | // for things like tell a friend | |
2324 | $session = CRM_Core_Session::singleton(); | |
2325 | if (!$session->get('userID')) { | |
2326 | $session->set('transaction.userID', $contactID); | |
2327 | } | |
2328 | else { | |
2329 | $session->set('transaction.userID', NULL); | |
2330 | } | |
2331 | ||
2332 | $this->_useForMember = $this->get('useForMember'); | |
2333 | ||
2334 | // store the fact that this is a membership and membership type is selected | |
c6f840fa | 2335 | if ($this->isMembershipSelected($membershipParams)) { |
75637f22 EM |
2336 | if (!$this->_useForMember) { |
2337 | $this->assign('membership_assign', TRUE); | |
2338 | $this->set('membershipTypeID', $this->_params['selectMembership']); | |
2339 | } | |
2340 | ||
2341 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
2342 | $membershipParams['is_test'] = 1; | |
2343 | } | |
2344 | if ($this->_params['is_pay_later']) { | |
2345 | $membershipParams['is_pay_later'] = 1; | |
2346 | } | |
75637f22 | 2347 | |
2dc204ea | 2348 | if (isset($this->_params['onbehalf_contact_id'])) { |
2349 | $membershipParams['onbehalf_contact_id'] = $this->_params['onbehalf_contact_id']; | |
2350 | } | |
75637f22 EM |
2351 | //inherit campaign from contribution page. |
2352 | if (!array_key_exists('campaign_id', $membershipParams)) { | |
9c1bc317 | 2353 | $membershipParams['campaign_id'] = $this->_values['campaign_id'] ?? NULL; |
75637f22 EM |
2354 | } |
2355 | ||
3be4a20e | 2356 | $this->_params = CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $membershipParams, TRUE); |
f2bca231 | 2357 | $this->doMembershipProcessing($contactID, $membershipParams, $premiumParams, $this->_lineItem); |
75637f22 EM |
2358 | } |
2359 | else { | |
2360 | // at this point we've created a contact and stored its address etc | |
2361 | // all the payment processors expect the name and address to be in the | |
2362 | // so we copy stuff over to first_name etc. | |
2363 | $paymentParams = $this->_params; | |
f2bca231 | 2364 | // Make it explict that we are letting the processConfirm function figure out the line items. |
2365 | $paymentParams['skipLineItem'] = 0; | |
75637f22 | 2366 | |
1659de20 PN |
2367 | if (!isset($paymentParams['line_item'])) { |
2368 | $paymentParams['line_item'] = $this->_lineItem; | |
2369 | } | |
2370 | ||
75637f22 EM |
2371 | if (!empty($paymentParams['onbehalf']) && |
2372 | is_array($paymentParams['onbehalf']) | |
2373 | ) { | |
2374 | foreach ($paymentParams['onbehalf'] as $key => $value) { | |
2375 | if (strstr($key, 'custom_')) { | |
2376 | $this->_params[$key] = $value; | |
2377 | } | |
2378 | } | |
75637f22 | 2379 | } |
10490499 | 2380 | |
1a3545fa | 2381 | $result = $this->processConfirm($paymentParams, |
5fb28746 | 2382 | $contactID, |
46763692 | 2383 | $this->wrangleFinancialTypeID($this->_values['financial_type_id']), |
449f4c90 | 2384 | ($this->_mode == 'test') ? 1 : 0, |
cbbbf0ef | 2385 | $paymentParams['is_recur'] ?? NULL |
75637f22 | 2386 | ); |
82583880 | 2387 | |
0193ebdb | 2388 | if (empty($result['is_payment_failure'])) { |
2389 | // @todo move premium processing to complete transaction if it truly is an 'after' action. | |
2390 | $this->postProcessPremium($premiumParams, $result['contribution']); | |
2391 | } | |
2392 | if (!empty($result['contribution'])) { | |
04b24bb5 | 2393 | // It seems this line is hit when there is a zero dollar transaction & in tests, not sure when else. |
0193ebdb | 2394 | $this->completeTransaction($result, $result['contribution']->id); |
a8a4259e EM |
2395 | } |
2396 | return $result; | |
75637f22 EM |
2397 | } |
2398 | } | |
2399 | ||
c6f840fa MWMC |
2400 | /** |
2401 | * Return True/False if we have a membership selected on the contribution page | |
2402 | * @param array $membershipParams | |
2403 | * | |
2404 | * @return bool | |
2405 | */ | |
2406 | private function isMembershipSelected($membershipParams) { | |
2407 | $priceFieldIds = $this->get('memberPriceFieldIDS'); | |
2408 | if ((!empty($membershipParams['selectMembership']) && $membershipParams['selectMembership'] != 'no_thanks') | |
2409 | && empty($priceFieldIds)) { | |
2410 | return TRUE; | |
2411 | } | |
2412 | else { | |
2413 | $membershipParams = $this->getMembershipParamsFromPriceSet($membershipParams); | |
2414 | } | |
2415 | return !empty($membershipParams['selectMembership']); | |
2416 | } | |
2417 | ||
2418 | /** | |
2419 | * Extract the selected memberships from a priceSet | |
2420 | * | |
2421 | * @param array $membershipParams | |
2422 | * | |
2423 | * @return array | |
2424 | */ | |
2425 | private function getMembershipParamsFromPriceSet($membershipParams) { | |
2426 | $priceFieldIds = $this->get('memberPriceFieldIDS'); | |
2427 | if (empty($priceFieldIds)) { | |
2428 | return $membershipParams; | |
2429 | } | |
2430 | $membershipParams['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceFieldIds['id'], 'financial_type_id'); | |
2431 | unset($priceFieldIds['id']); | |
2432 | $membershipTypeIds = []; | |
2433 | $membershipTypeTerms = []; | |
2434 | foreach ($priceFieldIds as $priceFieldId) { | |
2435 | $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id'); | |
2436 | if ($membershipTypeId) { | |
2437 | $membershipTypeIds[] = $membershipTypeId; | |
2438 | $term = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_num_terms') ?: 1; | |
2439 | $membershipTypeTerms[$membershipTypeId] = ($term > 1) ? $term : 1; | |
2440 | } | |
2441 | } | |
2442 | $membershipParams['selectMembership'] = $membershipTypeIds; | |
2443 | $membershipParams['types_terms'] = $membershipTypeTerms; | |
2444 | return $membershipParams; | |
2445 | } | |
2446 | ||
7dd9ca37 EM |
2447 | /** |
2448 | * Membership processing section. | |
2449 | * | |
2450 | * This is in a separate function as part of a move towards refactoring. | |
2451 | * | |
2452 | * @param int $contactID | |
2453 | * @param array $membershipParams | |
2454 | * @param array $premiumParams | |
f2bca231 | 2455 | * @param array $formLineItems |
7dd9ca37 | 2456 | */ |
f2bca231 | 2457 | protected function doMembershipProcessing($contactID, $membershipParams, $premiumParams, $formLineItems) { |
0d9ff2d4 | 2458 | // This could be set by a hook. |
2459 | if (!empty($this->_params['installments'])) { | |
2460 | $membershipParams['installments'] = $this->_params['installments']; | |
2461 | } | |
7dd9ca37 EM |
2462 | // added new parameter for cms user contact id, needed to distinguish behaviour for on behalf of sign-ups |
2463 | if (isset($this->_params['related_contact'])) { | |
2464 | $membershipParams['cms_contactID'] = $this->_params['related_contact']; | |
2465 | } | |
2466 | else { | |
2467 | $membershipParams['cms_contactID'] = $contactID; | |
2468 | } | |
2469 | ||
2470 | if (!empty($membershipParams['onbehalf']) && | |
2471 | is_array($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id']) | |
2472 | ) { | |
2473 | $this->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id']; | |
2474 | } | |
2475 | ||
be2fb01f | 2476 | $customFieldsFormatted = $fieldTypes = []; |
7dd9ca37 EM |
2477 | if (!empty($membershipParams['onbehalf']) && |
2478 | is_array($membershipParams['onbehalf']) | |
2479 | ) { | |
2480 | foreach ($membershipParams['onbehalf'] as $key => $value) { | |
2481 | if (strstr($key, 'custom_')) { | |
2482 | $customFieldId = explode('_', $key); | |
2483 | CRM_Core_BAO_CustomField::formatCustomField( | |
2484 | $customFieldId[1], | |
2485 | $customFieldsFormatted, | |
2486 | $value, | |
2487 | 'Membership', | |
2488 | NULL, | |
2489 | $contactID | |
2490 | ); | |
2491 | } | |
2492 | } | |
be2fb01f | 2493 | $fieldTypes = ['Contact', 'Organization', 'Membership']; |
7dd9ca37 EM |
2494 | } |
2495 | ||
c6f840fa | 2496 | $membershipParams = $this->getMembershipParamsFromPriceSet($membershipParams); |
7dd9ca37 EM |
2497 | if (!empty($membershipParams['selectMembership'])) { |
2498 | // CRM-12233 | |
f2bca231 | 2499 | $membershipLineItems = $formLineItems; |
7dd9ca37 | 2500 | if ($this->_separateMembershipPayment && $this->_values['amount_block_is_active']) { |
be2fb01f | 2501 | $membershipLineItems = []; |
7dd9ca37 EM |
2502 | foreach ($this->_values['fee'] as $key => $feeValues) { |
2503 | if ($feeValues['name'] == 'membership_amount') { | |
2504 | $fieldId = $this->_params['price_' . $key]; | |
2505 | $membershipLineItems[$this->_priceSetId][$fieldId] = $this->_lineItem[$this->_priceSetId][$fieldId]; | |
2506 | unset($this->_lineItem[$this->_priceSetId][$fieldId]); | |
2507 | break; | |
2508 | } | |
2509 | } | |
2510 | } | |
4b6c8c1e | 2511 | try { |
f2bca231 | 2512 | $this->processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, $membershipLineItems); |
4b6c8c1e | 2513 | } |
d23860d8 SP |
2514 | catch (\Civi\Payment\Exception\PaymentProcessorException $e) { |
2515 | CRM_Core_Session::singleton()->setStatus($e->getMessage()); | |
2516 | if (!empty($this->_contributionID)) { | |
2517 | CRM_Contribute_BAO_Contribution::failPayment($this->_contributionID, | |
2518 | $contactID, $e->getMessage()); | |
2519 | } | |
2520 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=true&qfKey={$this->_params['qfKey']}")); | |
2521 | } | |
4b6c8c1e | 2522 | catch (CRM_Core_Exception $e) { |
2523 | CRM_Core_Session::singleton()->setStatus($e->getMessage()); | |
2524 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=true&qfKey={$this->_params['qfKey']}")); | |
2525 | } | |
7dd9ca37 EM |
2526 | if (!$this->_amount > 0.0 || !$membershipParams['amount']) { |
2527 | // we need to explicitly create a CMS user in case of free memberships | |
2528 | // since it is done under processConfirm for paid memberships | |
2529 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($membershipParams, | |
2530 | $membershipParams['cms_contactID'], | |
2531 | 'email-' . $this->_bltID | |
2532 | ); | |
2533 | } | |
2534 | } | |
2535 | } | |
2536 | ||
0193ebdb | 2537 | /** |
2538 | * Complete transaction if payment has been processed. | |
2539 | * | |
2540 | * Check the result for a success outcome & if paid then complete the transaction. | |
2541 | * | |
2542 | * Completing will trigger update of related entities and emails. | |
2543 | * | |
2544 | * @param array $result | |
2545 | * @param int $contributionID | |
2546 | * | |
ec7e3954 E |
2547 | * @throws \CiviCRM_API3_Exception |
2548 | * @throws \Exception | |
0193ebdb | 2549 | */ |
2550 | protected function completeTransaction($result, $contributionID) { | |
cbbbf0ef | 2551 | if (($result['payment_status_id'] ?? NULL) == 1) { |
0193ebdb | 2552 | try { |
be2fb01f | 2553 | civicrm_api3('contribution', 'completetransaction', [ |
a55e39e9 | 2554 | 'id' => $contributionID, |
6b409353 | 2555 | 'trxn_id' => $result['trxn_id'] ?? NULL, |
cbbbf0ef | 2556 | 'payment_processor_id' => $result['payment_processor_id'] ?? $this->_paymentProcessor['id'], |
a55e39e9 | 2557 | 'is_transactional' => FALSE, |
6b409353 CW |
2558 | 'fee_amount' => $result['fee_amount'] ?? NULL, |
2559 | 'receive_date' => $result['receive_date'] ?? NULL, | |
2560 | 'card_type_id' => $result['card_type_id'] ?? NULL, | |
2561 | 'pan_truncation' => $result['pan_truncation'] ?? NULL, | |
be2fb01f | 2562 | ]); |
0193ebdb | 2563 | } |
2564 | catch (CiviCRM_API3_Exception $e) { | |
2565 | if ($e->getErrorCode() != 'contribution_completed') { | |
f30b037a | 2566 | \Civi::log()->error('CRM_Contribute_Form_Contribution_Confirm::completeTransaction CiviCRM_API3_Exception: ' . $e->getMessage()); |
0193ebdb | 2567 | throw new CRM_Core_Exception('Failed to update contribution in database'); |
2568 | } | |
2569 | } | |
2570 | } | |
2571 | } | |
2572 | ||
77beddbe | 2573 | /** |
2574 | * Bounce the user back to retry when an error occurs. | |
2575 | * | |
2576 | * @param string $message | |
2577 | */ | |
2578 | protected function bounceOnError($message) { | |
2579 | CRM_Core_Session::singleton() | |
2580 | ->setStatus(ts("Payment Processor Error message :") . | |
2581 | $message); | |
2582 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', | |
2583 | "_qf_Main_display=true&qfKey={$this->_params['qfKey']}" | |
2584 | )); | |
2585 | } | |
2586 | ||
74c1b622 | 2587 | /** |
2588 | * Is a payment being made. | |
2589 | * | |
2590 | * Note that setting is_monetary on the form is somewhat legacy and the behaviour around this setting is confusing. It would be preferable | |
2591 | * to look for the amount only (assuming this cannot refer to payment in goats or other non-monetary currency | |
2592 | * @param CRM_Core_Form $form | |
2593 | * | |
2594 | * @return bool | |
2595 | */ | |
2596 | protected static function isPaymentTransaction($form) { | |
2597 | return $form->_amount >= 0.0; | |
2598 | } | |
2599 | ||
2600 | /** | |
2601 | * Process payment after confirmation. | |
2602 | * | |
74c1b622 | 2603 | * @param array $paymentParams |
2604 | * Array with payment related key. | |
2605 | * value pairs | |
2606 | * @param int $contactID | |
2607 | * Contact id. | |
2608 | * @param int $financialTypeID | |
2609 | * Financial type id. | |
2610 | * @param bool $isTest | |
2611 | * @param bool $isRecur | |
2612 | * | |
2613 | * @throws CRM_Core_Exception | |
2614 | * @throws Exception | |
2615 | * @return array | |
2616 | * associated array | |
74c1b622 | 2617 | */ |
1a3545fa | 2618 | public function processConfirm( |
74c1b622 | 2619 | &$paymentParams, |
2620 | $contactID, | |
2621 | $financialTypeID, | |
2622 | $isTest, | |
2623 | $isRecur | |
fb8d25da | 2624 | ): array { |
1a3545fa | 2625 | $form = $this; |
74c1b622 | 2626 | CRM_Core_Payment_Form::mapParams($form->_bltID, $form->_params, $paymentParams, TRUE); |
2627 | $isPaymentTransaction = self::isPaymentTransaction($form); | |
2628 | ||
2629 | $financialType = new CRM_Financial_DAO_FinancialType(); | |
2630 | $financialType->id = $financialTypeID; | |
2631 | $financialType->find(TRUE); | |
2632 | if ($financialType->is_deductible) { | |
2633 | $form->assign('is_deductible', TRUE); | |
2634 | $form->set('is_deductible', TRUE); | |
2635 | } | |
2636 | ||
2637 | // add some financial type details to the params list | |
2638 | // if folks need to use it | |
c2978fe0 | 2639 | $paymentParams['financial_type_id'] = $paymentParams['financialTypeID'] = $financialType->id; |
74c1b622 | 2640 | //CRM-15297 - contributionType is obsolete - pass financial type as well so people can deprecate it |
2641 | $paymentParams['financialType_name'] = $paymentParams['contributionType_name'] = $form->_params['contributionType_name'] = $financialType->name; | |
2642 | //CRM-11456 | |
2643 | $paymentParams['financialType_accounting_code'] = $paymentParams['contributionType_accounting_code'] = $form->_params['contributionType_accounting_code'] = CRM_Financial_BAO_FinancialAccount::getAccountingCode($financialTypeID); | |
2644 | $paymentParams['contributionPageID'] = $form->_params['contributionPageID'] = $form->_values['id']; | |
2645 | $paymentParams['contactID'] = $form->_params['contactID'] = $contactID; | |
2646 | ||
2647 | //fix for CRM-16317 | |
2648 | if (empty($form->_params['receive_date'])) { | |
2649 | $form->_params['receive_date'] = date('YmdHis'); | |
2650 | } | |
2651 | if (!empty($form->_params['start_date'])) { | |
2652 | $form->_params['start_date'] = date('YmdHis'); | |
2653 | } | |
2654 | $form->assign('receive_date', | |
2655 | CRM_Utils_Date::mysqlToIso($form->_params['receive_date']) | |
2656 | ); | |
2657 | ||
2658 | if (empty($form->_values['amount'])) { | |
2659 | // If the amount is not in _values[], set it | |
2660 | $form->_values['amount'] = $form->_params['amount']; | |
2661 | } | |
2662 | ||
2663 | if (isset($paymentParams['contribution_source'])) { | |
2664 | $paymentParams['source'] = $paymentParams['contribution_source']; | |
2665 | } | |
2666 | if ($isPaymentTransaction) { | |
2667 | $contributionParams = [ | |
2668 | 'id' => $paymentParams['contribution_id'] ?? NULL, | |
2669 | 'contact_id' => $contactID, | |
2670 | 'is_test' => $isTest, | |
2671 | 'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)), | |
2672 | ]; | |
2673 | ||
2674 | // CRM-21200: Don't overwrite contribution details during 'Pay now' payment | |
2675 | if (empty($form->_params['contribution_id'])) { | |
2676 | $contributionParams['contribution_page_id'] = $form->_id; | |
2677 | $contributionParams['campaign_id'] = CRM_Utils_Array::value('campaign_id', $paymentParams, CRM_Utils_Array::value('campaign_id', $form->_values)); | |
2678 | } | |
2679 | // In case of 'Pay now' payment, append the contribution source with new text 'Paid later via page ID: N.' | |
2680 | else { | |
2681 | // contribution.source only allows 255 characters so we are using ellipsify(...) to ensure it. | |
2682 | $contributionParams['source'] = CRM_Utils_String::ellipsify( | |
2683 | ts('Paid later via page ID: %1. %2', [ | |
2684 | 1 => $form->_id, | |
2685 | 2 => $contributionParams['source'], | |
2686 | ]), | |
2687 | // eventually activity.description append price information to source text so keep it 220 to ensure string length doesn't exceed 255 characters. | |
2688 | 220 | |
2689 | ); | |
2690 | } | |
2691 | ||
2692 | if (isset($paymentParams['line_item'])) { | |
2693 | // @todo make sure this is consisently set at this point. | |
2694 | $contributionParams['line_item'] = $paymentParams['line_item']; | |
2695 | } | |
2696 | if (!empty($form->_paymentProcessor)) { | |
2697 | $contributionParams['payment_instrument_id'] = $paymentParams['payment_instrument_id'] = $form->_paymentProcessor['payment_instrument_id']; | |
2698 | } | |
a281a8ea | 2699 | $contribution = $this->processFormContribution( |
74c1b622 | 2700 | $paymentParams, |
2701 | NULL, | |
2702 | $contributionParams, | |
2703 | $financialType, | |
74c1b622 | 2704 | $form->_bltID, |
2705 | $isRecur | |
2706 | ); | |
2ebe2e8c | 2707 | // CRM-13074 - create the CMSUser after the transaction is completed as it |
2708 | // is not appropriate to delete a valid contribution if a user create problem occurs | |
99ee765c | 2709 | if (isset($this->_params['related_contact'])) { |
2710 | $contactID = $this->_params['related_contact']; | |
2ebe2e8c | 2711 | } |
99ee765c | 2712 | elseif (isset($this->_params['cms_contactID'])) { |
2713 | $contactID = $this->_params['cms_contactID']; | |
2ebe2e8c | 2714 | } |
99ee765c | 2715 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($this->_params, |
2ebe2e8c | 2716 | $contactID, |
2717 | 'email-' . $form->_bltID | |
2718 | ); | |
74c1b622 | 2719 | |
2720 | $paymentParams['item_name'] = $form->_params['description']; | |
2721 | ||
2722 | $paymentParams['qfKey'] = empty($paymentParams['qfKey']) ? $form->controller->_key : $paymentParams['qfKey']; | |
2723 | if ($paymentParams['skipLineItem']) { | |
2724 | // We are not processing the line item here because we are processing a membership. | |
2725 | // Do not continue with contribution processing in this function. | |
2726 | return ['contribution' => $contribution]; | |
2727 | } | |
2728 | ||
2729 | $paymentParams['contributionID'] = $contribution->id; | |
2730 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; | |
2731 | ||
2732 | if (!empty($form->_params['is_recur']) && $contribution->contribution_recur_id) { | |
2733 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; | |
2734 | } | |
2735 | if (isset($paymentParams['contribution_source'])) { | |
2736 | $form->_params['source'] = $paymentParams['contribution_source']; | |
2737 | } | |
2738 | ||
2739 | // get the price set values for receipt. | |
2740 | if ($form->_priceSetId && $form->_lineItem) { | |
2741 | $form->_values['lineItem'] = $form->_lineItem; | |
2742 | $form->_values['priceSetID'] = $form->_priceSetId; | |
2743 | } | |
2744 | ||
2745 | $form->_values['contribution_id'] = $contribution->id; | |
2746 | $form->_values['contribution_page_id'] = $contribution->contribution_page_id; | |
2747 | ||
2748 | if (!empty($form->_paymentProcessor)) { | |
2749 | try { | |
2750 | $payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor); | |
2751 | if ($form->_contributeMode == 'notify') { | |
2752 | // We want to get rid of this & make it generic - eg. by making payment processing the last thing | |
2753 | // and always calling it first. | |
2754 | $form->postProcessHook(); | |
2755 | } | |
2756 | $result = $payment->doPayment($paymentParams); | |
2757 | $form->_params = array_merge($form->_params, $result); | |
2758 | $form->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $result)); | |
2759 | if (!empty($result['trxn_id'])) { | |
2760 | $contribution->trxn_id = $result['trxn_id']; | |
2761 | } | |
2762 | if (!empty($result['payment_status_id'])) { | |
2763 | $contribution->payment_status_id = $result['payment_status_id']; | |
2764 | } | |
2765 | $result['contribution'] = $contribution; | |
2766 | if ($result['payment_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending') | |
2767 | && $payment->isSendReceiptForPending()) { | |
2768 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, | |
2769 | $form->_values, | |
2770 | $contribution->is_test | |
2771 | ); | |
2772 | } | |
2773 | return $result; | |
2774 | } | |
2775 | catch (\Civi\Payment\Exception\PaymentProcessorException $e) { | |
2776 | // Clean up DB as appropriate. | |
2777 | if (!empty($paymentParams['contributionID'])) { | |
2778 | CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'], | |
2779 | $paymentParams['contactID'], $e->getMessage()); | |
2780 | } | |
2781 | if (!empty($paymentParams['contributionRecurID'])) { | |
2782 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']); | |
2783 | } | |
2784 | ||
2785 | $result['is_payment_failure'] = TRUE; | |
2786 | $result['error'] = $e; | |
2787 | return $result; | |
2788 | } | |
2789 | } | |
2790 | } | |
2791 | ||
2792 | // Only pay later or unpaid should reach this point, although pay later likely does not & is handled via the | |
2793 | // manual processor, so it's unclear what this set is for and whether the following send ever fires. | |
2794 | $form->set('params', $form->_params); | |
2795 | ||
2796 | if ($form->_params['amount'] == 0) { | |
2797 | // This is kind of a back-up for pay-later $0 transactions. | |
2798 | // In other flows they pick up the manual processor & get dealt with above (I | |
2799 | // think that might be better...). | |
2800 | return [ | |
2801 | 'payment_status_id' => 1, | |
2802 | 'contribution' => $contribution, | |
2803 | 'payment_processor_id' => 0, | |
2804 | ]; | |
2805 | } | |
2806 | ||
2807 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, | |
2808 | $form->_values, | |
2809 | $contribution->is_test | |
2810 | ); | |
2811 | } | |
2812 | ||
ddb56033 EM |
2813 | /** |
2814 | * Temporary function to allow unit tests to access function being refactored away. | |
2815 | * | |
2816 | * @param int $contactID | |
2817 | * @param int $membershipTypeID | |
2818 | * @param int $membershipID | |
2819 | * | |
2820 | * @return array | |
2821 | * @throws \CRM_Core_Exception | |
2822 | * @throws \CiviCRM_API3_Exception | |
2823 | */ | |
2824 | public static function unitTestAccessTolegacyProcessMembership($contactID, $membershipTypeID, $membershipID = NULL) { | |
2825 | return self::legacyProcessMembership( | |
2826 | $contactID, | |
2827 | $membershipTypeID, | |
2828 | 0, | |
2829 | NULL, | |
2830 | NULL, | |
2831 | NULL, | |
2832 | 1, | |
2833 | $membershipID, | |
2834 | NULL, | |
2835 | NULL, | |
2836 | FALSE, | |
2837 | NULL | |
2838 | ); | |
2839 | } | |
2840 | ||
5187a13c EM |
2841 | /** |
2842 | * Interim function for processing memberships - this is being refactored out of existence. | |
2843 | * | |
2844 | * @param int $contactID | |
2845 | * @param int $membershipTypeID | |
2846 | * @param bool $is_test | |
2847 | * @param string $changeToday | |
2848 | * @param int $modifiedID | |
2849 | * @param $customFieldsFormatted | |
2850 | * @param $numRenewTerms | |
2851 | * @param int $membershipID | |
2852 | * @param $pending | |
2853 | * @param int $contributionRecurID | |
2854 | * @param $membershipSource | |
2855 | * @param $isPayLater | |
2856 | * @param array $memParams | |
2857 | * @param null|CRM_Contribute_BAO_Contribution $contribution | |
2858 | * @param array $lineItems | |
2859 | * | |
2860 | * @return array | |
2861 | * @throws \CRM_Core_Exception | |
2862 | * @throws \CiviCRM_API3_Exception | |
2863 | */ | |
ddb56033 | 2864 | protected static function legacyProcessMembership($contactID, $membershipTypeID, $is_test, $changeToday, $modifiedID, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $contributionRecurID, $membershipSource, $isPayLater, $memParams = [], $contribution = NULL, $lineItems = []) { |
5187a13c EM |
2865 | $renewalMode = $updateStatusId = FALSE; |
2866 | $allStatus = CRM_Member_PseudoConstant::membershipStatus(); | |
2867 | $format = '%Y%m%d'; | |
2868 | $statusFormat = '%Y-%m-%d'; | |
2869 | $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipType($membershipTypeID); | |
2870 | $dates = []; | |
2871 | $ids = []; | |
2872 | ||
2873 | // CRM-7297 - allow membership type to be be changed during renewal so long as the parent org of new membershipType | |
2874 | // is the same as the parent org of an existing membership of the contact | |
2875 | $currentMembership = CRM_Member_BAO_Membership::getContactMembership($contactID, $membershipTypeID, | |
2876 | $is_test, $membershipID, TRUE | |
2877 | ); | |
2878 | if ($currentMembership) { | |
2879 | $renewalMode = TRUE; | |
2880 | ||
2881 | // Do NOT do anything. | |
2882 | //1. membership with status : PENDING/CANCELLED (CRM-2395) | |
2883 | //2. Paylater/IPN renew. CRM-4556. | |
2884 | if ($pending || in_array($currentMembership['status_id'], [ | |
2885 | array_search('Pending', $allStatus), | |
2886 | // CRM-15475 | |
2887 | array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus(NULL, " name = 'Cancelled' ", 'name', FALSE, TRUE)), | |
2888 | ])) { | |
2889 | ||
2890 | $memParams = array_merge([ | |
2891 | 'id' => $currentMembership['id'], | |
2892 | 'contribution' => $contribution, | |
2893 | 'status_id' => $currentMembership['status_id'], | |
2894 | 'start_date' => $currentMembership['start_date'], | |
2895 | 'end_date' => $currentMembership['end_date'], | |
2896 | 'line_item' => $lineItems, | |
2897 | 'join_date' => $currentMembership['join_date'], | |
2898 | 'membership_type_id' => $membershipTypeID, | |
2899 | 'max_related' => !empty($membershipTypeDetails['max_related']) ? $membershipTypeDetails['max_related'] : NULL, | |
2900 | 'membership_activity_status' => ($pending || $isPayLater) ? 'Scheduled' : 'Completed', | |
2901 | ], $memParams); | |
2902 | if ($contributionRecurID) { | |
2903 | $memParams['contribution_recur_id'] = $contributionRecurID; | |
2904 | } | |
2905 | ||
2906 | $membership = CRM_Member_BAO_Membership::create($memParams); | |
2907 | return [$membership, $renewalMode, $dates]; | |
2908 | } | |
2909 | ||
2910 | // Check and fix the membership if it is STALE | |
2911 | CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday); | |
2912 | ||
2913 | // Now Renew the membership | |
2914 | if (!$currentMembership['is_current_member']) { | |
2915 | // membership is not CURRENT | |
2916 | ||
2917 | // CRM-7297 Membership Upsell - calculate dates based on new membership type | |
2918 | $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($currentMembership['id'], | |
2919 | $changeToday, | |
2920 | $membershipTypeID, | |
2921 | $numRenewTerms | |
2922 | ); | |
2923 | ||
2924 | $currentMembership['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format); | |
2925 | foreach (['start_date', 'end_date'] as $dateType) { | |
2926 | $currentMembership[$dateType] = $dates[$dateType] ?? NULL; | |
2927 | } | |
2928 | $currentMembership['is_test'] = $is_test; | |
2929 | ||
2930 | if (!empty($membershipSource)) { | |
2931 | $currentMembership['source'] = $membershipSource; | |
2932 | } | |
2933 | ||
2934 | if (!empty($currentMembership['id'])) { | |
2935 | $ids['membership'] = $currentMembership['id']; | |
2936 | } | |
2937 | $memParams = array_merge($currentMembership, $memParams); | |
2938 | $memParams['membership_type_id'] = $membershipTypeID; | |
2939 | ||
2940 | //set the log start date. | |
2941 | $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format); | |
2942 | } | |
2943 | else { | |
2944 | ||
2945 | // CURRENT Membership | |
2946 | $membership = new CRM_Member_DAO_Membership(); | |
2947 | $membership->id = $currentMembership['id']; | |
2948 | $membership->find(TRUE); | |
2949 | // CRM-7297 Membership Upsell - calculate dates based on new membership type | |
2950 | $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id, | |
2951 | $changeToday, | |
2952 | $membershipTypeID, | |
2953 | $numRenewTerms | |
2954 | ); | |
2955 | ||
2956 | // Insert renewed dates for CURRENT membership | |
2957 | $memParams['join_date'] = CRM_Utils_Date::isoToMysql($membership->join_date); | |
2958 | $memParams['start_date'] = CRM_Utils_Date::isoToMysql($membership->start_date); | |
2959 | $memParams['end_date'] = $dates['end_date'] ?? NULL; | |
2960 | $memParams['membership_type_id'] = $membershipTypeID; | |
2961 | ||
2962 | //set the log start date. | |
2963 | $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format); | |
2964 | ||
2965 | //CRM-18067 | |
2966 | if (!empty($membershipSource)) { | |
2967 | $memParams['source'] = $membershipSource; | |
2968 | } | |
2969 | elseif (empty($membership->source)) { | |
2970 | $memParams['source'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
2971 | $currentMembership['id'], | |
2972 | 'source' | |
2973 | ); | |
2974 | } | |
2975 | ||
2976 | if (!empty($currentMembership['id'])) { | |
2977 | $ids['membership'] = $currentMembership['id']; | |
2978 | } | |
2979 | $memParams['membership_activity_status'] = ($pending || $isPayLater) ? 'Scheduled' : 'Completed'; | |
2980 | } | |
2981 | } | |
2982 | else { | |
2983 | // NEW Membership | |
2984 | $memParams = array_merge([ | |
2985 | 'contact_id' => $contactID, | |
2986 | 'membership_type_id' => $membershipTypeID, | |
2987 | ], $memParams); | |
2988 | ||
2989 | if (!$pending) { | |
2990 | $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membershipTypeID, NULL, NULL, NULL, $numRenewTerms); | |
2991 | ||
2992 | foreach (['join_date', 'start_date', 'end_date'] as $dateType) { | |
2993 | $memParams[$dateType] = $dates[$dateType] ?? NULL; | |
2994 | } | |
2995 | ||
2996 | $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate(CRM_Utils_Date::customFormat($dates['start_date'], | |
2997 | $statusFormat | |
2998 | ), | |
2999 | CRM_Utils_Date::customFormat($dates['end_date'], | |
3000 | $statusFormat | |
3001 | ), | |
3002 | CRM_Utils_Date::customFormat($dates['join_date'], | |
3003 | $statusFormat | |
3004 | ), | |
3005 | 'now', | |
3006 | TRUE, | |
3007 | $membershipTypeID, | |
3008 | $memParams | |
3009 | ); | |
3010 | $updateStatusId = $status['id'] ?? NULL; | |
3011 | } | |
3012 | else { | |
3013 | // if IPN/Pay-Later set status to: PENDING | |
3014 | $updateStatusId = array_search('Pending', $allStatus); | |
3015 | } | |
3016 | ||
3017 | if (!empty($membershipSource)) { | |
3018 | $memParams['source'] = $membershipSource; | |
3019 | } | |
3020 | $memParams['is_test'] = $is_test; | |
3021 | $memParams['is_pay_later'] = $isPayLater; | |
3022 | } | |
3023 | // Putting this in an IF is precautionary as it seems likely that it would be ignored if empty, but | |
3024 | // perhaps shouldn't be? | |
3025 | if ($contributionRecurID) { | |
3026 | $memParams['contribution_recur_id'] = $contributionRecurID; | |
3027 | } | |
3028 | //CRM-4555 | |
3029 | //if we decided status here and want to skip status | |
3030 | //calculation in create( ); then need to pass 'skipStatusCal'. | |
3031 | if ($updateStatusId) { | |
3032 | $memParams['status_id'] = $updateStatusId; | |
3033 | $memParams['skipStatusCal'] = TRUE; | |
3034 | } | |
3035 | ||
3036 | //since we are renewing, | |
3037 | //make status override false. | |
3038 | $memParams['is_override'] = FALSE; | |
3039 | ||
3040 | //CRM-4027, create log w/ individual contact. | |
3041 | if ($modifiedID) { | |
3042 | // @todo this param is likely unused now. | |
3043 | $memParams['is_for_organization'] = TRUE; | |
3044 | } | |
3045 | $params['modified_id'] = $modifiedID ?? $contactID; | |
3046 | ||
3047 | $memParams['contribution'] = $contribution; | |
3048 | $memParams['custom'] = $customFieldsFormatted; | |
3049 | // Load all line items & process all in membership. Don't do in contribution. | |
3050 | // Relevant tests in api_v3_ContributionPageTest. | |
3051 | $memParams['line_item'] = $lineItems; | |
3052 | // @todo stop passing $ids (membership and userId may be set by this point) | |
3053 | $membership = CRM_Member_BAO_Membership::create($memParams, $ids); | |
3054 | ||
3055 | // not sure why this statement is here, seems quite odd :( - Lobo: 12/26/2010 | |
3056 | // related to: http://forum.civicrm.org/index.php/topic,11416.msg49072.html#msg49072 | |
3057 | $membership->find(TRUE); | |
3058 | ||
3059 | return [$membership, $renewalMode, $dates]; | |
3060 | } | |
3061 | ||
6a488035 | 3062 | } |