4 * Class CRM_Event_Cart_Form_Checkout_Payment
6 class CRM_Event_Cart_Form_Checkout_Payment
extends CRM_Event_Cart_Form_Cart
{
7 public $all_participants;
8 public $financial_type_id;
12 public $_paymentProcessor;
15 public $payment_required = TRUE;
16 public $payer_contact_id;
17 public $is_pay_later = FALSE;
18 public $pay_later_receipt;
21 * Register a participant.
23 * @param array $params
24 * @param CRM_Event_BAO_Participant $participant
25 * @param CRM_Event_BAO_Event $event
29 public function registerParticipant($params, &$participant, $event) {
30 $transaction = new CRM_Core_Transaction();
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),
47 if ($participant->must_wait
) {
48 $participant_status = 'On waitlist';
50 elseif (CRM_Utils_Array
::value('is_pay_later', $params, FALSE)) {
51 $participant_status = 'Pending from pay later';
54 $participant_status = 'Registered';
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;
61 $this->assign('isOnWaitlist', $participant->must_wait
);
63 if ($this->_action
& CRM_Core_Action
::PREVIEW || CRM_Utils_Array
::value('mode', $params) == 'test') {
64 $participantParams['is_test'] = 1;
67 $participantParams['is_test'] = 0;
70 if (self
::is_administrator()) {
71 if (!empty($params['note'])) {
73 'participant_id' => $participant->id
,
74 'contact_id' => self
::getContactID(),
75 'note' => $params['note'],
77 CRM_Event_BAO_Participant
::update_note($note_params);
81 $participant->copyValues($participantParams);
84 if (!empty($params['contributionID'])) {
86 'participant_id' => $participant->id
,
87 'contribution_id' => $params['contributionID'],
89 CRM_Event_BAO_ParticipantPayment
::create($payment_params);
92 $transaction->commit();
95 CRM_Core_DAO
::storeValues($event, $event_values);
98 if (CRM_Utils_Array
::value('is_show_location', $event_values) == 1) {
100 'entity_id' => $participant->event_id
,
101 'entity_table' => 'civicrm_event',
103 $location = CRM_Core_BAO_Location
::getValues($locationParams, TRUE);
104 CRM_Core_BAO_Address
::fixAddress($location['address'][1]);
107 list($pre_id, $post_id) = CRM_Event_Cart_Form_MerParticipant
::get_profile_groups($participant->event_id
);
112 if ($this->payer_contact_id
) {
113 $payer_contact_details = CRM_Contact_BAO_Contact
::getContactDetails($this->payer_contact_id
);
115 'email' => $payer_contact_details[1],
116 'name' => $payer_contact_details[0],
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,
127 CRM_Event_BAO_Event
::sendMail($participant->contact_id
, $values, $participant->id
);
133 * Build payment fields.
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
;
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. '));
150 if (!$event_in_cart->event
->is_pay_later
) {
151 $can_pay_later = FALSE;
155 $pay_later_text = $event_in_cart->event
->pay_later_text
;
156 $this->pay_later_receipt
= $event_in_cart->event
->pay_later_receipt
;
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).'));
164 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($payment_processor_id, $this->_mode
);
165 $this->assign('paymentProcessor', $this->_paymentProcessor
);
167 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, FALSE, FALSE);
169 if ($can_pay_later || self
::is_administrator()) {
170 $this->addElement('checkbox', 'is_pay_later',
173 $this->addElement('checkbox', 'payment_completed',
174 ts('Payment Completed')
176 $this->assign('pay_later_instructions', $this->pay_later_receipt
);
183 public function buildQuickForm() {
185 $this->line_items
= [];
186 $this->sub_total
= 0;
187 $this->_price_values
= $this->getValuesForPage('ParticipantsAndPrices');
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');
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
);
205 'name' => ts('Go Back'),
206 'spacing' => '  ',
211 'name' => ts('Complete Transaction'),
212 'spacing' => ' ',
217 $this->add('text', 'billing_contact_email', 'Billing Email', '', TRUE);
218 $this->assign('collect_billing_email', TRUE);
220 if (self
::is_administrator()) {
221 $this->add('textarea', 'note', 'Note');
222 $this->add('text', 'source', 'Source', ['size' => 80]);
224 CRM_Core_OptionGroup
::getAssoc('payment_instrument', $instruments, TRUE);
226 foreach ($instruments as $type) {
227 $options[] = $this->createElement('radio', NULL, '', $type['label'], $type['value']);
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'));
233 $this->assign('administrator', TRUE);
235 $this->addButtons($buttons);
237 $this->addFormRule(['CRM_Event_Cart_Form_Checkout_Payment', 'formRule'], $this);
239 if ($this->payment_required
) {
240 $this->buildPaymentFields();
245 * Process line item for event.
247 * @param bool $event_in_cart
248 * @param string $class
250 public function process_event_line_item(&$event_in_cart, $class = NULL) {
252 $price_set_id = CRM_Price_BAO_PriceSet
::getFor("civicrm_event", $event_in_cart->event_id
);
253 $amount_level = NULL;
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;
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
);
269 $cost = $event_price_values['amount'];
273 $cost = $event_price_values['amount'];
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;
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;
290 $this->add_line_item($event_in_cart, $class);
296 * @param CRM_Event_BAO_Event $event_in_cart
297 * @param string $class
299 public function add_line_item($event_in_cart, $class = NULL) {
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
),
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
),
316 $this->line_items
[] = [
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,
327 $this->sub_total +
= $amount;
331 * Send email receipt.
333 * @param array $events_in_cart
334 * @param array $params
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}"];
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]);
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',
357 'toEmail' => $contact_details[1],
358 'toName' => $contact_details[0],
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
,
380 'valueName' => 'event_registration_receipt',
381 'PDFFilename' => ts('confirmation') . '.pdf',
383 $template_params_to_copy = [
387 'billing_postal_code',
389 'billing_street_address',
390 'credit_card_exp_date',
392 'credit_card_number',
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]);
398 CRM_Core_BAO_MessageTemplate
::sendTemplate($send_template_params);
404 * @param array $fields
405 * @param array $files
406 * @param CRM_Core_Form $self
410 public static function formRule($fields, $files, $self) {
413 if ($self->payment_required
&& empty($self->_submitValues
['is_pay_later'])) {
414 CRM_Core_Form
::validateMandatoryFields($self->_fields
, $fields, $errors);
416 // validate payment instrument values (e.g. credit card number)
417 CRM_Core_Payment_Form
::validatePaymentInstrument($self->_paymentProcessor
['id'], $fields, $errors, NULL);
420 return empty($errors) ?
TRUE : $errors;
426 * @todo this should surely go! Test & remove.
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;
436 return parent
::validate();
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);
446 parent
::preProcess();
452 public function postProcess() {
454 $transaction = new CRM_Core_Transaction();
456 $params = $this->_submitValues
;
458 $main_participants = $this->cart
->get_main_event_participants();
459 foreach ($main_participants as $participant) {
461 $ids = ['contact_id' => $participant->contact_id
];
462 $contact = CRM_Contact_BAO_Contact
::retrieve($ids, $defaults);
463 $contact->is_deleted
= 0;
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,
476 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
477 $this->payer_contact_id
,
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,
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);
495 $params["email-{$this->_bltID}"] = $params['billing_contact_email'];
496 CRM_Contact_BAO_Contact
::createProfileContact(
499 $this->payer_contact_id
,
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'];
517 $this->cart
->completed
= TRUE;
519 $this->set('last_event_cart_id', $this->cart
->id
);
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');
528 $params['payment_instrument_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit Card');
530 if ($this->is_pay_later
&& empty($params['payment_completed'])) {
531 $params['contribution_status_id'] = array_search('Pending', $contribution_statuses);
534 $params['contribution_status_id'] = array_search('Completed', $contribution_statuses);
535 $params['participant_status'] = 'Registered';
536 $params['is_pay_later'] = 0;
538 if ($trxnDetails == NULL) {
539 $params['trxn_id'] = $trxn_prefix . strftime("%Y%m%d%H%M%S");
540 $params['trxn_date'] = $params['now'];
543 if ($this->payment_required
) {
544 $this->emailReceipt($this->cart
->events_in_carts
, $params);
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));
554 $this->all_participants
= array_merge($this->all_participants
, $main_participants);
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
);
560 $this->sub_trxn_index +
= 1;
562 unset($params['contributionID']);
563 if ($mer_participant->must_wait
) {
564 $this->registerParticipant($params, $mer_participant, $event_in_cart->event
);
567 $params['amount'] = $mer_participant->cost
- $mer_participant->discount_amount
;
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');
576 $this->registerParticipant($params, $mer_participant, $event_in_cart->event
);
579 $this->trxn_id
= $params['trxn_id'];
580 $this->trxn_date
= $params['trxn_date'];
581 $this->saveDataToSession();
582 $transaction->commit();
588 * @param array $params
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}"]);
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}"]);
602 $params['ip_address'] = CRM_Utils_System
::ipAddress();
603 $params['currencyID'] = $config->defaultCurrency
;
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'];
610 $result = $payment->doPayment($params);
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));
618 'trxn_id' => $result['trxn_id'],
619 'trxn_date' => $result['now'],
620 'currency' => CRM_Utils_Array
::value('currencyID', $result),
626 * Record contribution.
628 * @param CRM_Event_BAO_Participant $mer_participant
629 * @param array $params
630 * @param CRM_Event_BAO_Event $event
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'];
640 if ($this->payer_contact_id
) {
641 $payer = $this->payer_contact_id
;
643 elseif (self
::getContactID()) {
644 $payer = self
::getContactID();
647 $payer = $params['participant_contact_id'];
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),
668 if (is_array($this->_paymentProcessor
)) {
669 $contribParams['payment_processor'] = $this->_paymentProcessor
['id'];
672 $contribution = CRM_Contribute_BAO_Contribution
::add($contribParams);
673 $mer_participant->contribution_id
= $contribution->id
;
674 $params['contributionID'] = $contribution->id
;
676 return $contribution;
680 * Save data to session.
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;
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
);
701 * Set form default values.
705 public function setDefaultValues() {
707 $defaults = parent
::setDefaultValues();
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
;
715 if (self
::getContactID() && !self
::is_administrator()) {
716 $params = ['id' => self
::getContactID()];
717 $contact = CRM_Contact_BAO_Contact
::retrieve($params, $defaults);
719 foreach ($contact->email
as $email) {
720 if ($email['is_billing']) {
721 $defaults["billing_contact_email"] = $email['email'];
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'];
732 $defaults["billing_first_name"] = $contact->first_name
;
733 $defaults["billing_middle_name"] = $contact->middle_name
;
734 $defaults["billing_last_name"] = $contact->last_name
;
736 $billing_address = CRM_Event_Cart_BAO_MerParticipant
::billing_address_from_contact($contact);
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'];
747 $defaults["source"] = $this->description
;
755 * @param string $discountCode
756 * @param array $price_set_amount
758 * @param int $event_id
762 protected function apply_discount($discountCode, &$price_set_amount, &$cost, $event_id) {
763 //need better way to determine if cividiscount installed
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') {
772 $discounted_priceset_ids = _cividiscount_get_discounted_priceset_ids();
773 $discounts = _cividiscount_get_discounts();
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)) {
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) {
794 if ($discountValue['expire_on'] != NULL) {
795 $diff2 = date_diff(date_create($today), date_create($discountValue['expire_on']));
796 if ($diff2->days
> 0) {
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];
813 $cost +
= $price['line_total'];