Merge pull request #13890 from aydun/joomla#6
[civicrm-core.git] / CRM / Event / Cart / Form / Checkout / Payment.php
1 <?php
2
3 /**
4 * Class CRM_Event_Cart_Form_Checkout_Payment
5 */
6 class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
7 public $all_participants;
8 public $financial_type_id;
9 public $description;
10 public $line_items;
11 public $_fields = [];
12 public $_paymentProcessor;
13 public $total;
14 public $sub_total;
15 public $payment_required = TRUE;
16 public $payer_contact_id;
17 public $is_pay_later = FALSE;
18 public $pay_later_receipt;
19
20 /**
21 * Register a participant.
22 *
23 * @param array $params
24 * @param CRM_Event_BAO_Participant $participant
25 * @param CRM_Event_BAO_Event $event
26 *
27 * @return mixed
28 */
29 public function registerParticipant($params, &$participant, $event) {
30 $transaction = new CRM_Core_Transaction();
31
32 // handle register date CRM-4320
33 $registerDate = date('YmdHis');
34 $participantParams = [
35 'id' => $participant->id,
36 'event_id' => $event->id,
37 'register_date' => $registerDate,
38 'source' => CRM_Utils_Array::value('participant_source', $params, $this->description),
39 //'fee_level' => $participant->fee_level,
40 'is_pay_later' => $this->is_pay_later,
41 'fee_amount' => CRM_Utils_Array::value('amount', $params, 0),
42 //XXX why is this a ref to participant and not contact?:
43 //'registered_by_id' => $this->payer_contact_id,
44 'fee_currency' => CRM_Utils_Array::value('currencyID', $params),
45 ];
46
47 if ($participant->must_wait) {
48 $participant_status = 'On waitlist';
49 }
50 elseif (CRM_Utils_Array::value('is_pay_later', $params, FALSE)) {
51 $participant_status = 'Pending from pay later';
52 }
53 else {
54 $participant_status = 'Registered';
55 }
56 $participant_statuses = CRM_Event_PseudoConstant::participantStatus();
57 $participantParams['status_id'] = array_search($participant_status, $participant_statuses);
58 $participant_status_label = CRM_Utils_Array::value($participantParams['status_id'], CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'));
59 $participantParams['participant_status'] = $participant_status_label;
60
61 $this->assign('isOnWaitlist', $participant->must_wait);
62
63 if ($this->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
64 $participantParams['is_test'] = 1;
65 }
66 else {
67 $participantParams['is_test'] = 0;
68 }
69
70 if (self::is_administrator()) {
71 if (!empty($params['note'])) {
72 $note_params = [
73 'participant_id' => $participant->id,
74 'contact_id' => self::getContactID(),
75 'note' => $params['note'],
76 ];
77 CRM_Event_BAO_Participant::update_note($note_params);
78 }
79 }
80
81 $participant->copyValues($participantParams);
82 $participant->save();
83
84 if (!empty($params['contributionID'])) {
85 $payment_params = [
86 'participant_id' => $participant->id,
87 'contribution_id' => $params['contributionID'],
88 ];
89 CRM_Event_BAO_ParticipantPayment::create($payment_params);
90 }
91
92 $transaction->commit();
93
94 $event_values = [];
95 CRM_Core_DAO::storeValues($event, $event_values);
96
97 $location = [];
98 if (CRM_Utils_Array::value('is_show_location', $event_values) == 1) {
99 $locationParams = [
100 'entity_id' => $participant->event_id,
101 'entity_table' => 'civicrm_event',
102 ];
103 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
104 CRM_Core_BAO_Address::fixAddress($location['address'][1]);
105 }
106
107 list($pre_id, $post_id) = CRM_Event_Cart_Form_MerParticipant::get_profile_groups($participant->event_id);
108 $payer_values = [
109 'email' => '',
110 'name' => '',
111 ];
112 if ($this->payer_contact_id) {
113 $payer_contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id);
114 $payer_values = [
115 'email' => $payer_contact_details[1],
116 'name' => $payer_contact_details[0],
117 ];
118 }
119 $values = [
120 'params' => [$participant->id => $participantParams],
121 'event' => $event_values,
122 'location' => $location,
123 'custom_pre_id' => $pre_id,
124 'custom_post_id' => $post_id,
125 'payer' => $payer_values,
126 ];
127 CRM_Event_BAO_Event::sendMail($participant->contact_id, $values, $participant->id);
128
129 return $participant;
130 }
131
132 /**
133 * Build payment fields.
134 */
135 public function buildPaymentFields() {
136 $payment_processor_id = NULL;
137 $can_pay_later = TRUE;
138 $pay_later_text = "";
139 $this->pay_later_receipt = "";
140 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
141 if ($payment_processor_id == NULL && $event_in_cart->event->payment_processor != NULL) {
142 $payment_processor_id = $event_in_cart->event->payment_processor;
143 $this->financial_type_id = $event_in_cart->event->financial_type_id;
144 }
145 else {
146 if ($event_in_cart->event->payment_processor != NULL && $event_in_cart->event->payment_processor != $payment_processor_id) {
147 CRM_Core_Error::statusBounce(ts('When registering for multiple events all events must use the same payment processor. '));
148 }
149 }
150 if (!$event_in_cart->event->is_pay_later) {
151 $can_pay_later = FALSE;
152 }
153 else {
154 //XXX
155 $pay_later_text = $event_in_cart->event->pay_later_text;
156 $this->pay_later_receipt = $event_in_cart->event->pay_later_receipt;
157 }
158 }
159
160 if ($payment_processor_id == NULL) {
161 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).'));
162 }
163
164 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($payment_processor_id, $this->_mode);
165 $this->assign('paymentProcessor', $this->_paymentProcessor);
166
167 CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, FALSE);
168
169 if ($can_pay_later || self::is_administrator()) {
170 $this->addElement('checkbox', 'is_pay_later',
171 $pay_later_text
172 );
173 $this->addElement('checkbox', 'payment_completed',
174 ts('Payment Completed')
175 );
176 $this->assign('pay_later_instructions', $this->pay_later_receipt);
177 }
178 }
179
180 /**
181 * Build QuickForm.
182 */
183 public function buildQuickForm() {
184
185 $this->line_items = [];
186 $this->sub_total = 0;
187 $this->_price_values = $this->getValuesForPage('ParticipantsAndPrices');
188
189 // iterate over each event in cart
190 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
191 $this->process_event_line_item($event_in_cart);
192 foreach ($this->cart->get_events_in_carts_by_main_event_id($event_in_cart->event_id) as $subevent) {
193 $this->process_event_line_item($subevent, 'subevent');
194 }
195 }
196
197 $this->total = $this->sub_total;
198 $this->payment_required = ($this->total > 0);
199 $this->assign('payment_required', $this->payment_required);
200 $this->assign('line_items', $this->line_items);
201 $this->assign('sub_total', $this->sub_total);
202 $this->assign('total', $this->total);
203 $buttons = [];
204 $buttons[] = [
205 'name' => ts('Go Back'),
206 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp',
207 'type' => 'back',
208 ];
209 $buttons[] = [
210 'isDefault' => TRUE,
211 'name' => ts('Complete Transaction'),
212 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
213 'type' => 'next',
214 ];
215
216 if ($this->total) {
217 $this->add('text', 'billing_contact_email', 'Billing Email', '', TRUE);
218 $this->assign('collect_billing_email', TRUE);
219 }
220 if (self::is_administrator()) {
221 $this->add('textarea', 'note', 'Note');
222 $this->add('text', 'source', 'Source', ['size' => 80]);
223 $instruments = [];
224 CRM_Core_OptionGroup::getAssoc('payment_instrument', $instruments, TRUE);
225 $options = [];
226 foreach ($instruments as $type) {
227 $options[] = $this->createElement('radio', NULL, '', $type['label'], $type['value']);
228 }
229 $this->addGroup($options, 'payment_type', ts("Alternative Payment Type"));
230 $this->add('text', 'check_number', ts('Check No.'), ['size' => 20]);
231 $this->addElement('checkbox', 'is_pending', ts('Create a pending registration'));
232
233 $this->assign('administrator', TRUE);
234 }
235 $this->addButtons($buttons);
236
237 $this->addFormRule(['CRM_Event_Cart_Form_Checkout_Payment', 'formRule'], $this);
238
239 if ($this->payment_required) {
240 $this->buildPaymentFields();
241 }
242 }
243
244 /**
245 * Process line item for event.
246 *
247 * @param bool $event_in_cart
248 * @param string $class
249 */
250 public function process_event_line_item(&$event_in_cart, $class = NULL) {
251 $cost = 0;
252 $price_set_id = CRM_Price_BAO_PriceSet::getFor("civicrm_event", $event_in_cart->event_id);
253 $amount_level = NULL;
254 if ($price_set_id) {
255 $event_price_values = [];
256 foreach ($this->_price_values as $key => $value) {
257 if (preg_match("/event_{$event_in_cart->event_id}_(price.*)/", $key, $matches)) {
258 $event_price_values[$matches[1]] = $value;
259 }
260 }
261 $price_sets = CRM_Price_BAO_PriceSet::getSetDetail($price_set_id, TRUE);
262 $price_set = $price_sets[$price_set_id];
263 $price_set_amount = [];
264 CRM_Price_BAO_PriceSet::processAmount($price_set['fields'], $event_price_values, $price_set_amount);
265 $discountCode = $this->_price_values['discountcode'];
266 if (!empty($discountCode)) {
267 $ret = $this->apply_discount($discountCode, $price_set_amount, $cost, $event_in_cart->event_id);
268 if ($ret == FALSE) {
269 $cost = $event_price_values['amount'];
270 }
271 }
272 else {
273 $cost = $event_price_values['amount'];
274 }
275 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
276 // function to get correct amount level consistently. Remove setting of the amount level in
277 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
278 // to cover all variants.
279 $amount_level = $event_price_values['amount_level'];
280 $price_details[$price_set_id] = $price_set_amount;
281 }
282
283 // iterate over each participant in event
284 foreach ($event_in_cart->participants as & $participant) {
285 $participant->cost = $cost;
286 $participant->fee_level = $amount_level;
287 $participant->price_details = $price_details;
288 }
289
290 $this->add_line_item($event_in_cart, $class);
291 }
292
293 /**
294 * Add line item.
295 *
296 * @param CRM_Event_BAO_Event $event_in_cart
297 * @param string $class
298 */
299 public function add_line_item($event_in_cart, $class = NULL) {
300 $amount = 0;
301 $cost = 0;
302 $not_waiting_participants = [];
303 foreach ($event_in_cart->not_waiting_participants() as $participant) {
304 $amount += $participant->cost;
305 $cost = max($cost, $participant->cost);
306 $not_waiting_participants[] = [
307 'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
308 ];
309 }
310 $waiting_participants = [];
311 foreach ($event_in_cart->waiting_participants() as $participant) {
312 $waiting_participants[] = [
313 'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
314 ];
315 }
316 $this->line_items[] = [
317 'amount' => $amount,
318 'cost' => $cost,
319 'event' => $event_in_cart->event,
320 'participants' => $not_waiting_participants,
321 'num_participants' => count($not_waiting_participants),
322 'num_waiting_participants' => count($waiting_participants),
323 'waiting_participants' => $waiting_participants,
324 'class' => $class,
325 ];
326
327 $this->sub_total += $amount;
328 }
329
330 /**
331 * Send email receipt.
332 *
333 * @param array $events_in_cart
334 * @param array $params
335 */
336 public function emailReceipt($events_in_cart, $params) {
337 $contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id);
338 $state_province = new CRM_Core_DAO_StateProvince();
339 $state_province->id = $params["billing_state_province_id-{$this->_bltID}"];
340 $state_province->find();
341 $state_province->fetch();
342 $country = new CRM_Core_DAO_Country();
343 $country->id = $params["billing_country_id-{$this->_bltID}"];
344 $country->find();
345 $country->fetch();
346 foreach ($this->line_items as & $line_item) {
347 $location_params = ['entity_id' => $line_item['event']->id, 'entity_table' => 'civicrm_event'];
348 $line_item['location'] = CRM_Core_BAO_Location::getValues($location_params, TRUE);
349 CRM_Core_BAO_Address::fixAddress($line_item['location']['address'][1]);
350 }
351 $send_template_params = [
352 'table' => 'civicrm_msg_template',
353 'contactId' => $this->payer_contact_id,
354 'from' => CRM_Core_BAO_Domain::getNameAndEmail(TRUE, TRUE),
355 'groupName' => 'msg_tpl_workflow_event',
356 'isTest' => FALSE,
357 'toEmail' => $contact_details[1],
358 'toName' => $contact_details[0],
359 'tplParams' => [
360 'billing_name' => "{$params['billing_first_name']} {$params['billing_last_name']}",
361 'billing_city' => $params["billing_city-{$this->_bltID}"],
362 'billing_country' => $country->name,
363 'billing_postal_code' => $params["billing_postal_code-{$this->_bltID}"],
364 'billing_state' => $state_province->abbreviation,
365 'billing_street_address' => $params["billing_street_address-{$this->_bltID}"],
366 'credit_card_exp_date' => $params['credit_card_exp_date'],
367 'credit_card_type' => $params['credit_card_type'],
368 'credit_card_number' => "************" . substr($params['credit_card_number'], -4, 4),
369 // XXX cart->get_discounts
370 'discounts' => $this->discounts,
371 'email' => $contact_details[1],
372 'events_in_cart' => $events_in_cart,
373 'line_items' => $this->line_items,
374 'name' => $contact_details[0],
375 'transaction_id' => $params['trxn_id'],
376 'transaction_date' => $params['trxn_date'],
377 'is_pay_later' => $this->is_pay_later,
378 'pay_later_receipt' => $this->pay_later_receipt,
379 ],
380 'valueName' => 'event_registration_receipt',
381 'PDFFilename' => ts('confirmation') . '.pdf',
382 ];
383 $template_params_to_copy = [
384 'billing_name',
385 'billing_city',
386 'billing_country',
387 'billing_postal_code',
388 'billing_state',
389 'billing_street_address',
390 'credit_card_exp_date',
391 'credit_card_type',
392 'credit_card_number',
393 ];
394 foreach ($template_params_to_copy as $template_param_to_copy) {
395 $this->set($template_param_to_copy, $send_template_params['tplParams'][$template_param_to_copy]);
396 }
397
398 CRM_Core_BAO_MessageTemplate::sendTemplate($send_template_params);
399 }
400
401 /**
402 * Apply form rules.
403 *
404 * @param array $fields
405 * @param array $files
406 * @param CRM_Core_Form $self
407 *
408 * @return array|bool
409 */
410 public static function formRule($fields, $files, $self) {
411 $errors = [];
412
413 if ($self->payment_required && empty($self->_submitValues['is_pay_later'])) {
414 CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
415
416 // validate payment instrument values (e.g. credit card number)
417 CRM_Core_Payment_Form::validatePaymentInstrument($self->_paymentProcessor['id'], $fields, $errors, NULL);
418 }
419
420 return empty($errors) ? TRUE : $errors;
421 }
422
423 /**
424 * Validate form.
425 *
426 * @todo this should surely go! Test & remove.
427 * @return bool
428 */
429 public function validate() {
430 if ($this->is_pay_later) {
431 $this->_fields['credit_card_number']['is_required'] = FALSE;
432 $this->_fields['cvv2']['is_required'] = FALSE;
433 $this->_fields['credit_card_exp_date']['is_required'] = FALSE;
434 $this->_fields['credit_card_type']['is_required'] = FALSE;
435 }
436 return parent::validate();
437 }
438
439 /**
440 * Pre-process form.
441 */
442 public function preProcess() {
443 $params = $this->_submitValues;
444 $this->is_pay_later = CRM_Utils_Array::value('is_pay_later', $params, FALSE) && !CRM_Utils_Array::value('payment_completed', $params);
445
446 parent::preProcess();
447 }
448
449 /**
450 * Post process form.
451 */
452 public function postProcess() {
453
454 $transaction = new CRM_Core_Transaction();
455 $trxnDetails = NULL;
456 $params = $this->_submitValues;
457
458 $main_participants = $this->cart->get_main_event_participants();
459 foreach ($main_participants as $participant) {
460 $defaults = [];
461 $ids = ['contact_id' => $participant->contact_id];
462 $contact = CRM_Contact_BAO_Contact::retrieve($ids, $defaults);
463 $contact->is_deleted = 0;
464 $contact->save();
465 }
466
467 $trxn_prefix = 'VR';
468 if (array_key_exists('billing_contact_email', $params)) {
469 $this->payer_contact_id = self::find_or_create_contact($this->getContactID(), [
470 'email' => $params['billing_contact_email'],
471 'first_name' => $params['billing_first_name'],
472 'last_name' => $params['billing_last_name'],
473 'is_deleted' => FALSE,
474 ]);
475
476 $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
477 $this->payer_contact_id,
478 'contact_type'
479 );
480 $billing_fields = [
481 "billing_first_name" => 1,
482 "billing_middle_name" => 1,
483 "billing_last_name" => 1,
484 "billing_street_address-{$this->_bltID}" => 1,
485 "billing_city-{$this->_bltID}" => 1,
486 "billing_state_province_id-{$this->_bltID}" => 1,
487 "billing_postal_code-{$this->_bltID}" => 1,
488 "billing_country_id-{$this->_bltID}" => 1,
489 "address_name-{$this->_bltID}" => 1,
490 "email-{$this->_bltID}" => 1,
491 ];
492
493 $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);
494
495 $params["email-{$this->_bltID}"] = $params['billing_contact_email'];
496 CRM_Contact_BAO_Contact::createProfileContact(
497 $params,
498 $billing_fields,
499 $this->payer_contact_id,
500 NULL,
501 NULL,
502 $ctype,
503 TRUE
504 );
505 }
506
507 $params['now'] = date('YmdHis');
508 $params['invoiceID'] = md5(uniqid(rand(), TRUE));
509 $params['amount'] = $this->total;
510 $params['financial_type_id'] = $this->financial_type_id;
511 if ($this->payment_required && empty($params['is_pay_later'])) {
512 $trxnDetails = $this->make_payment($params);
513 $params['trxn_id'] = $trxnDetails['trxn_id'];
514 $params['trxn_date'] = $trxnDetails['trxn_date'];
515 $params['currencyID'] = $trxnDetails['currency'];
516 }
517 $this->cart->completed = TRUE;
518 $this->cart->save();
519 $this->set('last_event_cart_id', $this->cart->id);
520
521 $contribution_statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
522 $params['payment_instrument_id'] = NULL;
523 if (!empty($params['is_pay_later'])) {
524 $params['payment_instrument_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
525 $trxn_prefix = 'CK';
526 }
527 else {
528 $params['payment_instrument_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit Card');
529 }
530 if ($this->is_pay_later && empty($params['payment_completed'])) {
531 $params['contribution_status_id'] = array_search('Pending', $contribution_statuses);
532 }
533 else {
534 $params['contribution_status_id'] = array_search('Completed', $contribution_statuses);
535 $params['participant_status'] = 'Registered';
536 $params['is_pay_later'] = 0;
537 }
538 if ($trxnDetails == NULL) {
539 $params['trxn_id'] = $trxn_prefix . strftime("%Y%m%d%H%M%S");
540 $params['trxn_date'] = $params['now'];
541 }
542
543 if ($this->payment_required) {
544 $this->emailReceipt($this->cart->events_in_carts, $params);
545 }
546
547 // n.b. we need to process the subparticipants before main event
548 // participants so that session attendance can be included in the email
549 $main_participants = $this->cart->get_main_event_participants();
550 $this->all_participants = [];
551 foreach ($main_participants as $main_participant) {
552 $this->all_participants = array_merge($this->all_participants, $this->cart->get_subparticipants($main_participant));
553 }
554 $this->all_participants = array_merge($this->all_participants, $main_participants);
555
556 $this->sub_trxn_index = 0;
557 foreach ($this->all_participants as $mer_participant) {
558 $event_in_cart = $this->cart->get_event_in_cart_by_event_id($mer_participant->event_id);
559
560 $this->sub_trxn_index += 1;
561
562 unset($params['contributionID']);
563 if ($mer_participant->must_wait) {
564 $this->registerParticipant($params, $mer_participant, $event_in_cart->event);
565 }
566 else {
567 $params['amount'] = $mer_participant->cost - $mer_participant->discount_amount;
568
569 if ($event_in_cart->event->financial_type_id && $mer_participant->cost) {
570 $params['financial_type_id'] = $event_in_cart->event->financial_type_id;
571 $params['participant_contact_id'] = $mer_participant->contact_id;
572 $contribution = $this->record_contribution($mer_participant, $params, $event_in_cart->event);
573 // Record civicrm_line_item
574 CRM_Price_BAO_LineItem::processPriceSet($mer_participant->id, $mer_participant->price_details, $contribution, $entity_table = 'civicrm_participant');
575 }
576 $this->registerParticipant($params, $mer_participant, $event_in_cart->event);
577 }
578 }
579 $this->trxn_id = $params['trxn_id'];
580 $this->trxn_date = $params['trxn_date'];
581 $this->saveDataToSession();
582 $transaction->commit();
583 }
584
585 /**
586 * Make payment.
587 *
588 * @param array $params
589 *
590 * @return array
591 * @throws Exception
592 */
593 public function make_payment(&$params) {
594 $config = CRM_Core_Config::singleton();
595 if (isset($params["billing_state_province_id-{$this->_bltID}"]) && $params["billing_state_province_id-{$this->_bltID}"]) {
596 $params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params["billing_state_province_id-{$this->_bltID}"]);
597 }
598
599 if (isset($params["billing_country_id-{$this->_bltID}"]) && $params["billing_country_id-{$this->_bltID}"]) {
600 $params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($params["billing_country_id-{$this->_bltID}"]);
601 }
602 $params['ip_address'] = CRM_Utils_System::ipAddress();
603 $params['currencyID'] = $config->defaultCurrency;
604
605 $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
606 CRM_Core_Payment_Form::mapParams($this->_bltID, $params, $params, TRUE);
607 $params['month'] = $params['credit_card_exp_date']['M'];
608 $params['year'] = $params['credit_card_exp_date']['Y'];
609 try {
610 $result = $payment->doPayment($params);
611 }
612 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
613 CRM_Core_Error::displaySessionError($result);
614 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/cart_checkout', "_qf_Payment_display=1&qfKey={$this->controller->_key}", TRUE, NULL, FALSE));
615 }
616
617 $trxnDetails = [
618 'trxn_id' => $result['trxn_id'],
619 'trxn_date' => $result['now'],
620 'currency' => CRM_Utils_Array::value('currencyID', $result),
621 ];
622 return $trxnDetails;
623 }
624
625 /**
626 * Record contribution.
627 *
628 * @param CRM_Event_BAO_Participant $mer_participant
629 * @param array $params
630 * @param CRM_Event_BAO_Event $event
631 *
632 * @return object
633 * @throws Exception
634 */
635 public function record_contribution(&$mer_participant, &$params, $event) {
636 if (self::is_administrator() && !empty($params['payment_type'])) {
637 $params['payment_instrument_id'] = $params['payment_type'];
638 }
639
640 if ($this->payer_contact_id) {
641 $payer = $this->payer_contact_id;
642 }
643 elseif (self::getContactID()) {
644 $payer = self::getContactID();
645 }
646 else {
647 $payer = $params['participant_contact_id'];
648 }
649
650 $contribParams = [
651 'contact_id' => $payer,
652 'financial_type_id' => $params['financial_type_id'],
653 'receive_date' => $params['now'],
654 'total_amount' => $params['amount'],
655 'amount_level' => $mer_participant->fee_level,
656 'net_amount' => $params['amount'],
657 'invoice_id' => "{$params['invoiceID']}-{$this->sub_trxn_index}",
658 'trxn_id' => "{$params['trxn_id']}-{$this->sub_trxn_index}",
659 'currency' => CRM_Utils_Array::value('currencyID', $params),
660 'source' => $event->title,
661 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
662 'contribution_status_id' => $params['contribution_status_id'],
663 'payment_instrument_id' => $params['payment_instrument_id'],
664 'check_number' => CRM_Utils_Array::value('check_number', $params),
665 'skipLineItem' => 1,
666 ];
667
668 if (is_array($this->_paymentProcessor)) {
669 $contribParams['payment_processor'] = $this->_paymentProcessor['id'];
670 }
671
672 $contribution = CRM_Contribute_BAO_Contribution::add($contribParams);
673 $mer_participant->contribution_id = $contribution->id;
674 $params['contributionID'] = $contribution->id;
675
676 return $contribution;
677 }
678
679 /**
680 * Save data to session.
681 */
682 public function saveDataToSession() {
683 $session_line_items = [];
684 foreach ($this->line_items as $line_item) {
685 $session_line_item = [];
686 $session_line_item['amount'] = $line_item['amount'];
687 $session_line_item['cost'] = $line_item['cost'];
688 $session_line_item['event_id'] = $line_item['event']->id;
689 $session_line_items[] = $session_line_item;
690 }
691 $this->set('line_items', $session_line_items);
692 $this->set('payment_required', $this->payment_required);
693 $this->set('is_pay_later', $this->is_pay_later);
694 $this->set('pay_later_receipt', $this->pay_later_receipt);
695 $this->set('trxn_id', $this->trxn_id);
696 $this->set('trxn_date', $this->trxn_date);
697 $this->set('total', $this->total);
698 }
699
700 /**
701 * Set form default values.
702 *
703 * @return array
704 */
705 public function setDefaultValues() {
706
707 $defaults = parent::setDefaultValues();
708
709 $config = CRM_Core_Config::singleton();
710 $default_country = new CRM_Core_DAO_Country();
711 $default_country->iso_code = $config->defaultContactCountry();
712 $default_country->find(TRUE);
713 $defaults["billing_country_id-{$this->_bltID}"] = $default_country->id;
714
715 if (self::getContactID() && !self::is_administrator()) {
716 $params = ['id' => self::getContactID()];
717 $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
718
719 foreach ($contact->email as $email) {
720 if ($email['is_billing']) {
721 $defaults["billing_contact_email"] = $email['email'];
722 }
723 }
724 if (empty($defaults['billing_contact_email'])) {
725 foreach ($contact->email as $email) {
726 if ($email['is_primary']) {
727 $defaults["billing_contact_email"] = $email['email'];
728 }
729 }
730 }
731
732 $defaults["billing_first_name"] = $contact->first_name;
733 $defaults["billing_middle_name"] = $contact->middle_name;
734 $defaults["billing_last_name"] = $contact->last_name;
735
736 $billing_address = CRM_Event_Cart_BAO_MerParticipant::billing_address_from_contact($contact);
737
738 if ($billing_address != NULL) {
739 $defaults["billing_street_address-{$this->_bltID}"] = $billing_address['street_address'];
740 $defaults["billing_city-{$this->_bltID}"] = $billing_address['city'];
741 $defaults["billing_postal_code-{$this->_bltID}"] = $billing_address['postal_code'];
742 $defaults["billing_state_province_id-{$this->_bltID}"] = $billing_address['state_province_id'];
743 $defaults["billing_country_id-{$this->_bltID}"] = $billing_address['country_id'];
744 }
745 }
746
747 $defaults["source"] = $this->description;
748
749 return $defaults;
750 }
751
752 /**
753 * Apply discount.
754 *
755 * @param string $discountCode
756 * @param array $price_set_amount
757 * @param float $cost
758 * @param int $event_id
759 *
760 * @return bool
761 */
762 protected function apply_discount($discountCode, &$price_set_amount, &$cost, $event_id) {
763 //need better way to determine if cividiscount installed
764 $autoDiscount = [];
765 $sql = "select is_active from civicrm_extension where name like 'CiviDiscount%'";
766 $dao = CRM_Core_DAO::executeQuery($sql, '');
767 while ($dao->fetch()) {
768 if ($dao->is_active != '1') {
769 return FALSE;
770 }
771 }
772 $discounted_priceset_ids = _cividiscount_get_discounted_priceset_ids();
773 $discounts = _cividiscount_get_discounts();
774
775 $stat = FALSE;
776 foreach ($discounts as $key => $discountValue) {
777 if ($key == $discountCode) {
778 $events = CRM_Utils_Array::value('events', $discountValue);
779 $evt_ids = implode(",", $events);
780 if ($evt_ids == "0" || strpos($evt_ids, $event_id)) {
781 $event_match = TRUE;
782 }
783 //check priceset is_active
784 if ($discountValue['active_on'] != NULL) {
785 $today = date('Y-m-d');
786 $diff1 = date_diff(date_create($today), date_create($discountValue['active_on']));
787 if ($diff1->days > 0) {
788 $active1 = TRUE;
789 }
790 }
791 else {
792 $active1 = TRUE;
793 }
794 if ($discountValue['expire_on'] != NULL) {
795 $diff2 = date_diff(date_create($today), date_create($discountValue['expire_on']));
796 if ($diff2->days > 0) {
797 $active2 = TRUE;
798 }
799 }
800 else {
801 $active2 = TRUE;
802 }
803 }
804 if ($discountValue['is_active'] == TRUE && ($discountValue['count_max'] == 0 || ($discountValue['count_max'] > $discountValue['count_use'])) && $active1 == TRUE && $active2 == TRUE && $event_match == TRUE) {
805 foreach ($price_set_amount as $key => $price) {
806 if (array_search($price['price_field_value_id'], $discounted_priceset_ids) != NULL) {
807 $discounted = _cividiscount_calc_discount($price['line_total'], $price['label'], $discountValue, $autoDiscount, "USD");
808 $price_set_amount[$key]['line_total'] = $discounted[0];
809 $cost += $discounted[0];
810 $price_set_amount[$key]['label'] = $discounted[1];
811 }
812 else {
813 $cost += $price['line_total'];
814 }
815 }
816 $stat = TRUE;
817 }
818 }
819 return $stat;
820 }
821
822 }