Merge pull request #13487 from giant-rabbit/17880-after-first-reminder-for-membership-fix
[civicrm-core.git] / CRM / Event / Cart / Form / Checkout / Payment.php
index a1a2e850188beac66ecea54a7b8ee1a594a04140..143096e3525826f0ec24af0a33f7bcf5b880c73a 100644 (file)
@@ -8,7 +8,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
   public $financial_type_id;
   public $description;
   public $line_items;
-  public $_fields = array();
+  public $_fields = [];
   public $_paymentProcessor;
   public $total;
   public $sub_total;
@@ -31,7 +31,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
 
     // handle register date CRM-4320
     $registerDate = date('YmdHis');
-    $participantParams = array(
+    $participantParams = [
       'id' => $participant->id,
       'event_id' => $event->id,
       'register_date' => $registerDate,
@@ -42,7 +42,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       //XXX why is this a ref to participant and not contact?:
       //'registered_by_id' => $this->payer_contact_id,
       'fee_currency' => CRM_Utils_Array::value('currencyID', $params),
-    );
+    ];
 
     if ($participant->must_wait) {
       $participant_status = 'On waitlist';
@@ -69,11 +69,11 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
 
     if (self::is_administrator()) {
       if (!empty($params['note'])) {
-        $note_params = array(
+        $note_params = [
           'participant_id' => $participant->id,
           'contact_id' => self::getContactID(),
           'note' => $params['note'],
-        );
+        ];
         CRM_Event_BAO_Participant::update_note($note_params);
       }
     }
@@ -82,48 +82,48 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     $participant->save();
 
     if (!empty($params['contributionID'])) {
-      $payment_params = array(
+      $payment_params = [
         'participant_id' => $participant->id,
         'contribution_id' => $params['contributionID'],
-      );
+      ];
       CRM_Event_BAO_ParticipantPayment::create($payment_params);
     }
 
     $transaction->commit();
 
-    $event_values = array();
+    $event_values = [];
     CRM_Core_DAO::storeValues($event, $event_values);
 
-    $location = array();
+    $location = [];
     if (CRM_Utils_Array::value('is_show_location', $event_values) == 1) {
-      $locationParams = array(
+      $locationParams = [
         'entity_id' => $participant->event_id,
         'entity_table' => 'civicrm_event',
-      );
+      ];
       $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
       CRM_Core_BAO_Address::fixAddress($location['address'][1]);
     }
 
     list($pre_id, $post_id) = CRM_Event_Cart_Form_MerParticipant::get_profile_groups($participant->event_id);
-    $payer_values = array(
+    $payer_values = [
       'email' => '',
       'name' => '',
-    );
+    ];
     if ($this->payer_contact_id) {
       $payer_contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id);
-      $payer_values = array(
+      $payer_values = [
         'email' => $payer_contact_details[1],
         'name' => $payer_contact_details[0],
-      );
+      ];
     }
-    $values = array(
-      'params' => array($participant->id => $participantParams),
+    $values = [
+      'params' => [$participant->id => $participantParams],
       'event' => $event_values,
       'location' => $location,
       'custom_pre_id' => $pre_id,
       'custom_post_id' => $post_id,
       'payer' => $payer_values,
-    );
+    ];
     CRM_Event_BAO_Event::sendMail($participant->contact_id, $values, $participant->id);
 
     return $participant;
@@ -175,6 +175,10 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       );
       $this->assign('pay_later_instructions', $this->pay_later_receipt);
     }
