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