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