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