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