+
+    // Event Cart does not support multiple payment processors
+    // so we cannot call $this->preProcessPaymentOptions();
+    CRM_Financial_Form_Payment::addCreditCardJs($this->_paymentProcessor['id']);
   }
 
   /**
@@ -182,7 +186,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
    */
   public function buildQuickForm() {
 
-    $this->line_items = array();
+    $this->line_items = [];
     $this->sub_total = 0;
     $this->_price_values = $this->getValuesForPage('ParticipantsAndPrices');
 
@@ -200,18 +204,18 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     $this->assign('line_items', $this->line_items);
     $this->assign('sub_total', $this->sub_total);
     $this->assign('total', $this->total);
-    $buttons = array();
-    $buttons[] = array(
+    $buttons = [];
+    $buttons[] = [
       'name' => ts('Go Back'),
       'spacing' => '   &nbsp',
       'type' => 'back',
-    );
-    $buttons[] = array(
+    ];
+    $buttons[] = [
       'isDefault' => TRUE,
       'name' => ts('Complete Transaction'),
       'spacing' => '                  ',
       'type' => 'next',
-    );
+    ];
 
     if ($this->total) {
       $this->add('text', 'billing_contact_email', 'Billing Email', '', TRUE);
@@ -219,22 +223,22 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     }
     if (self::is_administrator()) {
       $this->add('textarea', 'note', 'Note');
-      $this->add('text', 'source', 'Source', array('size' => 80));
-      $instruments = array();
+      $this->add('text', 'source', 'Source', ['size' => 80]);
+      $instruments = [];
       CRM_Core_OptionGroup::getAssoc('payment_instrument', $instruments, TRUE);
-      $options = array();
+      $options = [];
       foreach ($instruments as $type) {
         $options[] = $this->createElement('radio', NULL, '', $type['label'], $type['value']);
       }
       $this->addGroup($options, 'payment_type', ts("Alternative Payment Type"));
-      $this->add('text', 'check_number', ts('Check No.'), array('size' => 20));
+      $this->add('text', 'check_number', ts('Check No.'), ['size' => 20]);
       $this->addElement('checkbox', 'is_pending', ts('Create a pending registration'));
 
       $this->assign('administrator', TRUE);
     }
     $this->addButtons($buttons);
 
-    $this->addFormRule(array('CRM_Event_Cart_Form_Checkout_Payment', 'formRule'), $this);
+    $this->addFormRule(['CRM_Event_Cart_Form_Checkout_Payment', 'formRule'], $this);
 
     if ($this->payment_required) {
       $this->buildPaymentFields();
@@ -252,7 +256,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     $price_set_id = CRM_Price_BAO_PriceSet::getFor("civicrm_event", $event_in_cart->event_id);
     $amount_level = NULL;
     if ($price_set_id) {
-      $event_price_values = array();
+      $event_price_values = [];
       foreach ($this->_price_values as $key => $value) {
         if (preg_match("/event_{$event_in_cart->event_id}_(price.*)/", $key, $matches)) {
           $event_price_values[$matches[1]] = $value;
@@ -260,7 +264,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       }
       $price_sets = CRM_Price_BAO_PriceSet::getSetDetail($price_set_id, TRUE);
       $price_set = $price_sets[$price_set_id];
-      $price_set_amount = array();
+      $price_set_amount = [];
       CRM_Price_BAO_PriceSet::processAmount($price_set['fields'], $event_price_values, $price_set_amount);
       $discountCode = $this->_price_values['discountcode'];
       if (!empty($discountCode)) {
@@ -299,21 +303,21 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
   public function add_line_item($event_in_cart, $class = NULL) {
     $amount = 0;
     $cost = 0;
-    $not_waiting_participants = array();
+    $not_waiting_participants = [];
     foreach ($event_in_cart->not_waiting_participants() as $participant) {
       $amount += $participant->cost;
       $cost = max($cost, $participant->cost);
-      $not_waiting_participants[] = array(
+      $not_waiting_participants[] = [
         'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
-      );
+      ];
     }
-    $waiting_participants = array();
+    $waiting_participants = [];
     foreach ($event_in_cart->waiting_participants() as $participant) {
-      $waiting_participants[] = array(
+      $waiting_participants[] = [
         'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
-      );
+      ];
     }
-    $this->line_items[] = array(
+    $this->line_items[] = [
       'amount' => $amount,
       'cost' => $cost,
       'event' => $event_in_cart->event,
@@ -322,7 +326,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       'num_waiting_participants' => count($waiting_participants),
       'waiting_participants' => $waiting_participants,
       'class' => $class,
-    );
+    ];
 
     $this->sub_total += $amount;
   }
@@ -344,19 +348,19 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     $country->find();
     $country->fetch();
     foreach ($this->line_items as & $line_item) {
-      $location_params = array('entity_id' => $line_item['event']->id, 'entity_table' => 'civicrm_event');
+      $location_params = ['entity_id' => $line_item['event']->id, 'entity_table' => 'civicrm_event'];
       $line_item['location'] = CRM_Core_BAO_Location::getValues($location_params, TRUE);
       CRM_Core_BAO_Address::fixAddress($line_item['location']['address'][1]);
     }
-    $send_template_params = array(
+    $send_template_params = [
       'table' => 'civicrm_msg_template',
       'contactId' => $this->payer_contact_id,
-      'from' => CRM_Core_BAO_Domain::getNameAndEmail(TRUE, TRUE),
+      'from' => current(CRM_Core_BAO_Domain::getNameAndEmail(TRUE, TRUE)),
       'groupName' => 'msg_tpl_workflow_event',
       'isTest' => FALSE,
       'toEmail' => $contact_details[1],
       'toName' => $contact_details[0],
-      'tplParams' => array(
+      'tplParams' => [
         'billing_name' => "{$params['billing_first_name']} {$params['billing_last_name']}",
         'billing_city' => $params["billing_city-{$this->_bltID}"],
         'billing_country' => $country->name,
@@ -376,11 +380,11 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
         'transaction_date' => $params['trxn_date'],
         'is_pay_later' => $this->is_pay_later,
         'pay_later_receipt' => $this->pay_later_receipt,
-      ),
+      ],
       'valueName' => 'event_registration_receipt',
       'PDFFilename' => ts('confirmation') . '.pdf',
-    );
-    $template_params_to_copy = array(
+    ];
+    $template_params_to_copy = [
       'billing_name',
       'billing_city',
       'billing_country',
@@ -390,7 +394,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       'credit_card_exp_date',
       'credit_card_type',
       'credit_card_number',
-    );
+    ];
     foreach ($template_params_to_copy as $template_param_to_copy) {
       $this->set($template_param_to_copy, $send_template_params['tplParams'][$template_param_to_copy]);
     }
@@ -408,7 +412,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
    * @return array|bool
    */
   public static function formRule($fields, $files, $self) {
-    $errors = array();
+    $errors = [];
 
     if ($self->payment_required && empty($self->_submitValues['is_pay_later'])) {
       CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
@@ -457,8 +461,8 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
 
     $main_participants = $this->cart->get_main_event_participants();
     foreach ($main_participants as $participant) {
-      $defaults = array();
-      $ids = array('contact_id' => $participant->contact_id);
+      $defaults = [];
+      $ids = ['contact_id' => $participant->contact_id];
       $contact = CRM_Contact_BAO_Contact::retrieve($ids, $defaults);
       $contact->is_deleted = 0;
       $contact->save();
@@ -466,18 +470,18 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
 
     $trxn_prefix = 'VR';
     if (array_key_exists('billing_contact_email', $params)) {
-      $this->payer_contact_id = self::find_or_create_contact($this->getContactID(), array(
+      $this->payer_contact_id = self::find_or_create_contact($this->getContactID(), [
         'email' => $params['billing_contact_email'],
         'first_name' => $params['billing_first_name'],
         'last_name' => $params['billing_last_name'],
         'is_deleted' => FALSE,
-      ));
+      ]);
 
       $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
         $this->payer_contact_id,
         'contact_type'
       );
-      $billing_fields = array(
+      $billing_fields = [
         "billing_first_name" => 1,
         "billing_middle_name" => 1,
         "billing_last_name" => 1,
@@ -488,7 +492,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
         "billing_country_id-{$this->_bltID}" => 1,
         "address_name-{$this->_bltID}" => 1,
         "email-{$this->_bltID}" => 1,
-      );
+      ];
 
       $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);
 
@@ -502,6 +506,8 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
         $ctype,
         TRUE
       );
+
+      $params['contact_id'] = $this->payer_contact_id;
     }
 
     $params['now'] = date('YmdHis');
@@ -547,7 +553,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     // n.b. we need to process the subparticipants before main event
     // participants so that session attendance can be included in the email
     $main_participants = $this->cart->get_main_event_participants();
-    $this->all_participants = array();
+    $this->all_participants = [];
     foreach ($main_participants as $main_participant) {
       $this->all_participants = array_merge($this->all_participants, $this->cart->get_subparticipants($main_participant));
     }
@@ -614,11 +620,11 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/cart_checkout', "_qf_Payment_display=1&qfKey={$this->controller->_key}", TRUE, NULL, FALSE));
     }
 
-    $trxnDetails = array(
+    $trxnDetails = [
       'trxn_id' => $result['trxn_id'],
       'trxn_date' => $result['now'],
       'currency' => CRM_Utils_Array::value('currencyID', $result),
-    );
+    ];
     return $trxnDetails;
   }
 
@@ -647,7 +653,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       $payer = $params['participant_contact_id'];
     }
 
-    $contribParams = array(
+    $contribParams = [
       'contact_id' => $payer,
       'financial_type_id' => $params['financial_type_id'],
       'receive_date' => $params['now'],
@@ -663,7 +669,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
       'payment_instrument_id' => $params['payment_instrument_id'],
       'check_number' => CRM_Utils_Array::value('check_number', $params),
       'skipLineItem' => 1,
-    );
+    ];
 
     if (is_array($this->_paymentProcessor)) {
       $contribParams['payment_processor'] = $this->_paymentProcessor['id'];
@@ -680,9 +686,9 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
    * Save data to session.
    */
   public function saveDataToSession() {
-    $session_line_items = array();
+    $session_line_items = [];
     foreach ($this->line_items as $line_item) {
-      $session_line_item = array();
+      $session_line_item = [];
       $session_line_item['amount'] = $line_item['amount'];
       $session_line_item['cost'] = $line_item['cost'];
       $session_line_item['event_id'] = $line_item['event']->id;
@@ -713,7 +719,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     $defaults["billing_country_id-{$this->_bltID}"] = $default_country->id;
 
     if (self::getContactID() && !self::is_administrator()) {
-      $params = array('id' => self::getContactID());
+      $params = ['id' => self::getContactID()];
       $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
 
       foreach ($contact->email as $email) {
@@ -761,7 +767,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
    */
   protected function apply_discount($discountCode, &$price_set_amount, &$cost, $event_id) {
     //need better way to determine if cividiscount installed
-    $autoDiscount = array();
+    $autoDiscount = [];
     $sql = "select is_active from civicrm_extension where name like 'CiviDiscount%'";
     $dao = CRM_Core_DAO::executeQuery($sql, '');
     while ($dao->fetch()) {