Duplicate code cleanup
[civicrm-core.git] / CRM / Event / Cart / Form / Checkout / Payment.php
CommitLineData
6a488035
TO
1<?php
2class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
3 public $all_participants;
4 public $financial_type_id;
5 public $description;
6 public $line_items;
7 public $_fields = array();
8 public $_paymentProcessor;
9 public $total;
10 public $sub_total;
11 public $payment_required = TRUE;
12 public $payer_contact_id;
13 public $is_pay_later = FALSE;
14 public $pay_later_receipt;
15
16 function registerParticipant($params, &$participant, $event) {
17 $transaction = new CRM_Core_Transaction();
18
19 // handle register date CRM-4320
20 $registerDate = date('YmdHis');
21 $participantParams = array(
22 'id' => $participant->id,
23 'event_id' => $event->id,
24 'register_date' => $registerDate,
25 'source' => CRM_Utils_Array::value('participant_source', $params, $this->description),
26 //'fee_level' => $participant->fee_level,
27 'is_pay_later' => $this->is_pay_later,
28 'fee_amount' => CRM_Utils_Array::value('amount', $params, 0),
29 //XXX why is this a ref to participant and not contact?:
30 //'registered_by_id' => $this->payer_contact_id,
31 'fee_currency' => CRM_Utils_Array::value('currencyID', $params),
32 );
33
34 if ($participant->must_wait) {
35 $participant_status = 'On waitlist';
36 }
37 elseif (CRM_Utils_Array::value('is_pay_later', $params, FALSE)) {
38 $participant_status = 'Pending from pay later';
39 }
40 else {
41 $participant_status = 'Registered';
42 }
43 $participant_statuses = CRM_Event_PseudoConstant::participantStatus();
44 $participantParams['status_id'] = array_search($participant_status, $participant_statuses);
45 $participant_status_label = CRM_Utils_Array::value($participantParams['status_id'], CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'));
46 $participantParams['participant_status'] = $participant_status_label;
47
48 $this->assign('isOnWaitlist', $participant->must_wait);
49
50 if ($this->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
51 $participantParams['is_test'] = 1;
52 }
53 else {
54 $participantParams['is_test'] = 0;
55 }
56
57 if (self::is_administrator()) {
58 if (CRM_Utils_Array::value('note', $params)) {
59 $note_params = array(
60 'participant_id' => $participant->id,
61 'contact_id' => self::getContactID(),
62 'note' => $params['note'],
63 );
64 CRM_Event_BAO_Participant::update_note($note_params);
65 }
66 }
67
68 $participant->copyValues($participantParams);
69 $participant->save();
70
71 if (CRM_Utils_Array::value('contributionID', $params)) {
72 $payment_params = array(
73 'participant_id' => $participant->id,
74 'contribution_id' => $params['contributionID'],
75 );
76 $ids = array();
77 $paymentParticpant = CRM_Event_BAO_ParticipantPayment::create($payment_params, $ids);
78 }
79
80 $transaction->commit();
81
82 $event_values = array();
83 CRM_Core_DAO::storeValues($event, $event_values);
84
85 $location = array();
86 if (CRM_Utils_Array::value('is_show_location', $event_values) == 1) {
87 $locationParams = array(
88 'entity_id' => $participant->event_id,
89 'entity_table' => 'civicrm_event',
90 );
91 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
92 CRM_Core_BAO_Address::fixAddress($location['address'][1]);
93 }
94
95 list($pre_id, $post_id) = CRM_Event_Cart_Form_MerParticipant::get_profile_groups($participant->event_id);
96 $payer_values = array(
97 'email' => '',
98 'name' => '',
99 );
100 if ($this->payer_contact_id) {
101 $payer_contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id);
102 $payer_values = array(
103 'email' => $payer_contact_details[1],
104 'name' => $payer_contact_details[0],
105 );
106 }
107 $values = array(
108 'params' => array($participant->id => $participantParams),
109 'event' => $event_values,
110 'location' => $location,
111 'custom_pre_id' => $pre_id,
112 'custom_post_id' => $post_id,
113 'payer' => $payer_values,
114 );
115 CRM_Event_BAO_Event::sendMail($participant->contact_id, $values, $participant->id);
116
117 return $participant;
118 }
119
120 function buildPaymentFields() {
121 $payment_processor_id = NULL;
122 $can_pay_later = TRUE;
123 $pay_later_text = "";
124 $this->pay_later_receipt = "";
125 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
126 if ($payment_processor_id == NULL && $event_in_cart->event->payment_processor != NULL) {
127 $payment_processor_id = $event_in_cart->event->payment_processor;
128 $this->financial_type_id = $event_in_cart->event->financial_type_id;
129 }
130 else {
131 if ($event_in_cart->event->payment_processor != NULL && $event_in_cart->event->payment_processor != $payment_processor_id) {
132 CRM_Core_Error::statusBounce(ts('When registering for multiple events all events must use the same payment processor. '));
133 }
134 }
135 if (!$event_in_cart->event->is_pay_later) {
136 $can_pay_later = FALSE;
137 }
138 else {
139 //XXX
140 $pay_later_text = $event_in_cart->event->pay_later_text;
141 $this->pay_later_receipt = $event_in_cart->event->pay_later_receipt;
142 }
143 }
144
145 if ($payment_processor_id == NULL) {
146 CRM_Core_Error::statusBounce(ts('A payment processor must be selected for this event registration page, or the event must be configured to give users the option to pay later (contact the site administrator for assistance).'));
147 }
148
149 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($payment_processor_id, $this->_mode);
150 $this->assign('paymentProcessor', $this->_paymentProcessor);
151
152 CRM_Core_Payment_Form::setCreditCardFields($this);
153 CRM_Core_Payment_Form::buildCreditCard($this);
154
155 if ($can_pay_later || self::is_administrator()) {
156 $this->addElement('checkbox', 'is_pay_later',
157 $pay_later_text
158 );
159 $this->addElement('checkbox', 'payment_completed',
160 ts('Payment Completed')
161 );
162 $this->assign('pay_later_instructions', $this->pay_later_receipt);
163 }
164 }
165
166 function buildQuickForm() {
167
168 $this->line_items = array();
169 $this->sub_total = 0;
170 $this->_price_values = $this->getValuesForPage('ParticipantsAndPrices');
171
172 // iterate over each event in cart
173 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
174 $this->process_event_line_item($event_in_cart);
175 foreach ($this->cart->get_events_in_carts_by_main_event_id($event_in_cart->event_id) as $subevent) {
176 $this->process_event_line_item($subevent, 'subevent');
177 }
178 }
179
180 $this->total = $this->sub_total;
181 $this->payment_required = ($this->total > 0);
182 $this->assign('payment_required', $this->payment_required);
183 $this->assign('line_items', $this->line_items);
184 $this->assign('sub_total', $this->sub_total);
185 $this->assign('total', $this->total);
186 $buttons = array();
187 $buttons[] = array(
188 'name' => ts('<< Go Back'),
189 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp',
190 'type' => 'back',
191 );
192 $buttons[] = array(
193 'isDefault' => TRUE,
194 'name' => ts('Complete Transaction >>'),
195 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
196 'type' => 'next',
197 );
198
199 if ($this->total) {
200 $this->add('text', 'billing_contact_email', 'Billing Email', '', TRUE);
201 $this->assign('collect_billing_email', TRUE);
202 }
203 if (self::is_administrator()) {
204 $this->add('textarea', 'note', 'Note');
205 $this->add('text', 'source', 'Source', array('size' => 80));
206 $instruments = array();
207 CRM_Core_OptionGroup::getAssoc('payment_instrument', $instruments, TRUE);
208 $options = array();
209 foreach ($instruments as $type) {
210 $options[] = $this->createElement('radio', NULL, '', $type['label'], $type['value']);
211 }
212 $this->addGroup($options, 'payment_type', ts("Alternative Payment Type"));
213 $this->add('text', 'check_number', ts('Check No.'), array('size' => 20));
214 $this->addElement('checkbox', 'is_pending', ts('Create a pending registration'));
215
216 $this->assign('administrator', TRUE);
217 }
218 $this->addButtons($buttons);
219
220 $this->addFormRule(array('CRM_Event_Cart_Form_Checkout_Payment', 'formRule'), $this);
221
222 if ($this->payment_required) {
223 $this->buildPaymentFields();
224 }
225 }
226
227 function process_event_line_item(&$event_in_cart, $class = NULL) {
228 $cost = 0;
229 $price_set_id = CRM_Price_BAO_Set::getFor("civicrm_event", $event_in_cart->event_id);
230 $amount_level = NULL;
231 if ($price_set_id) {
232 $event_price_values = array();
233 foreach ($this->_price_values as $key => $value) {
234 if (preg_match("/event_{$event_in_cart->event_id}_(price.*)/", $key, $matches)) {
235 $event_price_values[$matches[1]] = $value;
236 }
237 }
238 $price_sets = CRM_Price_BAO_Set::getSetDetail($price_set_id, TRUE);
239 $price_set = $price_sets[$price_set_id];
240 $price_set_amount = array();
241 CRM_Price_BAO_Set::processAmount($price_set['fields'], $event_price_values, $price_set_amount);
242 $cost = $event_price_values['amount'];
243 $amount_level = $event_price_values['amount_level'];
244 }
245
246 // iterate over each participant in event
247 foreach ($event_in_cart->participants as & $participant) {
248 $participant->cost = $cost;
249 $participant->fee_level = $amount_level;
250 }
251
252 $this->add_line_item($event_in_cart, $class);
253 }
254
255 function add_line_item($event_in_cart, $class = NULL) {
256 $amount = 0;
257 $cost = 0;
258 $not_waiting_participants = array();
259 foreach ($event_in_cart->not_waiting_participants() as $participant) {
260 $amount += $participant->cost;
261 $cost = max($cost, $participant->cost);
262 $not_waiting_participants[] = array(
263 'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
264 );
265 }
266 $waiting_participants = array();
267 foreach ($event_in_cart->waiting_participants() as $participant) {
268 $waiting_participants[] = array(
269 'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
270 );
271 }
272 $this->line_items[] = array(
273 'amount' => $amount,
274 'cost' => $cost,
275 'event' => $event_in_cart->event,
276 'participants' => $not_waiting_participants,
277 'num_participants' => count($not_waiting_participants),
278 'num_waiting_participants' => count($waiting_participants),
279 'waiting_participants' => $waiting_participants,
280 'class' => $class,
281 );
282
283 $this->sub_total += $amount;
284 }
285
286 function getDefaultFrom() {
287 $values = CRM_Core_OptionGroup::values('from_email_address');
288 return $values[1];
289 }
290
291 function emailReceipt($events_in_cart, $params) {
292 $contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id);
293 $state_province = new CRM_Core_DAO_StateProvince();
294 $state_province->id = $params["billing_state_province_id-{$this->_bltID}"];
295 $state_province->find();
296 $state_province->fetch();
297 $country = new CRM_Core_DAO_Country();
298 $country->id = $params["billing_country_id-{$this->_bltID}"];
299 $country->find();
300 $country->fetch();
301 foreach ($this->line_items as & $line_item) {
302 $location_params = array('entity_id' => $line_item['event']->id, 'entity_table' => 'civicrm_event');
303 $line_item['location'] = CRM_Core_BAO_Location::getValues($location_params, TRUE);
304 CRM_Core_BAO_Address::fixAddress($line_item['location']['address'][1]);
305 }
306 $send_template_params = array(
307 'table' => 'civicrm_msg_template',
308 'contactId' => $this->payer_contact_id,
309 'from' => $this->getDefaultFrom(),
310 'groupName' => 'msg_tpl_workflow_event',
311 'isTest' => FALSE,
312 'toEmail' => $contact_details[1],
313 'toName' => $contact_details[0],
314 'tplParams' => array
315 (
316 'billing_name' => "{$params['billing_first_name']} {$params['billing_last_name']}",
317 'billing_city' => $params["billing_city-{$this->_bltID}"],
318 'billing_country' => $country->name,
319 'billing_postal_code' => $params["billing_postal_code-{$this->_bltID}"],
320 'billing_state' => $state_province->abbreviation,
321 'billing_street_address' => $params["billing_street_address-{$this->_bltID}"],
322 'credit_card_exp_date' => $params['credit_card_exp_date'],
323 'credit_card_type' => $params['credit_card_type'],
324 'credit_card_number' => "************" . substr($params['credit_card_number'], -4, 4),
325 // XXX cart->get_discounts
326 'discounts' => $this->discounts,
327 'email' => $contact_details[1],
328 'events_in_cart' => $events_in_cart,
329 'line_items' => $this->line_items,
330 'name' => $contact_details[0],
331 'transaction_id' => $params['trxn_id'],
332 'transaction_date' => $params['trxn_date'],
333 'is_pay_later' => $this->is_pay_later,
334 'pay_later_receipt' => $this->pay_later_receipt,
335 ),
336 'valueName' => 'event_registration_receipt',
337 'PDFFilename' => 'eventReceipt.pdf',
338 );
339 $template_params_to_copy = array(
340 'billing_name',
341 'billing_city',
342 'billing_country',
343 'billing_postal_code',
344 'billing_state',
345 'billing_street_address',
346 'credit_card_exp_date',
347 'credit_card_type',
348 'credit_card_number',
349 );
350 foreach ($template_params_to_copy as $template_param_to_copy) {
351 $this->set($template_param_to_copy, $send_template_params['tplParams'][$template_param_to_copy]);
352 }
353
354 CRM_Core_BAO_MessageTemplates::sendTemplate($send_template_params);
355 }
356
357 static function formRule($fields, $files, $self) {
358 $errors = array();
359
360 if ($self->payment_required && !CRM_Utils_Array::value('is_pay_later', $self->_submitValues)) {
361 $payment = &CRM_Core_Payment::singleton($self->_mode, $self->_paymentProcessor, $this);
362 $error = $payment->checkConfig($self->_mode);
363 if ($error) {
364 $errors['_qf_default'] = $error;
365 }
7cb3d4f0 366 CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
6a488035 367
7cb3d4f0
CW
368 // make sure that credit card number and cvv are valid
369 CRM_Core_Payment_Form::validateCreditCard($fields, $errors);
6a488035
TO
370 }
371
372 return empty($errors) ? TRUE : $errors;
373 }
374
375 function validate() {
376 if ($this->is_pay_later) {
377 $this->_fields['credit_card_number']['is_required'] = FALSE;
378 $this->_fields['cvv2']['is_required'] = FALSE;
379 $this->_fields['credit_card_exp_date']['is_required'] = FALSE;
380 $this->_fields['credit_card_type']['is_required'] = FALSE;
381 }
382 return parent::validate();
383 }
384
385 function preProcess() {
386 $params = $this->_submitValues;
387 $this->is_pay_later = CRM_Utils_Array::value('is_pay_later', $params, FALSE) && !CRM_Utils_Array::value('payment_completed', $params);
388
389 parent::preProcess();
390 }
391
392 function postProcess() {
393
394 $transaction = new CRM_Core_Transaction();
395 $trxn = NULL;
396 $params = $this->_submitValues;
397
398 $main_participants = $this->cart->get_main_event_participants();
399 foreach ($main_participants as $participant) {
400 $defaults = array();
401 $ids = array('contact_id' => $participant->contact_id);
402 $contact = CRM_Contact_BAO_Contact::retrieve($ids, $defaults);
403 $contact->is_deleted = 0;
404 $contact->save();
405 }
406
407 $trxn_prefix = 'VR';
408 if (array_key_exists('billing_contact_email', $params)) {
409 $this->payer_contact_id = self::find_or_create_contact($this->getContactID(), array(
410 'email' => $params['billing_contact_email'],
411 'first_name' => $params['billing_first_name'],
412 'last_name' => $params['billing_last_name'],
413 'is_deleted' => FALSE,
414 ));
415
416 $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
417 $this->payer_contact_id,
418 'contact_type'
419 );
420 $billing_fields = array(
421 "billing_first_name" => 1,
422 "billing_middle_name" => 1,
423 "billing_last_name" => 1,
424 "billing_street_address-{$this->_bltID}" => 1,
425 "billing_city-{$this->_bltID}" => 1,
426 "billing_state_province_id-{$this->_bltID}" => 1,
427 "billing_postal_code-{$this->_bltID}" => 1,
428 "billing_country_id-{$this->_bltID}" => 1,
429 "address_name-{$this->_bltID}" => 1,
430 "email-{$this->_bltID}" => 1,
431 );
432
433 $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);
434
435 $params["email-{$this->_bltID}"] = $params['billing_contact_email'];
436 CRM_Contact_BAO_Contact::createProfileContact(
437 $params,
438 $billing_fields,
439 $this->payer_contact_id,
440 NULL,
441 NULL,
442 $ctype,
443 TRUE
444 );
445 }
446
447 $params['now'] = date('YmdHis');
448 $params['invoiceID'] = md5(uniqid(rand(), TRUE));
449 $params['amount'] = $this->total;
450 $params['financial_type_id'] = $this->financial_type_id;
451 if ($this->payment_required && !CRM_Utils_Array::value('is_pay_later', $params)) {
452 $trxn = $this->make_payment($params);
453 $params['trxn_id'] = $trxn->trxn_id;
454 $params['trxn_date'] = $trxn->trxn_date;
455 $params['currencyID'] = $trxn->currency;
456 $params['financial_trxn_id'] = $trxn->id;
457 }
458 $this->cart->completed = TRUE;
459 $this->cart->save();
460 $this->set('last_event_cart_id', $this->cart->id);
461
462 $contribution_statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
463 $params['payment_instrument_id'] = NULL;
464 if (CRM_Utils_Array::value('is_pay_later', $params)) {
465 $params['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', 'Check', 'name');
466 $trxn_prefix = 'CK';
467 }
468 else {
469 $params['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', 'Credit Card', 'name');
470 }
471 if ($this->is_pay_later && !CRM_Utils_Array::value('payment_completed', $params)) {
472 $params['contribution_status_id'] = array_search('Pending', $contribution_statuses);
473 }
474 else {
475 $params['contribution_status_id'] = array_search('Completed', $contribution_statuses);
476 $params['participant_status'] = 'Registered';
477 $params['is_pay_later'] = 0;
478 }
479 if ($trxn == NULL) {
480 $params['trxn_id'] = $trxn_prefix . strftime("%Y%m%d%H%M%S");
481 $params['trxn_date'] = $params['now'];
482 }
483
484 if ($this->payment_required) {
485 $this->emailReceipt($this->cart->events_in_carts, $params);
486 }
487
488 // n.b. we need to process the subparticipants before main event
489 // participants so that session attendance can be included in the email
490 $main_participants = $this->cart->get_main_event_participants();
491 $this->all_participants = array();
492 foreach ($main_participants as $main_participant) {
493 $this->all_participants = array_merge($this->all_participants, $this->cart->get_subparticipants($main_participant));
494 }
495 $this->all_participants = array_merge($this->all_participants, $main_participants);
496
497 $this->sub_trxn_index = 0;
498 foreach ($this->all_participants as $mer_participant) {
499 $event_in_cart = $this->cart->get_event_in_cart_by_event_id($mer_participant->event_id);
500
501 $this->sub_trxn_index += 1;
502
503 unset($params['contributionID']);
504 if ($mer_participant->must_wait) {
505 $this->registerParticipant($params, $mer_participant, $event_in_cart->event);
506 }
507 else {
508 // XXX move
509 $params['amount'] = $mer_participant->cost - $mer_participant->discount_amount;
510
511 if ($event_in_cart->event->financial_type_id && $mer_participant->cost) {
512 $params['financial_type_id'] = $event_in_cart->event->financial_type_id;
513 $params['participant_contact_id'] = $mer_participant->contact_id;
514 $this->record_contribution($mer_participant, $params, $event_in_cart->event);
515 }
516 $this->registerParticipant($params, $mer_participant, $event_in_cart->event);
517 }
518 }
519 $this->trxn_id = $params['trxn_id'];
520 $this->trxn_date = $params['trxn_date'];
521 $this->saveDataToSession();
522 $transaction->commit();
523 }
524
525 function make_payment(&$params) {
526 $config = CRM_Core_Config::singleton();
527 if (isset($params["billing_state_province_id-{$this->_bltID}"]) && $params["billing_state_province_id-{$this->_bltID}"]) {
528 $params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params["billing_state_province_id-{$this->_bltID}"]);
529 }
530
531 if (isset($params["billing_country_id-{$this->_bltID}"]) && $params["billing_country_id-{$this->_bltID}"]) {
532 $params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($params["billing_country_id-{$this->_bltID}"]);
533 }
534 $params['ip_address'] = CRM_Utils_System::ipAddress();
535 $params['currencyID'] = $config->defaultCurrency;
536 $params['payment_action'] = 'Sale';
537
538 $payment = &CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
539 CRM_Core_Payment_Form::mapParams($this->_bltID, $params, $params, TRUE);
540 $params['month'] = $params['credit_card_exp_date']['M'];
541 $params['year'] = $params['credit_card_exp_date']['Y'];
542 $result = &$payment->doDirectPayment($params);
543 if (is_a($result, 'CRM_Core_Error')) {
544 CRM_Core_Error::displaySessionError($result);
545 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/cart_checkout', "_qf_Payment_display=1&qfKey={$this->controller->_key}", TRUE, NULL, FALSE));
546 return;
547 }
548 elseif (!$result['trxn_id']) {
549 CRM_Core_Error::fatal(ts("Financial institution didn't return a transaction id."));
550 }
551
552 $trxnParams = array(
553 'trxn_date' => $params['now'],
554 'trxn_type' => 'Debit',
555 'total_amount' => $params['amount'],
556 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result),
557 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']),
558 'currency' => CRM_Utils_Array::value('currencyID', $params),
559 'payment_processor' => $this->_paymentProcessor['payment_processor_type'],
560 'trxn_id' => $result['trxn_id'],
561 );
562 $trxn = new CRM_Financial_DAO_FinancialTrxn();
563 $trxn->copyValues($trxnParams);
564 if (!CRM_Utils_Rule::currencyCode($trxn->currency)) {
565 $config = CRM_Core_Config::singleton();
566 $trxn->currency = $config->defaultCurrency;
567 }
568 $trxn->save();
569
570 return $trxn;
571 }
572
573 function record_contribution(&$mer_participant, &$params, $event) {
574 if (self::is_administrator() && CRM_Utils_Array::value('payment_type', $params)) {
575 $params['payment_instrument_id'] = $params['payment_type'];
576 }
577
578 if ($this->payer_contact_id) {
579 $payer = $this->payer_contact_id;
580 }
581 elseif (self::getContactID()) {
582 $payer = self::getContactID();
583 }
584 else {
585 $payer = $params['participant_contact_id'];
586 }
587
588 $contribParams = array(
589 'contact_id' => $payer,
590 'financial_type_id' => $params['financial_type_id'],
591 'receive_date' => $params['now'],
592 'total_amount' => $params['amount'],
593 'amount_level' => $mer_participant->fee_level,
594 'fee_amount' => $mer_participant->cost,
595 'net_amount' => $params['amount'],
596 'invoice_id' => "{$params['invoiceID']}-{$this->sub_trxn_index}",
597 'trxn_id' => "{$params['trxn_id']}-{$this->sub_trxn_index}",
598 'currency' => CRM_Utils_Array::value('currencyID', $params),
599 'source' => $event->title,
600 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
601 'contribution_status_id' => $params['contribution_status_id'],
602 'payment_instrument_id' => $params['payment_instrument_id'],
603 'check_number' => CRM_Utils_Array::value('check_number', $params),
604 );
605
606 $contribution = &CRM_Contribute_BAO_Contribution::add($contribParams, $ids);
607 if (is_a($contribution, 'CRM_Core_Error')) {
608 CRM_Core_Error::fatal(ts("There was an error creating a contribution record for your event. Please report this error to the webmaster. Details: %1\n", array(1 => $contribution->getMessages($contribution))));
609 }
610 $mer_participant->contribution_id = $contribution->id;
611 $params['contributionID'] = $contribution->id;
612 $params['receive_date'] = $contribution->receive_date;
613 if (CRM_Utils_Array::value('financial_trxn_id', $params)) {
614 $entity_financial_trxn_params = array(
615 'entity_table' => "civicrm_contribution",
616 'entity_id' => $contribution->id,
617 'financial_trxn_id' => $params['financial_trxn_id'],
618 'amount' => $params['amount'],
619 'currency' => CRM_Utils_Array::value('currencyID', $params),
620 );
621 $entity_trxn =& new CRM_Financial_DAO_EntityFinancialTrxn();
622 $entity_trxn->copyValues($entity_financial_trxn_params);
623 $entity_trxn->save();
624 }
625 }
626
627 function saveDataToSession() {
628 $session_line_items = array();
629 foreach ($this->line_items as $line_item) {
630 $session_line_item = array();
631 $session_line_item['amount'] = $line_item['amount'];
632 $session_line_item['cost'] = $line_item['cost'];
633 $session_line_item['event_id'] = $line_item['event']->id;
634 $session_line_items[] = $session_line_item;
635 }
636 $this->set('line_items', $session_line_items);
637 $this->set('payment_required', $this->payment_required);
638 $this->set('is_pay_later', $this->is_pay_later);
639 $this->set('pay_later_receipt', $this->pay_later_receipt);
640 $this->set('trxn_id', $this->trxn_id);
641 $this->set('trxn_date', $this->trxn_date);
642 $this->set('total', $this->total);
643 }
644
645 function setDefaultValues() {
646
647 $defaults = parent::setDefaultValues();
648
649 $config = CRM_Core_Config::singleton();
650 $default_country = new CRM_Core_DAO_Country();
651 $default_country->iso_code = $config->defaultContactCountry();
652 $default_country->find(TRUE);
653 $defaults["billing_country_id-{$this->_bltID}"] = $default_country->id;
654
655 if (self::getContactID() && !self::is_administrator()) {
656 $params = array('id' => self::getContactID());
657 $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
658
659 foreach ($contact->email as $email) {
660 if ($email['is_billing']) {
661 $defaults["billing_contact_email"] = $email['email'];
662 }
663 }
664 if (!CRM_Utils_Array::value('billing_contact_email', $defaults)) {
665 foreach ($contact->email as $email) {
666 if ($email['is_primary']) {
667 $defaults["billing_contact_email"] = $email['email'];
668 }
669 }
670 }
671
672 $defaults["billing_first_name"] = $contact->first_name;
673 $defaults["billing_middle_name"] = $contact->middle_name;
674 $defaults["billing_last_name"] = $contact->last_name;
675
676 $billing_address = CRM_Event_Cart_BAO_MerParticipant::billing_address_from_contact($contact);
677
678 if ($billing_address != NULL) {
679 $defaults["billing_street_address-{$this->_bltID}"] = $billing_address['street_address'];
680 $defaults["billing_city-{$this->_bltID}"] = $billing_address['city'];
681 $defaults["billing_postal_code-{$this->_bltID}"] = $billing_address['postal_code'];
682 $defaults["billing_state_province_id-{$this->_bltID}"] = $billing_address['state_province_id'];
683 $defaults["billing_country_id-{$this->_bltID}"] = $billing_address['country_id'];
684 }
685 }
686
687 $defaults["source"] = $this->description;
688
689 return $defaults;
690 }
691}
692