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