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