Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
bef9421f | 37 | * This class generates form components for processing a Contribution |
6a488035 TO |
38 | * |
39 | */ | |
40 | class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_ContributionBase { | |
41 | ||
42 | /** | |
874c9be7 | 43 | * Define default MembershipType Id |
6a488035 TO |
44 | */ |
45 | public $_defaultMemTypeId; | |
46 | ||
47 | public $_relatedOrganizationFound; | |
48 | ||
78f0ee1b CW |
49 | public $_onBehalfRequired = FALSE; |
50 | public $_onbehalf = FALSE; | |
6a488035 TO |
51 | public $_paymentProcessors; |
52 | protected $_defaults; | |
53 | ||
54 | public $_membershipTypeValues; | |
55 | ||
56 | public $_useForMember; | |
57 | ||
dde5a0ef | 58 | /** |
100fef9d | 59 | * Array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl |
dde5a0ef EM |
60 | * @var array |
61 | */ | |
62 | public $_paymentFields = array(); | |
63 | ||
e50ee6a3 | 64 | protected $_paymentProcessorID; |
7c8cc461 | 65 | protected $_snippet; |
6a488035 TO |
66 | |
67 | /** | |
100fef9d | 68 | * Set variables up before form is built |
6a488035 TO |
69 | * |
70 | * @return void | |
6a488035 TO |
71 | */ |
72 | public function preProcess() { | |
73 | parent::preProcess(); | |
74 | ||
7bf9cde2 | 75 | self::preProcessPaymentOptions($this); |
6a488035 TO |
76 | |
77 | // Make the contributionPageID avilable to the template | |
78 | $this->assign('contributionPageID', $this->_id); | |
79 | $this->assign('isShare', CRM_Utils_Array::value('is_share', $this->_values)); | |
80 | $this->assign('isConfirmEnabled', CRM_Utils_Array::value('is_confirm_enabled', $this->_values)); | |
81 | ||
82 | // make sure we have right permission to edit this user | |
da8d9879 | 83 | $csContactID = $this->getContactID(); |
353ffa53 | 84 | $reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); |
6a488035 TO |
85 | $mainDisplay = CRM_Utils_Request::retrieve('_qf_Main_display', 'Boolean', CRM_Core_DAO::$_nullObject); |
86 | ||
6a488035 TO |
87 | if ($reset) { |
88 | $this->assign('reset', $reset); | |
89 | } | |
90 | ||
91 | if ($mainDisplay) { | |
92 | $this->assign('mainDisplay', $mainDisplay); | |
93 | } | |
94 | ||
78f0ee1b CW |
95 | // Possible values for 'is_for_organization': |
96 | // * 0 - org profile disabled | |
97 | // * 1 - org profile optional | |
98 | // * 2 - org profile required | |
6a488035 | 99 | $this->_onbehalf = FALSE; |
78f0ee1b CW |
100 | if (!empty($this->_values['is_for_organization'])) { |
101 | if ($this->_values['is_for_organization'] == 2) { | |
102 | $this->_onBehalfRequired = TRUE; | |
103 | } | |
104 | // Add organization profile if 1 of the following are true: | |
105 | // If the org profile is required | |
106 | if ($this->_onBehalfRequired || | |
107 | // Or we are building the form for the first time | |
108 | empty($_POST) || | |
109 | // Or the user has submitted the form and checked the "On Behalf" checkbox | |
110 | !empty($_POST['is_for_organization']) | |
6a488035 | 111 | ) { |
78f0ee1b CW |
112 | $this->_onbehalf = TRUE; |
113 | CRM_Contribute_Form_Contribution_OnBehalfOf::preProcess($this); | |
6a488035 TO |
114 | } |
115 | } | |
78f0ee1b | 116 | $this->assign('onBehalfRequired', $this->_onBehalfRequired); |
6a488035 | 117 | |
8af73472 | 118 | if ($this->_honor_block_is_active) { |
9b10578b TO |
119 | CRM_Contact_Form_ProfileContact::preprocess($this); |
120 | } | |
121 | ||
ab234a8a CW |
122 | if ($this->_snippet) { |
123 | $this->assign('isOnBehalfCallback', CRM_Utils_Array::value('onbehalf', $_GET, FALSE)); | |
124 | return; | |
125 | } | |
133e2c99 | 126 | |
78f0ee1b | 127 | if (!empty($this->_pcpInfo['id']) && !empty($this->_pcpInfo['intro_text'])) { |
6a488035 TO |
128 | $this->assign('intro_text', $this->_pcpInfo['intro_text']); |
129 | } | |
78f0ee1b | 130 | elseif (!empty($this->_values['intro_text'])) { |
6a488035 TO |
131 | $this->assign('intro_text', $this->_values['intro_text']); |
132 | } | |
133 | ||
134 | $qParams = "reset=1&id={$this->_id}"; | |
78f0ee1b | 135 | if ($pcpId = CRM_Utils_Array::value('pcp_id', $this->_pcpInfo)) { |
6a488035 TO |
136 | $qParams .= "&pcpId={$pcpId}"; |
137 | } | |
78f0ee1b | 138 | $this->assign('qParams', $qParams); |
6a488035 | 139 | |
78f0ee1b | 140 | if (!empty($this->_values['footer_text'])) { |
6a488035 TO |
141 | $this->assign('footer_text', $this->_values['footer_text']); |
142 | } | |
143 | ||
144 | //CRM-5001 | |
1da35d57 | 145 | //CRM-15787 |
146 | $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id); | |
147 | if (!empty($this->_values['is_for_organization']) && empty($member['is_active'])) { | |
6a488035 | 148 | $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.'); |
9d7cae22 DG |
149 | $ufJoinParams = array( |
150 | 'module' => 'onBehalf', | |
151 | 'entity_table' => 'civicrm_contribution_page', | |
152 | 'entity_id' => $this->_id, | |
153 | ); | |
154 | $onBehalfProfileIDs = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
155 | // getUFGroupIDs returns an array with the first item being the ID we need | |
156 | $onBehalfProfileID = $onBehalfProfileIDs[0]; | |
157 | if ($onBehalfProfileID) { | |
158 | $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($onBehalfProfileID); | |
6a488035 | 159 | foreach (array( |
353ffa53 TO |
160 | 'Individual', |
161 | 'Organization', | |
162 | 'Household' | |
163 | ) as $contactType) { | |
9d7cae22 DG |
164 | if (in_array($contactType, $onBehalfProfile) && |
165 | (in_array('Membership', $onBehalfProfile) || | |
166 | in_array('Contribution', $onBehalfProfile) | |
6a488035 TO |
167 | ) |
168 | ) { | |
169 | CRM_Core_Error::fatal($msg); | |
170 | } | |
171 | } | |
172 | } | |
173 | ||
174 | if ($postID = CRM_Utils_Array::value('custom_post_id', $this->_values)) { | |
175 | $postProfile = CRM_Core_BAO_UFGroup::profileGroups($postID); | |
176 | foreach (array( | |
353ffa53 TO |
177 | 'Individual', |
178 | 'Organization', | |
179 | 'Household' | |
180 | ) as $contactType) { | |
6a488035 TO |
181 | if (in_array($contactType, $postProfile) && |
182 | (in_array('Membership', $postProfile) || | |
183 | in_array('Contribution', $postProfile) | |
184 | ) | |
185 | ) { | |
186 | CRM_Core_Error::fatal($msg); | |
187 | } | |
188 | } | |
189 | } | |
190 | } | |
6a488035 TO |
191 | } |
192 | ||
186c9c17 | 193 | /** |
100fef9d | 194 | * Set the default values |
186c9c17 EM |
195 | * |
196 | * @return void | |
186c9c17 EM |
197 | */ |
198 | /** | |
186c9c17 | 199 | */ |
00be9182 | 200 | public function setDefaultValues() { |
6a488035 | 201 | // check if the user is registered and we have a contact ID |
da8d9879 | 202 | $contactID = $this->getContactID(); |
6a488035 | 203 | |
da8d9879 | 204 | if (!empty($contactID)) { |
6a488035 TO |
205 | $fields = array(); |
206 | $removeCustomFieldTypes = array('Contribution', 'Membership'); | |
207 | $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields(); | |
208 | ||
209 | // remove component related fields | |
210 | foreach ($this->_fields as $name => $dontCare) { | |
211 | //don't set custom data Used for Contribution (CRM-1344) | |
212 | if (substr($name, 0, 7) == 'custom_') { | |
213 | $id = substr($name, 7); | |
214 | if (!CRM_Core_BAO_CustomGroup::checkCustomField($id, $removeCustomFieldTypes)) { | |
215 | continue; | |
216 | } | |
217 | // ignore component fields | |
218 | } | |
219 | elseif (array_key_exists($name, $contribFields) || (substr($name, 0, 11) == 'membership_') || (substr($name, 0, 13) == 'contribution_')) { | |
220 | continue; | |
221 | } | |
222 | $fields[$name] = 1; | |
223 | } | |
fc60f30e RN |
224 | |
225 | if (!empty($fields)) { | |
472561f4 | 226 | CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults); |
fc60f30e RN |
227 | } |
228 | ||
9d665938 | 229 | $billingDefaults = $this->getProfileDefaults('Billing', $contactID); |
230 | $this->_defaults = array_merge($this->_defaults, $billingDefaults); | |
6a488035 TO |
231 | } |
232 | ||
233 | //set custom field defaults set by admin if value is not set | |
234 | if (!empty($this->_fields)) { | |
235 | //load default campaign from page. | |
236 | if (array_key_exists('contribution_campaign_id', $this->_fields)) { | |
237 | $this->_defaults['contribution_campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values); | |
238 | } | |
239 | ||
240 | //set custom field defaults | |
241 | foreach ($this->_fields as $name => $field) { | |
242 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
243 | if (!isset($this->_defaults[$name])) { | |
244 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults, | |
245 | NULL, CRM_Profile_Form::MODE_REGISTER | |
246 | ); | |
247 | } | |
248 | } | |
249 | } | |
250 | } | |
251 | ||
6a488035 TO |
252 | // // hack to simplify credit card entry for testing |
253 | // $this->_defaults['credit_card_type'] = 'Visa'; | |
254 | // $this->_defaults['amount'] = 168; | |
255 | // $this->_defaults['credit_card_number'] = '4111111111111111'; | |
256 | // $this->_defaults['cvv2'] = '000'; | |
f0bf6c15 | 257 | // $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05'); |
6a488035 TO |
258 | |
259 | // // hack to simplify direct debit entry for testing | |
260 | // $this->_defaults['account_holder'] = 'Max Müller'; | |
261 | // $this->_defaults['bank_account_number'] = '12345678'; | |
262 | // $this->_defaults['bank_identification_number'] = '12030000'; | |
263 | // $this->_defaults['bank_name'] = 'Bankname'; | |
264 | ||
265 | //build set default for pledge overdue payment. | |
a7488080 | 266 | if (!empty($this->_values['pledge_id'])) { |
133e2c99 | 267 | //get all pledge payment records of current pledge id. |
268 | $pledgePayments = array(); | |
269 | ||
270 | //used to record completed pledge payment ids used later for honor default | |
271 | $completedContributionIds = array(); | |
272 | ||
273 | $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($this->_values['pledge_id']); | |
6a488035 | 274 | |
6a488035 | 275 | $duePayment = FALSE; |
133e2c99 | 276 | foreach ($pledgePayments as $payId => $value) { |
277 | if ($value['status'] == 'Overdue') { | |
6a488035 TO |
278 | $this->_defaults['pledge_amount'][$payId] = 1; |
279 | } | |
133e2c99 | 280 | elseif (!$duePayment && $value['status'] == 'Pending') { |
6a488035 TO |
281 | $this->_defaults['pledge_amount'][$payId] = 1; |
282 | $duePayment = TRUE; | |
283 | } | |
133e2c99 | 284 | elseif ($value['status'] == 'Completed' && $value['contribution_id']) { |
285 | $completedContributionIds[] = $value['contribution_id']; | |
286 | } | |
287 | } | |
288 | ||
8af73472 | 289 | if ($this->_honor_block_is_active && count($completedContributionIds)) { |
133e2c99 | 290 | $softCredit = array(); |
291 | foreach ($completedContributionIds as $id) { | |
292 | $softCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id); | |
293 | } | |
294 | if (isset($softCredit['soft_credit'])) { | |
295 | $this->_defaults['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type']; | |
296 | ||
297 | //since honoree profile fieldname of fields are prefixed with 'honor' | |
298 | //we need to reformat the fieldname to append prefix during setting default values | |
299 | CRM_Core_BAO_UFGroup::setProfileDefaults( | |
300 | $softCredit['soft_credit'][1]['contact_id'], | |
301 | CRM_Core_BAO_UFGroup::getFields($this->_honoreeProfileId), | |
302 | $defaults | |
303 | ); | |
304 | foreach ($defaults as $fieldName => $value) { | |
305 | $this->_defaults['honor[' . $fieldName . ']'] = $value; | |
306 | } | |
307 | } | |
6a488035 TO |
308 | } |
309 | } | |
a7488080 | 310 | elseif (!empty($this->_values['pledge_block_id'])) { |
6a488035 TO |
311 | //set default to one time contribution. |
312 | $this->_defaults['is_pledge'] = 0; | |
313 | } | |
314 | ||
315 | // to process Custom data that are appended to URL | |
316 | $getDefaults = CRM_Core_BAO_CustomGroup::extractGetParams($this, "'Contact', 'Individual', 'Contribution'"); | |
03110609 | 317 | $this->_defaults = array_merge($this->_defaults, $getDefaults); |
6a488035 TO |
318 | |
319 | $config = CRM_Core_Config::singleton(); | |
320 | // set default country from config if no country set | |
a7488080 | 321 | if (empty($this->_defaults["billing_country_id-{$this->_bltID}"])) { |
6a488035 TO |
322 | $this->_defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
323 | } | |
324 | ||
325 | // set default state/province from config if no state/province set | |
a7488080 | 326 | if (empty($this->_defaults["billing_state_province_id-{$this->_bltID}"])) { |
6a488035 TO |
327 | $this->_defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; |
328 | } | |
329 | ||
6a488035 | 330 | if ($this->_priceSetId) { |
a1a68e64 | 331 | if (($this->_useForMember && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) { |
6a488035 TO |
332 | $selectedCurrentMemTypes = array(); |
333 | foreach ($this->_priceSet['fields'] as $key => $val) { | |
334 | foreach ($val['options'] as $keys => $values) { | |
335 | $opMemTypeId = CRM_Utils_Array::value('membership_type_id', $values); | |
336 | if ($opMemTypeId && | |
337 | in_array($opMemTypeId, $this->_currentMemberships) && | |
338 | !in_array($opMemTypeId, $selectedCurrentMemTypes) | |
339 | ) { | |
340 | if ($val['html_type'] == 'CheckBox') { | |
341 | $this->_defaults["price_{$key}"][$keys] = 1; | |
342 | } | |
343 | else { | |
344 | $this->_defaults["price_{$key}"] = $keys; | |
345 | } | |
346 | $selectedCurrentMemTypes[] = $values['membership_type_id']; | |
347 | } | |
a7488080 | 348 | elseif (!empty($values['is_default']) && |
6a488035 TO |
349 | !$opMemTypeId && |
350 | (!isset($this->_defaults["price_{$key}"]) || | |
351 | ($val['html_type'] == 'CheckBox' && !isset($this->_defaults["price_{$key}"][$keys])) | |
352 | ) | |
353 | ) { | |
354 | if ($val['html_type'] == 'CheckBox') { | |
355 | $this->_defaults["price_{$key}"][$keys] = 1; | |
356 | } | |
357 | else { | |
358 | $this->_defaults["price_{$key}"] = $keys; | |
359 | } | |
360 | } | |
361 | } | |
362 | } | |
363 | } | |
364 | else { | |
9da8dc8c | 365 | CRM_Price_BAO_PriceSet::setDefaultPriceSet($this, $this->_defaults); |
6a488035 TO |
366 | } |
367 | } | |
368 | ||
369 | if (!empty($this->_paymentProcessors)) { | |
370 | foreach ($this->_paymentProcessors as $pid => $value) { | |
a7488080 | 371 | if (!empty($value['is_default'])) { |
6a488035 TO |
372 | $this->_defaults['payment_processor'] = $pid; |
373 | } | |
374 | } | |
375 | } | |
376 | ||
377 | return $this->_defaults; | |
378 | } | |
379 | ||
380 | /** | |
c490a46a | 381 | * Build the form object |
6a488035 | 382 | * |
355ba699 | 383 | * @return void |
6a488035 TO |
384 | */ |
385 | public function buildQuickForm() { | |
4839c695 KJ |
386 | // build profiles first so that we can determine address fields etc |
387 | // and then show copy address checkbox | |
388 | $this->buildCustom($this->_values['custom_pre_id'], 'customPre'); | |
389 | $this->buildCustom($this->_values['custom_post_id'], 'customPost'); | |
390 | ||
7d613bb7 | 391 | if (!empty($this->_fields) && !empty($this->_values['custom_pre_id'])) { |
4839c695 KJ |
392 | $profileAddressFields = array(); |
393 | foreach ($this->_fields as $key => $value) { | |
7d613bb7 | 394 | CRM_Core_BAO_UFField::assignAddressField($key, $profileAddressFields, array('uf_group_id' => $this->_values['custom_pre_id'])); |
4839c695 KJ |
395 | } |
396 | $this->set('profileAddressFields', $profileAddressFields); | |
397 | } | |
398 | ||
7bf9cde2 | 399 | // Build payment processor form |
67399793 | 400 | if (empty($_GET['onbehalf'])) { |
7c8cc461 | 401 | CRM_Core_Payment_ProcessorForm::buildQuickForm($this); |
7bf9cde2 | 402 | // Return if we are in an ajax callback |
7c8cc461 CW |
403 | if ($this->_snippet) { |
404 | return; | |
405 | } | |
6a488035 TO |
406 | } |
407 | ||
408 | $config = CRM_Core_Config::singleton(); | |
78f0ee1b | 409 | |
6a488035 | 410 | if ($this->_onbehalf) { |
061a96be | 411 | CRM_Contribute_Form_Contribution_OnBehalfOf::buildQuickForm($this); |
ab234a8a CW |
412 | // Return if we are in an ajax callback |
413 | if ($this->_snippet) { | |
414 | return; | |
415 | } | |
6a488035 TO |
416 | } |
417 | ||
418 | $this->applyFilter('__ALL__', 'trim'); | |
419 | $this->add('text', "email-{$this->_bltID}", | |
420 | ts('Email Address'), | |
421 | array('size' => 30, 'maxlength' => 60, 'class' => 'email'), | |
422 | TRUE | |
423 | ); | |
424 | $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email'); | |
6a488035 | 425 | $pps = array(); |
67399793 | 426 | //@todo - this should be replaced by a check as to whether billing fields are set |
596bff78 | 427 | $onlinePaymentProcessorEnabled = FALSE; |
6a488035 | 428 | if (!empty($this->_paymentProcessors)) { |
596bff78 | 429 | foreach ($this->_paymentProcessors as $key => $name) { |
22e263ad | 430 | if ($name['billing_mode'] == 1) { |
596bff78 | 431 | $onlinePaymentProcessorEnabled = TRUE; |
432 | } | |
6a488035 TO |
433 | $pps[$key] = $name['name']; |
434 | } | |
435 | } | |
a7488080 | 436 | if (!empty($this->_values['is_pay_later'])) { |
6a488035 TO |
437 | $pps[0] = $this->_values['pay_later_text']; |
438 | } | |
439 | ||
440 | if (count($pps) > 1) { | |
441 | $this->addRadio('payment_processor', ts('Payment Method'), $pps, | |
442 | NULL, " ", TRUE | |
443 | ); | |
444 | } | |
445 | elseif (!empty($pps)) { | |
446 | $key = array_keys($pps); | |
447 | $key = array_pop($key); | |
448 | $this->addElement('hidden', 'payment_processor', $key); | |
449 | if ($key === 0) { | |
450 | $this->assign('is_pay_later', $this->_values['is_pay_later']); | |
451 | $this->assign('pay_later_text', $this->_values['pay_later_text']); | |
452 | } | |
453 | } | |
596bff78 | 454 | |
455 | $contactID = $this->getContactID(); | |
22e263ad | 456 | if ($this->getContactID() === '0') { |
596bff78 | 457 | $this->addCidZeroOptions($onlinePaymentProcessorEnabled); |
458 | } | |
6a488035 TO |
459 | //build pledge block. |
460 | $this->_useForMember = 0; | |
461 | //don't build membership block when pledge_id is passed | |
a7488080 | 462 | if (empty($this->_values['pledge_id'])) { |
6a488035 TO |
463 | $this->_separateMembershipPayment = FALSE; |
464 | if (in_array('CiviMember', $config->enableComponents)) { | |
465 | $isTest = 0; | |
466 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
467 | $isTest = 1; | |
468 | } | |
469 | ||
470 | if ($this->_priceSetId && | |
471 | (CRM_Core_Component::getComponentID('CiviMember') == CRM_Utils_Array::value('extends', $this->_priceSet)) | |
472 | ) { | |
473 | $this->_useForMember = 1; | |
474 | $this->set('useForMember', $this->_useForMember); | |
475 | } | |
476 | ||
477 | $this->_separateMembershipPayment = CRM_Member_BAO_Membership::buildMembershipBlock($this, | |
478 | $this->_id, | |
5b757295 | 479 | $this->_membershipContactID, |
6a488035 | 480 | TRUE, NULL, FALSE, |
5b757295 | 481 | $isTest |
6a488035 TO |
482 | ); |
483 | } | |
484 | $this->set('separateMembershipPayment', $this->_separateMembershipPayment); | |
485 | } | |
486 | $this->assign('useForMember', $this->_useForMember); | |
487 | // If we configured price set for contribution page | |
488 | // we are not allow membership signup as well as any | |
489 | // other contribution amount field, CRM-5095 | |
490 | if (isset($this->_priceSetId) && $this->_priceSetId) { | |
491 | $this->add('hidden', 'priceSetId', $this->_priceSetId); | |
492 | // build price set form. | |
493 | $this->set('priceSetId', $this->_priceSetId); | |
9da8dc8c | 494 | CRM_Price_BAO_PriceSet::buildPriceSet($this); |
6a488035 | 495 | if ($this->_values['is_monetary'] && |
353ffa53 TO |
496 | $this->_values['is_recur'] && empty($this->_values['pledge_id']) |
497 | ) { | |
6a488035 TO |
498 | self::buildRecur($this); |
499 | } | |
500 | } | |
6a488035 TO |
501 | |
502 | if ($this->_priceSetId) { | |
9da8dc8c | 503 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
6a488035 TO |
504 | if ($is_quick_config) { |
505 | $this->_useForMember = 0; | |
506 | $this->set('useForMember', $this->_useForMember); | |
507 | } | |
508 | } | |
509 | ||
510 | if ($this->_values['is_for_organization']) { | |
511 | $this->buildOnBehalfOrganization(); | |
512 | } | |
513 | ||
514 | //we allow premium for pledge during pledge creation only. | |
a7488080 | 515 | if (empty($this->_values['pledge_id'])) { |
6a488035 TO |
516 | CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, TRUE); |
517 | } | |
518 | ||
133e2c99 | 519 | //add honor block |
8af73472 | 520 | if ($this->_honor_block_is_active) { |
133e2c99 | 521 | $this->assign('honor_block_is_active', TRUE); |
133e2c99 | 522 | |
523 | //build soft-credit section | |
524 | CRM_Contribute_Form_SoftCredit::buildQuickForm($this); | |
525 | //build honoree profile section | |
526 | CRM_Contact_Form_ProfileContact::buildQuickForm($this); | |
6a488035 TO |
527 | } |
528 | ||
6a488035 TO |
529 | //don't build pledge block when mid is passed |
530 | if (!$this->_mid) { | |
531 | $config = CRM_Core_Config::singleton(); | |
8cc574cf | 532 | if (in_array('CiviPledge', $config->enableComponents) && !empty($this->_values['pledge_block_id'])) { |
6a488035 TO |
533 | CRM_Pledge_BAO_PledgeBlock::buildPledgeBlock($this); |
534 | } | |
535 | } | |
536 | ||
6a488035 | 537 | //to create an cms user |
1fcf2d71 | 538 | if (!$this->_contactID) { |
6a488035 TO |
539 | $createCMSUser = FALSE; |
540 | ||
541 | if ($this->_values['custom_pre_id']) { | |
542 | $profileID = $this->_values['custom_pre_id']; | |
543 | $createCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user'); | |
544 | } | |
545 | ||
546 | if (!$createCMSUser && | |
547 | $this->_values['custom_post_id'] | |
548 | ) { | |
549 | if (!is_array($this->_values['custom_post_id'])) { | |
550 | $profileIDs = array($this->_values['custom_post_id']); | |
551 | } | |
552 | else { | |
553 | $profileIDs = $this->_values['custom_post_id']; | |
554 | } | |
555 | foreach ($profileIDs as $pid) { | |
556 | if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) { | |
557 | $profileID = $pid; | |
558 | $createCMSUser = TRUE; | |
559 | break; | |
560 | } | |
561 | } | |
562 | } | |
563 | ||
564 | if ($createCMSUser) { | |
565 | CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE); | |
566 | } | |
567 | } | |
568 | if ($this->_pcpId) { | |
569 | if ($pcpSupporter = CRM_PCP_BAO_PCP::displayName($this->_pcpId)) { | |
b942a32a | 570 | $pcp_supporter_text = ts('This contribution is being made thanks to the effort of <strong>%1</strong>, who supports our campaign.', array(1 => $pcpSupporter)); |
eea141c0 DG |
571 | // Only tell people that can also create a PCP if the contribution page has a non-empty value in the "Create Personal Campaign Page link" field. |
572 | $text = CRM_PCP_BAO_PCP::getPcpBlockStatus($this->_id, 'contribute'); | |
22e263ad | 573 | if (!empty($text)) { |
b942a32a | 574 | $pcp_supporter_text .= ts("You can support it as well - once you complete the donation, you will be able to create your own Personal Campaign Page!"); |
eea141c0 DG |
575 | } |
576 | $this->assign('pcpSupporterText', $pcp_supporter_text); | |
6a488035 | 577 | } |
31ed2806 JM |
578 | $prms = array('id' => $this->_pcpId); |
579 | CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo); | |
580 | if ($pcpInfo['is_honor_roll']) { | |
4d327c2d | 581 | $this->assign('isHonor', TRUE); |
31ed2806 JM |
582 | $this->add('checkbox', 'pcp_display_in_roll', ts('Show my contribution in the public honor roll'), NULL, NULL, |
583 | array('onclick' => "showHideByValue('pcp_display_in_roll','','nameID|nickID|personalNoteID','block','radio',false); pcpAnonymous( );") | |
584 | ); | |
585 | $extraOption = array('onclick' => "return pcpAnonymous( );"); | |
353ffa53 TO |
586 | $elements = array(); |
587 | $elements[] = &$this->createElement('radio', NULL, '', ts('Include my name and message'), 0, $extraOption); | |
588 | $elements[] = &$this->createElement('radio', NULL, '', ts('List my contribution anonymously'), 1, $extraOption); | |
31ed2806 | 589 | $this->addGroup($elements, 'pcp_is_anonymous', NULL, ' '); |
f26a6b99 JM |
590 | $this->setDefaults(array('pcp_display_in_roll' => 1)); |
591 | $this->setDefaults(array('pcp_is_anonymous' => 1)); | |
31ed2806 JM |
592 | |
593 | $this->add('text', 'pcp_roll_nickname', ts('Name'), array('maxlength' => 30)); | |
594 | $this->add('textarea', 'pcp_personal_note', ts('Personal Note'), array('style' => 'height: 3em; width: 40em;')); | |
595 | } | |
6a488035 TO |
596 | } |
597 | ||
598 | //we have to load confirm contribution button in template | |
599 | //when multiple payment processor as the user | |
600 | //can toggle with payment processor selection | |
601 | $billingModePaymentProcessors = 0; | |
481a74f4 | 602 | if (!empty($this->_paymentProcessors)) { |
6a488035 TO |
603 | foreach ($this->_paymentProcessors as $key => $values) { |
604 | if ($values['billing_mode'] == CRM_Core_Payment::BILLING_MODE_BUTTON) { | |
605 | $billingModePaymentProcessors++; | |
606 | } | |
607 | } | |
608 | } | |
609 | ||
610 | if ($billingModePaymentProcessors && count($this->_paymentProcessors) == $billingModePaymentProcessors) { | |
611 | $allAreBillingModeProcessors = TRUE; | |
0db6c3e1 TO |
612 | } |
613 | else { | |
6a488035 TO |
614 | $allAreBillingModeProcessors = FALSE; |
615 | } | |
616 | ||
617 | if (!($allAreBillingModeProcessors && !$this->_values['is_pay_later'])) { | |
61189839 CW |
618 | $submitButton = array( |
619 | 'type' => 'upload', | |
c0321a2d | 620 | 'name' => CRM_Utils_Array::value('is_confirm_enabled', $this->_values) ? ts('Confirm Contribution') : ts('Contribute'), |
61189839 CW |
621 | 'spacing' => ' ', |
622 | 'isDefault' => TRUE, | |
6a488035 | 623 | ); |
61189839 CW |
624 | // Add submit-once behavior when confirm page disabled |
625 | if (empty($this->_values['is_confirm_enabled'])) { | |
626 | $submitButton['js'] = array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"); | |
627 | } | |
628 | $this->addButtons(array($submitButton)); | |
6a488035 TO |
629 | } |
630 | ||
631 | $this->addFormRule(array('CRM_Contribute_Form_Contribution_Main', 'formRule'), $this); | |
6a488035 TO |
632 | } |
633 | ||
6a488035 | 634 | /** |
100fef9d | 635 | * Build elements to enable pay on behalf of an organization. |
6a488035 | 636 | */ |
00be9182 | 637 | public function buildOnBehalfOrganization() { |
6a488035 TO |
638 | if ($this->_membershipContactID) { |
639 | $entityBlock = array('contact_id' => $this->_membershipContactID); | |
640 | CRM_Core_BAO_Location::getValues($entityBlock, $this->_defaults); | |
641 | } | |
642 | ||
78f0ee1b | 643 | if (!$this->_onBehalfRequired) { |
6a488035 TO |
644 | $this->addElement('checkbox', 'is_for_organization', |
645 | $this->_values['for_organization'], | |
646 | NULL, array('onclick' => "showOnBehalf( );") | |
647 | ); | |
648 | } | |
6a488035 TO |
649 | |
650 | $this->assign('is_for_organization', TRUE); | |
651 | $this->assign('urlPath', 'civicrm/contribute/transact'); | |
652 | } | |
653 | ||
654 | /** | |
100fef9d | 655 | * Build elements to collect information for recurring contributions |
6a488035 | 656 | * |
03110609 EM |
657 | * |
658 | * @param CRM_Core_Form $form | |
6a488035 | 659 | */ |
a5aef36c | 660 | public static function buildRecur(&$form) { |
6a488035 TO |
661 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionRecur'); |
662 | $className = get_class($form); | |
663 | ||
a5aef36c | 664 | $form->assign('is_recur_interval', CRM_Utils_Array::value('is_recur_interval', $form->_values)); |
665 | $form->assign('is_recur_installments', CRM_Utils_Array::value('is_recur_installments', $form->_values)); | |
6a488035 TO |
666 | |
667 | $form->add('checkbox', 'is_recur', ts('I want to contribute this amount'), NULL); | |
668 | ||
a7488080 | 669 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
6a488035 TO |
670 | $form->add('text', 'frequency_interval', ts('Every'), $attributes['frequency_interval']); |
671 | $form->addRule('frequency_interval', ts('Frequency must be a whole number (EXAMPLE: Every 3 months).'), 'integer'); | |
672 | } | |
673 | else { | |
674 | // make sure frequency_interval is submitted as 1 if given no choice to user. | |
675 | $form->add('hidden', 'frequency_interval', 1); | |
676 | } | |
677 | ||
678 | $frUnits = CRM_Utils_Array::value('recur_frequency_unit', $form->_values); | |
679 | if (empty($frUnits) && | |
680 | $className == 'CRM_Contribute_Form_Contribution' | |
681 | ) { | |
682 | $frUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
683 | CRM_Core_OptionGroup::values('recur_frequency_units') | |
684 | ); | |
685 | } | |
686 | ||
687 | $unitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $frUnits); | |
688 | ||
689 | // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit | |
22e263ad | 690 | if (sizeof($unitVals) == 1) { |
874c9be7 | 691 | $form->assign('one_frequency_unit', TRUE); |
6a488035 TO |
692 | $unit = $unitVals[0]; |
693 | $form->add('hidden', 'frequency_unit', $unit); | |
a7488080 | 694 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
6a488035 TO |
695 | $unit .= "(s)"; |
696 | } | |
a5aef36c | 697 | $form->assign('frequency_unit', $unit); |
0db6c3e1 TO |
698 | } |
699 | else { | |
874c9be7 | 700 | $form->assign('one_frequency_unit', FALSE); |
6a488035 TO |
701 | $units = array(); |
702 | $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units'); | |
703 | foreach ($unitVals as $key => $val) { | |
704 | if (array_key_exists($val, $frequencyUnits)) { | |
705 | $units[$val] = $frequencyUnits[$val]; | |
a7488080 | 706 | if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') { |
6a488035 TO |
707 | $units[$val] = "{$frequencyUnits[$val]}(s)"; |
708 | } | |
709 | } | |
710 | } | |
711 | $frequencyUnit = &$form->add('select', 'frequency_unit', NULL, $units); | |
712 | } | |
713 | ||
6a488035 TO |
714 | // FIXME: Ideally we should freeze select box if there is only |
715 | // one option but looks there is some problem /w QF freeze. | |
716 | //if ( count( $units ) == 1 ) { | |
717 | //$frequencyUnit->freeze( ); | |
718 | //} | |
719 | ||
720 | $form->add('text', 'installments', ts('installments'), | |
721 | $attributes['installments'] | |
722 | ); | |
723 | $form->addRule('installments', ts('Number of installments must be a whole number.'), 'integer'); | |
724 | } | |
725 | ||
726 | /** | |
100fef9d | 727 | * Global form rule |
6a488035 | 728 | * |
014c4014 TO |
729 | * @param array $fields |
730 | * The input form values. | |
731 | * @param array $files | |
732 | * The uploaded files if any. | |
2a6da8d7 EM |
733 | * @param $self |
734 | * | |
6a488035 | 735 | * |
72b3a70c CW |
736 | * @return bool|array |
737 | * true if no errors, else array of errors | |
6a488035 TO |
738 | * @static |
739 | */ | |
00be9182 | 740 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
741 | $errors = array(); |
742 | $amount = self::computeAmount($fields, $self); | |
743 | ||
8cc574cf | 744 | if ((!empty($fields['selectMembership']) && |
6a488035 TO |
745 | $fields['selectMembership'] != 'no_thanks' |
746 | ) || | |
8cc574cf | 747 | (!empty($fields['priceSetId']) && |
6a488035 TO |
748 | $self->_useForMember |
749 | ) | |
750 | ) { | |
4005a2ae | 751 | $lifeMember = CRM_Member_BAO_Membership::getAllContactMembership($self->_membershipContactID, FALSE, TRUE); |
6a488035 TO |
752 | |
753 | $membershipOrgDetails = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization(); | |
754 | ||
755 | $unallowedOrgs = array(); | |
756 | foreach (array_keys($lifeMember) as $memTypeId) { | |
757 | $unallowedOrgs[] = $membershipOrgDetails[$memTypeId]; | |
758 | } | |
759 | } | |
760 | ||
761 | //check for atleast one pricefields should be selected | |
a7488080 | 762 | if (!empty($fields['priceSetId'])) { |
9da8dc8c | 763 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
764 | $priceField->price_set_id = $fields['priceSetId']; |
765 | $priceField->orderBy('weight'); | |
766 | $priceField->find(); | |
767 | ||
768 | $check = array(); | |
769 | $membershipIsActive = TRUE; | |
770 | $previousId = $otherAmount = FALSE; | |
771 | while ($priceField->fetch()) { | |
772 | ||
773 | if ($self->_quickConfig && ($priceField->name == 'contribution_amount' || $priceField->name == 'membership_amount')) { | |
774 | $previousId = $priceField->id; | |
481a74f4 | 775 | if ($priceField->name == 'membership_amount' && !$priceField->is_active) { |
6a488035 TO |
776 | $membershipIsActive = FALSE; |
777 | } | |
778 | } | |
779 | if ($priceField->name == 'other_amount') { | |
8cc574cf | 780 | if ($self->_quickConfig && empty($fields["price_{$priceField->id}"]) && |
353ffa53 TO |
781 | array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"]) |
782 | ) { | |
6a488035 TO |
783 | $otherAmount = $priceField->id; |
784 | } | |
785 | elseif (!empty($fields["price_{$priceField->id}"])) { | |
ef88f444 | 786 | $otherAmountVal = CRM_Utils_Rule::cleanMoney($fields["price_{$priceField->id}"]); |
353ffa53 TO |
787 | $min = CRM_Utils_Array::value('min_amount', $self->_values); |
788 | $max = CRM_Utils_Array::value('max_amount', $self->_values); | |
6a488035 TO |
789 | if ($min && $otherAmountVal < $min) { |
790 | $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1', | |
353ffa53 | 791 | array(1 => $min) |
6a488035 TO |
792 | ); |
793 | } | |
794 | if ($max && $otherAmountVal > $max) { | |
795 | $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.', | |
353ffa53 | 796 | array(1 => $max) |
6a488035 TO |
797 | ); |
798 | } | |
799 | } | |
800 | } | |
801 | if (!empty($fields["price_{$priceField->id}"]) || ($previousId == $priceField->id && isset($fields["price_{$previousId}"]) | |
353ffa53 TO |
802 | && empty($fields["price_{$previousId}"])) |
803 | ) { | |
6a488035 TO |
804 | $check[] = $priceField->id; |
805 | } | |
806 | } | |
807 | ||
b5a62499 PN |
808 | $currentMemberships = NULL; |
809 | if ($membershipIsActive) { | |
810 | $is_test = $self->_mode != 'live' ? 1 : 0; | |
811 | $memContactID = $self->_membershipContactID; | |
7f7fa13a EM |
812 | |
813 | // For anonymous user check using dedupe rule | |
b5a62499 PN |
814 | // if user has Cancelled Membership |
815 | if (!$memContactID) { | |
816 | $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual'); | |
817 | $dedupeParams['check_permission'] = FALSE; | |
818 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); | |
819 | // if we find more than one contact, use the first one | |
820 | $memContactID = CRM_Utils_Array::value(0, $ids); | |
821 | } | |
822 | $currentMemberships = CRM_Member_BAO_Membership::getContactsCancelledMembership($memContactID, | |
823 | $is_test | |
824 | ); | |
7f7fa13a | 825 | |
b5a62499 PN |
826 | $errorText = 'Your %1 membership was previously cancelled and can not be renewed online. Please contact the site administrator for assistance.'; |
827 | foreach ($self->_values['fee'] as $fieldKey => $fieldValue) { | |
828 | if ($fieldValue['html_type'] != 'Text' && CRM_Utils_Array::value('price_' . $fieldKey, $fields)) { | |
829 | if (!is_array($fields['price_' . $fieldKey])) { | |
7f7fa13a | 830 | if (array_key_exists('membership_type_id', $fieldValue['options'][$fields['price_' . $fieldKey]]) |
353ffa53 TO |
831 | && in_array($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id'], $currentMemberships) |
832 | ) { | |
3a96b63b | 833 | $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$fields['price_' . $fieldKey]]['membership_type_id']))); |
b5a62499 PN |
834 | } |
835 | } | |
836 | else { | |
837 | foreach ($fields['price_' . $fieldKey] as $key => $ignore) { | |
7f7fa13a | 838 | if (array_key_exists('membership_type_id', $fieldValue['options'][$key]) |
353ffa53 TO |
839 | && in_array($fieldValue['options'][$key]['membership_type_id'], $currentMemberships) |
840 | ) { | |
b5a62499 PN |
841 | $errors['price_' . $fieldKey] = ts($errorText, array(1 => CRM_Member_PseudoConstant::membershipType($fieldValue['options'][$key]['membership_type_id']))); |
842 | } | |
843 | } | |
844 | } | |
845 | } | |
846 | } | |
847 | } | |
7f7fa13a | 848 | |
9ce8bcfc | 849 | // CRM-12233 |
665e5ec7 | 850 | if ($membershipIsActive && !$self->_membershipBlock['is_required'] |
353ffa53 TO |
851 | && $self->_values['amount_block_is_active'] |
852 | ) { | |
9ce8bcfc PN |
853 | $membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL; |
854 | foreach ($self->_values['fee'] as $fieldKey => $fieldValue) { | |
41f27e49 | 855 | // if 'No thank you' membership is selected then set $membershipFieldId |
9ce8bcfc PN |
856 | if ($fieldValue['name'] == 'membership_amount' && CRM_Utils_Array::value('price_' . $fieldKey, $fields) == 0) { |
857 | $membershipFieldId = $fieldKey; | |
858 | } | |
859 | elseif ($membershipFieldId) { | |
860 | if ($fieldValue['name'] == 'other_amount') { | |
861 | $otherFieldId = $fieldKey; | |
862 | } | |
863 | elseif ($fieldValue['name'] == 'contribution_amount') { | |
864 | $contributionFieldId = $fieldKey; | |
865 | } | |
665e5ec7 | 866 | |
9ce8bcfc PN |
867 | if (!$errorKey || CRM_Utils_Array::value('price_' . $contributionFieldId, $fields) == '0') { |
868 | $errorKey = $fieldKey; | |
869 | } | |
870 | } | |
871 | } | |
41f27e49 | 872 | // $membershipFieldId is set and additional amount is 'No thank you' or NULL then throw error |
8cc574cf | 873 | if ($membershipFieldId && !(CRM_Utils_Array::value('price_' . $contributionFieldId, $fields, -1) > 0) && empty($fields['price_' . $otherFieldId])) { |
1cd7e100 | 874 | $errors["price_{$errorKey}"] = ts('Additional Contribution is required.'); |
9ce8bcfc PN |
875 | } |
876 | } | |
6a488035 TO |
877 | if (empty($check)) { |
878 | if ($self->_useForMember == 1 && $membershipIsActive) { | |
879 | $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).'); | |
880 | } | |
881 | else { | |
882 | $errors['_qf_default'] = ts('Select at least one option from Contribution(s).'); | |
883 | } | |
884 | } | |
22e263ad | 885 | if ($otherAmount && !empty($check)) { |
6a488035 TO |
886 | $errors["price_{$otherAmount}"] = ts('Amount is required field.'); |
887 | } | |
888 | ||
889 | if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) { | |
890 | $priceFieldIDS = array(); | |
891 | $priceFieldMemTypes = array(); | |
892 | ||
893 | foreach ($self->_priceSet['fields'] as $priceId => $value) { | |
8cc574cf | 894 | if (!empty($fields['price_' . $priceId]) || ($self->_quickConfig && $value['name'] == 'membership_amount' && empty($self->_membershipBlock['is_required']))) { |
a7488080 | 895 | if (!empty($fields['price_' . $priceId]) && is_array($fields['price_' . $priceId])) { |
6a488035 TO |
896 | foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) { |
897 | if ($isSet) { | |
898 | $priceFieldIDS[] = $priceFldVal; | |
899 | } | |
900 | } | |
901 | } | |
8cc574cf | 902 | elseif (!$value['is_enter_qty'] && !empty($fields['price_' . $priceId])) { |
6a488035 TO |
903 | // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering |
904 | // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577 | |
905 | $priceFieldIDS[] = $fields['price_' . $priceId]; | |
906 | } | |
907 | ||
a7488080 | 908 | if (!empty($value['options'])) { |
6a488035 | 909 | foreach ($value['options'] as $val) { |
88c6259e | 910 | if (!empty($val['membership_type_id']) && ( |
fb4aa4d9 | 911 | ($fields['price_' . $priceId] == $val['id']) || |
912 | (isset($fields['price_' . $priceId]) && !empty($fields['price_' . $priceId][$val['id']])) | |
913 | ) | |
914 | ) { | |
6a488035 TO |
915 | $priceFieldMemTypes[] = $val['membership_type_id']; |
916 | } | |
917 | } | |
918 | } | |
919 | } | |
920 | } | |
921 | ||
922 | if (!empty($lifeMember)) { | |
923 | foreach ($priceFieldIDS as $priceFieldId) { | |
9da8dc8c | 924 | if (($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) && |
6a488035 TO |
925 | in_array($membershipOrgDetails[$id], $unallowedOrgs) |
926 | ) { | |
927 | $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.'); | |
928 | break; | |
929 | } | |
930 | } | |
931 | } | |
932 | ||
933 | if (!empty($priceFieldIDS)) { | |
934 | $ids = implode(',', $priceFieldIDS); | |
935 | ||
936 | $priceFieldIDS['id'] = $fields['priceSetId']; | |
937 | $self->set('memberPriceFieldIDS', $priceFieldIDS); | |
9da8dc8c | 938 | $count = CRM_Price_BAO_PriceSet::getMembershipCount($ids); |
6a488035 TO |
939 | foreach ($count as $id => $occurance) { |
940 | if ($occurance > 1) { | |
941 | $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity. Contact the site administrator if you need assistance.'); | |
942 | } | |
943 | } | |
944 | } | |
945 | ||
946 | if (empty($priceFieldMemTypes)) { | |
947 | $errors['_qf_default'] = ts('Please select at least one membership option.'); | |
948 | } | |
949 | } | |
950 | ||
9da8dc8c | 951 | CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'], |
6a488035 TO |
952 | $fields, $lineItem |
953 | ); | |
665e5ec7 | 954 | |
6a488035 TO |
955 | if ($fields['amount'] < 0) { |
956 | $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly'); | |
957 | } | |
958 | $amount = $fields['amount']; | |
959 | } | |
960 | ||
961 | if (isset($fields['selectProduct']) && | |
353ffa53 TO |
962 | $fields['selectProduct'] != 'no_thanks' |
963 | ) { | |
6a488035 TO |
964 | $productDAO = new CRM_Contribute_DAO_Product(); |
965 | $productDAO->id = $fields['selectProduct']; | |
966 | $productDAO->find(TRUE); | |
967 | $min_amount = $productDAO->min_contribution; | |
968 | ||
969 | if ($amount < $min_amount) { | |
970 | $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount))); | |
971 | CRM_Core_Session::setStatus($errors['selectProduct']); | |
972 | } | |
973 | } | |
974 | ||
a7488080 | 975 | if (!empty($fields['is_recur'])) { |
6a488035 TO |
976 | if ($fields['frequency_interval'] <= 0) { |
977 | $errors['frequency_interval'] = ts('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).'); | |
978 | } | |
979 | if ($fields['frequency_unit'] == '0') { | |
980 | $errors['frequency_unit'] = ts('Please select a period (e.g. months, years ...) for how often you want to make this recurring contribution (EXAMPLE: Every 3 MONTHS).'); | |
981 | } | |
982 | } | |
983 | ||
a7488080 | 984 | if (!empty($fields['is_recur']) && |
353ffa53 TO |
985 | CRM_Utils_Array::value('payment_processor', $fields) == 0 |
986 | ) { | |
6a488035 TO |
987 | $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.'); |
988 | } | |
989 | ||
a7488080 | 990 | if (!empty($fields['is_for_organization']) && |
6a488035 TO |
991 | !property_exists($self, 'organizationName') |
992 | ) { | |
993 | ||
a7488080 CW |
994 | if (empty($fields['onbehalf']['organization_name'])) { |
995 | if (!empty($fields['org_option']) && !$fields['onbehalfof_id']) { | |
6a488035 TO |
996 | $errors['organization_id'] = ts('Please select an organization or enter a new one.'); |
997 | } | |
a7488080 | 998 | elseif (empty($fields['org_option'])) { |
6a488035 TO |
999 | $errors['onbehalf']['organization_name'] = ts('Please enter the organization name.'); |
1000 | } | |
1001 | } | |
1002 | ||
1003 | foreach ($fields['onbehalf'] as $key => $value) { | |
1004 | if (strstr($key, 'email')) { | |
1005 | $emailLocType = explode('-', $key); | |
1006 | } | |
1007 | } | |
a7488080 | 1008 | if (empty($fields['onbehalf']["email-{$emailLocType[1]}"])) { |
6a488035 TO |
1009 | $errors['onbehalf']["email-{$emailLocType[1]}"] = ts('Organization email is required.'); |
1010 | } | |
1011 | } | |
1012 | ||
1013 | // validate PCP fields - if not anonymous, we need a nick name value | |
8cc574cf | 1014 | if ($self->_pcpId && !empty($fields['pcp_display_in_roll']) && |
6a488035 TO |
1015 | (CRM_Utils_Array::value('pcp_is_anonymous', $fields) == 0) && |
1016 | CRM_Utils_Array::value('pcp_roll_nickname', $fields) == '' | |
1017 | ) { | |
1018 | $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.'); | |
1019 | } | |
1020 | ||
1021 | // return if this is express mode | |
1022 | $config = CRM_Core_Config::singleton(); | |
f92fc7eb CW |
1023 | if ($self->_paymentProcessor && |
1024 | $self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON | |
1025 | ) { | |
8cc574cf | 1026 | if (!empty($fields[$self->_expressButtonName . '_x']) || !empty($fields[$self->_expressButtonName . '_y']) || |
6a488035 TO |
1027 | CRM_Utils_Array::value($self->_expressButtonName, $fields) |
1028 | ) { | |
1029 | return $errors; | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | //validate the pledge fields. | |
a7488080 | 1034 | if (!empty($self->_values['pledge_block_id'])) { |
6a488035 | 1035 | //validation for pledge payment. |
a7488080 | 1036 | if (!empty($self->_values['pledge_id'])) { |
6a488035 TO |
1037 | if (empty($fields['pledge_amount'])) { |
1038 | $errors['pledge_amount'] = ts('At least one payment option needs to be checked.'); | |
1039 | } | |
1040 | } | |
a7488080 | 1041 | elseif (!empty($fields['is_pledge'])) { |
6a488035 | 1042 | if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == FALSE) { |
917a9954 | 1043 | $errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.'); |
6a488035 TO |
1044 | } |
1045 | else { | |
1046 | if (CRM_Utils_Array::value('pledge_installments', $fields) == NULL) { | |
1047 | $errors['pledge_installments'] = ts('Pledge Installments is required field.'); | |
1048 | } | |
1049 | elseif (CRM_Utils_array::value('pledge_installments', $fields) == 1) { | |
1050 | $errors['pledge_installments'] = ts('Pledges consist of multiple scheduled payments. Select one-time contribution if you want to make your gift in a single payment.'); | |
1051 | } | |
1052 | elseif (CRM_Utils_array::value('pledge_installments', $fields) == 0) { | |
1053 | $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.'); | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | //validation for Pledge Frequency Interval. | |
1058 | if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == FALSE) { | |
1059 | $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.'); | |
1060 | } | |
1061 | else { | |
1062 | if (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == NULL) { | |
1063 | $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.'); | |
1064 | } | |
1065 | elseif (CRM_Utils_array::value('pledge_frequency_interval', $fields) == 0) { | |
1066 | $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0'); | |
1067 | } | |
1068 | } | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | // also return if paylater mode | |
8ae4d0d3 | 1073 | if (CRM_Utils_Array::value('payment_processor', $fields) == 0 && $self->_isBillingAddressRequiredForPayLater == 0) { |
6a488035 TO |
1074 | return empty($errors) ? TRUE : $errors; |
1075 | } | |
1076 | ||
1077 | // if the user has chosen a free membership or the amount is less than zero | |
1078 | // i.e. we skip calling the payment processor and hence dont need credit card | |
1079 | // info | |
f92fc7eb | 1080 | if ((float) $amount <= 0.0) { |
6a488035 TO |
1081 | return $errors; |
1082 | } | |
1083 | ||
7cb3d4f0 CW |
1084 | if (!empty($self->_paymentFields)) { |
1085 | CRM_Core_Form::validateMandatoryFields($self->_paymentFields, $fields, $errors); | |
6a488035 | 1086 | } |
f9901e7f | 1087 | CRM_Core_Payment_Form::validateCreditCard($fields, $errors); |
6a488035 | 1088 | |
6a488035 TO |
1089 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { |
1090 | if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { | |
1091 | $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); | |
1092 | if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) { | |
1093 | $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.', | |
353ffa53 | 1094 | array(1 => ucwords(str_replace('_', " ", $greeting))) |
6a488035 TO |
1095 | ); |
1096 | } | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | return empty($errors) ? TRUE : $errors; | |
1101 | } | |
1102 | ||
186c9c17 | 1103 | /** |
c490a46a CW |
1104 | * @param array $params |
1105 | * @param CRM_Core_Form $form | |
186c9c17 EM |
1106 | * |
1107 | * @return int|mixed|null|string | |
1108 | */ | |
cde484fd | 1109 | public static function computeAmount(&$params, &$form) { |
6a488035 TO |
1110 | $amount = NULL; |
1111 | ||
1112 | // first clean up the other amount field if present | |
1113 | if (isset($params['amount_other'])) { | |
1114 | $params['amount_other'] = CRM_Utils_Rule::cleanMoney($params['amount_other']); | |
1115 | } | |
1116 | ||
8cc574cf | 1117 | if (CRM_Utils_Array::value('amount', $params) == 'amount_other_radio' || !empty($params['amount_other'])) { |
6a488035 TO |
1118 | $amount = $params['amount_other']; |
1119 | } | |
1120 | elseif (!empty($params['pledge_amount'])) { | |
1121 | $amount = 0; | |
1122 | foreach ($params['pledge_amount'] as $paymentId => $dontCare) { | |
1123 | $amount += CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $paymentId, 'scheduled_amount'); | |
1124 | } | |
1125 | } | |
1126 | else { | |
a7488080 | 1127 | if (!empty($form->_values['amount'])) { |
6a488035 TO |
1128 | $amountID = CRM_Utils_Array::value('amount', $params); |
1129 | ||
1130 | if ($amountID) { | |
1131 | $params['amount_level'] = CRM_Utils_Array::value('label', $form->_values[$amountID]); | |
1132 | $amount = CRM_Utils_Array::value('value', $form->_values[$amountID]); | |
1133 | } | |
1134 | } | |
1135 | } | |
1136 | return $amount; | |
1137 | } | |
1138 | ||
1139 | /** | |
c490a46a | 1140 | * Process the form submission |
6a488035 | 1141 | * |
6a488035 | 1142 | * |
355ba699 | 1143 | * @return void |
6a488035 TO |
1144 | */ |
1145 | public function postProcess() { | |
1146 | $config = CRM_Core_Config::singleton(); | |
6a488035 TO |
1147 | // we first reset the confirm page so it accepts new values |
1148 | $this->controller->resetPage('Confirm'); | |
1149 | ||
1150 | // get the submitted form values. | |
1151 | $params = $this->controller->exportValues($this->_name); | |
da8d9879 | 1152 | |
4625312e EM |
1153 | //carry campaign from profile. |
1154 | if (array_key_exists('contribution_campaign_id', $params)) { | |
1155 | $params['campaign_id'] = $params['contribution_campaign_id']; | |
1156 | } | |
1157 | ||
1158 | if (!empty($params['onbehalfof_id'])) { | |
1159 | $params['organization_id'] = $params['onbehalfof_id']; | |
1160 | } | |
1161 | ||
1162 | $params['currencyID'] = $config->defaultCurrency; | |
1163 | ||
a7488080 | 1164 | if (!empty($params['priceSetId'])) { |
9da8dc8c | 1165 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
6a488035 | 1166 | if ($is_quick_config) { |
9da8dc8c | 1167 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
1168 | $priceField->price_set_id = $params['priceSetId']; |
1169 | $priceField->orderBy('weight'); | |
1170 | $priceField->find(); | |
1171 | ||
1d9d37c7 | 1172 | $priceOptions = array(); |
6a488035 | 1173 | while ($priceField->fetch()) { |
1d9d37c7 FG |
1174 | CRM_Price_BAO_PriceFieldValue::getValues($priceField->id, $priceOptions); |
1175 | if ($selectedPriceOptionID = CRM_Utils_Array::value("price_{$priceField->id}", $params)) { | |
1176 | switch ($priceField->name) { | |
1177 | case 'membership_amount': | |
1178 | $this->_params['selectMembership'] = $params['selectMembership'] = CRM_Utils_Array::value('membership_type_id', $priceOptions[$selectedPriceOptionID]); | |
1179 | $this->set('selectMembership', $params['selectMembership']); | |
1180 | if (CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) == 0) { | |
1181 | $this->_values['amount'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]); | |
1182 | } | |
1183 | break; | |
1184 | ||
1185 | case 'contribution_amount': | |
1186 | $params['amount'] = $selectedPriceOptionID; | |
1187 | $this->_values['amount'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]); | |
1188 | $this->_values[$selectedPriceOptionID]['value'] = CRM_Utils_Array::value('amount', $priceOptions[$selectedPriceOptionID]); | |
1189 | $this->_values[$selectedPriceOptionID]['label'] = CRM_Utils_Array::value('label', $priceOptions[$selectedPriceOptionID]); | |
1190 | $this->_values[$selectedPriceOptionID]['amount_id'] = CRM_Utils_Array::value('id', $priceOptions[$selectedPriceOptionID]); | |
1191 | $this->_values[$selectedPriceOptionID]['weight'] = CRM_Utils_Array::value('weight', $priceOptions[$selectedPriceOptionID]); | |
1192 | break; | |
1193 | ||
1194 | case 'other_amount': | |
1195 | $params['amount_other'] = $selectedPriceOptionID; | |
1196 | break; | |
6a488035 TO |
1197 | } |
1198 | } | |
6a488035 TO |
1199 | } |
1200 | } | |
1201 | } | |
1202 | ||
1203 | if (($this->_values['is_pay_later'] && | |
1204 | empty($this->_paymentProcessor) && | |
1205 | !array_key_exists('hidden_processor', $params)) || | |
353ffa53 TO |
1206 | (!empty($params['payment_processor']) && $params['payment_processor'] == 0) |
1207 | ) { | |
6a488035 TO |
1208 | $params['is_pay_later'] = 1; |
1209 | } | |
1210 | else { | |
1211 | $params['is_pay_later'] = 0; | |
1212 | } | |
1213 | ||
1214 | $this->set('is_pay_later', $params['is_pay_later']); | |
1215 | // assign pay later stuff | |
1216 | $this->_params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE); | |
1217 | $this->assign('is_pay_later', $params['is_pay_later']); | |
1218 | if ($params['is_pay_later']) { | |
1219 | $this->assign('pay_later_text', $this->_values['pay_later_text']); | |
1220 | $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']); | |
1221 | } | |
1222 | ||
8bff2456 | 1223 | // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID |
6a488035 TO |
1224 | $params['amount'] = self::computeAmount($params, $this); |
1225 | $params['separate_amount'] = $params['amount']; | |
1226 | $memFee = NULL; | |
a7488080 | 1227 | if (!empty($params['selectMembership'])) { |
6a488035 TO |
1228 | if (!empty($this->_membershipTypeValues)) { |
1229 | $membershipTypeValues = $this->_membershipTypeValues[$params['selectMembership']]; | |
1230 | } | |
1231 | else { | |
1232 | $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, | |
1233 | $params['selectMembership'] | |
1234 | ); | |
1235 | } | |
1236 | $memFee = $membershipTypeValues['minimum_fee']; | |
1237 | if (!$params['amount'] && !$this->_separateMembershipPayment) { | |
1238 | $params['amount'] = $memFee ? $memFee : 0; | |
1239 | } | |
1240 | } | |
e6d4f555 | 1241 | //If the membership & contribution is used in contribution page & not separate payment |
9ce8bcfc PN |
1242 | $fieldId = $memPresent = $membershipLabel = $fieldOption = $is_quick_config = NULL; |
1243 | $proceFieldAmount = 0; | |
8af73472 | 1244 | if (property_exists($this, '_separateMembershipPayment') && $this->_separateMembershipPayment == 0) { |
9da8dc8c | 1245 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); |
6a488035 TO |
1246 | if ($is_quick_config) { |
1247 | foreach ($this->_priceSet['fields'] as $fieldKey => $fieldVal) { | |
874c9be7 | 1248 | if ($fieldVal['name'] == 'membership_amount' && !empty($params['price_' . $fieldKey])) { |
353ffa53 | 1249 | $fieldId = $fieldVal['id']; |
6a488035 | 1250 | $fieldOption = $params['price_' . $fieldId]; |
9ce8bcfc | 1251 | $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount']; |
353ffa53 | 1252 | $memPresent = TRUE; |
6a488035 TO |
1253 | } |
1254 | else { | |
a7488080 | 1255 | if (!empty($params['price_' . $fieldKey]) && $memPresent && ($fieldVal['name'] == 'other_amount' || $fieldVal['name'] == 'contribution_amount')) { |
6a488035 TO |
1256 | $fieldId = $fieldVal['id']; |
1257 | if ($fieldVal['name'] == 'other_amount') { | |
9ce8bcfc | 1258 | $proceFieldAmount += $this->_submitValues['price_' . $fieldId]; |
6a488035 | 1259 | } |
abcf506a | 1260 | elseif ($fieldVal['name'] == 'contribution_amount' && $this->_submitValues['price_' . $fieldId] > 0) { |
9ce8bcfc | 1261 | $proceFieldAmount += $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount']; |
ce9024ca | 1262 | } |
6a488035 TO |
1263 | unset($params['price_' . $fieldId]); |
1264 | break; | |
1265 | } | |
1266 | } | |
1267 | } | |
1268 | } | |
1269 | } | |
874c9be7 | 1270 | |
6a488035 TO |
1271 | if (!isset($params['amount_other'])) { |
1272 | $this->set('amount_level', CRM_Utils_Array::value('amount_level', $params)); | |
1273 | } | |
1274 | ||
1275 | if ($priceSetId = CRM_Utils_Array::value('priceSetId', $params)) { | |
1276 | $lineItem = array(); | |
481a74f4 TO |
1277 | $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); |
1278 | if ($is_quick_config) { | |
1279 | foreach ($this->_values['fee'] as $key => & $val) { | |
353ffa53 | 1280 | if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && array_key_exists('price_' . $key, $params)) { |
92fcb95f | 1281 | $params['price_' . $key] = CRM_Utils_Rule::cleanMoney($params['price_' . $key]); //Clean out any currency symbols |
353ffa53 | 1282 | if ($params['price_' . $key] != 0) { |
481a74f4 | 1283 | foreach ($val['options'] as $optionKey => & $options) { |
353ffa53 | 1284 | $options['amount'] = CRM_Utils_Array::value('price_' . $key, $params); |
ef88f444 PC |
1285 | break; |
1286 | } | |
6a488035 | 1287 | } |
92fcb95f | 1288 | $params['price_' . $key] = 1; |
6a488035 TO |
1289 | break; |
1290 | } | |
1291 | } | |
1292 | } | |
6a488035 TO |
1293 | $component = ''; |
1294 | if ($this->_membershipBlock) { | |
1295 | $component = 'membership'; | |
1296 | } | |
ef88f444 | 1297 | |
9da8dc8c | 1298 | CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $component); |
dc428161 | 1299 | if ($params['tax_amount']) { |
1300 | $this->set('tax_amount', $params['tax_amount']); | |
1301 | } | |
6a488035 TO |
1302 | |
1303 | if ($proceFieldAmount) { | |
6a488035 | 1304 | $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount; |
c40e1ff4 | 1305 | $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount; |
dc428161 | 1306 | if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) { |
1307 | $proceFieldAmount += $lineItem[$params['priceSetId']][$fieldOption]['tax_amount']; | |
1308 | } | |
6a488035 TO |
1309 | if (!$this->_membershipBlock['is_separate_payment']) { |
1310 | $params['amount'] = $proceFieldAmount; //require when separate membership not used | |
1311 | } | |
1312 | } | |
1313 | $this->set('lineItem', $lineItem); | |
1314 | } | |
1315 | ||
8cc574cf | 1316 | if ($this->_membershipBlock['is_separate_payment'] && !empty($params['separate_amount'])) { |
6a488035 | 1317 | $this->set('amount', $params['separate_amount']); |
0db6c3e1 TO |
1318 | } |
1319 | else { | |
6a488035 TO |
1320 | $this->set('amount', $params['amount']); |
1321 | } | |
1322 | ||
1323 | // generate and set an invoiceID for this transaction | |
1324 | $invoiceID = md5(uniqid(rand(), TRUE)); | |
1325 | $this->set('invoiceID', $invoiceID); | |
1326 | ||
e6d4f555 | 1327 | // required only if is_monetary and valid positive amount |
6a488035 TO |
1328 | if ($this->_values['is_monetary'] && |
1329 | is_array($this->_paymentProcessor) && | |
1330 | ((float ) $params['amount'] > 0.0 || $memFee > 0.0) | |
1331 | ) { | |
1332 | ||
1333 | // default mode is direct | |
1334 | $this->set('contributeMode', 'direct'); | |
1335 | ||
f92fc7eb CW |
1336 | if ($this->_paymentProcessor && |
1337 | $this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON | |
1338 | ) { | |
6a488035 TO |
1339 | //get the button name |
1340 | $buttonName = $this->controller->getButtonName(); | |
1341 | if (in_array($buttonName, | |
1342 | array($this->_expressButtonName, $this->_expressButtonName . '_x', $this->_expressButtonName . '_y') | |
353ffa53 TO |
1343 | ) && empty($params['is_pay_later']) |
1344 | ) { | |
6a488035 TO |
1345 | $this->set('contributeMode', 'express'); |
1346 | ||
353ffa53 | 1347 | $donateURL = CRM_Utils_System::url('civicrm/contribute', '_qf_Contribute_display=1'); |
6a488035 TO |
1348 | $params['cancelURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE); |
1349 | $params['returnURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Confirm_display=1&rfp=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE); | |
1350 | $params['invoiceID'] = $invoiceID; | |
fc7063a4 | 1351 | $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']); |
6a488035 TO |
1352 | |
1353 | //default action is Sale | |
1354 | $params['payment_action'] = 'Sale'; | |
1355 | ||
1356 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); | |
1357 | $token = $payment->setExpressCheckout($params); | |
1358 | if (is_a($token, 'CRM_Core_Error')) { | |
1359 | CRM_Core_Error::displaySessionError($token); | |
1360 | CRM_Utils_System::redirect($params['cancelURL']); | |
1361 | } | |
1362 | ||
1363 | $this->set('token', $token); | |
1364 | ||
1365 | $paymentURL = $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token"; | |
1366 | CRM_Utils_System::redirect($paymentURL); | |
1367 | } | |
1368 | } | |
f92fc7eb CW |
1369 | elseif ($this->_paymentProcessor && |
1370 | $this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_NOTIFY | |
1371 | ) { | |
6a488035 TO |
1372 | $this->set('contributeMode', 'notify'); |
1373 | } | |
1374 | } | |
1375 | ||
1376 | // should we skip the confirm page? | |
a7488080 | 1377 | if (empty($this->_values['is_confirm_enabled'])) { |
94fe3c35 DL |
1378 | // call the post process hook for the main page before we switch to confirm |
1379 | $this->postProcessHook(); | |
1380 | ||
6a488035 TO |
1381 | // build the confirm page |
1382 | $confirmForm = &$this->controller->_pages['Confirm']; | |
1383 | $confirmForm->preProcess(); | |
1384 | $confirmForm->buildQuickForm(); | |
1385 | ||
1386 | // the confirmation page is valid | |
1387 | $data = &$this->controller->container(); | |
1388 | $data['valid']['Confirm'] = 1; | |
1389 | ||
1390 | // confirm the contribution | |
94fe3c35 DL |
1391 | // mainProcess calls the hook also |
1392 | $confirmForm->mainProcess(); | |
6a488035 TO |
1393 | $qfKey = $this->controller->_key; |
1394 | ||
1395 | // redirect to thank you page | |
1396 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey=$qfKey", TRUE, NULL, FALSE)); | |
1397 | } | |
874c9be7 | 1398 | |
6a488035 | 1399 | } |
7bf9cde2 CW |
1400 | |
1401 | /** | |
1402 | * Handle Payment Processor switching | |
1403 | * For contribution and event registration forms | |
cc789d46 | 1404 | * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register $form |
03110609 | 1405 | * @param bool $noFees |
7bf9cde2 | 1406 | */ |
00be9182 | 1407 | public static function preProcessPaymentOptions(&$form, $noFees = FALSE) { |
7bf9cde2 | 1408 | $form->_snippet = CRM_Utils_Array::value('snippet', $_GET); |
7bf9cde2 | 1409 | |
b6a469c5 | 1410 | $form->_paymentProcessors = $noFees ? array() : $form->get('paymentProcessors'); |
e50ee6a3 | 1411 | $form->_paymentProcessorID = NULL; |
b6a469c5 | 1412 | if ($form->_paymentProcessors) { |
f92fc7eb CW |
1413 | // Fetch type during ajax request |
1414 | if (isset($_GET['type']) && $form->_snippet) { | |
67399793 | 1415 | $form->_paymentProcessorID = CRM_Utils_Request::retrieve('type', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, 0); |
754bdbd4 | 1416 | } |
f92fc7eb | 1417 | // Remember type during form post |
afc675df | 1418 | elseif (!empty($form->_submitValues)) { |
e50ee6a3 EM |
1419 | $form->_paymentProcessorID = CRM_Utils_Array::value('payment_processor', $form->_submitValues); |
1420 | $form->_paymentProcessor = CRM_Utils_Array::value($form->_paymentProcessorID, $form->_paymentProcessors); | |
1421 | $form->set('type', $form->_paymentProcessorID); | |
f92fc7eb CW |
1422 | $form->set('mode', $form->_mode); |
1423 | $form->set('paymentProcessor', $form->_paymentProcessor); | |
7bf9cde2 CW |
1424 | } |
1425 | // Set default payment processor | |
1426 | else { | |
b6a469c5 CW |
1427 | foreach ($form->_paymentProcessors as $values) { |
1428 | if (!empty($values['is_default']) || count($form->_paymentProcessors) == 1) { | |
e50ee6a3 | 1429 | $form->_paymentProcessorID = $values['id']; |
f92fc7eb | 1430 | break; |
7bf9cde2 CW |
1431 | } |
1432 | } | |
1433 | } | |
e50ee6a3 | 1434 | if ($form->_paymentProcessorID) { |
7bf9cde2 CW |
1435 | CRM_Core_Payment_ProcessorForm::preProcess($form); |
1436 | } | |
67399793 EM |
1437 | else { |
1438 | $form->_paymentProcessor = array(); | |
1439 | } | |
7bf9cde2 CW |
1440 | |
1441 | //get payPal express id and make it available to template | |
b6a469c5 | 1442 | foreach ($form->_paymentProcessors as $ppId => $values) { |
7bf9cde2 CW |
1443 | $payPalExpressId = ($values['payment_processor_type'] == 'PayPal_Express') ? $values['id'] : 0; |
1444 | $form->assign('payPalExpressId', $payPalExpressId); | |
1445 | if ($payPalExpressId) { | |
1446 | break; | |
1447 | } | |
1448 | } | |
bef9421f CW |
1449 | if (!$form->_snippet) { |
1450 | // Add JS to show icons for the accepted credit cards | |
1451 | $creditCardTypes = CRM_Core_Payment_Form::getCreditCardCSSNames(); | |
1452 | CRM_Core_Resources::singleton() | |
f3902ac6 CW |
1453 | ->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10) |
1454 | // workaround for CRM-13634 | |
1455 | // ->addSetting(array('config' => array('creditCardTypes' => $creditCardTypes))); | |
1456 | ->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';'); | |
bef9421f | 1457 | } |
7bf9cde2 | 1458 | } |
e50ee6a3 | 1459 | $form->assign('paymentProcessorID', $form->_paymentProcessorID); |
7bf9cde2 | 1460 | } |
6a488035 | 1461 | } |