Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\Front;
- use App\Http\Controllers\FrontController;
- use Carbon\Carbon;
- use Auth;
- use DB;
- use Response;
- use Mail;
- use App\Models\Room;
- use App\Models\Hotel;
- use App\Models\Extra;
- use App\Models\Booking;
- use App\Models\Voucher;
- use App\Models\Promotion;
- use App\Models\BookingRoom;
- use App\Models\BookingExtra;
- use App\Models\GuestRequest;
- use App\Models\PurchasedVoucher;
- use App\Models\File;
- use App\Models\Feedback;
- use App\Models\Admin;
- use App\Models\MenuCategory;
- use App\Models\MenuItem;
- class IndexController extends FrontController
- {
- public function index() {
- return $this->ShowView('index', array(
- ));
- }
- private function is_admin() {
- if( Auth::guard('admin')->user() && Auth::guard('admin')->user()->hotel_id ) {
- return Auth::guard('admin')->user()->hotel;
- }
- return null;
- }
- private function getHotel() {
- $admin_hotel = $this->is_admin();
- if( $admin_hotel ) {
- return $admin_hotel;
- }
- if(session('hotel_id')) {
- return Hotel::find( session('hotel_id') );
- }
- $parse = parse_url( request()->server('HTTP_REFERER') );
- $domain = $this->get_domain( $parse['host'] );
- return Hotel::where('domain', 'LIKE', $domain)->first();
- }
- public function chatbot() {
- \App::setLocale( request('lang') );
- $ret = array(
- 'success' => true,
- );
- $hotel = $this->getHotel();
- if(!$hotel) {
- return Response::json( [
- 'success' => false,
- 'message' => 'Invalid Hotel',
- ] );
- } else if(!$hotel->disabled && !$this->is_admin()) {
- return Response::json( [
- 'success' => false,
- 'message' => 'Hotel Disabled',
- ] );
- }
- $story = config('chatbot.stories.'.request('story'));
- $step = config('chatbot.stories.'.request('story').'.steps.'.request('step'));
- if( $story && $step ) {
- $ret = $step;
- if( request('story')=='services' && (request('step')=='concierge' || request('step')=='start' ) ) {
- $file_fields = [
- 'hotel-map',
- 'rent-a-car',
- 'change',
- 'parking',
- 'activities',
- ];
- foreach ($ret['controllers'] as $k => $controller) {
- if(in_array($controller['step'], $file_fields)) {
- $list = File::where('hotel_id', $hotel->id)->where('item_name', $controller['step'])->get();
- if($list->isEmpty()) {
- unset($ret['controllers'][$k]);
- }
- }
- }
- } else if( request('story')=='room-service' && request('step')=='final' ) {
- $data = request('data');
- $details = '';
- $list = $data['room-service']['confirm'];
- $menuItems = MenuItem::whereIn('id', array_keys($list))->get();
- foreach ($menuItems as $menuItem) {
- $details .= $list[$menuItem->id].'x '.$menuItem->name
- # code...
- }
- $detailsSteps = [
- 'housekeeping-other',
- 'maintenance-other',
- 'newspaper',
- ];
- $details .= '<br/>'.$data['room-service']['comment']['comment'];
- $gr = new GuestRequest;
- $gr->hotel_id = $hotel->id;
- $gr->item_name = 'room-service';
- $gr->room_number = $data['room-service']['guest-info']['room_number'];
- $gr->guest_name = $data['room-service']['guest-info']['name'];
- $gr->details = $details;
- $gr->save();
- } else if( request('story')=='services' && request('step')=='done' ) {
- $data = request('data');
- if( isset( $data['services'][ $data['services']['start'] ] ) && !empty( $data['services']['guest-info'] ) ) {
- $details = '';
- $service = $data['services'][ $data['services']['start'] ];
- $detailsSteps = [
- 'housekeeping-other',
- 'maintenance-other',
- 'newspaper',
- ];
- if( in_array($service, $detailsSteps)!==false ) {
- $details = $data['services'][$service];
- }
- $gr = new GuestRequest;
- $gr->hotel_id = $hotel->id;
- $gr->item_name = $service;
- $gr->room_number = $data['services']['guest-info']['room_number'];
- $gr->guest_name = $data['services']['guest-info']['name'];
- $gr->details = $details;
- $gr->save();
- }
- } else if( request('story')=='welcome' && (request('step')=='start' || request('step')=='start-again') ) {
- foreach ($ret['controllers'] as $k => $controller) {
- if(!in_array($controller['story'], $hotel->menu_buttons)) {
- unset($ret['controllers'][$k]);
- }
- }
- } else if( request('story')=='checkin' && request('step')=='checkin-else' ) {
- $data = request('data');
- //dd($data);
- $booking_id = $data['checkin']['start']['id'];
- $booking = Booking::find($booking_id);
- $checkin = $data['checkin']['checkin-guests'];
- //dd($checkin, $booking->checkin_data);
- /*
- foreach ($checkin['guest_info'] as $gk => $gi) {
- foreach ($gi as $guest_num => $guest) {
- if(!empty($guest['checkin_email']) && empty($booking->checkin_data['guest_info'][$gk][$guest_num]['checkin_email'])) {
- $subject = 'Вашата регистрация';
- $receiver = $guest['checkin_email'];
- Mail::send('emails.chekcin', [
- 'hotel_name' => $hotel->name,
- 'hotel_privacy_policy' => $hotel->privacy_policy,
- 'hotel_website' => $hotel->website,
- 'hotel_address' => $hotel->address,
- 'hotel_coordinates' => $hotel->coordinates,
- 'hotel_phone' => $hotel->phone,
- 'hotel_phone2' => $hotel->phone2,
- 'hotel_email' => $hotel->email,
- 'hotel_email2' => $hotel->email2,
- ], function ($message) use ($subject, $receiver) {
- //$message->from($sender, $sender_name);
- $message->to( $receiver );
- $message->subject($subject);
- });
- }
- }
- }
- */
- $booking->checkin_data = $checkin;
- $flat = array();
- array_walk_recursive($checkin,function($v) use (&$flat){ $flat[] = $v; });
- //dd($data, $flat);
- $booking->checkin_status = array_search(null, $flat, true)!==false ? 'partial' : 'new';
- $booking->check_updated = Carbon::now();
- $booking->save();
- //////
- } else if( request('story')=='feedback' && request('step')=='end' ) {
- $data = request('data');
- $gr = new Feedback;
- $gr->hotel_id = $hotel->id;
- $gr->name = $data['feedback']['start']['name'];
- $gr->phone = $data['feedback']['start']['phone'];
- $gr->email = $data['feedback']['start']['email'];
- $gr->message = $data['feedback']['start']['message'];
- $gr->save();
- }
- foreach ($ret['controllers'] as $k => $controller) {
- if($controller['type']=='vouchers') {
- $ret['controllers'][$k]['vouchers'] = Voucher::where('hotel_id', $hotel->id)->get();
- foreach ($ret['controllers'][$k]['vouchers'] as $rid => $r) {
- $rarr = $r->toArray();
- $rarr['image'] = $r->getImageUrl();
- $ret['controllers'][$k]['vouchers'][$rid] = $rarr;
- }
- } else if($controller['type']=='room-service' || $controller['type']=='room-service-confirm') {
- $ret['controllers'][$k]['menu'] = MenuCategory::with('items')->where('hotel_id', $hotel->id)->get();
- foreach ($ret['controllers'][$k]['menu'] as $rid => $r) {
- $rarr = $r->toArray();
- $ret['controllers'][$k]['menu'][$rid] = $rarr;
- }
- } else if($controller['type']=='promos') {
- $ret['controllers'][$k]['promos'] = Promotion::where('hotel_id', $hotel->id)
- ->where('date_from', '<=', Carbon::now())
- ->where('to_date', '>=', Carbon::now())
- ->where('max_number_uses', '>=', 'times_used')
- ->get();
- foreach ($ret['controllers'][$k]['promos'] as $rid => $r) {
- $rarr = $r->toArray();
- $rarr['image'] = $r->getImageUrl();
- $ret['controllers'][$k]['promos'][$rid] = $rarr;
- }
- } else if($controller['type']=='rooms') {
- $ret['controllers'][$k]['rooms'] = Room::where('hotel_id', $hotel->id)->get();
- $prices = $hotel->getPricesForPeriod( request()->input('data.bookings.start'), request()->input('data.bookings.booking_out') );
- foreach ($ret['controllers'][$k]['rooms'] as $rid => $r) {
- if(!isset($prices[$r->id])) {
- unset( $ret['controllers'][$k]['rooms'][$rid] );
- continue;
- }
- $rarr = $r->toArray();
- $rarr['image'] = $r->getImageUrl();
- $rarr['prices'] = $prices[$r->id];
- $ret['controllers'][$k]['rooms'][$rid] = $rarr;
- }
- } else if($controller['type']=='files') {
- $list = File::where('hotel_id', $hotel->id)->where('item_name', $controller['item'])->where('lang', \App::getLocale())->get();
- $ret['controllers'][$k]['files'] = [];
- foreach ($list as $file) {
- $ret['controllers'][$k]['files'][] = [
- 'url' => $file->getImageUrl(),
- 'thumb' => $file->getImageUrl(true),
- 'image' => $file->isImage(),
- 'file_name' => $file->file_name,
- ];
- }
- } else if($controller['type']=='calendar' && request('story')=='promos') {
- $data = request('data');
- $pid = $data['promos']['start']['id'];
- $promo = Promotion::find($pid);
- //dd($promo);
- $dates = [];
- $start = new Carbon($promo->date_from);
- $end = new Carbon($promo->to_date);
- $wdays = [
- 'monday','tuesday','wednesday','thursday','friday','saturday','sunday'
- ];
- while( $start->timestamp<=$end->timestamp ) {
- $weekday = $wdays[$start->dayOfWeek];
- if(in_array($weekday, $promo->days_of_week)) {
- $dates[] = $start->toDateString();
- } else {
- $ok = $promo->room->getPriceForPeriod( $start, $start->copy()->addDays($promo->days_number) );
- if(empty($ok)) {
- $dates[] = $start->toDateString();
- }
- }
- $start->addDays(1);
- }
- $ret['controllers'][$k]['unavailable'] = $dates;
- $ret['controllers'][$k]['start'] = $promo->date_from->toDateString();
- $ret['controllers'][$k]['end'] = $promo->to_date->toDateString();
- $ret['controllers'][$k]['duration'] = $promo->days_number;
- } else if($controller['type']=='check-in') {
- $data = request('data');
- $booking_id = $data['checkin']['start']['id'];
- $booking = Booking::where('id', $booking_id)->where('hotel_id', $hotel->id)->whereIn('checkin_status', ['pending', 'partial'])->first();
- if(!empty( $booking )) {
- $ret['controllers'][$k]['rooms'] = [];
- $ret['controllers'][$k]['config'] = [
- 'rooms' => [],
- 'guests' => [],
- ];
- $fields = [
- "checkin_name",
- "checkin_email",
- "checkin_phone",
- "checkin_address",
- "checkin_id",
- ];
- foreach ($fields as $f) {
- if( $hotel->$f=='room' ) {
- $ret['controllers'][$k]['config']['rooms'][] = $f;
- } else if( $hotel->$f=='guest' ) {
- $ret['controllers'][$k]['config']['guests'][] = $f;
- }
- }
- foreach ($booking->rooms as $br) {
- $br_info = [
- 'guest_count' => $br->adults + $br->children + $br->infants,
- 'name' => $br->room->name
- ];
- $ret['controllers'][$k]['rooms'][] = $br_info;
- }
- $ret['controllers'][$k]['info'] = $booking->checkin_data;
- }
- } else if($controller['type']=='extras') {
- $ret['controllers'][$k]['items'] = Extra::where('hotel_id', $hotel->id)->get()->toArray();
- }
- }
- $ret['nextvideo'] = null;
- if( count($ret['controllers'])==1 ) {
- $nextStory = !empty( current($ret['controllers'])['story'] ) ? current($ret['controllers'])['story'] : request('story');
- $nextStep = !empty( current($ret['controllers'])['step'] ) ? current($ret['controllers'])['step'] : 'start';
- $ret['nextvideo'] = config('chatbot.stories.'.$nextStory.'.steps.'.$nextStep.'.video');
- }
- if(!empty($ret['video'])) {
- $ret['video'] = url('/front/video/LANGPLACEHOLDER/'.$ret['video'].'.mp4');
- }
- if(!empty($ret['nextvideo'])) {
- $ret['nextvideo'] = url('/front/video/LANGPLACEHOLDER/'.$ret['nextvideo'].'.mp4');
- }
- } else {
- $step = config('chatbot.stories.'.request('story').'.steps.'.request('step'));
- $ret['success'] = false;
- }
- if( request()->input('config') ) {
- $ret['config'] = $hotel->toArray();
- $ret['config']['debug'] = $this->is_admin();
- $ret['config']['captcha_key'] = '6LfBlIoUAAAAAB88HAzexnaUAKrneEYZGSVVe_KD';
- $ret['config']['sentences'] = [
- 'adults' => [
- 'bg' => 'Възрастни',
- 'en' => 'Adults',
- ],
- 'children' => [
- 'bg' => 'Деца до '.$ret['config']['child_age'].'г.',
- 'en' => 'Children up to '.$ret['config']['child_age'].' y.o.',
- ],
- 'infants' => [
- 'bg' => 'Бебета до '.$ret['config']['infant_age'].'г.',
- 'en' => 'Babies up to '.$ret['config']['infant_age'].' y.o.',
- ],
- 'room_price' => [
- 'bg' => 'Цена за стаята',
- 'en' => 'Room price',
- ],
- 'remaining' => [
- 'bg' => 'остават',
- 'en' => 'remaining',
- ],
- 'room' => [
- 'bg' => 'Стая',
- 'en' => 'Room',
- ],
- 'guest' => [
- 'bg' => 'Гост',
- 'en' => 'Guest',
- ],
- 'id_documents' => [
- [
- 'bg' => 'Лична карта',
- 'en' => 'ID Card',
- ],
- [
- 'bg' => 'Международен паспорт',
- 'en' => 'International Passport',
- ]
- ],
- 'weights' => [
- 'ml' => [
- 'bg' => 'мл',
- 'en' => 'ml',
- ],
- 'gr' => [
- 'bg' => 'гр',
- 'en' => 'gr',
- ],
- 'pcs' => [
- 'bg' => 'бр',
- 'en' => 'pcs',
- ],
- ],
- 'checkin_address' => [
- 'bg' => 'Адрес',
- 'en' => 'Address',
- ],
- 'checkin_phone' => [
- 'bg' => 'Телефонен номер',
- 'en' => 'Phone number',
- ],
- 'checkin_email' => [
- 'bg' => 'Email адрес',
- 'en' => 'Email address',
- ],
- 'checkin_name' => [
- 'bg' => 'Име и Фамилия',
- 'en' => 'Names',
- ],
- 'checkin_id_id' => [
- 'bg' => 'Номер на документа',
- 'en' => 'Document Number',
- ],
- 'checkin_id_type' => [
- 'bg' => 'Документ за самоличност',
- 'en' => 'Type of document',
- ],
- 'checkin_id_nationality' => [
- 'bg' => 'Гражданство',
- 'en' => 'Nationality',
- ],
- 'not_bot' => [
- 'bg' => 'Моля, потвърдете, че не сте робот',
- 'en' => 'Please, prove you are not a robot',
- ],
- 'checkin-early' => [
- 'bg' => 'Ранно настаняване',
- 'en' => 'Early Check-in',
- ],
- 'checkout-late' => [
- 'bg' => 'Късно напускане',
- 'en' => 'Late Check-out',
- ],
- 'before' => [
- 'bg' => 'преди',
- 'en' => 'before',
- ],
- 'after' => [
- 'bg' => 'след',
- 'en' => 'after',
- ],
- 'total' => [
- 'bg' => 'Общо',
- 'en' => 'Total',
- ],
- ];
- unset($ret['config']['payment_secret']);
- unset($ret['config']['payment_password']);
- }
- return Response::json($ret);
- }
- public function pay() {
- $ret = array(
- 'success' => true,
- );
- $hotel = $this->getHotel();
- $input = request()->input();
- //dd($input);
- $type = null;
- $total = 0;
- $room_id = null;
- if(!empty($input['data']['bookings']['rooms'])) {
- $type = 'booking';
- $rooms = $input['data']['bookings']['rooms'];
- foreach ($rooms as $k => $room_info) {
- $room_id = $room_info['room_id'];
- $room = Room::find($room_info['room_id']);
- $prices = $room->getPriceForPeriod( request()->input('data.bookings.start'), request()->input('data.bookings.booking_out') );
- $room_total = $room_info['adults']==1 ? $prices['base_double'] : $prices['base'];
- if($room_info['adults'] > $room->capacity) {
- $room_total += $prices['adult'] * ($room_info['adults'] - $room->capacity);
- }
- if($room_info['children'] > $room->capacity) {
- $room_total += $prices['child'] * ($room_info['children'] - $room->capacity);
- }
- if($room_info['checkin']) {
- $room_total += $prices['extra_checkin'];
- }
- if($room_info['checkout']) {
- $room_total += $prices['extra_checkout'];
- }
- $total += $room_total;
- $rooms[$k]['name'] = $room->name;
- $rooms[$k]['price'] = $room_total;
- }
- $extras = $input['data']['bookings']['extras'];
- $names = $input['data']['bookings']['names'];
- $date_in = request()->input('data.bookings.start');
- $date_out = request()->input('data.bookings.booking_out');
- $payment_token = request()->input('data.bookings.payment.token');
- $item_name = 'Резервация в Хотел '.$hotel->name;
- } else if(!empty($input['data']['promos']['start'])) {
- $type = 'booking';
- $promo = Promotion::find($input['data']['promos']['start']['id']);
- $rooms = [];
- for($i=0;$i<$input['data']['promos']['start']['amount'];$i++) {
- $rooms[] = [
- "room_id" => $promo->room_id,
- "name" => $promo->room->name,
- "adults" => $promo->room->capacity,
- "children" => 0,
- "infants" => 0,
- "price" => $promo->discount_price
- ];
- }
- $total += $promo->discount_price*$input['data']['promos']['start']['amount'];
- $room_id = $promo->room_id;
- $extras = $input['data']['promos']['extras'];
- $names = $input['data']['promos']['names'];
- $date_in = request()->input('data.promos.checkin');
- $cin = new Carbon( request()->input('data.promos.checkin') );
- $date_out = $cin->addDays($promo->days_number)->toDateString();
- $payment_token = request()->input('data.promos.payment.token');
- $item_name = 'Промо Резервация в Хотел '.$hotel->name;
- } else if(!empty($input['data']['vouchers']['start'])) {
- $type = 'voucher';
- $voucher = Voucher::find($input['data']['vouchers']['start']);
- $total = $voucher->price;
- $names = $input['data']['vouchers']['names'];
- $payment_token = request()->input('data.vouchers.payment.token');
- $item_name = 'Подаръчен Ваучер от Хотел '.$hotel->name;
- } else {
- return Response::json( [
- 'success' => false
- ] );;
- }
- if( $type=='booking' ) {
- foreach ($extras as $e_id => $amount) {
- if(intval( $amount )) {
- $extra = Extra::find($e_id);
- $total += intval( $amount ) * $extra->price;
- }
- }
- }
- try {
- if( $hotel->payment=='pay' ) {
- if($hotel->payment_method == 'stripe') {
- \Stripe\Stripe::setApiKey ( $hotel->payment_secret );
- \Stripe\Charge::create ( array (
- "amount" => $total*100,
- "currency" => $hotel->currency,
- "source" => $payment_token, // obtained with Stripe.js
- "description" => $item_name
- ) );
- } else if($hotel->payment_method == 'paylane') {
- $paylaneClient = new PayLaneRestClient($hotel->payment_secret, $hotel->payment_password);
- $card_params = array(
- 'sale' => array(
- 'amount' => $total,
- 'currency' => $hotel->currency,
- 'description' => $item_name
- ),
- 'customer' => array(
- 'email' => $names['email'],
- 'ip' => request()->ip(),
- ),
- 'card' => array(
- 'token' => $payment_token,
- ),
- );
- try {
- $status = $paylaneClient->cardSaleByToken($card_params);
- } catch (Exception $e) {
- return Response::json( [
- 'success' => false,
- 'message' => $e->getMessage()
- ] );
- }
- // checking transaction status example (optional):
- if ($paylaneClient->isSuccess()) {
- // $status['id_sale']
- // Cool
- } else {
- return Response::json( [
- 'success' => false,
- 'message' => $status['error']['error_description'].' ('.$status['error']['id_error'].' - '.$status['error']['error_number'].')'
- ] );
- }
- }
- }
- } catch ( \Exception $e ) {
- return Response::json( [
- 'success' => false,
- 'message' => $e->getMessage()
- ] );
- }
- $payment_id = null;
- if( $type=='booking' ) {
- $booking = new Booking;
- $booking->name = $names['name'];
- $booking->email = $names['email'];
- $booking->phone = $names['phone'];
- $booking->note = $names['note'];
- $booking->hotel_id = $hotel->id;
- $booking->room_id = $room_id;
- $booking->price = $total;
- $booking->check_in = $date_in;
- $booking->check_out = $date_out;
- $booking->payment_method = $hotel->payment_method;
- $booking->save();
- $booking->setReservationId();
- $payment_id = $booking->reservation_id;
- foreach ($rooms as $room_info) {
- $booking_room = new BookingRoom;
- $booking_room->booking_id = $booking->id;
- $booking_room->room_id = $room_info['room_id'];
- $booking_room->adults = $room_info['adults'];
- $booking_room->children = $room_info['children'];
- $booking_room->infants = $room_info['infants'];
- $booking_room->checkin = $room_info['checkin'];
- $booking_room->checkout = $room_info['checkout'];
- $booking_room->save();
- }
- $extras_list = [];
- foreach ($extras as $e_id => $amount) {
- if(intval( $amount )) {
- $booking_extra = new BookingExtra;
- $booking_extra->booking_id = $booking->id;
- $booking_extra->extra_id = $e_id;
- $booking_extra->amount = $amount;
- $booking_extra->save();
- $extra = Extra::find($e_id);
- $extras_list[] = [
- 'name' => $extra->name,
- 'amount' => $amount,
- 'price' => $extra->price,
- 'total' => $extra->price * intval($amount),
- ];
- }
- }
- $start = new Carbon( $date_in );
- $duration = $start->diffInDays( new Carbon( $date_out ) );
- $rooms_list_arr = [];
- $rooms_list = [];
- foreach ($rooms as $room) {
- if(!isset($rooms_list_arr[$room['name']])) {
- $rooms_list_arr[$room['name']] = 0;
- }
- $rooms_list_arr[$room['name']] ++;
- }
- foreach ($rooms_list_arr as $key => $value) {
- $rooms_list[] = $value.'x '.$key;
- }
- $rooms_list = implode(', ', $rooms_list);
- $reservation_number = $booking->getReservationNumber();
- $subject = 'Вашата резервация';
- $receiver = $booking->email;
- Mail::send('emails.booking', [
- 'hotel_name' => $hotel->name,
- 'hotel_privacy_policy' => $hotel->privacy_policy,
- 'hotel_website' => $hotel->website,
- 'hotel_address' => $hotel->address,
- 'hotel_coordinates' => $hotel->coordinates,
- 'hotel_phone' => $hotel->phone,
- 'hotel_phone2' => $hotel->phone2,
- 'hotel_email' => $hotel->email,
- 'hotel_email2' => $hotel->email2,
- 'name' => $booking->name,
- 'room_count' => count($rooms),
- 'duration' => $duration,
- 'check_in' => $date_in,
- 'check_out' => $date_out,
- 'rooms' => $rooms,
- 'extras' => $extras_list,
- 'currency' => $hotel->currency,
- 'total_price' => $total,
- 'rooms_list' => $rooms_list,
- 'reservation_number' => $reservation_number,
- ], function ($message) use ($subject, $receiver) {
- //$message->from($sender, $sender_name);
- $message->to( $receiver );
- $message->subject($subject);
- });
- /*
- $notify = Admin::where('hotel_id', $hotel->id)->get();
- $notify_content = 'Току що получих нова резервация. Подробности можеш да видиш тук: http://eva.youpluswe.com/bookings';
- foreach ($notify as $nuser) {
- $admin_email = $nuser->email;
- Mail::raw($notify_content, function ($message) use ($admin_email) {
- $message->to($admin_email);
- $message->subject('Нова резервация');
- });
- }
- */
- } else if( $type=='voucher' ) {
- $pvoucher = new PurchasedVoucher;
- $pvoucher->hotel_id = $hotel->id;
- $pvoucher->voucher_id = $voucher->id;
- $pvoucher->name = $names['name'];
- $pvoucher->price = $total;
- $pvoucher->display_name = $names['display_name'];
- $pvoucher->email = $names['email'];
- $pvoucher->phone = $names['phone'];
- $pvoucher->receiver_name = $names['receiver_name'];
- $pvoucher->message = $names['message'];
- $pvoucher->payment_method = $hotel->payment_method;
- $pvoucher->save();
- $pvoucher->setReservationId();
- $pvoucher->generateImage();
- $payment_id = $pvoucher->reservation_id;
- $subject = 'Вашият ваучер';
- $receiver = $pvoucher->email;
- $duration = Carbon::now()->addMonths( $voucher->validity )->toDateString();
- Mail::send('emails.voucher', [
- 'hotel_name' => $hotel->name,
- 'hotel_website' => $hotel->website,
- 'hotel_address' => $hotel->address,
- 'hotel_coordinates' => $hotel->coordinates,
- 'hotel_phone' => $hotel->phone,
- 'hotel_phone2' => $hotel->phone2,
- 'hotel_email' => $hotel->email,
- 'hotel_email2' => $hotel->email2,
- 'name' => $pvoucher->receiver_name,
- 'service_name' => $voucher->name,
- 'voucher_message' => $pvoucher->message,
- 'duration' => $duration,
- 'voucher_link' => $pvoucher->getImage(),
- ], function ($message) use ($subject, $receiver) {
- //$message->from($sender, $sender_name);
- $message->to( $receiver );
- $message->subject($subject);
- });
- }
- return Response::json( [
- 'success' => true,
- 'total' => $total,
- 'id' => $payment_id
- ] );
- }
- public function booking_id() {
- $ret = array(
- 'success' => false,
- );
- $hotel = $this->getHotel();
- if( request()->input('booking_id') ) {
- $bid = request()->input('booking_id');
- $bid = preg_replace("/[^0-9]/", "", $bid );
- $bid = ltrim($bid, '0');
- $booking = Booking::where('reservation_id', $bid)->where('hotel_id', $hotel->id)->first();
- if( $booking ) {
- $ret['success'] = true;
- $ret['booking'] = $booking->toArray();
- $ret['booking']['rooms'] = $booking->rooms->toArray();
- }
- }
- return Response::json($ret);
- }
- private function get_domain($domain, $debug = false)
- {
- $original = $domain = strtolower($domain);
- if (filter_var($domain, FILTER_VALIDATE_IP)) { return $domain; }
- $debug ? print('<strong style="color:green">»</strong> Parsing: '.$original) : false;
- $arr = array_slice(array_filter(explode('.', $domain, 4), function($value){
- return $value !== 'www';
- }), 0); //rebuild array indexes
- if (count($arr) > 2)
- {
- $count = count($arr);
- $_sub = explode('.', $count === 4 ? $arr[3] : $arr[2]);
- $debug ? print(" (parts count: {$count})") : false;
- if (count($_sub) === 2) // two level TLD
- {
- $removed = array_shift($arr);
- if ($count === 4) // got a subdomain acting as a domain
- {
- $removed = array_shift($arr);
- }
- $debug ? print("<br>\n" . '[*] Two level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false;
- }
- elseif (count($_sub) === 1) // one level TLD
- {
- $removed = array_shift($arr); //remove the subdomain
- if (strlen($_sub[0]) === 2 && $count === 3) // TLD domain must be 2 letters
- {
- array_unshift($arr, $removed);
- }
- else
- {
- // non country TLD according to IANA
- $tlds = array(
- 'aero',
- 'arpa',
- 'asia',
- 'biz',
- 'cat',
- 'com',
- 'coop',
- 'edu',
- 'gov',
- 'info',
- 'jobs',
- 'mil',
- 'mobi',
- 'museum',
- 'name',
- 'net',
- 'org',
- 'post',
- 'pro',
- 'tel',
- 'travel',
- 'xxx',
- );
- if (count($arr) > 2 && in_array($_sub[0], $tlds) !== false) //special TLD don't have a country
- {
- array_shift($arr);
- }
- }
- $debug ? print("<br>\n" .'[*] One level TLD: <strong>'.join('.', $_sub).'</strong> ') : false;
- }
- else // more than 3 levels, something is wrong
- {
- for ($i = count($_sub); $i > 1; $i--)
- {
- $removed = array_shift($arr);
- }
- $debug ? print("<br>\n" . '[*] Three level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false;
- }
- }
- elseif (count($arr) === 2)
- {
- $arr0 = array_shift($arr);
- if (strpos(join('.', $arr), '.') === false
- && in_array($arr[0], array('localhost','test','invalid')) === false) // not a reserved domain
- {
- $debug ? print("<br>\n" .'Seems invalid domain: <strong>'.join('.', $arr).'</strong> re-adding: <strong>'.$arr0.'</strong> ') : false;
- // seems invalid domain, restore it
- array_unshift($arr, $arr0);
- }
- }
- $debug ? print("<br>\n".'<strong style="color:gray">«</strong> Done parsing: <span style="color:red">' . $original . '</span> as <span style="color:blue">'. join('.', $arr) ."</span><br>\n") : false;
- return join('.', $arr);
- }
- }
- /**
- * Client library for Paylane REST Server.
- * More info at http://devzone.paylane.com
- */
- class PayLaneRestClient
- {
- /**
- * @var string
- */
- protected $api_url = 'https://direct.paylane.com/rest/';
- /**
- * @var string
- */
- protected $username = null, $password = null;
- /**
- * @var array
- */
- protected $http_errors = array
- (
- 400 => '400 Bad Request',
- 401 => '401 Unauthorized',
- 500 => '500 Internal Server Error',
- 501 => '501 Not Implemented',
- 502 => '502 Bad Gateway',
- 503 => '503 Service Unavailable',
- 504 => '504 Gateway Timeout',
- );
- /**
- * @var bool
- */
- protected $is_success = false;
- /**
- * @var array
- */
- protected $allowed_request_methods = array(
- 'get',
- 'put',
- 'post',
- 'delete',
- );
- /**
- * @var boolean
- */
- protected $ssl_verify = true;
- /**
- * Constructor
- *
- * @param string $username Username
- * @param string $password Password
- */
- public function __construct($username, $password)
- {
- $this->username = $username;
- $this->password = $password;
- $validate_params = array
- (
- false === extension_loaded('curl') => 'The curl extension must be loaded for using this class!',
- false === extension_loaded('json') => 'The json extension must be loaded for using this class!'
- );
- $this->checkForErrors($validate_params);
- }
- /**
- * Set Api URL
- *
- * @param string $url Api URL
- */
- public function setUrl($url)
- {
- $this->api_url = $url;
- }
- /**
- * Sets SSL verify
- *
- * @param bool $ssl_verify SSL verify
- */
- public function setSSLverify($ssl_verify)
- {
- $this->ssl_verify = $ssl_verify;
- }
- /**
- * Request state getter
- *
- * @return bool
- */
- public function isSuccess()
- {
- return $this->is_success;
- }
- /**
- * Performs card sale
- *
- * @param array $params Sale Params
- * @return array
- */
- public function cardSale($params)
- {
- return $this->call(
- 'cards/sale',
- 'post',
- $params
- );
- }
- /**
- * Performs card sale by token
- *
- * @param array $params Sale Params
- * @return array
- */
- public function cardSaleByToken($params)
- {
- return $this->call(
- 'cards/saleByToken',
- 'post',
- $params
- );
- }
- /**
- * Card authorization
- *
- * @param array $params Authorization params
- * @return array
- */
- public function cardAuthorization($params)
- {
- return $this->call(
- 'cards/authorization',
- 'post',
- $params
- );
- }
- /**
- * Card authorization by token
- *
- * @param array $params Authorization params
- * @return array
- */
- public function cardAuthorizationByToken($params)
- {
- return $this->call(
- 'cards/authorizationByToken',
- 'post',
- $params
- );
- }
- /**
- * PayPal authorization
- *
- * @param $params
- * @return array
- */
- public function paypalAuthorization($params)
- {
- return $this->call(
- 'paypal/authorization',
- 'post',
- $params
- );
- }
- /**
- * Performs capture from authorized card
- *
- * @param array $params Capture authorization params
- * @return array
- */
- public function captureAuthorization($params)
- {
- return $this->call(
- 'authorizations/capture',
- 'post',
- $params
- );
- }
- /**
- * Performs closing of card authorization, basing on authorization card ID
- *
- * @param array $params Close authorization params
- * @return array
- */
- public function closeAuthorization($params)
- {
- return $this->call(
- 'authorizations/close',
- 'post',
- $params
- );
- }
- /**
- * Performs refund
- *
- * @param array $params Refund params
- * @return array
- */
- public function refund($params)
- {
- return $this->call(
- 'refunds',
- 'post',
- $params
- );
- }
- /**
- * Get sale info
- *
- * @param array $params Get sale info params
- * @return array
- */
- public function getSaleInfo($params)
- {
- return $this->call(
- 'sales/info',
- 'get',
- $params
- );
- }
- /**
- * Get sale authorization info
- *
- * @param array $params Get sale authorization info params
- * @return array
- */
- public function getAuthorizationInfo($params)
- {
- return $this->call(
- 'authorizations/info',
- 'get',
- $params
- );
- }
- /**
- * Performs sale status check
- *
- * @param array $params Check sale status
- * @return array
- */
- public function checkSaleStatus($params)
- {
- return $this->call(
- 'sales/status',
- 'get',
- $params
- );
- }
- /**
- * Direct debit sale
- *
- * @param array $params Direct debit params
- * @return array
- */
- public function directDebitSale($params)
- {
- return $this->call(
- 'directdebits/sale',
- 'post',
- $params
- );
- }
- /**
- * Sofort sale
- *
- * @param array $params Sofort params
- * @return array
- */
- public function sofortSale($params)
- {
- return $this->call(
- 'sofort/sale',
- 'post',
- $params
- );
- }
- /**
- * iDeal sale
- *
- * @param $params iDeal transaction params
- * @return array
- */
- public function idealSale($params)
- {
- return $this->call(
- 'ideal/sale',
- 'post',
- $params
- );
- }
- /**
- * iDeal banks list
- *
- * @return array
- */
- public function idealBankCodes()
- {
- return $this->call(
- 'ideal/bankcodes',
- 'get',
- array()
- );
- }
- /**
- * Bank transfer sale
- *
- * @param array $params Bank transfer sale params
- * @return array
- */
- public function bankTransferSale($params)
- {
- return $this->call(
- 'banktransfers/sale',
- 'post',
- $params
- );
- }
- /**
- * PayPal sale
- *
- * @param array $params Paypal sale params
- * @return array
- */
- public function paypalSale($params)
- {
- return $this->call(
- 'paypal/sale',
- 'post',
- $params
- );
- }
- /**
- * Cancels Paypal recurring profile
- *
- * @param array $params Paypal params
- * @return array
- */
- public function paypalStopRecurring($params)
- {
- return $this->call('paypal/stopRecurring',
- 'post',
- $params
- );
- }
- /**
- * Performs resale by sale ID
- *
- * @param array $params Resale by sale params
- * @return array
- */
- public function resaleBySale($params)
- {
- return $this->call(
- 'resales/sale',
- 'post',
- $params
- );
- }
- /**
- * Performs resale by authorization ID
- *
- * @param array $params Resale by authorization params
- * @return array
- */
- public function resaleByAuthorization($params)
- {
- return $this->call(
- 'resales/authorization',
- 'post',
- $params
- );
- }
- /**
- * Checks if a card is enrolled in the 3D-Secure program.
- *
- * @param array $params Is card 3d secure params
- * @return array
- */
- public function checkCard3DSecure($params)
- {
- return $this->call(
- '3DSecure/checkCard',
- 'get',
- $params
- );
- }
- /**
- * Checks if a card is enrolled in the 3D-Secure program, based on the card's token.
- *
- * @param array $params Is card 3d secure params
- * @return array
- */
- public function checkCard3DSecureByToken($params)
- {
- return $this->call(
- '3DSecure/checkCardByToken',
- 'get',
- $params
- );
- }
- /**
- * Performs sale by ID 3d secure authorization
- *
- * @param array $params Sale by 3d secure authorization params
- * @return array
- */
- public function saleBy3DSecureAuthorization($params)
- {
- return $this->call(
- '3DSecure/authSale',
- 'post',
- $params
- );
- }
- /**
- * Perform check card
- *
- * @param array $params Check card params
- * @return array
- */
- public function checkCard($params)
- {
- return $this->call(
- 'cards/check',
- 'get',
- $params
- );
- }
- /**
- * Perform check card by token
- *
- * @param array $params Check card params
- * @return array
- */
- public function checkCardByToken($params)
- {
- return $this->call(
- 'cards/checkByToken',
- 'get',
- $params
- );
- }
- /**
- * Performs Apple Pay sale
- *
- * @param array $params Apple Pay sale params
- * @return array
- */
- public function applePaySale(array $params)
- {
- return $this->call(
- 'applepay/sale',
- 'post',
- $params
- );
- }
- /**
- * Performs Apple Pay authorization
- *
- * @param array $params Apple Pay authorization params
- * @return array
- */
- public function applePayAuthorization(array $params)
- {
- return $this->call(
- 'applepay/authorization',
- 'post',
- $params
- );
- }
- /**
- * Method responsible for preparing, setting state and returning answer from rest server
- *
- * @param string $method
- * @param string $request
- * @param array $params
- * @return array
- */
- protected function call($method, $request, $params)
- {
- $this->is_success = false;
- if (is_object($params))
- {
- $params = (array) $params;
- }
- $validate_params = array
- (
- false === is_string($method) => 'Method name must be string',
- false === $this->checkRequestMethod($request) => 'Not allowed request method type',
- );
- $this->checkForErrors($validate_params);
- $params_encoded = json_encode($params);
- $response = $this->pushData($method, $request, $params_encoded);
- $response = json_decode($response, true);
- if (isset($response['success']) && $response['success'] === true)
- {
- $this->is_success = true;
- }
- return $response;
- }
- /**
- * Checking error mechanism
- *
- * @param array $validate_params
- * @throws \Exception
- */
- protected function checkForErrors($validate_params)
- {
- foreach ($validate_params as $key => $error)
- {
- if ($key)
- {
- throw new \Exception($error);
- }
- }
- }
- /**
- * Check if method is allowed
- *
- * @param string $method_type
- * @return bool
- */
- protected function checkRequestMethod($method_type)
- {
- $request_method = strtolower($method_type);
- if(in_array($request_method, $this->allowed_request_methods))
- {
- return true;
- }
- return false;
- }
- /**
- * Method responsible for pushing data to REST server
- *
- * @param string $method
- * @param string $method_type
- * @param string $request - JSON
- * @return array
- * @throws \Exception
- */
- protected function pushData($method, $method_type, $request)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $this->api_url . $method);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
- curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method_type));
- curl_setopt($ch, CURLOPT_HTTPAUTH, 1);
- curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl_verify);
- $response = curl_exec($ch);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if (isset($this->http_errors[$http_code]))
- {
- throw new \Exception('Response Http Error - ' . $this->http_errors[$http_code]);
- }
- if (0 < curl_errno($ch))
- {
- throw new \Exception('Unable to connect to ' . $this->api_url . ' Error: ' . curl_error($ch));
- }
- curl_close($ch);
- return $response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement