File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/app/Http/Controllers/PaymentGatewayController.php
<?php
namespace App\Http\Controllers;
use App\CorporateIdentity;
use App\GatewayPayment;
use App\Parameter;
use Illuminate\Http\Request;
use DataTables;
use Illuminate\Support\Facades\Storage;
class PaymentGatewayController extends Controller
{
private $util;
private $types = [];
public function __construct()
{
$this->util = new UtilController();
$this->types[] = (object) [
'name' => 'Todos',
'value' => 'all'
];
$this->types[] = (object) [
'name' => 'Tienda',
'value' => 'order'
];
$this->types[] = (object) [
'name' => 'BoleterĂa',
'value' => 'ticket'
];
$this->types[] = (object) [
'name' => 'Academia',
'value' => 'academy'
];
$this->types[] = (object) [
'name' => 'Cuentas de cobro',
'value' => 'invoice'
];
$this->types[] = (object) [
'name' => 'Experiencias',
'value' => 'experience'
];
$this->types[] = (object) [
'name' => 'Membresias',
'value' => 'membership'
];
}
public function index()
{
return view('paymentGateway.list');
}
public function create()
{
return view('paymentGateway.create')->with('types', $this->types);
}
public function selection($type, $paymentObjectId)
{
$gateways = json_decode($this->getActived($type)->getContent())->data;
$corporateIdentity = CorporateIdentity::first();
$col = (count($gateways) ? 12 / count($gateways) : 12);
$urlComplement = '';
switch ($type) {
case 'ticket':
$urlComplement = '&ticketId=' . $paymentObjectId;
break;
}
if (count($gateways) == 1) {
header('Location: ../../../store/webcheckout?paymentGatewayId=' . $gateways[0]->id . $urlComplement);
exit();
}
return view('paymentGateway.selection', compact('gateways', 'corporateIdentity', 'col', 'urlComplement'));
}
public function tableFilter()
{
$obj = GatewayPayment::select('id', 'name', 'description', 'gw_url_prd', 'gw_url_sandbox', 'is_productive', 'active', 'image', 'valid_payments')->orderBy('id', 'DESC');
return DataTables::of($obj)
->addColumn('actions', function ($obj) {
return '
<i class="fa fa-pencil iconMini" onClick="clickEdit(' . $obj->id . ')" data-id="' . $obj->id . '" data-toggle="tooltip" data-placement="bottom" title="Editar" style="cursor:pointer;"></i>
<i class="fa fa-trash iconMini" onClick="clickDelete(' . $obj->id . ')" data-id="' . $obj->id . '" data-toggle="tooltip" data-placement="bottom" style="cursor:pointer;"></i>
';
})
->editColumn('active', function ($obj) {
if ($obj->active == 0) {
return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" /> <span></span>' . __('messages.yes') . ' </label></div> </label> </div>';
} else {
return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" checked="" />
<span></span> ' . __('messages.yes') . ' </label> </div> </label> </div>';
}
})
->editColumn('is_productive', function ($obj) {
if ($obj->is_productive == 0) {
return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chkIsProductive(' . $obj->id . ')" data-id="' . $obj->id . '" id="chkIsProductive' . $obj->id . '" name="chkIsProductive" /> <span></span>' . __('messages.yes') . ' </label></div> </label> </div>';
} else {
return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chkIsProductive(' . $obj->id . ')" data-id="' . $obj->id . '" id="chkIsProductive' . $obj->id . '" name="chkIsProductive" checked="" />
<span></span> ' . __('messages.yes') . ' </label> </div> </label> </div>';
}
})
->editColumn('valid_payments', function ($obj) {
$valid_payments = [];
if ($obj->valid_payments) {
foreach (explode(',', $obj->valid_payments) as $item) {
$object = collect($this->types)->where('value', $item)->first();
if ($object)
$valid_payments[] = $object->name;
}
}
return implode(',', $valid_payments);
})
->editColumn('image', function ($obj) {
if (!$obj->image) {
return $this->util->generateEmptyImageColumn();
} else {
$image = str_contains($obj->image, 'http') ? $obj->image : config('filesystems.disks.s3.url') . '/paymentGateway/' . $obj->image;
return $this->util->generateImageColumn($image, $obj->name, 'paymentGateway' . $obj->id);
}
})
->rawColumns(['actions', 'is_productive', 'active', 'image'])
->make(true);
}
public function store(Request $request)
{
try {
if (GatewayPayment::where([['name', $request->input('name')], ['client_public', $request->input('client_public')]])->first()) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('tag')]), "data" => null));
}
if ($model = GatewayPayment::create($request->all())) {
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
}
} catch (\Exception $e) {
return response(
array(
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __($e->getMessage()),
"data" => null
)
);
}
}
public function edit($id)
{
$paymentGateway = GatewayPayment::findOrFail($id);
return view('paymentGateway.edit', compact('paymentGateway'))->with('types', $this->types);
}
public function update(Request $request, $id)
{
try {
$request['id'] = $id;
if (GatewayPayment::where([['id', '!=', $id], ['name', $request->input('name')], ['client_public', $request->input('client_public')]])->first()) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
}
if (GatewayPayment::where('id', $id)->update($request->all())) {
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $id));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
}
} catch (\Exception $e) {
return response(
array(
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __($e->getMessage()),
"data" => null
)
);
}
}
public function destroy($id)
{
try {
if (GatewayPayment::where('id', $id)->delete()) {
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.deleted_successfully'), "data" => null));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_removing'), "data" => null));
}
} catch (\Illuminate\Database\QueryException $e) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.delete_relation_data'), "data" => null));
}
}
public function activate(Request $request)
{
try {
$id = $request['id'];
$state = $request['state'];
$gateway = GatewayPayment::find($id);
$gateway->active = $state;
$gateway->update();
return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
} catch (\Throwable $th) {
return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
}
}
public function isProductive(Request $request)
{
try {
$id = $request['id'];
$state = $request['state'];
$gateway = GatewayPayment::find($id);
$gateway->is_productive = $state;
$gateway->update();
return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
} catch (\Throwable $th) {
return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
}
}
public function getById($id)
{
return GatewayPayment::find($id);
}
public function saveImage(Request $request)
{
try {
$extension = $request->file('image')->getClientOriginalExtension();
$filenametostore = $request->id . '.' . $extension;
Storage::disk('s3')->put(config('s3.paymentGateway') . $filenametostore, fopen($request->file('image'), 'r+'), 'public');
$gateway = GatewayPayment::find($request->id);
$gateway->image = $filenametostore;
$gateway->update();
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => null));
} catch (\Throwable $th) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => $th->getMessage()));
}
}
public function getActived($type, $gateway = null)
{
$collection = new CollectionInvoiceController();
$availableCollectionInvoice = $collection->validateAvailableCollectionInvoice();
if ($availableCollectionInvoice) {
return response(array(
"status" => true,
"type" => "success",
"title" => "ok",
"message" => "",
"data" => $availableCollectionInvoice
));
}
if ($gateway && $gateway != 'undefined' && $gateway != 'null') {
return response(array(
"status" => true,
"type" => "success",
"title" => "ok",
"message" => "",
"data" => GatewayPayment::select('id', 'name', 'image')->where('id', $gateway)->get()
));
}
$query = GatewayPayment::query();
$query->select('id', 'name', 'image');
$query->where('active', true);
$query->whereRaw('image IS NOT NULL');
if (isset($type)) {
$query->whereIn('valid_payments', ['all', $type]);
}
$parameters = Parameter::select('is_production_gateway')->first();
$is_production = $parameters->is_production_gateway; // TRUE: Production ; FALSE: Test
$query->where('is_productive', $is_production);
return response(array(
"status" => true,
"type" => "success",
"title" => "ok",
"message" => "",
"data" => $query->get()
));
}
public function paymentsValidator()
{
return view('payments.payments_validator');
}
}