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