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