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