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