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