Minor fixes
[civicrm-core.git] / CRM / Contribute / Form / AdditionalPayment.php
CommitLineData
0f602e3f
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
0f602e3f 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
0f602e3f
PJ
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 | |
74ab7ba8 14 | CiviCRM is distributed in the hope that it will be useful, but |
0f602e3f
PJ
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 */
0f602e3f
PJ
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
0f602e3f
PJ
32 */
33
34/**
07f8d162 35 * This form records additional payments needed when event/contribution is partially paid.
0f602e3f
PJ
36 */
37class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_AbstractEditPayment {
38 public $_contributeMode = 'direct';
39
40 /**
95cdcc0f 41 * Related component whose financial payment is being processed.
0f602e3f
PJ
42 *
43 * @var string
0f602e3f
PJ
44 */
45 protected $_component = NULL;
46
47 /**
100fef9d 48 * Id of the component entity
0f602e3f
PJ
49 */
50 public $_id = NULL;
51
52 protected $_owed = NULL;
53
54 protected $_refund = NULL;
55
0dc4ef42 56 /**
57 * @deprecated - use parent $this->contactID
58 *
59 * @var int
60 */
0f602e3f
PJ
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
bd99f5fe
PJ
73 protected $fromEmailId = NULL;
74
7cdb890a
PJ
75 protected $_fromEmails = NULL;
76
81f3d017
PJ
77 protected $_view = NULL;
78
79 public $_action = NULL;
80
0f602e3f 81 public function preProcess() {
8f262a0b 82
83 parent::preProcess();
0f602e3f 84 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
0dc4ef42 85 // @todo don't set this - rely on parent $this->contactID
0f602e3f
PJ
86 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
87 $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this, TRUE);
81f3d017 88 $this->_view = CRM_Utils_Request::retrieve('view', 'String', $this, FALSE);
81f3d017
PJ
89 $this->assign('component', $this->_component);
90 $this->assign('id', $this->_id);
d4c0653f 91 $this->assign('suppressPaymentFormButtons', $this->isBeingCalledFromSelectorContext());
ad96d702 92
81f3d017
PJ
93 if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
94 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, TRUE);
536b8316
PJ
95 $title = ts('View Payment');
96 if ($this->_component == 'event') {
97 $info = CRM_Event_BAO_Participant::participantDetails($this->_id);
98 $title .= " - {$info['title']}";
99 }
100 CRM_Utils_System::setTitle($title);
81f3d017 101 $this->assign('transaction', TRUE);
9b2e3ee6 102 $this->assign('payments', $paymentInfo['transaction']);
81f3d017
PJ
103 return;
104 }
0f602e3f 105 $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
0f602e3f 106
8f4f19ca 107 $entityType = 'contribution';
0f602e3f 108 if ($this->_component == 'event') {
8f4f19ca 109 $entityType = 'participant';
0f602e3f 110 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'contribution_id', 'participant_id');
685dc433
PN
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 else {
115 $this->_contributionId = $this->_id;
116 $this->_fromEmails['from_email_id'] = CRM_Core_BAO_Email::getFromEmail();
0f602e3f
PJ
117 }
118
8f4f19ca 119 $paymentInfo = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($this->_id, $entityType);
d5397f2f 120 $paymentDetails = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, FALSE, TRUE);
7cdb890a
PJ
121
122 $this->_amtPaid = $paymentDetails['paid'];
123 $this->_amtTotal = $paymentDetails['total'];
0f602e3f
PJ
124
125 if (!empty($paymentInfo['refund_due'])) {
126 $paymentAmt = $this->_refund = $paymentInfo['refund_due'];
127 $this->_paymentType = 'refund';
128 }
129 elseif (!empty($paymentInfo['amount_owed'])) {
130 $paymentAmt = $this->_owed = $paymentInfo['amount_owed'];
131 $this->_paymentType = 'owed';
132 }
133 else {
134 CRM_Core_Error::fatal(ts('No payment information found for this record'));
135 }
136
e8cf3013
PJ
137 if (!empty($this->_mode) && $this->_paymentType == 'refund') {
138 CRM_Core_Error::fatal(ts('Credit card payment is not for Refund payments use'));
139 }
140
141 list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
142
0f602e3f
PJ
143 $this->assign('contributionMode', $this->_mode);
144 $this->assign('contactId', $this->_contactId);
0f602e3f 145 $this->assign('paymentType', $this->_paymentType);
e8cf3013 146 $this->assign('paymentAmt', abs($paymentAmt));
0f602e3f 147
2c13688c 148 $this->setPageTitle($this->_refund ? ts('Refund') : ts('Payment'));
0f602e3f
PJ
149 }
150
d4c0653f 151 /**
152 * Is this function being called from a datatable selector.
153 *
154 * If so we don't want to show the buttons.
155 */
156 protected function isBeingCalledFromSelectorContext() {
157 return CRM_Utils_Request::retrieve('selector', 'Positive');
158 }
159
186c9c17
EM
160 /**
161 * This virtual function is used to set the default values of
162 * various form elements
163 *
164 * access public
165 *
a6c01b45
CW
166 * @return array
167 * reference to the array of default values
186c9c17
EM
168 */
169 /**
170 * @return array
171 */
0f602e3f 172 public function setDefaultValues() {
81f3d017 173 if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
acb1052e 174 return NULL;
81f3d017 175 }
481a74f4 176 $defaults = array();
0f602e3f 177 if ($this->_mode) {
9f89c7a2
PN
178 CRM_Core_Payment_Form::setDefaultValues($this, $this->_contactId);
179 $defaults = array_merge($defaults, $this->_defaults);
0f602e3f
PJ
180 }
181
f7fb5615 182 if (empty($defaults['trxn_date'])) {
183 $defaults['trxn_date'] = date('Y-m-d H:i:s');
efa3d1d1
PJ
184 }
185
820fbcfb
PJ
186 if ($this->_refund) {
187 $defaults['total_amount'] = abs($this->_refund);
188 }
aea78df0
PN
189 elseif ($this->_owed) {
190 $defaults['total_amount'] = number_format($this->_owed, 2);
191 }
820fbcfb 192
0f602e3f 193 // Set $newCredit variable in template to control whether link to credit card mode is included
9be1374d 194 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
efa3d1d1 195 return $defaults;
0f602e3f
PJ
196 }
197
9f89c7a2
PN
198 /**
199 * Build the form object.
200 */
0f602e3f 201 public function buildQuickForm() {
81f3d017
PJ
202 if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
203 $this->addButtons(array(
204 array(
205 'type' => 'cancel',
206 'name' => ts('Done'),
207 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
208 'isDefault' => TRUE,
209 ),
210 )
211 );
212 return;
213 }
254ce393 214
18135422 215 CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE, CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer'));
23cb875c 216 $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors, NULL);
217
0f602e3f
PJ
218 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialTrxn');
219
58438e5b 220 $label = ($this->_refund) ? ts('Refund Amount') : ts('Payment Amount');
0f602e3f 221 $this->addMoney('total_amount',
58438e5b 222 $label,
43c6eb86 223 TRUE,
0f602e3f
PJ
224 $attributes['total_amount'],
225 TRUE, 'currency', NULL
226 );
227
0f602e3f
PJ
228 //add receipt for offline contribution
229 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
230
ad96d702 231 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
0f602e3f 232
7cdb890a
PJ
233 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
234
7bc6b5bb 235 $dateLabel = ($this->_refund) ? ts('Refund Date') : ts('Date Received');
f7fb5615 236 $this->addField('trxn_date', array('entity' => 'FinancialTrxn', 'label' => $dateLabel, 'context' => 'Contribution'), FALSE, FALSE);
0f602e3f
PJ
237
238 if ($this->_contactId && $this->_id) {
239 if ($this->_component == 'event') {
240 $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id');
241 $event = CRM_Event_BAO_Event::getEvents(0, $eventId);
242 $this->assign('eventName', $event[$eventId]);
243 }
244 }
245
246 $this->assign('displayName', $this->_contributorDisplayName);
247 $this->assign('component', $this->_component);
248 $this->assign('email', $this->_contributorEmail);
249
0f602e3f 250 $js = NULL;
23cb875c 251 // render backoffice payment fields only on offline mode
0f602e3f
PJ
252 if (!$this->_mode) {
253 $js = array('onclick' => "return verify( );");
23cb875c 254
255 $this->add('select', 'payment_instrument_id',
256 ts('Payment Method'),
257 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(),
1f24c7f5 258 TRUE,
23cb875c 259 array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
260 );
261
262 $this->add('text', 'check_number', ts('Check Number'), $attributes['financial_trxn_check_number']);
263 $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') + $attributes['trxn_id']);
264
265 $this->add('text', 'fee_amount', ts('Fee Amount'),
266 $attributes['fee_amount']
267 );
268 $this->addRule('fee_amount', ts('Please enter a valid monetary value for Fee Amount.'), 'money');
269
270 $this->add('text', 'net_amount', ts('Net Amount'),
271 $attributes['net_amount']
272 );
273 $this->addRule('net_amount', ts('Please enter a valid monetary value for Net Amount.'), 'money');
0f602e3f
PJ
274 }
275
276 $buttonName = $this->_refund ? 'Record Refund' : 'Record Payment';
277 $this->addButtons(array(
278 array(
279 'type' => 'upload',
280 'name' => ts('%1', array(1 => $buttonName)),
281 'js' => $js,
21dfd5f5 282 'isDefault' => TRUE,
0f602e3f
PJ
283 ),
284 array(
285 'type' => 'cancel',
21dfd5f5 286 'name' => ts('Cancel'),
0f602e3f
PJ
287 ),
288 )
289 );
aaffa79f 290 $mailingInfo = Civi::settings()->get('mailing_backend');
0f602e3f
PJ
291 $this->assign('outBound_option', $mailingInfo['outBound_option']);
292
293 $this->addFormRule(array('CRM_Contribute_Form_AdditionalPayment', 'formRule'), $this);
294 }
295
186c9c17
EM
296 /**
297 * @param $fields
298 * @param $files
299 * @param $self
300 *
301 * @return array
302 */
00be9182 303 public static function formRule($fields, $files, $self) {
0f602e3f
PJ
304 $errors = array();
305 if ($self->_paymentType == 'owed' && $fields['total_amount'] > $self->_owed) {
306 $errors['total_amount'] = ts('Payment amount cannot be greater than owed amount');
307 }
e8cf3013 308 if ($self->_paymentType == 'refund' && $fields['total_amount'] != abs($self->_refund)) {
0f2c3c03 309 $errors['total_amount'] = ts('Refund amount must equal refund due amount.');
0f602e3f 310 }
922becfb 311 $netAmt = $fields['total_amount'] - CRM_Utils_Array::value('fee_amount', $fields, 0);
0f602e3f 312 if (!empty($fields['net_amount']) && $netAmt != $fields['net_amount']) {
0f2c3c03 313 $errors['net_amount'] = ts('Net amount should be equal to the difference between payment amount and fee amount.');
0f602e3f 314 }
43c6eb86 315 if ($self->_paymentProcessor['id'] === 0 && empty($fields['payment_instrument_id'])) {
316 $errors['payment_instrument_id'] = ts('Payment method is a required field');
317 }
318
0f602e3f
PJ
319 return $errors;
320 }
321
922becfb
PN
322 /**
323 * Process the form submission.
324 */
0f602e3f 325 public function postProcess() {
922becfb
PN
326 $submittedValues = $this->controller->exportValues($this->_name);
327 $this->submit($submittedValues);
685dc433 328 $childTab = 'contribute';
0f602e3f 329 if ($this->_component == 'event') {
685dc433 330 $childTab = 'participant';
0f602e3f 331 }
922becfb
PN
332 $session = CRM_Core_Session::singleton();
333 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
334 "reset=1&cid={$this->_contactId}&selectedChild={$childTab}"
335 ));
336 }
337
338 /**
339 * Process Payments.
340 * @param array $submittedValues
341 *
342 */
343 public function submit($submittedValues) {
80c9b98c
PN
344 $this->_params = $submittedValues;
345 $this->beginPostProcess();
23cb875c 346 $this->_contributorContactID = $this->_contactID;
347 $this->processBillingAddress();
922becfb
PN
348 $participantId = NULL;
349 if ($this->_component == 'event') {
350 $participantId = $this->_id;
351 }
685dc433
PN
352 $contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution',
353 'contribution_status_id',
354 array('labelColumn' => 'name')
355 );
356 $contributionStatusID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionId, 'contribution_status_id');
357 if ($contributionStatuses[$contributionStatusID] == 'Pending') {
2848105c
PN
358 civicrm_api3('Contribution', 'create',
359 array(
360 'id' => $this->_contributionId,
361 'contribution_status_id' => array_search('Partially paid', $contributionStatuses),
362 'is_pay_later' => 0,
363 )
364 );
685dc433 365 }
f7fb5615 366
0f602e3f
PJ
367 if ($this->_mode) {
368 // process credit card
820fbcfb 369 $this->assign('contributeMode', 'direct');
23cb875c 370 $this->processCreditCard();
0f602e3f 371 }
aac57c29 372
922becfb
PN
373 $defaults = array();
374 $contribution = civicrm_api3('Contribution', 'getsingle', array(
375 'return' => array("contribution_status_id"),
376 'id' => $this->_contributionId,
377 ));
378 $contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $contribution);
a15d98d8 379 $result = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $this->_params, $this->_paymentType, $participantId);
922becfb
PN
380 // Fetch the contribution & do proportional line item assignment
381 $params = array('id' => $this->_contributionId);
382 $contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params);
383 CRM_Contribute_BAO_Contribution::addPayments(array($contribution), $contributionStatusId);
94c8aed1 384 if ($this->_contributionId && CRM_Core_Permission::access('CiviMember')) {
5a53dd1f
E
385 $membershipPaymentCount = civicrm_api3('MembershipPayment', 'getCount', array('contribution_id' => $this->_contributionId));
386 if ($membershipPaymentCount) {
94c8aed1
E
387 $this->ajaxResponse['updateTabs']['#tab_member'] = CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactID);
388 }
389 }
5a53dd1f
E
390 if ($this->_contributionId && CRM_Core_Permission::access('CiviEvent')) {
391 $participantPaymentCount = civicrm_api3('ParticipantPayment', 'getCount', array('contribution_id' => $this->_contributionId));
392 if ($participantPaymentCount) {
393 $this->ajaxResponse['updateTabs']['#tab_participant'] = CRM_Contact_BAO_Contact::getCountComponent('participant', $this->_contactID);
394 }
395 }
922becfb
PN
396
397 $statusMsg = ts('The payment record has been processed.');
398 // send email
a15d98d8 399 if (!empty($result) && !empty($this->_params['is_email_receipt'])) {
400 $this->_params['contact_id'] = $this->_contactId;
401 $this->_params['contribution_id'] = $this->_contributionId;
922becfb 402 // to get 'from email id' for send receipt
a15d98d8 403 $this->fromEmailId = $this->_params['from_email_address'];
404 $sendReceipt = $this->emailReceipt($this->_params);
922becfb 405 if ($sendReceipt) {
aac57c29
PJ
406 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
407 }
0f602e3f 408 }
922becfb
PN
409
410 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
0f602e3f 411 }
bd99f5fe 412
23cb875c 413 public function processCreditCard() {
b4989753
PJ
414 $config = CRM_Core_Config::singleton();
415 $session = CRM_Core_Session::singleton();
416
b4989753
PJ
417 $now = date('YmdHis');
418 $fields = array();
419
420 // we need to retrieve email address
23cb875c 421 if ($this->_context == 'standalone' && !empty($this->_params['is_email_receipt'])) {
b4989753
PJ
422 list($this->userDisplayName,
423 $this->userEmail
353ffa53 424 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
b4989753
PJ
425 $this->assign('displayName', $this->userDisplayName);
426 }
427
23cb875c 428 $this->formatParamsForPaymentProcessor($this->_params);
b4989753 429
b4989753 430 $this->_params['amount'] = $this->_params['total_amount'];
c039f658 431 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
432 // function to get correct amount level consistently. Remove setting of the amount level in
433 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
434 // to cover all variants.
b4989753
PJ
435 $this->_params['amount_level'] = 0;
436 $this->_params['currencyID'] = CRM_Utils_Array::value('currency',
437 $this->_params,
438 $config->defaultCurrency
439 );
aefd7f6b 440
a7488080 441 if (!empty($this->_params['trxn_date'])) {
f7fb5615 442 $this->_params['receive_date'] = $this->_params['trxn_date'];
b4989753
PJ
443 }
444
23cb875c 445 if (empty($this->_params['receive_date'])) {
446 $this->_params['receive_date'] = date('YmdHis');
447 }
448
b4989753
PJ
449 if (empty($this->_params['invoice_id'])) {
450 $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
451 }
452 else {
453 $this->_params['invoiceID'] = $this->_params['invoice_id'];
454 }
455
0b50eca0 456 $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters(
23cb875c 457 $this->_params,
0b50eca0 458 $this->_bltID
459 ));
460
b4989753 461 //Add common data to formatted params
23cb875c 462 $params = $this->_params;
b4989753
PJ
463 CRM_Contribute_Form_AdditionalInfo::postProcessCommon($params, $this->_params, $this);
464 // at this point we've created a contact and stored its address etc
465 // all the payment processors expect the name and address to be in the
466 // so we copy stuff over to first_name etc.
467 $paymentParams = $this->_params;
468 $paymentParams['contactID'] = $this->_contactId;
469 CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE);
470
b4989753 471 $paymentParams['contributionPageID'] = NULL;
a7488080 472 if (!empty($this->_params['is_email_receipt'])) {
b4989753 473 $paymentParams['email'] = $this->_contributorEmail;
23cb875c 474 $paymentParams['is_email_receipt'] = TRUE;
b4989753
PJ
475 }
476 else {
23cb875c 477 $paymentParams['is_email_receipt'] = $this->_params['is_email_receipt'] = FALSE;
b4989753
PJ
478 }
479
480 $result = NULL;
481
482 if ($paymentParams['amount'] > 0.0) {
248141b9
EM
483 try {
484 // force a reget of the payment processor in case the form changed it, CRM-7179
485 $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
486 $result = $payment->doPayment($paymentParams);
487 }
488 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
23cb875c 489 Civi::log()->error('Payment processor exception: ' . $e->getMessage());
490 $urlParams = "action=add&cid={$this->_contactId}&id={$this->_contributionId}&component={$this->_component}&mode={$this->_mode}";
491 CRM_Core_Error::statusBounce(CRM_Utils_System::url($e->getMessage(), 'civicrm/payment/add', $urlParams));
b4989753 492 }
b4989753
PJ
493 }
494
23cb875c 495 if (!empty($result)) {
b4989753
PJ
496 $this->_params = array_merge($this->_params, $result);
497 }
498
820fbcfb
PJ
499 if (empty($this->_params['receive_date'])) {
500 $this->_params['receive_date'] = $now;
501 }
b4989753
PJ
502
503 $this->set('params', $this->_params);
b4989753
PJ
504
505 // set source if not set
506 if (empty($this->_params['source'])) {
507 $userID = $session->get('userID');
508 $userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID,
509 'sort_name'
510 );
511 $this->_params['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
512 }
b4989753
PJ
513 }
514
186c9c17 515 /**
61bfc595
PN
516 * Function to send email receipt.
517 *
c490a46a 518 * @param array $params
186c9c17 519 *
61bfc595 520 * @return bool
186c9c17 521 */
00be9182 522 public function emailReceipt(&$params) {
bd99f5fe 523 // email receipt sending
7cdb890a
PJ
524 // send message template
525 if ($this->_component == 'event') {
7cdb890a 526
23cb875c 527 // fetch event information from participant ID using API
528 $eventId = civicrm_api3('Participant', 'getvalue', array(
529 'return' => "event_id",
530 'id' => $this->_id,
531 ));
532 $event = civicrm_api3('Event', 'getsingle', array('id' => $eventId));
7cdb890a 533
820fbcfb 534 $this->assign('event', $event);
7cdb890a
PJ
535 $this->assign('isShowLocation', $event['is_show_location']);
536 if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
537 $locationParams = array(
538 'entity_id' => $eventId,
539 'entity_table' => 'civicrm_event',
540 );
541 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
542 $this->assign('location', $location);
543 }
544 }
545
546 // assign payment info here
547 $paymentConfig['confirm_email_text'] = CRM_Utils_Array::value('confirm_email_text', $params);
548 $this->assign('paymentConfig', $paymentConfig);
23cb875c 549
550 $this->assign('totalAmount', $this->_amtTotal);
551
7cdb890a
PJ
552 $isRefund = ($this->_paymentType == 'refund') ? TRUE : FALSE;
553 $this->assign('isRefund', $isRefund);
554 if ($isRefund) {
555 $this->assign('totalPaid', $this->_amtPaid);
7cdb890a
PJ
556 $this->assign('refundAmount', $params['total_amount']);
557 }
558 else {
559 $balance = $this->_amtTotal - ($this->_amtPaid + $params['total_amount']);
820fbcfb 560 $paymentsComplete = ($balance == 0) ? 1 : 0;
7cdb890a 561 $this->assign('amountOwed', $balance);
7cdb890a 562 $this->assign('paymentAmount', $params['total_amount']);
820fbcfb 563 $this->assign('paymentsComplete', $paymentsComplete);
7cdb890a
PJ
564 }
565 $this->assign('contactDisplayName', $this->_contributorDisplayName);
566
567 // assign trxn details
568 $this->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $params));
820fbcfb 569 $this->assign('receive_date', CRM_Utils_Array::value('trxn_date', $params));
23cb875c 570 $this->assign('paidBy', CRM_Core_PseudoConstant::getLabel(
e3d511f5 571 'CRM_Contribute_BAO_Contribution',
23cb875c 572 'payment_instrument_id',
573 $params['payment_instrument_id']
574 ));
7cdb890a
PJ
575 $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
576
577 $sendTemplateParams = array(
578 'groupName' => 'msg_tpl_workflow_contribution',
579 'valueName' => 'payment_or_refund_notification',
580 'contactId' => $this->_contactId,
92fcb95f 581 'PDFFilename' => ts('notification') . '.pdf',
7cdb890a
PJ
582 );
583
584 // try to send emails only if email id is present
585 // and the do-not-email option is not checked for that contact
586 if ($this->_contributorEmail && !$this->_toDoNotEmail) {
587 if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
588 $receiptFrom = $params['from_email_address'];
589 }
590
591 $sendTemplateParams['from'] = $receiptFrom;
592 $sendTemplateParams['toName'] = $this->_contributorDisplayName;
593 $sendTemplateParams['toEmail'] = $this->_contributorEmail;
7cdb890a
PJ
594 }
595 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
ad96d702 596 return $mailSent;
bd99f5fe 597 }
96025800 598
61bfc595
PN
599 /**
600 * Wrapper for unit testing the post process submit function.
601 *
61bfc595
PN
602 * @param array $params
603 * @param string|null $creditCardMode
9fbf312f 604 * @param string $entityType
61bfc595
PN
605 *
606 * @throws \CiviCRM_API3_Exception
607 */
9fbf312f 608 public function testSubmit($params, $creditCardMode = NULL, $entityType = 'contribute') {
61bfc595 609 $this->_bltID = 5;
80c9b98c
PN
610 // Required because processCreditCard calls set method on this.
611 $_SERVER['REQUEST_METHOD'] = 'GET';
612 $this->controller = new CRM_Core_Controller();
613
614 $this->assignPaymentRelatedVariables();
9fbf312f 615
61bfc595
PN
616 if (!empty($params['contribution_id'])) {
617 $this->_contributionId = $params['contribution_id'];
618
9fbf312f 619 $paymentInfo = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($this->_contributionId, $entityType);
620 $paymentDetails = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_contributionId, $entityType, FALSE, TRUE);
61bfc595
PN
621
622 $this->_amtPaid = $paymentDetails['paid'];
623 $this->_amtTotal = $paymentDetails['total'];
624
625 if (!empty($paymentInfo['refund_due'])) {
626 $this->_refund = $paymentInfo['refund_due'];
627 $this->_paymentType = 'refund';
628 }
629 elseif (!empty($paymentInfo['amount_owed'])) {
630 $this->_owed = $paymentInfo['amount_owed'];
631 $this->_paymentType = 'owed';
632 }
633 }
634
635 if (!empty($params['contact_id'])) {
636 $this->_contactId = $params['contact_id'];
637 }
638
639 if ($creditCardMode) {
640 $this->_mode = $creditCardMode;
641 }
642
61bfc595 643 $this->_fields = array();
23cb875c 644 $this->set('cid', $this->_contactId);
645 parent::preProcess();
61bfc595
PN
646 $this->submit($params);
647 }
648
d5397f2f 649}