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