Merge pull request #10986 from eileenmcnaughton/pp
[civicrm-core.git] / CRM / Contribute / Form / AbstractEditPayment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be usefusul, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
35 * This class generates form components for processing a contribution
803d4bc0 36 * CRM-16229 - During the event registration bulk action via search we
37 * need to inherit CRM_Contact_Form_Task so that we can inherit functions
38 * like getContactIds and make use of controller state. But this is not possible
39 * because CRM_Event_Form_Participant inherits this class.
40 * Ideal situation would be something like
41 * CRM_Event_Form_Participant extends CRM_Contact_Form_Task,
42 * CRM_Contribute_Form_AbstractEditPayment
43 * However this is not possible. Currently PHP does not support multiple
44 * inheritance. So work around solution is to extend this class with
45 * CRM_Contact_Form_Task which further extends CRM_Core_Form.
6a488035
TO
46 *
47 */
5b7c9ebc 48class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task {
6a488035
TO
49 public $_mode;
50
51 public $_action;
52
53 public $_bltID;
54
a8215a8d 55 public $_fields = array();
6a488035 56
a6513ad5
EM
57 /**
58 * @var array current payment processor including a copy of the object in 'object' key
59 */
6a488035 60 public $_paymentProcessor;
7036a6d0
EM
61
62 /**
63 * Available recurring processors.
64 *
65 * @var array
66 */
67 public $_recurPaymentProcessors = array();
6a488035 68
dc913073 69 /**
100fef9d 70 * Array of processor options in the format id => array($id => $label)
dc913073
EM
71 * WARNING it appears that the format used to differ to this and there are places in the code that
72 * expect the old format. $this->_paymentProcessors provides the additional data which this
73 * array seems to have provided in the past
74 * @var array
75 */
6a488035
TO
76 public $_processors;
77
78 /**
100fef9d 79 * Available payment processors with full details including the key 'object' indexed by their id
dc913073
EM
80 * @var array
81 */
82 protected $_paymentProcessors = array();
bf4253e7
EM
83
84 /**
85 * Instance of the payment processor object.
86 *
87 * @var CRM_Core_Payment
88 */
89 protected $_paymentObject;
90
dc913073 91 /**
fe482240 92 * The id of the contribution that we are processing.
6a488035
TO
93 *
94 * @var int
6a488035
TO
95 */
96 public $_id;
97
98 /**
fe482240 99 * The id of the premium that we are proceessing.
6a488035
TO
100 *
101 * @var int
6a488035
TO
102 */
103 public $_premiumID = NULL;
4691b077
EM
104
105 /**
106 * @var CRM_Contribute_DAO_ContributionProduct
107 */
6a488035
TO
108 public $_productDAO = NULL;
109
110 /**
100fef9d 111 * The id of the note
6a488035
TO
112 *
113 * @var int
6a488035
TO
114 */
115 public $_noteID;
116
117 /**
100fef9d 118 * The id of the contact associated with this contribution
6a488035
TO
119 *
120 * @var int
6a488035
TO
121 */
122 public $_contactID;
123
124 /**
100fef9d 125 * The id of the pledge payment that we are processing
6a488035
TO
126 *
127 * @var int
6a488035
TO
128 */
129 public $_ppID;
130
131 /**
100fef9d 132 * The id of the pledge that we are processing
6a488035
TO
133 *
134 * @var int
6a488035
TO
135 */
136 public $_pledgeID;
137
138 /**
100fef9d 139 * Is this contribution associated with an online
6a488035
TO
140 * financial transaction
141 *
142 * @var boolean
6a488035
TO
143 */
144 public $_online = FALSE;
145
146 /**
147 * Stores all product option
148 *
149 * @var array
6a488035
TO
150 */
151 public $_options;
152
153 /**
100fef9d 154 * Stores the honor id
6a488035
TO
155 *
156 * @var int
6a488035
TO
157 */
158 public $_honorID = NULL;
159
160 /**
dde5a0ef 161 * Store the financial Type ID
6a488035
TO
162 *
163 * @var array
164 */
165 public $_contributionType;
166
167 /**
168 * The contribution values if an existing contribution
169 */
170 public $_values;
171
172 /**
173 * The pledge values if this contribution is associated with pledge
174 */
175 public $_pledgeValues;
176
177 public $_contributeMode = 'direct';
178
179 public $_context;
180
181 public $_compId;
182
d424ffde 183 /**
6a488035
TO
184 * Store the line items if price set used.
185 */
186 public $_lineItems;
187
0be0b79d
EM
188 /**
189 * Is this a backoffice form
18135422 190 *
0be0b79d
EM
191 * @var bool
192 */
193 public $isBackOffice = TRUE;
194
6a488035 195 protected $_formType;
dde5a0ef 196
18135422 197 /**
198 * Payment instrument id for the transaction.
199 *
200 * @var int
201 */
202 public $paymentInstrumentID;
203
dde5a0ef 204 /**
100fef9d 205 * 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
206 * be all in this array in order like
207 * 'credit_card' => array('credit_card_number' ...
208 * 'billing_details' => array('first_name' ...
209 *
210 * such that both the fields and the order can be more easily altered by payment processors & other extensions
211 * @var array
212 */
213 public $billingFieldSets = array();
214
42e8b05c
EM
215 /**
216 * Pre process function with common actions.
217 */
218 public function preProcess() {
219 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
220 $this->assign('contactID', $this->_contactID);
9977ae61 221 CRM_Core_Resources::singleton()->addVars('coreForm', array('contact_id' => (int) $this->_contactID));
42e8b05c 222 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
66264aed 223 $this->_mode = empty($this->_mode) ? CRM_Utils_Request::retrieve('mode', 'String', $this) : $this->_mode;
a55e39e9 224 $this->assign('isBackOffice', $this->isBackOffice);
0dc4ef42 225 $this->assignPaymentRelatedVariables();
42e8b05c
EM
226 }
227
186c9c17 228 /**
100fef9d 229 * @param int $id
186c9c17 230 */
d5397f2f
PJ
231 public function showRecordLinkMesssage($id) {
232 $statusId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id');
233 if (CRM_Contribute_PseudoConstant::contributionStatus($statusId, 'name') == 'Partially paid') {
234 if ($pid = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) {
235 $recordPaymentLink = CRM_Utils_System::url('civicrm/payment',
236 "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event"
237 );
238 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.', array(1 => $recordPaymentLink)), ts('Notice'), 'alert');
239 }
240 }
241 }
242
186c9c17 243 /**
100fef9d 244 * @param int $id
186c9c17
EM
245 * @param $values
246 */
6a488035
TO
247 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
248 $ids = array();
249 $params = array('id' => $id);
250 CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids);
251
252 //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL)
253 $this->_online = FALSE;
254 $fids = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($id);
a7488080 255 if (!empty($fids['financialTrxnId'])) {
6a488035
TO
256 $this->_online = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
257 }
eea16664 258
6a488035
TO
259 // Also don't allow user to update some fields for recurring contributions.
260 if (!$this->_online) {
261 $this->_online = CRM_Utils_Array::value('contribution_recur_id', $values);
262 }
eea16664 263
6a488035
TO
264 $this->assign('isOnline', $this->_online ? TRUE : FALSE);
265
6a488035
TO
266 //to get note id
267 $daoNote = new CRM_Core_BAO_Note();
268 $daoNote->entity_table = 'civicrm_contribution';
269 $daoNote->entity_id = $id;
270 if ($daoNote->find(TRUE)) {
271 $this->_noteID = $daoNote->id;
272 $values['note'] = $daoNote->note;
273 }
274 $this->_contributionType = $values['financial_type_id'];
6a488035
TO
275 }
276
277 /**
014c4014
TO
278 * @param string $type
279 * Eg 'Contribution'.
6a488035
TO
280 * @param string $subType
281 * @param int $entityId
282 */
283 public function applyCustomData($type, $subType, $entityId) {
284 $this->set('type', $type);
285 $this->set('subType', $subType);
286 $this->set('entityId', $entityId);
287
288 CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId);
289 CRM_Custom_Form_CustomData::buildQuickForm($this);
290 CRM_Custom_Form_CustomData::setDefaultValues($this);
291 }
292
4691b077 293 /**
100fef9d 294 * @param int $id
4691b077
EM
295 * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find();
296 */
6ea503d4 297 public function assignPremiumProduct($id) {
6a488035
TO
298 $sql = "
299SELECT *
300FROM civicrm_contribution_product
301WHERE contribution_id = {$id}
302";
303 $dao = CRM_Core_DAO::executeQuery($sql,
304 CRM_Core_DAO::$_nullArray
305 );
306 if ($dao->fetch()) {
307 $this->_premiumID = $dao->id;
308 $this->_productDAO = $dao;
309 }
310 $dao->free();
311 }
312
6a488035 313 /**
a6c01b45 314 * @return array
16b10e64 315 * Array of valid processors. The array resembles the DB table but also has 'object' as a key
fbcb6fba
EM
316 * @throws Exception
317 */
318 public function getValidProcessors() {
fbcb6fba 319 $capabilities = array('BackOffice');
52767de0
EM
320 if ($this->_mode) {
321 $capabilities[] = (ucfirst($this->_mode) . 'Mode');
fbcb6fba
EM
322 }
323 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors($capabilities);
44b6505d
EM
324 return $processors;
325
6a488035
TO
326 }
327
6a488035
TO
328 /**
329 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
330 */
331 public function assignProcessors() {
332 //ensure that processor has a valid config
333 //only valid processors get display to user
18135422 334 $this->assign('processorSupportsFutureStartDate', CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('FutureRecurStartDate')));
335 $this->_paymentProcessors = $this->getValidProcessors();
336 if (!isset($this->_paymentProcessor['id'])) {
337 // 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
338 $this->_paymentProcessor = reset($this->_paymentProcessors);
339 }
340 if (!$this->_mode) {
341 $this->_paymentProcessor = $this->_paymentProcessors[0];
342 }
343 elseif (empty($this->_paymentProcessors) || array_keys($this->_paymentProcessors) === array(0)) {
344 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.', array(1 => $this->_mode)));
345 }
346 $this->_processors = array();
347 foreach ($this->_paymentProcessors as $id => $processor) {
348 // @todo review this. The inclusion of this IF was to address test processors being incorrectly loaded.
349 // However the function $this->getValidProcessors() is expected to only return the processors relevant
350 // to the mode (using the actual id - ie. the id of the test processor for the test processor).
351 // for some reason there was a need to filter here per commit history - but this indicates a problem
352 // somewhere else.
353 if ($processor['is_test'] == ($this->_mode == 'test')) {
354 $this->_processors[$id] = ts($processor['name']);
355 if (!empty($processor['description'])) {
356 $this->_processors[$id] .= ' : ' . ts($processor['description']);
357 }
358 if ($processor['is_recur']) {
359 $this->_recurPaymentProcessors[$id] = $this->_processors[$id];
7036a6d0 360 }
44b6505d 361 }
6a488035 362 }
6c0670d1 363 // CRM-21002: pass the default payment processor ID whose credit card type icons should be populated first
364 CRM_Financial_Form_Payment::addCreditCardJs($this->_paymentProcessor['id']);
18135422 365
6a488035
TO
366 $this->assign('recurringPaymentProcessorIds',
367 empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors))
368 );
369
370 // this required to show billing block
03110609 371 // @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 372 $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor);
6a488035
TO
373 }
374
186c9c17 375 /**
6ea503d4
TO
376 * Get current currency from DB or use default currency.
377 *
186c9c17
EM
378 * @param $submittedValues
379 *
380 * @return mixed
381 */
6ea503d4 382 public function getCurrency($submittedValues) {
6a488035
TO
383 $config = CRM_Core_Config::singleton();
384
385 $currentCurrency = CRM_Utils_Array::value('currency',
386 $this->_values,
387 $config->defaultCurrency
388 );
389
390 // use submitted currency if present else use current currency
391 $result = CRM_Utils_Array::value('currency',
392 $submittedValues,
393 $currentCurrency
394 );
395 return $result;
396 }
397
6a488035
TO
398 public function preProcessPledge() {
399 //get the payment values associated with given pledge payment id OR check for payments due.
400 $this->_pledgeValues = array();
401 if ($this->_ppID) {
402 $payParams = array('id' => $this->_ppID);
403
404 CRM_Pledge_BAO_PledgePayment::retrieve($payParams, $this->_pledgeValues['pledgePayment']);
405 $this->_pledgeID = CRM_Utils_Array::value('pledge_id', $this->_pledgeValues['pledgePayment']);
406 $paymentStatusID = CRM_Utils_Array::value('status_id', $this->_pledgeValues['pledgePayment']);
407 $this->_id = CRM_Utils_Array::value('contribution_id', $this->_pledgeValues['pledgePayment']);
408
409 //get all status
410 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
411 if (!($paymentStatusID == array_search('Pending', $allStatus) || $paymentStatusID == array_search('Overdue', $allStatus))) {
412 CRM_Core_Error::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
413 }
414
415 //get the pledge values associated with given pledge payment.
416
417 $ids = array();
418 $pledgeParams = array('id' => $this->_pledgeID);
419 CRM_Pledge_BAO_Pledge::getValues($pledgeParams, $this->_pledgeValues, $ids);
420 $this->assign('ppID', $this->_ppID);
421 }
422 else {
423 // 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
424 if (isset($this->_contactID)) {
425 $contactPledges = CRM_Pledge_BAO_Pledge::getContactPledges($this->_contactID);
426
427 if (!empty($contactPledges)) {
428 $payments = $paymentsDue = NULL;
429 $multipleDue = FALSE;
430 foreach ($contactPledges as $key => $pledgeId) {
431 $payments = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
432 if ($payments) {
433 if ($paymentsDue) {
434 $multipleDue = TRUE;
435 break;
436 }
437 else {
438 $paymentsDue = $payments;
439 }
440 }
441 }
442 if ($multipleDue) {
443 // Show link to pledge tab since more than one pledge has a payment due
444 $pledgeTab = CRM_Utils_System::url('civicrm/contact/view',
445 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
446 );
447 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.', array(1 => $pledgeTab)), ts('Notice'), 'alert');
448 }
449 elseif ($paymentsDue) {
450 // Show user link to oldest Pending or Overdue pledge payment
451 $ppAmountDue = CRM_Utils_Money::format($payments['amount'], $payments['currency']);
452 $ppSchedDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
453 if ($this->_mode) {
454 $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution',
455 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
456 );
457 }
458 else {
459 $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution',
460 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
461 );
462 }
463 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>.', array(
464 1 => $ppUrl,
465 2 => $ppAmountDue,
21dfd5f5 466 3 => $ppSchedDate,
6a488035
TO
467 )), ts('Notice'), 'alert');
468 }
469 }
470 }
471 }
472 }
473
186c9c17 474 /**
07f8d162 475 * @param array $submittedValues
186c9c17
EM
476 *
477 * @return mixed
478 */
6a488035
TO
479 public function unsetCreditCardFields($submittedValues) {
480 //Offline Contribution.
481 $unsetParams = array(
482 'payment_processor_id',
483 "email-{$this->_bltID}",
484 'hidden_buildCreditCard',
485 'hidden_buildDirectDebit',
486 'billing_first_name',
487 'billing_middle_name',
488 'billing_last_name',
489 'street_address-5',
490 "city-{$this->_bltID}",
491 "state_province_id-{$this->_bltID}",
492 "postal_code-{$this->_bltID}",
493 "country_id-{$this->_bltID}",
494 'credit_card_number',
495 'cvv2',
496 'credit_card_exp_date',
497 'credit_card_type',
498 );
499 foreach ($unsetParams as $key) {
500 if (isset($submittedValues[$key])) {
501 unset($submittedValues[$key]);
502 }
503 }
504 return $submittedValues;
505 }
506
cc984198 507 /**
100fef9d 508 * Common block for setting up the parts of a form that relate to credit / debit card
cc984198
EM
509 * @throws Exception
510 */
511 protected function assignPaymentRelatedVariables() {
512 try {
99efbbf8
EM
513 if ($this->_contactID) {
514 list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
515 $this->assign('displayName', $this->userDisplayName);
516 }
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 if (in_array('credit_card_exp_date', array_keys($this->_params))) {
541 $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params);
542 $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params);
543 }
544 $this->assign('credit_card_exp_date', CRM_Utils_Date::mysqlToIso(CRM_Utils_Date::format($this->_params['credit_card_exp_date'])));
545 $this->assign('credit_card_number',
546 CRM_Utils_System::mungeCreditCard($this->_params['credit_card_number'])
547 );
275fba6b 548 $this->assign('credit_card_type', CRM_Utils_Array::value('credit_card_type', $this->_params));
3b8e6c3f 549 }
3b8e6c3f 550 $this->_params['ip_address'] = CRM_Utils_System::ipAddress();
5d3a2b9f 551
a55e39e9 552 self::formatCreditCardDetails($this->_params);
3b8e6c3f 553 }
554
a55e39e9 555 /**
556 * Format credit card details like:
557 * 1. Retrieve last 4 digit from credit card number as pan_truncation
558 * 2. Retrieve credit card type id from name
559 *
560 * @param array $params
561 *
562 * @return void
563 */
564 public static function formatCreditCardDetails(&$params) {
565 if (in_array('credit_card_type', array_keys($params))) {
566 $params['card_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'card_type_id', $params['credit_card_type']);
567 }
568 if (!empty($params['credit_card_number']) && empty($params['pan_truncation'])) {
569 $params['pan_truncation'] = substr($params['credit_card_number'], -4);
570 }
571 }
3b8e6c3f 572
573 /**
574 * Add the billing address to the contact who paid.
575 *
576 * Note that this function works based on the presence or otherwise of billing fields & can be called regardless of
577 * whether they are 'expected' (due to assumptions about the payment processor type or the setting to collect billing
578 * for pay later.
579 */
580 protected function processBillingAddress() {
581 $fields = array();
582
583 $fields['email-Primary'] = 1;
584 $this->_params['email-5'] = $this->_params['email-Primary'] = $this->_contributorEmail;
585 // now set the values for the billing location.
586 foreach (array_keys($this->_fields) as $name) {
587 $fields[$name] = 1;
588 }
589
3b8e6c3f 590 $fields["address_name-{$this->_bltID}"] = 1;
591
592 //ensure we don't over-write the payer's email with the member's email
593 if ($this->_contributorContactID == $this->_contactID) {
594 $fields["email-{$this->_bltID}"] = 1;
595 }
596
597 list($hasBillingField, $addressParams) = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID);
bddc8a28 598 $fields = $this->formatParamsForPaymentProcessor($fields);
3b8e6c3f 599
600 if ($hasBillingField) {
601 $addressParams = array_merge($this->_params, $addressParams);
dc0ca56c
ML
602 // CRM-18277 don't let this get passed in because we don't want contribution source to override contact source.
603 // Ideally we wouldn't just randomly merge everything into addressParams but just pass in a relevant array.
604 // Note this source field is covered by a unit test.
605 if (isset($addressParams['source'])) {
606 unset($addressParams['source']);
607 }
3b8e6c3f 608 //here we are setting up the billing contact - if different from the member they are already created
609 // but they will get billing details assigned
610 CRM_Contact_BAO_Contact::createProfileContact($addressParams, $fields,
611 $this->_contributorContactID, NULL, NULL,
612 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type')
613 );
614 }
23cb875c 615
616 $this->assignBillingName($this->_params);
3b8e6c3f 617 }
618
44032b26 619 /**
620 * Get default values for billing fields.
621 *
622 * @todo this function still replicates code in several other places in the code.
623 *
624 * Also - the call to getProfileDefaults possibly covers the state_province & country already.
625 *
626 * @param $defaults
627 *
628 * @return array
629 */
630 protected function getBillingDefaults($defaults) {
631 // set default country from config if no country set
632 $config = CRM_Core_Config::singleton();
633 if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
634 $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
635 }
636
637 if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) {
638 $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
639 }
640
641 $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID);
642 return array_merge($defaults, $billingDefaults);
643 }
644
0dc4ef42 645 /**
646 * Get the default payment instrument id.
647 *
648 * @return int
649 */
650 protected function getDefaultPaymentInstrumentId() {
651 $paymentInstrumentID = CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer');
652 if ($paymentInstrumentID) {
653 return $paymentInstrumentID;
654 }
655 return key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
656 }
657
b15c60e1 658 /**
659 * Add the payment processor select to the form.
660 *
661 * @param bool $isRequired
662 * Is it a mandatory field.
663 * @param bool $isBuildRecurBlock
664 * True if we want to build recur on change
665 * @param bool $isBuildAutoRenewBlock
666 * True if we want to build autorenew on change.
667 */
668 protected function addPaymentProcessorSelect($isRequired, $isBuildRecurBlock = FALSE, $isBuildAutoRenewBlock = FALSE) {
669 if (!$this->_mode) {
670 return;
671 }
672 $js = ($isBuildRecurBlock ? array('onChange' => "buildRecurBlock( this.value ); return false;") : NULL);
673 if ($isBuildAutoRenewBlock) {
674 $js = array('onChange' => "buildAutoRenew( null, this.value, '{$this->_mode}');");
675 }
676 $element = $this->add('select',
677 'payment_processor_id',
678 ts('Payment Processor'),
679 array_diff_key($this->_processors, array(0 => 1)),
680 $isRequired,
681 $js
682 );
683 // The concept of _online is not really explained & the code is old
684 // @todo figure out & document.
685 if ($this->_online) {
686 $element->freeze();
687 }
688 }
689
6a488035 690}