phpcs - Fix error, "Visibility must be declared on method"
[civicrm-core.git] / CRM / Contribute / Form / AdditionalPayment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This form records additional payments needed when
38 * event/contribution is partially paid
39 *
40 */
41 class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_AbstractEditPayment {
42 public $_contributeMode = 'direct';
43
44 /**
45 * Related component whose financial payment is being processed
46 *
47 * @var string
48 * @public
49 */
50 protected $_component = NULL;
51
52 /**
53 * Id of the component entity
54 */
55 public $_id = NULL;
56
57 protected $_owed = NULL;
58
59 protected $_refund = NULL;
60
61 protected $_contactId = NULL;
62
63 protected $_contributorDisplayName = NULL;
64
65 protected $_contributorEmail = NULL;
66
67 protected $_toDoNotEmail = NULL;
68
69 protected $_paymentType = NULL;
70
71 protected $_contributionId = NULL;
72
73 protected $fromEmailId = NULL;
74
75 protected $_fromEmails = NULL;
76
77 protected $_view = NULL;
78
79 public $_action = NULL;
80
81 public function preProcess() {
82 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
83 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
84 $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this, TRUE);
85 $this->_view = CRM_Utils_Request::retrieve('view', 'String', $this, FALSE);
86 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
87 $this->assign('component', $this->_component);
88 $this->assign('id', $this->_id);
89
90 if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
91 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, TRUE);
92 $transactionRows = $paymentInfo['transaction'];
93 $title = ts('View Payment');
94 if ($this->_component == 'event') {
95 $info = CRM_Event_BAO_Participant::participantDetails($this->_id);
96 $title .= " - {$info['title']}";
97 }
98 CRM_Utils_System::setTitle($title);
99 $this->assign('transaction', TRUE);
100 $this->assign('rows', $transactionRows);
101 return;
102 }
103 $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
104 $this->_formType = CRM_Utils_Array::value('formType', $_GET);
105
106 $enitityType = NULL;
107 if ($this->_component == 'event') {
108 $enitityType = 'participant';
109 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'contribution_id', 'participant_id');
110 }
111 $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id');
112 $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($eventId);
113
114 $paymentInfo = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($this->_id, $enitityType);
115 $paymentDetails = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, FALSE, TRUE);
116
117 $this->_amtPaid = $paymentDetails['paid'];
118 $this->_amtTotal = $paymentDetails['total'];
119
120 if (!empty($paymentInfo['refund_due'])) {
121 $paymentAmt = $this->_refund = $paymentInfo['refund_due'];
122 $this->_paymentType = 'refund';
123 }
124 elseif (!empty($paymentInfo['amount_owed'])) {
125 $paymentAmt = $this->_owed = $paymentInfo['amount_owed'];
126 $this->_paymentType = 'owed';
127 }
128 else {
129 CRM_Core_Error::fatal(ts('No payment information found for this record'));
130 }
131
132 //set the payment mode - _mode property is defined in parent class
133 $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
134
135 if (!empty($this->_mode) && $this->_paymentType == 'refund') {
136 CRM_Core_Error::fatal(ts('Credit card payment is not for Refund payments use'));
137 }
138
139 list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
140
141 $this->assignPaymentRelatedVariables();
142
143 $this->assign('contributionMode', $this->_mode);
144 $this->assign('contactId', $this->_contactId);
145 $this->assign('paymentType', $this->_paymentType);
146 $this->assign('paymentAmt', abs($paymentAmt));
147
148 $this->setPageTitle($this->_refund ? ts('Refund') : ts('Payment'));
149 }
150
151 /**
152 * This virtual function is used to set the default values of
153 * various form elements
154 *
155 * access public
156 *
157 * @return array reference to the array of default values
158 *
159 */
160 /**
161 * @return array
162 */
163 public function setDefaultValues() {
164 if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
165 return;
166 }
167 $defaults = array( );
168 if ($this->_mode) {
169 $defaults = $this->_values;
170
171 $config = CRM_Core_Config::singleton();
172 // set default country from config if no country set
173 if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
174 $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
175 }
176
177 if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) {
178 $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
179 }
180
181 $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactId);
182 $defaults = array_merge($defaults, $billingDefaults);
183 }
184
185 if (empty($defaults['trxn_date']) && empty($defaults['trxn_date_time'])) {
186 list($defaults['trxn_date'],
187 $defaults['trxn_date_time']
188 ) = CRM_Utils_Date::setDateDefaults(
189 CRM_Utils_Array::value('register_date', $defaults), 'activityDateTime'
190 );
191 }
192
193 if ($this->_refund) {
194 $defaults['total_amount'] = abs($this->_refund);
195 }
196
197 // Set $newCredit variable in template to control whether link to credit card mode is included
198 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
199 return $defaults;
200 }
201
202 public function buildQuickForm() {
203 if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
204 $this->addButtons(array(
205 array(
206 'type' => 'cancel',
207 'name' => ts('Done'),
208 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
209 'isDefault' => TRUE,
210 ),
211 )
212 );
213 return;
214 }
215 $ccPane = NULL;
216 if ($this->_mode) {
217 if (CRM_Utils_Array::value('payment_type', $this->_processors) & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
218 ) {
219 $ccPane = array(ts('Direct Debit Information') => 'DirectDebit');
220 }
221 else {
222 $ccPane = array(ts('Credit Card Information') => 'CreditCard');
223 }
224 $defaults = $this->_values;
225 $showAdditionalInfo = FALSE;
226
227 foreach ($ccPane as $name => $type) {
228 if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) ||
229 CRM_Utils_Array::value("hidden_{$type}", $defaults)
230 ) {
231 $showAdditionalInfo = TRUE;
232 $allPanes[$name]['open'] = 'true';
233 }
234
235 $urlParams = "snippet=4&formType={$type}";
236 if ($this->_mode) {
237 $urlParams .= "&mode={$this->_mode}";
238 }
239 $open = 'false';
240 if ($type == 'CreditCard' ||
241 $type == 'DirectDebit'
242 ) {
243 $open = 'true';
244 }
245
246 $allPanes[$name] = array(
247 'url' => CRM_Utils_System::url('civicrm/payment/add', $urlParams),
248 'open' => $open,
249 'id' => $type
250 );
251
252 CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE);
253
254 $qfKey = $this->controller->_key;
255 $this->assign('qfKey', $qfKey);
256 $this->assign('allPanes', $allPanes);
257 $this->assign('showAdditionalInfo', $showAdditionalInfo);
258
259 if ($this->_formType) {
260 $this->assign('formType', $this->_formType);
261 return;
262 }
263 }
264 }
265 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialTrxn');
266
267 $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors, NULL);
268 $label = ($this->_refund) ? 'Refund Amount' : 'Payment Amount';
269 $this->addMoney('total_amount',
270 ts('%1', array(1 => $label)),
271 FALSE,
272 $attributes['total_amount'],
273 TRUE, 'currency', NULL
274 );
275
276 $this->add('select', 'payment_instrument_id',
277 ts('Paid By'),
278 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(),
279 TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
280 );
281
282 $this->add('text', 'check_number', ts('Check Number'), $attributes['financial_trxn_check_number']);
283 $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') + $attributes['trxn_id']);
284
285 //add receipt for offline contribution
286 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
287
288 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
289
290 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
291
292 // add various dates
293 $dateLabel = ($this->_refund) ? 'Refund Date' : 'Received Date';
294 $this->addDateTime('trxn_date', ts('%1', array(1 => $dateLabel)), FALSE, array('formatType' => 'activityDateTime'));
295
296 if ($this->_contactId && $this->_id) {
297 if ($this->_component == 'event') {
298 $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id');
299 $event = CRM_Event_BAO_Event::getEvents(0, $eventId);
300 $this->assign('eventName', $event[$eventId]);
301 }
302 }
303
304 $this->assign('displayName', $this->_contributorDisplayName);
305 $this->assign('component', $this->_component);
306 $this->assign('email', $this->_contributorEmail);
307
308 $this->add('text', 'fee_amount', ts('Fee Amount'),
309 $attributes['fee_amount']
310 );
311 $this->addRule('fee_amount', ts('Please enter a valid monetary value for Fee Amount.'), 'money');
312
313 $this->add('text', 'net_amount', ts('Net Amount'),
314 $attributes['net_amount']
315 );
316 $this->addRule('net_amount', ts('Please enter a valid monetary value for Net Amount.'), 'money');
317
318 $js = NULL;
319 if (!$this->_mode) {
320 $js = array('onclick' => "return verify( );");
321 }
322
323 $buttonName = $this->_refund ? 'Record Refund' : 'Record Payment';
324 $this->addButtons(array(
325 array(
326 'type' => 'upload',
327 'name' => ts('%1', array(1 => $buttonName)),
328 'js' => $js,
329 'isDefault' => TRUE
330 ),
331 array(
332 'type' => 'cancel',
333 'name' => ts('Cancel')
334 ),
335 )
336 );
337
338
339 $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
340 'mailing_backend'
341 );
342 $this->assign('outBound_option', $mailingInfo['outBound_option']);
343
344 $this->addFormRule(array('CRM_Contribute_Form_AdditionalPayment', 'formRule'), $this);
345 }
346
347 /**
348 * @param $fields
349 * @param $files
350 * @param $self
351 *
352 * @return array
353 */
354 public static function formRule($fields, $files, $self) {
355 $errors = array();
356 if ($self->_paymentType == 'owed' && $fields['total_amount'] > $self->_owed) {
357 $errors['total_amount'] = ts('Payment amount cannot be greater than owed amount');
358 }
359 if ($self->_paymentType == 'refund' && $fields['total_amount'] != abs($self->_refund)) {
360 $errors['total_amount'] = ts('Refund amount must equal refund due amount.');
361 }
362 $netAmt = $fields['total_amount'] - $fields['fee_amount'];
363 if (!empty($fields['net_amount']) && $netAmt != $fields['net_amount']) {
364 $errors['net_amount'] = ts('Net amount should be equal to the difference between payment amount and fee amount.');
365 }
366 return $errors;
367 }
368
369 public function postProcess() {
370 $participantId = NULL;
371 if ($this->_component == 'event') {
372 $participantId = $this->_id;
373 }
374 $submittedValues = $this->controller->exportValues($this->_name);
375 $submittedValues['confirm_email_text'] = CRM_Utils_Array::value('receipt_text', $submittedValues);
376
377 $submittedValues['trxn_date'] = CRM_Utils_Date::processDate($submittedValues['trxn_date'], $submittedValues['trxn_date_time']);
378 if ($this->_mode) {
379 // process credit card
380 $this->assign('contributeMode', 'direct');
381 $this->processCreditCard($submittedValues);
382 }
383 else {
384 $result = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, $this->_paymentType, $participantId);
385
386 // email sending
387 if (!empty($result) && !empty($submittedValues['is_email_receipt'])) {
388 $submittedValues['contact_id'] = $this->_contactId;
389 $submittedValues['contribution_id'] = $this->_contributionId;
390
391 // to get 'from email id' for send receipt
392 $this->fromEmailId = $submittedValues['from_email_address'];
393 $sendReceipt = $this->emailReceipt($submittedValues);
394 }
395
396 $statusMsg = ts('The payment record has been processed.');
397 if (!empty($submittedValues['is_email_receipt']) && $sendReceipt) {
398 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
399 }
400
401 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
402
403 $session = CRM_Core_Session::singleton();
404 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
405 "reset=1&cid={$this->_contactId}&selectedChild=participant"
406 ));
407 }
408 }
409
410 /**
411 * @param $submittedValues
412 */
413 public function processCreditCard($submittedValues) {
414 $config = CRM_Core_Config::singleton();
415 $session = CRM_Core_Session::singleton();
416
417 $unsetParams = array(
418 'trxn_id',
419 'payment_instrument_id',
420 'contribution_status_id',
421 );
422 foreach ($unsetParams as $key) {
423 if (isset($submittedValues[$key])) {
424 unset($submittedValues[$key]);
425 }
426 }
427
428 //Get the rquire fields value only.
429 $params = $this->_params = $submittedValues;
430
431 //get the payment processor id as per mode.
432 //@todo unclear relevance of mode - seems like a lot of duplicated params here!
433 $this->_params['payment_processor'] = $params['payment_processor_id'] =
434 $this->_params['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor['id'];
435
436 $now = date('YmdHis');
437 $fields = array();
438
439 // we need to retrieve email address
440 if ($this->_context == 'standalone' && !empty($submittedValues['is_email_receipt'])) {
441 list($this->userDisplayName,
442 $this->userEmail
443 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
444 $this->assign('displayName', $this->userDisplayName);
445 }
446
447 //set email for primary location.
448 $fields['email-Primary'] = 1;
449 $params['email-Primary'] = $this->_contributorEmail;
450
451 // now set the values for the billing location.
452 foreach ($this->_fields as $name => $dontCare) {
453 $fields[$name] = 1;
454 }
455
456 // also add location name to the array
457 $params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params);
458 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
459 $fields["address_name-{$this->_bltID}"] = 1;
460
461 $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
462 $this->_contactId,
463 'contact_type'
464 );
465
466 $nameFields = array('first_name', 'middle_name', 'last_name');
467 foreach ($nameFields as $name) {
468 $fields[$name] = 1;
469 if (array_key_exists("billing_$name", $params)) {
470 $params[$name] = $params["billing_{$name}"];
471 $params['preserveDBName'] = TRUE;
472 }
473 }
474
475 if (!empty($params['source'])) {
476 unset($params['source']);
477 }
478 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields,
479 $this->_contactId,
480 NULL, NULL,
481 $ctype
482 );
483
484 // add all the additioanl payment params we need
485 $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]);
486 $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]);
487
488 if ($this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_CREDIT_CARD) {
489 $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params);
490 $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params);
491 }
492 $this->_params['ip_address'] = CRM_Utils_System::ipAddress();
493 $this->_params['amount'] = $this->_params['total_amount'];
494 $this->_params['amount_level'] = 0;
495 $this->_params['currencyID'] = CRM_Utils_Array::value('currency',
496 $this->_params,
497 $config->defaultCurrency
498 );
499 $this->_params['payment_action'] = 'Sale';
500 if (!empty($this->_params['trxn_date'])) {
501 $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['trxn_date'], $this->_params['trxn_date_time']);
502 }
503
504 if (empty($this->_params['invoice_id'])) {
505 $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
506 }
507 else {
508 $this->_params['invoiceID'] = $this->_params['invoice_id'];
509 }
510
511 // billing name and Address
512 $name = CRM_Utils_Array::value('billing_first_name', $params);
513 if (!empty($params['billing_middle_name'])) {
514 $name .= " {$params['billing_middle_name']}";
515 }
516 $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $params);
517 $name = trim($name);
518 $this->assign('billingName', $name);
519
520 //assign the address formatted up for display
521 $addressParts = array(
522 "street_address" => "billing_street_address-{$this->_bltID}",
523 "city" => "billing_city-{$this->_bltID}",
524 "postal_code" => "billing_postal_code-{$this->_bltID}",
525 "state_province" => "state_province-{$this->_bltID}",
526 "country" => "country-{$this->_bltID}",
527 );
528 $addressFields = array();
529 foreach ($addressParts as $name => $field) {
530 $addressFields[$name] = CRM_Utils_Array::value($field, $params);
531 }
532 $this->assign('address', CRM_Utils_Address::format($addressFields));
533 $date = CRM_Utils_Date::format($params['credit_card_exp_date']);
534 $date = CRM_Utils_Date::mysqlToIso($date);
535 $this->assign('credit_card_type', CRM_Utils_Array::value('credit_card_type', $params));
536 $this->assign('credit_card_exp_date', $date);
537 $this->assign('credit_card_number',
538 CRM_Utils_System::mungeCreditCard($params['credit_card_number'])
539 );
540
541 //Add common data to formatted params
542 CRM_Contribute_Form_AdditionalInfo::postProcessCommon($params, $this->_params, $this);
543 // at this point we've created a contact and stored its address etc
544 // all the payment processors expect the name and address to be in the
545 // so we copy stuff over to first_name etc.
546 $paymentParams = $this->_params;
547 $paymentParams['contactID'] = $this->_contactId;
548 CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE);
549
550 // add some financial type details to the params list
551 // if folks need to use it
552 $paymentParams['contributionType_name'] = $this->_params['contributionType_name'] = $contributionType->name;
553 $paymentParams['contributionPageID'] = NULL;
554 if (!empty($this->_params['is_email_receipt'])) {
555 $paymentParams['email'] = $this->_contributorEmail;
556 $paymentParams['is_email_receipt'] = 1;
557 }
558 else {
559 $paymentParams['is_email_receipt'] = 0;
560 $this->_params['is_email_receipt'] = 0;
561 }
562 if (!empty($this->_params['receive_date'])) {
563 $paymentParams['receive_date'] = $this->_params['receive_date'];
564 }
565 if (!empty($this->_params['receive_date'])) {
566 $paymentParams['receive_date'] = $this->_params['receive_date'];
567 }
568
569 $result = NULL;
570
571 if ($paymentParams['amount'] > 0.0) {
572 // force a reget of the payment processor in case the form changed it, CRM-7179
573 $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this, TRUE);
574 $result = $payment->doDirectPayment($paymentParams);
575 }
576
577 if (is_a($result, 'CRM_Core_Error')) {
578 //set the contribution mode.
579 $urlParams = "action=add&cid={$this->_contactId}&id={$this->_id}&component={$this->_component}";
580 if ($this->_mode) {
581 $urlParams .= "&mode={$this->_mode}";
582 }
583 CRM_Core_Error::displaySessionError($result);
584 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/payment/add', $urlParams));
585 }
586
587 if ($result) {
588 $this->_params = array_merge($this->_params, $result);
589 }
590
591 if (empty($this->_params['receive_date'])) {
592 $this->_params['receive_date'] = $now;
593 }
594
595 $this->set('params', $this->_params);
596
597 // set source if not set
598 if (empty($this->_params['source'])) {
599 $userID = $session->get('userID');
600 $userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID,
601 'sort_name'
602 );
603 $this->_params['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
604 }
605
606 // process the additional payment
607 $participantId = NULL;
608 if ($this->_component == 'event') {
609 $participantId = $this->_id;
610 }
611 $trxnRecord = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, $this->_paymentType, $participantId);
612
613 if ($trxnRecord->id && !empty($this->_params['is_email_receipt'])) {
614 $sendReceipt = $this->emailReceipt($this->_params);
615 }
616
617 if ($trxnRecord->id) {
618 $statusMsg = ts('The payment record has been processed.');
619 if (!empty($this->_params['is_email_receipt']) && $sendReceipt) {
620 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
621 }
622
623 CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success');
624 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
625 "reset=1&cid={$this->_contactId}&selectedChild=participant"
626 ));
627 }
628 }
629
630 /**
631 * @param array $params
632 *
633 * @return mixed
634 */
635 public function emailReceipt(&$params) {
636 // email receipt sending
637 // send message template
638 if ($this->_component == 'event') {
639 $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id');
640
641 $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title');
642 CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $eventId, $events, $returnProperties);
643 $event = $events[$eventId];
644 unset($event['start_date']);
645 unset($event['end_date']);
646
647 $this->assign('event', $event);
648 $this->assign('isShowLocation', $event['is_show_location']);
649 if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
650 $locationParams = array(
651 'entity_id' => $eventId,
652 'entity_table' => 'civicrm_event',
653 );
654 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
655 $this->assign('location', $location);
656 }
657 }
658
659 // assign payment info here
660 $paymentConfig['confirm_email_text'] = CRM_Utils_Array::value('confirm_email_text', $params);
661 $this->assign('paymentConfig', $paymentConfig);
662 $isRefund = ($this->_paymentType == 'refund') ? TRUE : FALSE;
663 $this->assign('isRefund', $isRefund);
664 if ($isRefund) {
665 $this->assign('totalPaid', $this->_amtPaid);
666 $this->assign('totalAmount', $this->_amtTotal);
667 $this->assign('refundAmount', $params['total_amount']);
668 }
669 else {
670 $balance = $this->_amtTotal - ($this->_amtPaid + $params['total_amount']);
671 $paymentsComplete = ($balance == 0) ? 1 : 0;
672 $this->assign('amountOwed', $balance);
673 $this->assign('totalAmount', $this->_amtTotal);
674 $this->assign('paymentAmount', $params['total_amount']);
675 $this->assign('paymentsComplete', $paymentsComplete);
676 }
677 $this->assign('contactDisplayName', $this->_contributorDisplayName);
678
679 // assign trxn details
680 $this->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $params));
681 $this->assign('receive_date', CRM_Utils_Array::value('trxn_date', $params));
682 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
683 if (array_key_exists('payment_instrument_id', $params)) {
684 $this->assign('paidBy',
685 CRM_Utils_Array::value($params['payment_instrument_id'],
686 $paymentInstrument
687 )
688 );
689 }
690 $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
691
692 $sendTemplateParams = array(
693 'groupName' => 'msg_tpl_workflow_contribution',
694 'valueName' => 'payment_or_refund_notification',
695 'contactId' => $this->_contactId,
696 'PDFFilename' => ts('notification').'.pdf',
697 );
698
699 // try to send emails only if email id is present
700 // and the do-not-email option is not checked for that contact
701 if ($this->_contributorEmail && !$this->_toDoNotEmail) {
702 if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
703 $receiptFrom = $params['from_email_address'];
704 }
705
706 $sendTemplateParams['from'] = $receiptFrom;
707 $sendTemplateParams['toName'] = $this->_contributorDisplayName;
708 $sendTemplateParams['toEmail'] = $this->_contributorEmail;
709 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails);
710 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails);
711 }
712 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
713 return $mailSent;
714 }
715 }