Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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 { |
a6e29c95 | 49 | |
50 | use CRM_Financial_Form_SalesTaxTrait; | |
51 | ||
6a488035 TO |
52 | public $_mode; |
53 | ||
54 | public $_action; | |
55 | ||
56 | public $_bltID; | |
57 | ||
be2fb01f | 58 | public $_fields = []; |
6a488035 | 59 | |
a6513ad5 | 60 | /** |
5ba7d840 | 61 | * Current payment processor including a copy of the object in 'object' key. |
62 | * | |
63 | * @var array | |
a6513ad5 | 64 | */ |
6a488035 | 65 | public $_paymentProcessor; |
7036a6d0 EM |
66 | |
67 | /** | |
68 | * Available recurring processors. | |
69 | * | |
70 | * @var array | |
71 | */ | |
be2fb01f | 72 | public $_recurPaymentProcessors = []; |
6a488035 | 73 | |
dc913073 | 74 | /** |
100fef9d | 75 | * Array of processor options in the format id => array($id => $label) |
dc913073 EM |
76 | * WARNING it appears that the format used to differ to this and there are places in the code that |
77 | * expect the old format. $this->_paymentProcessors provides the additional data which this | |
78 | * array seems to have provided in the past | |
79 | * @var array | |
80 | */ | |
6a488035 TO |
81 | public $_processors; |
82 | ||
83 | /** | |
100fef9d | 84 | * Available payment processors with full details including the key 'object' indexed by their id |
dc913073 EM |
85 | * @var array |
86 | */ | |
be2fb01f | 87 | protected $_paymentProcessors = []; |
bf4253e7 EM |
88 | |
89 | /** | |
90 | * Instance of the payment processor object. | |
91 | * | |
92 | * @var CRM_Core_Payment | |
93 | */ | |
94 | protected $_paymentObject; | |
95 | ||
dc913073 | 96 | /** |
fe482240 | 97 | * The id of the contribution that we are processing. |
6a488035 TO |
98 | * |
99 | * @var int | |
6a488035 TO |
100 | */ |
101 | public $_id; | |
102 | ||
c0406a91 | 103 | /** |
104 | * Entity that $this->_id relates to. | |
105 | * | |
106 | * If set the contact id is not required in the url. | |
107 | * | |
108 | * @var string | |
109 | */ | |
110 | protected $entity; | |
111 | ||
6a488035 | 112 | /** |
fe482240 | 113 | * The id of the premium that we are proceessing. |
6a488035 TO |
114 | * |
115 | * @var int | |
6a488035 TO |
116 | */ |
117 | public $_premiumID = NULL; | |
4691b077 EM |
118 | |
119 | /** | |
120 | * @var CRM_Contribute_DAO_ContributionProduct | |
121 | */ | |
6a488035 TO |
122 | public $_productDAO = NULL; |
123 | ||
124 | /** | |
100fef9d | 125 | * The id of the note |
6a488035 TO |
126 | * |
127 | * @var int | |
6a488035 TO |
128 | */ |
129 | public $_noteID; | |
130 | ||
131 | /** | |
100fef9d | 132 | * The id of the contact associated with this contribution |
6a488035 TO |
133 | * |
134 | * @var int | |
6a488035 TO |
135 | */ |
136 | public $_contactID; | |
137 | ||
138 | /** | |
100fef9d | 139 | * The id of the pledge payment that we are processing |
6a488035 TO |
140 | * |
141 | * @var int | |
6a488035 TO |
142 | */ |
143 | public $_ppID; | |
144 | ||
145 | /** | |
100fef9d | 146 | * The id of the pledge that we are processing |
6a488035 TO |
147 | * |
148 | * @var int | |
6a488035 TO |
149 | */ |
150 | public $_pledgeID; | |
151 | ||
152 | /** | |
100fef9d | 153 | * Is this contribution associated with an online |
6a488035 TO |
154 | * financial transaction |
155 | * | |
b67daa72 | 156 | * @var bool |
6a488035 TO |
157 | */ |
158 | public $_online = FALSE; | |
159 | ||
160 | /** | |
161 | * Stores all product option | |
162 | * | |
163 | * @var array | |
6a488035 TO |
164 | */ |
165 | public $_options; | |
166 | ||
167 | /** | |
100fef9d | 168 | * Stores the honor id |
6a488035 TO |
169 | * |
170 | * @var int | |
6a488035 TO |
171 | */ |
172 | public $_honorID = NULL; | |
173 | ||
174 | /** | |
dde5a0ef | 175 | * Store the financial Type ID |
6a488035 TO |
176 | * |
177 | * @var array | |
178 | */ | |
179 | public $_contributionType; | |
180 | ||
181 | /** | |
182 | * The contribution values if an existing contribution | |
1330f57a | 183 | * @var array |
6a488035 TO |
184 | */ |
185 | public $_values; | |
186 | ||
187 | /** | |
188 | * The pledge values if this contribution is associated with pledge | |
1330f57a | 189 | * @var array |
6a488035 TO |
190 | */ |
191 | public $_pledgeValues; | |
192 | ||
193 | public $_contributeMode = 'direct'; | |
194 | ||
195 | public $_context; | |
196 | ||
197 | public $_compId; | |
198 | ||
d424ffde | 199 | /** |
6a488035 | 200 | * Store the line items if price set used. |
1330f57a | 201 | * @var array |
6a488035 TO |
202 | */ |
203 | public $_lineItems; | |
204 | ||
0be0b79d EM |
205 | /** |
206 | * Is this a backoffice form | |
18135422 | 207 | * |
0be0b79d EM |
208 | * @var bool |
209 | */ | |
210 | public $isBackOffice = TRUE; | |
211 | ||
6a488035 | 212 | protected $_formType; |
dde5a0ef | 213 | |
18135422 | 214 | /** |
215 | * Payment instrument id for the transaction. | |
216 | * | |
217 | * @var int | |
218 | */ | |
219 | public $paymentInstrumentID; | |
220 | ||
bd4cefac | 221 | /** |
222 | * Component - event, membership or contribution. | |
223 | * | |
224 | * @var string | |
225 | */ | |
226 | protected $_component; | |
227 | ||
dde5a0ef | 228 | /** |
100fef9d | 229 | * 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 |
230 | * be all in this array in order like |
231 | * 'credit_card' => array('credit_card_number' ... | |
232 | * 'billing_details' => array('first_name' ... | |
233 | * | |
234 | * such that both the fields and the order can be more easily altered by payment processors & other extensions | |
235 | * @var array | |
236 | */ | |
be2fb01f | 237 | public $billingFieldSets = []; |
dde5a0ef | 238 | |
189f0360 | 239 | /** |
240 | * Monetary fields that may be submitted. | |
241 | * | |
242 | * These should get a standardised format in the beginPostProcess function. | |
243 | * | |
244 | * These fields are common to many forms. Some may override this. | |
1330f57a | 245 | * @var array |
189f0360 | 246 | */ |
247 | protected $submittableMoneyFields = ['total_amount', 'net_amount', 'non_deductible_amount', 'fee_amount']; | |
248 | ||
42e8b05c EM |
249 | /** |
250 | * Pre process function with common actions. | |
251 | */ | |
252 | public function preProcess() { | |
253 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
c0406a91 | 254 | if (empty($this->_contactID) && !empty($this->_id) && $this->entity) { |
be2fb01f | 255 | $this->_contactID = civicrm_api3($this->entity, 'getvalue', ['id' => $this->_id, 'return' => 'contact_id']); |
c0406a91 | 256 | } |
42e8b05c | 257 | $this->assign('contactID', $this->_contactID); |
be2fb01f | 258 | CRM_Core_Resources::singleton()->addVars('coreForm', ['contact_id' => (int) $this->_contactID]); |
42e8b05c | 259 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); |
19046166 | 260 | $this->_mode = empty($this->_mode) ? CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this) : $this->_mode; |
a55e39e9 | 261 | $this->assign('isBackOffice', $this->isBackOffice); |
80bb54b2 | 262 | $this->assignContactEmailDetails(); |
0dc4ef42 | 263 | $this->assignPaymentRelatedVariables(); |
42e8b05c EM |
264 | } |
265 | ||
186c9c17 | 266 | /** |
100fef9d | 267 | * @param int $id |
186c9c17 | 268 | */ |
d5397f2f PJ |
269 | public function showRecordLinkMesssage($id) { |
270 | $statusId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id'); | |
271 | if (CRM_Contribute_PseudoConstant::contributionStatus($statusId, 'name') == 'Partially paid') { | |
272 | if ($pid = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) { | |
273 | $recordPaymentLink = CRM_Utils_System::url('civicrm/payment', | |
274 | "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event" | |
275 | ); | |
be2fb01f | 276 | 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.', [1 => $recordPaymentLink]), ts('Notice'), 'alert'); |
d5397f2f PJ |
277 | } |
278 | } | |
279 | } | |
280 | ||
186c9c17 | 281 | /** |
100fef9d | 282 | * @param int $id |
186c9c17 EM |
283 | * @param $values |
284 | */ | |
6a488035 | 285 | public function buildValuesAndAssignOnline_Note_Type($id, &$values) { |
be2fb01f CW |
286 | $ids = []; |
287 | $params = ['id' => $id]; | |
6a488035 TO |
288 | CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids); |
289 | ||
290 | //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL) | |
291 | $this->_online = FALSE; | |
292 | $fids = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($id); | |
a7488080 | 293 | if (!empty($fids['financialTrxnId'])) { |
6a488035 TO |
294 | $this->_online = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id'); |
295 | } | |
eea16664 | 296 | |
6a488035 TO |
297 | // Also don't allow user to update some fields for recurring contributions. |
298 | if (!$this->_online) { | |
299 | $this->_online = CRM_Utils_Array::value('contribution_recur_id', $values); | |
300 | } | |
eea16664 | 301 | |
6a488035 TO |
302 | $this->assign('isOnline', $this->_online ? TRUE : FALSE); |
303 | ||
6a488035 TO |
304 | //to get note id |
305 | $daoNote = new CRM_Core_BAO_Note(); | |
306 | $daoNote->entity_table = 'civicrm_contribution'; | |
307 | $daoNote->entity_id = $id; | |
308 | if ($daoNote->find(TRUE)) { | |
309 | $this->_noteID = $daoNote->id; | |
310 | $values['note'] = $daoNote->note; | |
311 | } | |
312 | $this->_contributionType = $values['financial_type_id']; | |
6a488035 TO |
313 | } |
314 | ||
315 | /** | |
014c4014 TO |
316 | * @param string $type |
317 | * Eg 'Contribution'. | |
6a488035 TO |
318 | * @param string $subType |
319 | * @param int $entityId | |
320 | */ | |
321 | public function applyCustomData($type, $subType, $entityId) { | |
322 | $this->set('type', $type); | |
323 | $this->set('subType', $subType); | |
324 | $this->set('entityId', $entityId); | |
325 | ||
326 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId); | |
327 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
328 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
329 | } | |
330 | ||
4691b077 | 331 | /** |
100fef9d | 332 | * @param int $id |
4691b077 EM |
333 | * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find(); |
334 | */ | |
6ea503d4 | 335 | public function assignPremiumProduct($id) { |
6a488035 TO |
336 | $sql = " |
337 | SELECT * | |
338 | FROM civicrm_contribution_product | |
339 | WHERE contribution_id = {$id} | |
340 | "; | |
e03e1641 | 341 | $dao = CRM_Core_DAO::executeQuery($sql); |
6a488035 TO |
342 | if ($dao->fetch()) { |
343 | $this->_premiumID = $dao->id; | |
344 | $this->_productDAO = $dao; | |
345 | } | |
6a488035 TO |
346 | } |
347 | ||
6a488035 | 348 | /** |
a6c01b45 | 349 | * @return array |
16b10e64 | 350 | * Array of valid processors. The array resembles the DB table but also has 'object' as a key |
fbcb6fba EM |
351 | * @throws Exception |
352 | */ | |
353 | public function getValidProcessors() { | |
be2fb01f | 354 | $capabilities = ['BackOffice']; |
52767de0 EM |
355 | if ($this->_mode) { |
356 | $capabilities[] = (ucfirst($this->_mode) . 'Mode'); | |
fbcb6fba EM |
357 | } |
358 | $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors($capabilities); | |
44b6505d EM |
359 | return $processors; |
360 | ||
6a488035 TO |
361 | } |
362 | ||
6a488035 TO |
363 | /** |
364 | * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables | |
365 | */ | |
366 | public function assignProcessors() { | |
367 | //ensure that processor has a valid config | |
368 | //only valid processors get display to user | |
be2fb01f | 369 | $this->assign('processorSupportsFutureStartDate', CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(['FutureRecurStartDate'])); |
18135422 | 370 | $this->_paymentProcessors = $this->getValidProcessors(); |
371 | if (!isset($this->_paymentProcessor['id'])) { | |
372 | // 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 | |
373 | $this->_paymentProcessor = reset($this->_paymentProcessors); | |
374 | } | |
375 | if (!$this->_mode) { | |
376 | $this->_paymentProcessor = $this->_paymentProcessors[0]; | |
377 | } | |
be2fb01f CW |
378 | elseif (empty($this->_paymentProcessors) || array_keys($this->_paymentProcessors) === [0]) { |
379 | 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.', [1 => $this->_mode])); | |
18135422 | 380 | } |
a58e6d09 JP |
381 | //Assign submitted processor value if it is different from the loaded one. |
382 | if (!empty($this->_submitValues['payment_processor_id']) | |
383 | && $this->_paymentProcessor['id'] != $this->_submitValues['payment_processor_id']) { | |
384 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_submitValues['payment_processor_id']); | |
385 | } | |
be2fb01f | 386 | $this->_processors = []; |
18135422 | 387 | foreach ($this->_paymentProcessors as $id => $processor) { |
388 | // @todo review this. The inclusion of this IF was to address test processors being incorrectly loaded. | |
389 | // However the function $this->getValidProcessors() is expected to only return the processors relevant | |
390 | // to the mode (using the actual id - ie. the id of the test processor for the test processor). | |
391 | // for some reason there was a need to filter here per commit history - but this indicates a problem | |
392 | // somewhere else. | |
393 | if ($processor['is_test'] == ($this->_mode == 'test')) { | |
6dabf459 | 394 | $this->_processors[$id] = $processor['name']; |
18135422 | 395 | if (!empty($processor['description'])) { |
6dabf459 | 396 | $this->_processors[$id] .= ' : ' . $processor['description']; |
18135422 | 397 | } |
398 | if ($processor['is_recur']) { | |
399 | $this->_recurPaymentProcessors[$id] = $this->_processors[$id]; | |
7036a6d0 | 400 | } |
44b6505d | 401 | } |
6a488035 | 402 | } |
6c0670d1 | 403 | // CRM-21002: pass the default payment processor ID whose credit card type icons should be populated first |
404 | CRM_Financial_Form_Payment::addCreditCardJs($this->_paymentProcessor['id']); | |
18135422 | 405 | |
6a488035 TO |
406 | $this->assign('recurringPaymentProcessorIds', |
407 | empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors)) | |
408 | ); | |
409 | ||
410 | // this required to show billing block | |
03110609 | 411 | // @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 |
ebc4703a | 412 | $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor); |
6a488035 TO |
413 | } |
414 | ||
186c9c17 | 415 | /** |
6ea503d4 TO |
416 | * Get current currency from DB or use default currency. |
417 | * | |
e9bb043a | 418 | * @param array $submittedValues |
186c9c17 | 419 | * |
e9bb043a | 420 | * @return string |
186c9c17 | 421 | */ |
be2fb01f | 422 | public function getCurrency($submittedValues = []) { |
6a488035 TO |
423 | $config = CRM_Core_Config::singleton(); |
424 | ||
425 | $currentCurrency = CRM_Utils_Array::value('currency', | |
426 | $this->_values, | |
427 | $config->defaultCurrency | |
428 | ); | |
429 | ||
430 | // use submitted currency if present else use current currency | |
431 | $result = CRM_Utils_Array::value('currency', | |
432 | $submittedValues, | |
433 | $currentCurrency | |
434 | ); | |
435 | return $result; | |
436 | } | |
437 | ||
6a488035 TO |
438 | public function preProcessPledge() { |
439 | //get the payment values associated with given pledge payment id OR check for payments due. | |
be2fb01f | 440 | $this->_pledgeValues = []; |
6a488035 | 441 | if ($this->_ppID) { |
be2fb01f | 442 | $payParams = ['id' => $this->_ppID]; |
6a488035 TO |
443 | |
444 | CRM_Pledge_BAO_PledgePayment::retrieve($payParams, $this->_pledgeValues['pledgePayment']); | |
445 | $this->_pledgeID = CRM_Utils_Array::value('pledge_id', $this->_pledgeValues['pledgePayment']); | |
446 | $paymentStatusID = CRM_Utils_Array::value('status_id', $this->_pledgeValues['pledgePayment']); | |
447 | $this->_id = CRM_Utils_Array::value('contribution_id', $this->_pledgeValues['pledgePayment']); | |
448 | ||
449 | //get all status | |
450 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
451 | if (!($paymentStatusID == array_search('Pending', $allStatus) || $paymentStatusID == array_search('Overdue', $allStatus))) { | |
452 | CRM_Core_Error::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'.")); | |
453 | } | |
454 | ||
455 | //get the pledge values associated with given pledge payment. | |
456 | ||
be2fb01f CW |
457 | $ids = []; |
458 | $pledgeParams = ['id' => $this->_pledgeID]; | |
6a488035 TO |
459 | CRM_Pledge_BAO_Pledge::getValues($pledgeParams, $this->_pledgeValues, $ids); |
460 | $this->assign('ppID', $this->_ppID); | |
461 | } | |
462 | else { | |
463 | // 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 | |
464 | if (isset($this->_contactID)) { | |
465 | $contactPledges = CRM_Pledge_BAO_Pledge::getContactPledges($this->_contactID); | |
466 | ||
467 | if (!empty($contactPledges)) { | |
468 | $payments = $paymentsDue = NULL; | |
469 | $multipleDue = FALSE; | |
470 | foreach ($contactPledges as $key => $pledgeId) { | |
471 | $payments = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId); | |
472 | if ($payments) { | |
473 | if ($paymentsDue) { | |
474 | $multipleDue = TRUE; | |
475 | break; | |
476 | } | |
477 | else { | |
478 | $paymentsDue = $payments; | |
479 | } | |
480 | } | |
481 | } | |
482 | if ($multipleDue) { | |
483 | // Show link to pledge tab since more than one pledge has a payment due | |
484 | $pledgeTab = CRM_Utils_System::url('civicrm/contact/view', | |
485 | "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge" | |
486 | ); | |
be2fb01f | 487 | 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.', [1 => $pledgeTab]), ts('Notice'), 'alert'); |
6a488035 TO |
488 | } |
489 | elseif ($paymentsDue) { | |
490 | // Show user link to oldest Pending or Overdue pledge payment | |
491 | $ppAmountDue = CRM_Utils_Money::format($payments['amount'], $payments['currency']); | |
492 | $ppSchedDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date')); | |
493 | if ($this->_mode) { | |
494 | $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', | |
495 | "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live" | |
496 | ); | |
497 | } | |
498 | else { | |
499 | $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', | |
500 | "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge" | |
501 | ); | |
502 | } | |
be2fb01f | 503 | 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>.', [ |
6a488035 TO |
504 | 1 => $ppUrl, |
505 | 2 => $ppAmountDue, | |
21dfd5f5 | 506 | 3 => $ppSchedDate, |
be2fb01f | 507 | ]), ts('Notice'), 'alert'); |
6a488035 TO |
508 | } |
509 | } | |
510 | } | |
511 | } | |
512 | } | |
513 | ||
186c9c17 | 514 | /** |
07f8d162 | 515 | * @param array $submittedValues |
186c9c17 EM |
516 | * |
517 | * @return mixed | |
518 | */ | |
6a488035 TO |
519 | public function unsetCreditCardFields($submittedValues) { |
520 | //Offline Contribution. | |
be2fb01f | 521 | $unsetParams = [ |
6a488035 TO |
522 | 'payment_processor_id', |
523 | "email-{$this->_bltID}", | |
524 | 'hidden_buildCreditCard', | |
525 | 'hidden_buildDirectDebit', | |
526 | 'billing_first_name', | |
527 | 'billing_middle_name', | |
528 | 'billing_last_name', | |
529 | 'street_address-5', | |
530 | "city-{$this->_bltID}", | |
531 | "state_province_id-{$this->_bltID}", | |
532 | "postal_code-{$this->_bltID}", | |
533 | "country_id-{$this->_bltID}", | |
534 | 'credit_card_number', | |
535 | 'cvv2', | |
536 | 'credit_card_exp_date', | |
537 | 'credit_card_type', | |
be2fb01f | 538 | ]; |
6a488035 TO |
539 | foreach ($unsetParams as $key) { |
540 | if (isset($submittedValues[$key])) { | |
541 | unset($submittedValues[$key]); | |
542 | } | |
543 | } | |
544 | return $submittedValues; | |
545 | } | |
546 | ||
cc984198 | 547 | /** |
100fef9d | 548 | * Common block for setting up the parts of a form that relate to credit / debit card |
cc984198 EM |
549 | * @throws Exception |
550 | */ | |
551 | protected function assignPaymentRelatedVariables() { | |
552 | try { | |
18135422 | 553 | $this->assignProcessors(); |
554 | $this->assignBillingType(); | |
a576ecfa | 555 | CRM_Core_Payment_Form::setPaymentFieldsByProcessor($this, $this->_paymentProcessor, FALSE, TRUE, CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer', $this)); |
cc984198 EM |
556 | } |
557 | catch (CRM_Core_Exception $e) { | |
a5e16c5d | 558 | CRM_Core_Error::statusBounce($e->getMessage()); |
cc984198 EM |
559 | } |
560 | } | |
96025800 | 561 | |
3b8e6c3f | 562 | /** |
563 | * Begin post processing. | |
564 | * | |
565 | * This function aims to start to bring together common postProcessing functions. | |
566 | * | |
567 | * Eventually these are also shared with the front end forms & may need to be moved to where they can also | |
568 | * access this function. | |
569 | */ | |
570 | protected function beginPostProcess() { | |
ba2f3f65 | 571 | if ($this->_mode) { |
2c89742a | 572 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment( |
573 | $this->_params['payment_processor_id'], | |
574 | ($this->_mode == 'test') | |
575 | ); | |
ba2f3f65 | 576 | if (in_array('credit_card_exp_date', array_keys($this->_params))) { |
577 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
578 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
579 | } | |
580 | $this->assign('credit_card_exp_date', CRM_Utils_Date::mysqlToIso(CRM_Utils_Date::format($this->_params['credit_card_exp_date']))); | |
581 | $this->assign('credit_card_number', | |
582 | CRM_Utils_System::mungeCreditCard($this->_params['credit_card_number']) | |
583 | ); | |
275fba6b | 584 | $this->assign('credit_card_type', CRM_Utils_Array::value('credit_card_type', $this->_params)); |
3b8e6c3f | 585 | } |
3b8e6c3f | 586 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); |
5d3a2b9f | 587 | |
a55e39e9 | 588 | self::formatCreditCardDetails($this->_params); |
189f0360 | 589 | foreach ($this->submittableMoneyFields as $moneyField) { |
590 | if (isset($this->_params[$moneyField])) { | |
591 | $this->_params[$moneyField] = CRM_Utils_Rule::cleanMoney($this->_params[$moneyField]); | |
592 | } | |
593 | } | |
80bb54b2 MWMC |
594 | if (!empty($this->_params['contact_id']) && empty($this->_contactID)) { |
595 | // Contact ID has been set in the standalone form. | |
596 | $this->_contactID = $this->_params['contact_id']; | |
597 | $this->assignContactEmailDetails(); | |
598 | } | |
3b8e6c3f | 599 | } |
600 | ||
a55e39e9 | 601 | /** |
602 | * Format credit card details like: | |
603 | * 1. Retrieve last 4 digit from credit card number as pan_truncation | |
604 | * 2. Retrieve credit card type id from name | |
605 | * | |
606 | * @param array $params | |
607 | * | |
608 | * @return void | |
609 | */ | |
610 | public static function formatCreditCardDetails(&$params) { | |
89537f41 | 611 | if (!empty($params['credit_card_type'])) { |
a55e39e9 | 612 | $params['card_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'card_type_id', $params['credit_card_type']); |
613 | } | |
614 | if (!empty($params['credit_card_number']) && empty($params['pan_truncation'])) { | |
615 | $params['pan_truncation'] = substr($params['credit_card_number'], -4); | |
616 | } | |
617 | } | |
3b8e6c3f | 618 | |
619 | /** | |
620 | * Add the billing address to the contact who paid. | |
621 | * | |
622 | * Note that this function works based on the presence or otherwise of billing fields & can be called regardless of | |
623 | * whether they are 'expected' (due to assumptions about the payment processor type or the setting to collect billing | |
624 | * for pay later. | |
625 | */ | |
626 | protected function processBillingAddress() { | |
be2fb01f | 627 | $fields = []; |
3b8e6c3f | 628 | |
629 | $fields['email-Primary'] = 1; | |
630 | $this->_params['email-5'] = $this->_params['email-Primary'] = $this->_contributorEmail; | |
631 | // now set the values for the billing location. | |
632 | foreach (array_keys($this->_fields) as $name) { | |
633 | $fields[$name] = 1; | |
634 | } | |
635 | ||
3b8e6c3f | 636 | $fields["address_name-{$this->_bltID}"] = 1; |
637 | ||
638 | //ensure we don't over-write the payer's email with the member's email | |
639 | if ($this->_contributorContactID == $this->_contactID) { | |
640 | $fields["email-{$this->_bltID}"] = 1; | |
641 | } | |
642 | ||
643 | list($hasBillingField, $addressParams) = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID); | |
bddc8a28 | 644 | $fields = $this->formatParamsForPaymentProcessor($fields); |
3b8e6c3f | 645 | |
646 | if ($hasBillingField) { | |
647 | $addressParams = array_merge($this->_params, $addressParams); | |
dc0ca56c ML |
648 | // CRM-18277 don't let this get passed in because we don't want contribution source to override contact source. |
649 | // Ideally we wouldn't just randomly merge everything into addressParams but just pass in a relevant array. | |
650 | // Note this source field is covered by a unit test. | |
651 | if (isset($addressParams['source'])) { | |
652 | unset($addressParams['source']); | |
653 | } | |
3b8e6c3f | 654 | //here we are setting up the billing contact - if different from the member they are already created |
655 | // but they will get billing details assigned | |
656 | CRM_Contact_BAO_Contact::createProfileContact($addressParams, $fields, | |
657 | $this->_contributorContactID, NULL, NULL, | |
658 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type') | |
659 | ); | |
660 | } | |
23cb875c | 661 | |
662 | $this->assignBillingName($this->_params); | |
3b8e6c3f | 663 | } |
664 | ||
44032b26 | 665 | /** |
666 | * Get default values for billing fields. | |
667 | * | |
668 | * @todo this function still replicates code in several other places in the code. | |
669 | * | |
670 | * Also - the call to getProfileDefaults possibly covers the state_province & country already. | |
671 | * | |
672 | * @param $defaults | |
673 | * | |
674 | * @return array | |
675 | */ | |
676 | protected function getBillingDefaults($defaults) { | |
677 | // set default country from config if no country set | |
678 | $config = CRM_Core_Config::singleton(); | |
679 | if (empty($defaults["billing_country_id-{$this->_bltID}"])) { | |
680 | $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; | |
681 | } | |
682 | ||
683 | if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { | |
684 | $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; | |
685 | } | |
686 | ||
687 | $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); | |
688 | return array_merge($defaults, $billingDefaults); | |
689 | } | |
690 | ||
0dc4ef42 | 691 | /** |
692 | * Get the default payment instrument id. | |
693 | * | |
694 | * @return int | |
695 | */ | |
696 | protected function getDefaultPaymentInstrumentId() { | |
697 | $paymentInstrumentID = CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer'); | |
698 | if ($paymentInstrumentID) { | |
699 | return $paymentInstrumentID; | |
700 | } | |
701 | return key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')); | |
702 | } | |
703 | ||
b15c60e1 | 704 | /** |
705 | * Add the payment processor select to the form. | |
706 | * | |
707 | * @param bool $isRequired | |
708 | * Is it a mandatory field. | |
709 | * @param bool $isBuildRecurBlock | |
710 | * True if we want to build recur on change | |
711 | * @param bool $isBuildAutoRenewBlock | |
712 | * True if we want to build autorenew on change. | |
713 | */ | |
714 | protected function addPaymentProcessorSelect($isRequired, $isBuildRecurBlock = FALSE, $isBuildAutoRenewBlock = FALSE) { | |
715 | if (!$this->_mode) { | |
716 | return; | |
717 | } | |
be2fb01f | 718 | $js = ($isBuildRecurBlock ? ['onChange' => "buildRecurBlock( this.value ); return false;"] : NULL); |
b15c60e1 | 719 | if ($isBuildAutoRenewBlock) { |
be2fb01f | 720 | $js = ['onChange' => "buildAutoRenew( null, this.value, '{$this->_mode}');"]; |
b15c60e1 | 721 | } |
722 | $element = $this->add('select', | |
723 | 'payment_processor_id', | |
724 | ts('Payment Processor'), | |
be2fb01f | 725 | array_diff_key($this->_processors, [0 => 1]), |
b15c60e1 | 726 | $isRequired, |
727 | $js | |
728 | ); | |
729 | // The concept of _online is not really explained & the code is old | |
730 | // @todo figure out & document. | |
731 | if ($this->_online) { | |
732 | $element->freeze(); | |
733 | } | |
734 | } | |
735 | ||
c0406a91 | 736 | /** |
737 | * Assign the values to build the payment info block. | |
738 | * | |
1330f57a | 739 | * @return string |
c0406a91 | 740 | * Block title. |
741 | */ | |
742 | protected function assignPaymentInfoBlock() { | |
743 | $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, TRUE); | |
744 | $title = ts('View Payment'); | |
745 | if (!empty($this->_component) && $this->_component == 'event') { | |
746 | $info = CRM_Event_BAO_Participant::participantDetails($this->_id); | |
747 | $title .= " - {$info['title']}"; | |
748 | } | |
749 | $this->assign('transaction', TRUE); | |
750 | $this->assign('payments', $paymentInfo['transaction']); | |
751 | $this->assign('paymentLinks', $paymentInfo['payment_links']); | |
752 | return $title; | |
753 | } | |
754 | ||
80bb54b2 MWMC |
755 | protected function assignContactEmailDetails() { |
756 | if ($this->_contactID) { | |
757 | list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
a94b009a MWMC |
758 | if (empty($this->userDisplayName)) { |
759 | $this->userDisplayName = civicrm_api3('contact', 'getvalue', ['id' => $this->_contactID, 'return' => 'display_name']); | |
760 | } | |
80bb54b2 MWMC |
761 | $this->assign('displayName', $this->userDisplayName); |
762 | } | |
763 | } | |
764 | ||
6a488035 | 765 | } |