/** * Instert a new booking */ public function newBookig() { // init post validation $valid = true; $app = CHClient::getApp(); $config = CHClient::getConfig(); $post = CHLib::input()->post; // build the new booking request $request = (object) []; $request->hotel_id = $post->getUint('hotel_id'); // required string fields foreach (['start_date', 'end_date', 'first_name', 'last_name', 'email', 'phone'] as $field) { $request->{$field} = $post->getString($field); if (strlen($request->{$field}) <= 2) { $valid = false; $errors[] = $field; } } // not required strings foreach (['country', 'promo_code', 'checkin_time', 'requests', 'confirm', 'lang', 'currency'] as $field) { $request->{$field} = $post->getString($field); } // bool fields $request->pay_full = $post->get('pay_full') ? 1 : 0; $request->newsletter = $post->get('newsletter') ? 1 : 0; // collect card info // @todo server validation if ($config->confirm_card_hosted) { // set confirmation method for api request $request->confirm = 'card_hosted'; // init card object $card = (object) []; $card->type = $post->get('card_type'); $card->cvc = $post->get('card_cvc'); $card->expiration_date = $post->get('card_expiration_month') . '/' . $post->get('card_expiration_year'); // split card number details for security $card_number = (int) str_replace(' ', '', $post->getString('card_number')); $request->card_number = substr($card_number, 0, -4) . '****'; $card->number = '**** **** **** ' . substr($card_number, -4); } // invalid data, try again if (!$valid) { $app->setUserState('chclient.booking_request', $request); $app->setUserState('chclient.booking_errors', $errors); return false; } // attach rooms array $request->rooms = []; $party = CHClient::loadParty($post->get('party')); list($rates, $boards) = CHClient::loadRoomsFromRequest($post->get('rooms')); $extras = CHClient::loadExtrasFromRequest($post->get('extras')); foreach ($party as $i => $r_party) { $room = (object) ['party' => $r_party]; $room->rate = $rates[$i]; $room->board = $boards[$i]; $room->extras = isset($extras[$i]) ? $extras[$i] : []; $room->guest = (object) ['name' => $post->getString('guest_' . $i), 'bed' => $post->get('bed_' . $i), 'smoking' => $post->get('smoking_' . $i)]; $request->rooms[] = $room; } // attach app object $user = JFactory::getUser(); $request->app = (object) []; $request->app->app_id = $config->data_source_app_id; $request->app->user_id = $user->guest ? null : $user->id; $request->app->user_ip = CHLib::getIp(); // attach tracking object $request->tracking = json_decode($app->getUserState('plg_racking.tracking', 'null')); // init request $api_request = $this->apiRequest('booking_add', $request); // check for errors if ($api_request->errors->errors) { $app->setUserState('chclient.api_errors', $api_request->errors); return false; } // load booking $booking = $api_request->response; // confirmed & pending bookings (1x, 2x) if ($booking->booking_status < 30) { // send notification CHClientBooking::emailNotification($booking, $card); } else { // set booking confirm submit $app->setUserState('chclient.submit_confirm', true); } // set new booking state (for analytics tracking) $app->setUserState('chclient.new_booking', true); // set booking data state for late use in booking view $app->setUserState('chclient.booking', $booking); return true; }