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