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