Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 usefusul, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class generates form components for processing a contribution | |
803d4bc0 | 36 | * CRM-16229 - During the event registration bulk action via search we |
37 | * need to inherit CRM_Contact_Form_Task so that we can inherit functions | |
38 | * like getContactIds and make use of controller state. But this is not possible | |
39 | * because CRM_Event_Form_Participant inherits this class. | |
40 | * Ideal situation would be something like | |
41 | * CRM_Event_Form_Participant extends CRM_Contact_Form_Task, | |
42 | * CRM_Contribute_Form_AbstractEditPayment | |
43 | * However this is not possible. Currently PHP does not support multiple | |
44 | * inheritance. So work around solution is to extend this class with | |
45 | * CRM_Contact_Form_Task which further extends CRM_Core_Form. | |
6a488035 TO |
46 | * |
47 | */ | |
5b7c9ebc | 48 | class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { |
6a488035 TO |
49 | public $_mode; |
50 | ||
51 | public $_action; | |
52 | ||
53 | public $_bltID; | |
54 | ||
a8215a8d | 55 | public $_fields = array(); |
6a488035 | 56 | |
a6513ad5 EM |
57 | /** |
58 | * @var array current payment processor including a copy of the object in 'object' key | |
59 | */ | |
6a488035 | 60 | public $_paymentProcessor; |
7036a6d0 EM |
61 | |
62 | /** | |
63 | * Available recurring processors. | |
64 | * | |
65 | * @var array | |
66 | */ | |
67 | public $_recurPaymentProcessors = array(); | |
6a488035 | 68 | |
dc913073 | 69 | /** |
100fef9d | 70 | * Array of processor options in the format id => array($id => $label) |
dc913073 EM |
71 | * WARNING it appears that the format used to differ to this and there are places in the code that |
72 | * expect the old format. $this->_paymentProcessors provides the additional data which this | |
73 | * array seems to have provided in the past | |
74 | * @var array | |
75 | */ | |
6a488035 TO |
76 | public $_processors; |
77 | ||
78 | /** | |
100fef9d | 79 | * Available payment processors with full details including the key 'object' indexed by their id |
dc913073 EM |
80 | * @var array |
81 | */ | |
82 | protected $_paymentProcessors = array(); | |
bf4253e7 EM |
83 | |
84 | /** | |
85 | * Instance of the payment processor object. | |
86 | * | |
87 | * @var CRM_Core_Payment | |
88 | */ | |
89 | protected $_paymentObject; | |
90 | ||
dc913073 | 91 | /** |
fe482240 | 92 | * The id of the contribution that we are processing. |
6a488035 TO |
93 | * |
94 | * @var int | |
6a488035 TO |
95 | */ |
96 | public $_id; | |
97 | ||
98 | /** | |
fe482240 | 99 | * The id of the premium that we are proceessing. |
6a488035 TO |
100 | * |
101 | * @var int | |
6a488035 TO |
102 | */ |
103 | public $_premiumID = NULL; | |
4691b077 EM |
104 | |
105 | /** | |
106 | * @var CRM_Contribute_DAO_ContributionProduct | |
107 | */ | |
6a488035 TO |
108 | public $_productDAO = NULL; |
109 | ||
110 | /** | |
100fef9d | 111 | * The id of the note |
6a488035 TO |
112 | * |
113 | * @var int | |
6a488035 TO |
114 | */ |
115 | public $_noteID; | |
116 | ||
117 | /** | |
100fef9d | 118 | * The id of the contact associated with this contribution |
6a488035 TO |
119 | * |
120 | * @var int | |
6a488035 TO |
121 | */ |
122 | public $_contactID; | |
123 | ||
124 | /** | |
100fef9d | 125 | * The id of the pledge payment that we are processing |
6a488035 TO |
126 | * |
127 | * @var int | |
6a488035 TO |
128 | */ |
129 | public $_ppID; | |
130 | ||
131 | /** | |
100fef9d | 132 | * The id of the pledge that we are processing |
6a488035 TO |
133 | * |
134 | * @var int | |
6a488035 TO |
135 | */ |
136 | public $_pledgeID; | |
137 | ||
138 | /** | |
100fef9d | 139 | * Is this contribution associated with an online |
6a488035 TO |
140 | * financial transaction |
141 | * | |
142 | * @var boolean | |
6a488035 TO |
143 | */ |
144 | public $_online = FALSE; | |
145 | ||
146 | /** | |
147 | * Stores all product option | |
148 | * | |
149 | * @var array | |
6a488035 TO |
150 | */ |
151 | public $_options; | |
152 | ||
153 | /** | |
100fef9d | 154 | * Stores the honor id |
6a488035 TO |
155 | * |
156 | * @var int | |
6a488035 TO |
157 | */ |
158 | public $_honorID = NULL; | |
159 | ||
160 | /** | |
dde5a0ef | 161 | * Store the financial Type ID |
6a488035 TO |
162 | * |
163 | * @var array | |
164 | */ | |
165 | public $_contributionType; | |
166 | ||
167 | /** | |
168 | * The contribution values if an existing contribution | |
169 | */ | |
170 | public $_values; | |
171 | ||
172 | /** | |
173 | * The pledge values if this contribution is associated with pledge | |
174 | */ | |
175 | public $_pledgeValues; | |
176 | ||
177 | public $_contributeMode = 'direct'; | |
178 | ||
179 | public $_context; | |
180 | ||
181 | public $_compId; | |
182 | ||
d424ffde | 183 | /** |
6a488035 TO |
184 | * Store the line items if price set used. |
185 | */ | |
186 | public $_lineItems; | |
187 | ||
0be0b79d EM |
188 | /** |
189 | * Is this a backoffice form | |
190 | * (this will affect whether paypal express code is displayed) | |
191 | * @var bool | |
192 | */ | |
193 | public $isBackOffice = TRUE; | |
194 | ||
6a488035 | 195 | protected $_formType; |
dde5a0ef | 196 | |
dde5a0ef | 197 | /** |
100fef9d | 198 | * Array of fields to display on billingBlock.tpl - this is not fully implemented but basically intent is the panes/fieldsets on this page should |
dde5a0ef EM |
199 | * be all in this array in order like |
200 | * 'credit_card' => array('credit_card_number' ... | |
201 | * 'billing_details' => array('first_name' ... | |
202 | * | |
203 | * such that both the fields and the order can be more easily altered by payment processors & other extensions | |
204 | * @var array | |
205 | */ | |
206 | public $billingFieldSets = array(); | |
207 | ||
42e8b05c EM |
208 | /** |
209 | * Pre process function with common actions. | |
210 | */ | |
211 | public function preProcess() { | |
212 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
213 | $this->assign('contactID', $this->_contactID); | |
9977ae61 | 214 | CRM_Core_Resources::singleton()->addVars('coreForm', array('contact_id' => (int) $this->_contactID)); |
42e8b05c EM |
215 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); |
216 | } | |
217 | ||
186c9c17 | 218 | /** |
100fef9d | 219 | * @param int $id |
186c9c17 | 220 | */ |
d5397f2f PJ |
221 | public function showRecordLinkMesssage($id) { |
222 | $statusId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id'); | |
223 | if (CRM_Contribute_PseudoConstant::contributionStatus($statusId, 'name') == 'Partially paid') { | |
224 | if ($pid = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) { | |
225 | $recordPaymentLink = CRM_Utils_System::url('civicrm/payment', | |
226 | "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event" | |
227 | ); | |
228 | CRM_Core_Session::setStatus(ts('Please use the <a href="%1">Record Payment</a> form if you have received an additional payment for this Partially paid contribution record.', array(1 => $recordPaymentLink)), ts('Notice'), 'alert'); | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
186c9c17 | 233 | /** |
100fef9d | 234 | * @param int $id |
186c9c17 EM |
235 | * @param $values |
236 | */ | |
6a488035 TO |
237 | public function buildValuesAndAssignOnline_Note_Type($id, &$values) { |
238 | $ids = array(); | |
239 | $params = array('id' => $id); | |
240 | CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids); | |
241 | ||
242 | //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL) | |
243 | $this->_online = FALSE; | |
244 | $fids = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($id); | |
a7488080 | 245 | if (!empty($fids['financialTrxnId'])) { |
6a488035 TO |
246 | $this->_online = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id'); |
247 | } | |
eea16664 | 248 | |
6a488035 TO |
249 | // Also don't allow user to update some fields for recurring contributions. |
250 | if (!$this->_online) { | |
251 | $this->_online = CRM_Utils_Array::value('contribution_recur_id', $values); | |
252 | } | |
eea16664 | 253 | |
6a488035 TO |
254 | $this->assign('isOnline', $this->_online ? TRUE : FALSE); |
255 | ||
6a488035 TO |
256 | //to get note id |
257 | $daoNote = new CRM_Core_BAO_Note(); | |
258 | $daoNote->entity_table = 'civicrm_contribution'; | |
259 | $daoNote->entity_id = $id; | |
260 | if ($daoNote->find(TRUE)) { | |
261 | $this->_noteID = $daoNote->id; | |
262 | $values['note'] = $daoNote->note; | |
263 | } | |
264 | $this->_contributionType = $values['financial_type_id']; | |
6a488035 TO |
265 | } |
266 | ||
267 | /** | |
014c4014 TO |
268 | * @param string $type |
269 | * Eg 'Contribution'. | |
6a488035 TO |
270 | * @param string $subType |
271 | * @param int $entityId | |
272 | */ | |
273 | public function applyCustomData($type, $subType, $entityId) { | |
274 | $this->set('type', $type); | |
275 | $this->set('subType', $subType); | |
276 | $this->set('entityId', $entityId); | |
277 | ||
278 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId); | |
279 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
280 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
281 | } | |
282 | ||
4691b077 | 283 | /** |
100fef9d | 284 | * @param int $id |
4691b077 EM |
285 | * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find(); |
286 | */ | |
6ea503d4 | 287 | public function assignPremiumProduct($id) { |
6a488035 TO |
288 | $sql = " |
289 | SELECT * | |
290 | FROM civicrm_contribution_product | |
291 | WHERE contribution_id = {$id} | |
292 | "; | |
293 | $dao = CRM_Core_DAO::executeQuery($sql, | |
294 | CRM_Core_DAO::$_nullArray | |
295 | ); | |
296 | if ($dao->fetch()) { | |
297 | $this->_premiumID = $dao->id; | |
298 | $this->_productDAO = $dao; | |
299 | } | |
300 | $dao->free(); | |
301 | } | |
302 | ||
303 | /** | |
304 | * This function process contribution related objects. | |
03110609 | 305 | * |
014c4014 TO |
306 | * @param int $contributionId |
307 | * @param int $statusId | |
308 | * @param int|null $previousStatusId | |
03110609 EM |
309 | * |
310 | * @return null|string | |
6a488035 | 311 | */ |
35874a67 | 312 | protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL, $receiveDate = NULL) { |
6a488035 TO |
313 | $statusMsg = NULL; |
314 | if (!$contributionId || !$statusId) { | |
315 | return $statusMsg; | |
316 | } | |
317 | ||
318 | $params = array( | |
319 | 'contribution_id' => $contributionId, | |
320 | 'contribution_status_id' => $statusId, | |
321 | 'previous_contribution_status_id' => $previousStatusId, | |
35874a67 | 322 | 'receive_date' => $receiveDate, |
6a488035 TO |
323 | ); |
324 | ||
325 | $updateResult = CRM_Contribute_BAO_Contribution::transitionComponents($params); | |
326 | ||
327 | if (!is_array($updateResult) || | |
328 | !($updatedComponents = CRM_Utils_Array::value('updatedComponents', $updateResult)) || | |
329 | !is_array($updatedComponents) || | |
330 | empty($updatedComponents) | |
331 | ) { | |
332 | return $statusMsg; | |
333 | } | |
334 | ||
335 | // get the user display name. | |
336 | $sql = " | |
337 | SELECT display_name as displayName | |
338 | FROM civicrm_contact | |
339 | LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id ) | |
340 | WHERE civicrm_contribution.id = {$contributionId}"; | |
341 | $userDisplayName = CRM_Core_DAO::singleValueQuery($sql); | |
342 | ||
343 | // get the status message for user. | |
344 | foreach ($updatedComponents as $componentName => $updatedStatusId) { | |
345 | ||
346 | if ($componentName == 'CiviMember') { | |
347 | $updatedStatusName = CRM_Utils_Array::value($updatedStatusId, | |
348 | CRM_Member_PseudoConstant::membershipStatus() | |
349 | ); | |
350 | if ($updatedStatusName == 'Cancelled') { | |
351 | $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName)); | |
352 | } | |
353 | elseif ($updatedStatusName == 'Expired') { | |
354 | $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName)); | |
355 | } | |
d008a264 | 356 | else { |
13299a2b JM |
357 | $endDate = CRM_Utils_Array::value('membership_end_date', $updateResult); |
358 | if ($endDate) { | |
d008a264 | 359 | $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.", |
353ffa53 TO |
360 | array( |
361 | 1 => $userDisplayName, | |
362 | 2 => $endDate, | |
363 | ) | |
364 | ); | |
d008a264 | 365 | } |
6a488035 TO |
366 | } |
367 | } | |
368 | ||
369 | if ($componentName == 'CiviEvent') { | |
370 | $updatedStatusName = CRM_Utils_Array::value($updatedStatusId, | |
371 | CRM_Event_PseudoConstant::participantStatus() | |
372 | ); | |
373 | if ($updatedStatusName == 'Cancelled') { | |
374 | $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName)); | |
375 | } | |
376 | elseif ($updatedStatusName == 'Registered') { | |
377 | $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName)); | |
378 | } | |
379 | } | |
380 | ||
381 | if ($componentName == 'CiviPledge') { | |
382 | $updatedStatusName = CRM_Utils_Array::value($updatedStatusId, | |
383 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
384 | ); | |
385 | if ($updatedStatusName == 'Cancelled') { | |
386 | $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName)); | |
387 | } | |
388 | elseif ($updatedStatusName == 'Failed') { | |
389 | $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName)); | |
390 | } | |
391 | elseif ($updatedStatusName == 'Completed') { | |
392 | $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName)); | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | return $statusMsg; | |
398 | } | |
399 | ||
400 | /** | |
a6c01b45 | 401 | * @return array |
16b10e64 | 402 | * Array of valid processors. The array resembles the DB table but also has 'object' as a key |
fbcb6fba EM |
403 | * @throws Exception |
404 | */ | |
405 | public function getValidProcessors() { | |
fbcb6fba EM |
406 | $defaultID = NULL; |
407 | $capabilities = array('BackOffice'); | |
52767de0 EM |
408 | if ($this->_mode) { |
409 | $capabilities[] = (ucfirst($this->_mode) . 'Mode'); | |
fbcb6fba EM |
410 | } |
411 | $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors($capabilities); | |
44b6505d EM |
412 | return $processors; |
413 | ||
6a488035 TO |
414 | } |
415 | ||
6a488035 TO |
416 | /** |
417 | * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables | |
418 | */ | |
419 | public function assignProcessors() { | |
420 | //ensure that processor has a valid config | |
421 | //only valid processors get display to user | |
44b6505d | 422 | |
6a488035 | 423 | if ($this->_mode) { |
11d48665 | 424 | $this->assign('processorSupportsFutureStartDate', CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('FutureRecurStartDate'))); |
dc913073 EM |
425 | $this->_paymentProcessors = $this->getValidProcessors(); |
426 | if (!isset($this->_paymentProcessor['id'])) { | |
427 | // if the payment processor isn't set yet (as indicated by the presence of an id,) we'll grab the first one which should be the default | |
428 | $this->_paymentProcessor = reset($this->_paymentProcessors); | |
429 | } | |
430 | if (empty($this->_paymentProcessors)) { | |
44b6505d EM |
431 | throw new CRM_Core_Exception(ts('You will need to configure the %1 settings for your Payment Processor before you can submit a credit card transactions.', array(1 => $this->_mode))); |
432 | } | |
353ffa53 | 433 | $this->_processors = array(); |
dc913073 | 434 | foreach ($this->_paymentProcessors as $id => $processor) { |
bc8526fa CW |
435 | if ($processor['is_test'] == ($this->_mode == 'test')) { |
436 | $this->_processors[$id] = ts($processor['name']); | |
437 | if (!empty($processor['description'])) { | |
438 | $this->_processors[$id] .= ' : ' . ts($processor['description']); | |
439 | } | |
440 | if ($processor['is_recur']) { | |
441 | $this->_recurPaymentProcessors[$id] = $this->_processors[$id]; | |
442 | } | |
7036a6d0 | 443 | } |
44b6505d | 444 | } |
071fee77 | 445 | CRM_Financial_Form_Payment::addCreditCardJs(); |
6a488035 TO |
446 | } |
447 | $this->assign('recurringPaymentProcessorIds', | |
448 | empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors)) | |
449 | ); | |
450 | ||
451 | // this required to show billing block | |
03110609 | 452 | // @todo remove this assignment the billing block is now designed to be always included but will not show fieldsets unless those sets of fields are assigned |
44b6505d | 453 | $this->assign_by_ref('paymentProcessor', $processor); |
6a488035 TO |
454 | } |
455 | ||
186c9c17 | 456 | /** |
6ea503d4 TO |
457 | * Get current currency from DB or use default currency. |
458 | * | |
186c9c17 EM |
459 | * @param $submittedValues |
460 | * | |
461 | * @return mixed | |
462 | */ | |
6ea503d4 | 463 | public function getCurrency($submittedValues) { |
6a488035 TO |
464 | $config = CRM_Core_Config::singleton(); |
465 | ||
466 | $currentCurrency = CRM_Utils_Array::value('currency', | |
467 | $this->_values, | |
468 | $config->defaultCurrency | |
469 | ); | |
470 | ||
471 | // use submitted currency if present else use current currency | |
472 | $result = CRM_Utils_Array::value('currency', | |
473 | $submittedValues, | |
474 | $currentCurrency | |
475 | ); | |
476 | return $result; | |
477 | } | |
478 | ||
186c9c17 | 479 | /** |
100fef9d | 480 | * @param int $financialTypeId |
186c9c17 EM |
481 | * |
482 | * @return array | |
483 | */ | |
6a488035 TO |
484 | public function getFinancialAccounts($financialTypeId) { |
485 | $financialAccounts = array(); | |
486 | CRM_Core_PseudoConstant::populate($financialAccounts, | |
487 | 'CRM_Financial_DAO_EntityFinancialAccount', | |
488 | $all = TRUE, | |
489 | $retrieve = 'financial_account_id', | |
490 | $filter = NULL, | |
491 | " entity_id = {$financialTypeId} ", NULL, 'account_relationship'); | |
492 | return $financialAccounts; | |
493 | } | |
494 | ||
186c9c17 | 495 | /** |
100fef9d CW |
496 | * @param int $financialTypeId |
497 | * @param int $relationTypeId | |
186c9c17 EM |
498 | * |
499 | * @return mixed | |
500 | */ | |
6a488035 TO |
501 | public function getFinancialAccount($financialTypeId, $relationTypeId) { |
502 | $financialAccounts = $this->getFinancialAccounts($financialTypeId); | |
503 | return CRM_Utils_Array::value($relationTypeId, $financialAccounts); | |
504 | } | |
505 | ||
506 | public function preProcessPledge() { | |
507 | //get the payment values associated with given pledge payment id OR check for payments due. | |
508 | $this->_pledgeValues = array(); | |
509 | if ($this->_ppID) { | |
510 | $payParams = array('id' => $this->_ppID); | |
511 | ||
512 | CRM_Pledge_BAO_PledgePayment::retrieve($payParams, $this->_pledgeValues['pledgePayment']); | |
513 | $this->_pledgeID = CRM_Utils_Array::value('pledge_id', $this->_pledgeValues['pledgePayment']); | |
514 | $paymentStatusID = CRM_Utils_Array::value('status_id', $this->_pledgeValues['pledgePayment']); | |
515 | $this->_id = CRM_Utils_Array::value('contribution_id', $this->_pledgeValues['pledgePayment']); | |
516 | ||
517 | //get all status | |
518 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
519 | if (!($paymentStatusID == array_search('Pending', $allStatus) || $paymentStatusID == array_search('Overdue', $allStatus))) { | |
520 | CRM_Core_Error::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'.")); | |
521 | } | |
522 | ||
523 | //get the pledge values associated with given pledge payment. | |
524 | ||
525 | $ids = array(); | |
526 | $pledgeParams = array('id' => $this->_pledgeID); | |
527 | CRM_Pledge_BAO_Pledge::getValues($pledgeParams, $this->_pledgeValues, $ids); | |
528 | $this->assign('ppID', $this->_ppID); | |
529 | } | |
530 | else { | |
531 | // Not making a pledge payment, so if adding a new contribution we should check if pledge payment(s) are due for this contact so we can alert the user. CRM-5206 | |
532 | if (isset($this->_contactID)) { | |
533 | $contactPledges = CRM_Pledge_BAO_Pledge::getContactPledges($this->_contactID); | |
534 | ||
535 | if (!empty($contactPledges)) { | |
536 | $payments = $paymentsDue = NULL; | |
537 | $multipleDue = FALSE; | |
538 | foreach ($contactPledges as $key => $pledgeId) { | |
539 | $payments = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId); | |
540 | if ($payments) { | |
541 | if ($paymentsDue) { | |
542 | $multipleDue = TRUE; | |
543 | break; | |
544 | } | |
545 | else { | |
546 | $paymentsDue = $payments; | |
547 | } | |
548 | } | |
549 | } | |
550 | if ($multipleDue) { | |
551 | // Show link to pledge tab since more than one pledge has a payment due | |
552 | $pledgeTab = CRM_Utils_System::url('civicrm/contact/view', | |
553 | "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge" | |
554 | ); | |
555 | CRM_Core_Session::setStatus(ts('This contact has pending or overdue pledge payments. <a href="%1">Click here to view their Pledges tab</a> and verify whether this contribution should be applied as a pledge payment.', array(1 => $pledgeTab)), ts('Notice'), 'alert'); | |
556 | } | |
557 | elseif ($paymentsDue) { | |
558 | // Show user link to oldest Pending or Overdue pledge payment | |
559 | $ppAmountDue = CRM_Utils_Money::format($payments['amount'], $payments['currency']); | |
560 | $ppSchedDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date')); | |
561 | if ($this->_mode) { | |
562 | $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', | |
563 | "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live" | |
564 | ); | |
565 | } | |
566 | else { | |
567 | $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', | |
568 | "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge" | |
569 | ); | |
570 | } | |
571 | CRM_Core_Session::setStatus(ts('This contact has a pending or overdue pledge payment of %2 which is scheduled for %3. <a href="%1">Click here to enter a pledge payment</a>.', array( | |
572 | 1 => $ppUrl, | |
573 | 2 => $ppAmountDue, | |
21dfd5f5 | 574 | 3 => $ppSchedDate, |
6a488035 TO |
575 | )), ts('Notice'), 'alert'); |
576 | } | |
577 | } | |
578 | } | |
579 | } | |
580 | } | |
581 | ||
186c9c17 | 582 | /** |
07f8d162 | 583 | * @param array $submittedValues |
186c9c17 EM |
584 | * |
585 | * @return mixed | |
586 | */ | |
6a488035 TO |
587 | public function unsetCreditCardFields($submittedValues) { |
588 | //Offline Contribution. | |
589 | $unsetParams = array( | |
590 | 'payment_processor_id', | |
591 | "email-{$this->_bltID}", | |
592 | 'hidden_buildCreditCard', | |
593 | 'hidden_buildDirectDebit', | |
594 | 'billing_first_name', | |
595 | 'billing_middle_name', | |
596 | 'billing_last_name', | |
597 | 'street_address-5', | |
598 | "city-{$this->_bltID}", | |
599 | "state_province_id-{$this->_bltID}", | |
600 | "postal_code-{$this->_bltID}", | |
601 | "country_id-{$this->_bltID}", | |
602 | 'credit_card_number', | |
603 | 'cvv2', | |
604 | 'credit_card_exp_date', | |
605 | 'credit_card_type', | |
606 | ); | |
607 | foreach ($unsetParams as $key) { | |
608 | if (isset($submittedValues[$key])) { | |
609 | unset($submittedValues[$key]); | |
610 | } | |
611 | } | |
612 | return $submittedValues; | |
613 | } | |
614 | ||
cc984198 | 615 | /** |
100fef9d | 616 | * Common block for setting up the parts of a form that relate to credit / debit card |
cc984198 EM |
617 | * @throws Exception |
618 | */ | |
619 | protected function assignPaymentRelatedVariables() { | |
620 | try { | |
99efbbf8 EM |
621 | if ($this->_contactID) { |
622 | list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
623 | $this->assign('displayName', $this->userDisplayName); | |
624 | } | |
cc984198 EM |
625 | if ($this->_mode) { |
626 | $this->assignProcessors(); | |
cc984198 EM |
627 | |
628 | $this->assignBillingType(); | |
629 | ||
dfc68e82 | 630 | CRM_Core_Payment_Form::setPaymentFieldsByProcessor($this, $this->_paymentProcessor, FALSE, TRUE); |
cc984198 EM |
631 | } |
632 | } | |
633 | catch (CRM_Core_Exception $e) { | |
634 | CRM_Core_Error::fatal($e->getMessage()); | |
635 | } | |
636 | } | |
96025800 | 637 | |
3b8e6c3f | 638 | /** |
639 | * Begin post processing. | |
640 | * | |
641 | * This function aims to start to bring together common postProcessing functions. | |
642 | * | |
643 | * Eventually these are also shared with the front end forms & may need to be moved to where they can also | |
644 | * access this function. | |
645 | */ | |
646 | protected function beginPostProcess() { | |
ba2f3f65 | 647 | if ($this->_mode) { |
2c89742a | 648 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment( |
649 | $this->_params['payment_processor_id'], | |
650 | ($this->_mode == 'test') | |
651 | ); | |
ba2f3f65 | 652 | if (in_array('credit_card_exp_date', array_keys($this->_params))) { |
653 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
654 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
655 | } | |
656 | $this->assign('credit_card_exp_date', CRM_Utils_Date::mysqlToIso(CRM_Utils_Date::format($this->_params['credit_card_exp_date']))); | |
657 | $this->assign('credit_card_number', | |
658 | CRM_Utils_System::mungeCreditCard($this->_params['credit_card_number']) | |
659 | ); | |
275fba6b | 660 | $this->assign('credit_card_type', CRM_Utils_Array::value('credit_card_type', $this->_params)); |
3b8e6c3f | 661 | } |
3b8e6c3f | 662 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); |
663 | } | |
664 | ||
665 | ||
666 | /** | |
667 | * Add the billing address to the contact who paid. | |
668 | * | |
669 | * Note that this function works based on the presence or otherwise of billing fields & can be called regardless of | |
670 | * whether they are 'expected' (due to assumptions about the payment processor type or the setting to collect billing | |
671 | * for pay later. | |
672 | */ | |
673 | protected function processBillingAddress() { | |
674 | $fields = array(); | |
675 | ||
676 | $fields['email-Primary'] = 1; | |
677 | $this->_params['email-5'] = $this->_params['email-Primary'] = $this->_contributorEmail; | |
678 | // now set the values for the billing location. | |
679 | foreach (array_keys($this->_fields) as $name) { | |
680 | $fields[$name] = 1; | |
681 | } | |
682 | ||
683 | // also add location name to the array | |
684 | $this->_params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params); | |
685 | $this->_params["address_name-{$this->_bltID}"] = trim($this->_params["address_name-{$this->_bltID}"]); | |
686 | ||
687 | $fields["address_name-{$this->_bltID}"] = 1; | |
688 | ||
689 | //ensure we don't over-write the payer's email with the member's email | |
690 | if ($this->_contributorContactID == $this->_contactID) { | |
691 | $fields["email-{$this->_bltID}"] = 1; | |
692 | } | |
693 | ||
694 | list($hasBillingField, $addressParams) = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID); | |
695 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
696 | ||
697 | foreach ($nameFields as $name) { | |
698 | $fields[$name] = 1; | |
699 | if (array_key_exists("billing_$name", $this->_params)) { | |
700 | $this->_params[$name] = $this->_params["billing_{$name}"]; | |
701 | $this->_params['preserveDBName'] = TRUE; | |
702 | } | |
703 | } | |
704 | ||
705 | if ($hasBillingField) { | |
706 | $addressParams = array_merge($this->_params, $addressParams); | |
707 | //here we are setting up the billing contact - if different from the member they are already created | |
708 | // but they will get billing details assigned | |
709 | CRM_Contact_BAO_Contact::createProfileContact($addressParams, $fields, | |
710 | $this->_contributorContactID, NULL, NULL, | |
711 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type') | |
712 | ); | |
713 | } | |
714 | // Add additional parameters that the payment processors are used to receiving. | |
715 | if (!empty($this->_params["billing_state_province_id-{$this->_bltID}"])) { | |
716 | $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); | |
717 | } | |
718 | if (!empty($this->_params["billing_country_id-{$this->_bltID}"])) { | |
719 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
720 | } | |
721 | } | |
722 | ||
6a488035 | 723 | } |