File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/AbonadosController.php
<?php
namespace App\Http\Controllers;
use App\Core\Payment\PaymentStatusEnum;
use App\Core\Ticket\TicketTypesEnum;
use App\CorporateIdentity;
use DB;
use App\User;
use App\Parameter;
use App\TicketMain;
use App\PreSubscriber;
use Carbon\Carbon;
use App\Http\Controllers\Api\TicketApiController;
use App\Http\Controllers\Soap\Types\Ticket;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AbonadosController extends Controller
{
public function index()
{
$corporateIdentity = CorporateIdentity::first();
$parameters = Parameter::select('web_presuscription')->first();
if ($parameters->web_presuscription)
return view('subscriber.abonados');
else
return view('subscriber.notAvailable', compact('corporateIdentity'));
}
public function validation(Request $request, $document = null)
{
$document = $request->input('document', $document);
$matchEventId = $request->input('matchEventId', null);
$api = $request->input('api', false);
$corporateIdentity = CorporateIdentity::first();
$parameters = Parameter::select('web_presuscription', 'presuscription_msj')->first();
if (!$parameters->web_presuscription && !$api) {
return view('subscriber.notAvailable', compact('corporateIdentity'));
}
if (Auth::user() && $api) {
$user = Auth::user();
} else {
$user = User::select('id', 'document', 'email', 'first_name', 'last_name')
->where([['document', $document], ['active', 1]])->orderBy('last_session', 'desc')->first();
}
if (!$user) {
return response(array('r' => false, 'message' => trans('messages.subscribers.title_1')));
}
$pre_subscribers = PreSubscriber::select('id', 'document', 'email', 'payment', 'is_credit', 'price', 'seat_id')
->with('seat', 'seat.letter', 'seat.zone')
->where([['document', $document], ['email', 'LIKE', '%' . $user->email . '%']])
->get();
if (count($pre_subscribers) == 0) {
return response(array('r' => false, 'message' => $parameters->presuscription_msj));
}
$tickets = DB::table('ticket_mains as tm')
->select('tm.id')
->rightJoin('ticket_user_blocks as tu', function ($join) {
$join->on('tu.ticket_main_id', '=', 'tm.id')
->where('tu.is_social_distancing', false)
->where('tu.ticket_type_id', TicketTypesEnum::SUBSCRIBER);
})
->where('tm.user_id_log', $user->id)
->where('tm.payment_state', PaymentStatusEnum::PENDING)
->get();
if (count($tickets) > 0) {
return response(array('r' => false, 'message' => 'Se registra un pago pendiente, por favor finalice el pago y vuelva a intentarlo'));
}
$controller = new ServiceChargeController();
if ($controller->validateServiceCharge()) {
foreach ($pre_subscribers as $pre_subscriber) {
$zone = $pre_subscriber->seat->zone;
$requestServiceCharge = new Request([
'services' => 'ticket',
'match_events' => [$matchEventId],
'zones' => $zone ? [$zone->id, $zone->zone_id] : [],
'ticket_types' => TicketTypesEnum::SUBSCRIBER,
]);
$serviceCharge = $controller->calculateServiceCharge($requestServiceCharge);
$pre_subscriber->service_charge = $serviceCharge;
}
}
return response(array('r' => true, 'message' => 'Usuario registrado', 'user' => $user, 'pre_subscriber' => $pre_subscribers));
}
public function makePayment(Request $request, $api = false)
{
$corporateIdentity = CorporateIdentity::first();
$parameters = Parameter::select('web_presuscription', 'presuscription_msj')->first();
if (!$parameters->web_presuscription && !$api)
return view('subscriber.notAvailable', compact('corporateIdentity'));
$subscribedUser = User::select('id')->where([['document', $request['user_abonado']], ['active', 1]])->orderBy('last_session', 'desc')->first();
$pre_subscribers = PreSubscriber::where([['document', $request['user_abonado']], ['email', 'LIKE', '%' . $subscribedUser->email . '%']])->whereIn('id', $request['abonados_array'])->with('seat')->get();
if (count($pre_subscribers) == 0) {
return response(array('r' => false, 'm' => $parameters->presuscription_msj ?? 'No se han encontrado abonos, por favor revisar si el usuario que intenta realizar la compra no corresponde al usuario registrado para el abono'));
}
$subtotal = 0;
$tickets_array = [];
if (isset($request['match_event_id']))
$match_events = DB::table('match_events')
->select('match_events.id', 'match_events.name', 'match_events.season_id')
->whereIn('match_events.id', [$request['match_event_id']])
->get();
else
$match_events = DB::table('match_events')
->select('match_events.id', 'match_events.name', 'match_events.season_id')
->join('seasons', 'seasons.id', '=', 'match_events.season_id')
->where([['seasons.is_suscription', true], ['seasons.active', true]])
->where([['match_events.event_start', '>', Carbon::now()], ['match_events.active', true]])
->get();
// Calcula el valor total a pagar por los abonos seleccionados
$seasonId = null;
foreach ($pre_subscribers as $item) {
$data_ticket = array();
$price = $item->price;
$subtotal += $price;
foreach ($match_events as $key => $match_event) {
if ($key == 0) {
$seasonId = $match_event->season_id;
}
$data_ticket = array(
'seat' => $item->seat,
'match_event_id' => $match_event->id,
'ticket_type_id' => TicketTypesEnum::SUBSCRIBER,
'match_event_price' => null,
'price' => $price,
'zone_name' => $item->seat->zone->name,
);
}
array_push($tickets_array, $data_ticket);
}
$serviceCharge = $request->input('serviceCharge', 0);
$subtotal = $subtotal;
$total = $subtotal + $serviceCharge;
$request = new Request([
'tickets' => $tickets_array,
'type_process' => "block",
'ticket_type_id' => TicketTypesEnum::SUBSCRIBER,
'season_id' => $seasonId,
'subtotal' => $subtotal,
'serviceCharge' => $serviceCharge,
'total' => $total,
]);
$ticketApiController = new TicketApiController;
return $ticketApiController->createBlocks($request, $subscribedUser->id);
}
public function purchaseAbono($id)
{
$ticketApiController = new TicketApiController;
$ticketApiController->determinateWebService();
$wompiController = new WompiController;
// Verifico sin consultar a Wompi.
$ticket = TicketMain::where('id', $id)->first();
if ($ticket->payment_state == 'CONFIRMED') {
return view("wompi.webcheckout-done");
}
// Consulto a Wompi el estado
$ticketApiController->updateStatusOrderFromReference($id);
$ticket = TicketMain::where('id', $id)->first();
if ($ticket->payment_state == 'CONFIRMED') {
return view("wompi.webcheckout-done");
}
// Se actualiza el ticket con la referencia de pago
$ticket->payment_reference = strtoupper(hash("md5", $ticket->id)) . '_' . $ticket->pin;
$ticket->payment_state = "PENDING";
$ticket->gateway_payments_id = $ticketApiController->gateway_data->id;
$ticket->update();
$amountInCents = $ticket->total * 100;
$expirationTime = $wompiController->expirationTime(25);
$signature = $wompiController->generateSignature($ticket->payment_reference, $amountInCents, $ticketApiController->gateway_data->currency, $ticketApiController->gateway_data->client_signature, $expirationTime);
return view("wompi.webcheckout")
->with('gateway_data', $ticketApiController->gateway_data)
->with('amountInCents', $amountInCents)
->with('reference', $ticket->payment_reference)
->with('confirm_url', config('app.url') . '/store/payment?paymentGatewayId=' . $ticket->gateway_payments_id . '&origin=ticket')
->with('signature', $signature)
->with('expirationTime', $expirationTime);
}
}