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