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