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