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