v2 first commit
This commit is contained in:
28
app/Abstracts/Http/ApiController.php
Normal file
28
app/Abstracts/Http/ApiController.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Abstracts\Http;
|
||||
|
||||
use Dingo\Api\Exception\ResourceException;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
abstract class ApiController extends Controller
|
||||
{
|
||||
use Helpers;
|
||||
|
||||
/**
|
||||
* Create the response for when a request fails validation.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param array $errors
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
protected function buildFailedValidationResponse(Request $request, array $errors)
|
||||
{
|
||||
if ($request->expectsJson()) {
|
||||
throw new ResourceException('Validation Error', $errors);
|
||||
}
|
||||
|
||||
return redirect()->to($this->getRedirectUrl())->withInput($request->input())->withErrors($errors, $this->errorBag());
|
||||
}
|
||||
}
|
122
app/Abstracts/Http/Controller.php
Normal file
122
app/Abstracts/Http/Controller.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Abstracts\Http;
|
||||
|
||||
use App\Traits\Jobs;
|
||||
use App\Traits\Relationships;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
abstract class Controller extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, Jobs, Relationships, ValidatesRequests;
|
||||
|
||||
/**
|
||||
* Instantiate a new controller instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->setPermissions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign permissions to methods.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setPermissions()
|
||||
{
|
||||
// No need to check for permission in console
|
||||
if (app()->runningInConsole()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$route = app(Route::class);
|
||||
|
||||
// Get the controller array
|
||||
$arr = array_reverse(explode('\\', explode('@', $route->getAction()['uses'])[0]));
|
||||
|
||||
$controller = '';
|
||||
|
||||
// Add folder
|
||||
if (strtolower($arr[1]) != 'controllers') {
|
||||
$controller .= Str::kebab($arr[1]) . '-';
|
||||
}
|
||||
|
||||
// Add module
|
||||
if (isset($arr[3]) && isset($arr[4]) && (strtolower($arr[4]) == 'modules')) {
|
||||
$controller .= Str::kebab($arr[3]) . '-';
|
||||
}
|
||||
|
||||
// Add file
|
||||
$controller .= Str::kebab($arr[0]);
|
||||
|
||||
// Skip ACL
|
||||
$skip = ['common-dashboard', 'portal-dashboard'];
|
||||
if (in_array($controller, $skip)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add CRUD permission check
|
||||
$this->middleware('permission:create-' . $controller)->only(['create', 'store', 'duplicate', 'import']);
|
||||
$this->middleware('permission:read-' . $controller)->only(['index', 'show', 'edit', 'export']);
|
||||
$this->middleware('permission:update-' . $controller)->only(['update', 'enable', 'disable']);
|
||||
$this->middleware('permission:delete-' . $controller)->only('destroy');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a pagination collection.
|
||||
*
|
||||
* @param array|Collection $items
|
||||
* @param int $perPage
|
||||
* @param int $page
|
||||
* @param array $options
|
||||
*
|
||||
* @return LengthAwarePaginator
|
||||
*/
|
||||
public function paginate($items, $perPage = 15, $page = null, $options = [])
|
||||
{
|
||||
$perPage = $perPage ?: request('limit', setting('default.list_limit', '25'));
|
||||
|
||||
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
|
||||
|
||||
$items = $items instanceof Collection ? $items : Collection::make($items);
|
||||
|
||||
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch a job to its appropriate handler and return a response array for ajax calls.
|
||||
*
|
||||
* @param mixed $job
|
||||
* @return mixed
|
||||
*/
|
||||
public function ajaxDispatch($job)
|
||||
{
|
||||
try {
|
||||
$data = $this->dispatch($job);
|
||||
|
||||
$response = [
|
||||
'success' => true,
|
||||
'error' => false,
|
||||
'data' => $data,
|
||||
'message' => '',
|
||||
];
|
||||
} catch(\Exception $e) {
|
||||
$response = [
|
||||
'success' => false,
|
||||
'error' => true,
|
||||
'data' => null,
|
||||
'message' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
42
app/Abstracts/Http/FormRequest.php
Normal file
42
app/Abstracts/Http/FormRequest.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Abstracts\Http;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
abstract class FormRequest extends BaseFormRequest
|
||||
{
|
||||
/**
|
||||
* Set the company id to the request.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Validation\Validator
|
||||
*/
|
||||
protected function getValidatorInstance()
|
||||
{
|
||||
// Get request data
|
||||
$data = $this->all();
|
||||
|
||||
// Add active company id
|
||||
$data['company_id'] = session('company_id');
|
||||
|
||||
// Reset the request data
|
||||
$this->getInputSource()->replace($data);
|
||||
|
||||
return parent::getValidatorInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given offset exists.
|
||||
*
|
||||
* @param string $offset
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return Arr::has(
|
||||
$this->route() ? $this->all() + $this->route()->parameters() : $this->all(),
|
||||
$offset
|
||||
);
|
||||
}
|
||||
}
|
202
app/Abstracts/Http/PaymentController.php
Normal file
202
app/Abstracts/Http/PaymentController.php
Normal file
@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
namespace App\Abstracts\Http;
|
||||
|
||||
use App\Http\Requests\Portal\InvoicePayment as PaymentRequest;
|
||||
use App\Models\Income\Invoice;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
|
||||
abstract class PaymentController extends BaseController
|
||||
{
|
||||
public $alias = '';
|
||||
|
||||
public $type = ''; // hosted, redirect
|
||||
|
||||
public $setting = [];
|
||||
|
||||
public $logger = null;
|
||||
|
||||
public $user = null;
|
||||
|
||||
public $module = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware(function ($request, $next) {
|
||||
$this->setting = setting($this->alias);
|
||||
$this->setting['code'] = $this->alias;
|
||||
$this->setting['language'] = app()->getLocale();
|
||||
|
||||
$this->logger = $this->getLogger();
|
||||
|
||||
$this->user = user();
|
||||
|
||||
$this->module = module($this->alias);
|
||||
|
||||
return $next($request);
|
||||
});
|
||||
}
|
||||
|
||||
public function show(Invoice $invoice, PaymentRequest $request)
|
||||
{
|
||||
return $this->getInvoiceShow($invoice, 'show');
|
||||
}
|
||||
|
||||
public function signed(Invoice $invoice, PaymentRequest $request)
|
||||
{
|
||||
return $this->getInvoiceShow($invoice, 'signed');
|
||||
}
|
||||
|
||||
public function cancel(Invoice $invoice, $force_redirect = false)
|
||||
{
|
||||
$message = trans('messages.warning.payment_cancel', ['method' => setting($this->alias . '.name')]);
|
||||
|
||||
$this->logger->info($this->module->getName() . ':: Invoice: ' . $invoice->id . ' - Cancel Message: ' . $message);
|
||||
|
||||
flash($message)->warning();
|
||||
|
||||
$invoice_url = $this->getInvoiceUrl($invoice);
|
||||
|
||||
if ($force_redirect || ($this->type == 'redirect')) {
|
||||
return redirect($invoice_url);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => $message,
|
||||
'redirect' => $invoice_url,
|
||||
'success' => false,
|
||||
'data' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function finish($invoice, $request, $force_redirect = false)
|
||||
{
|
||||
$this->dispatchPaidEvent($invoice, $request);
|
||||
|
||||
$this->forgetReference($invoice);
|
||||
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
|
||||
|
||||
$this->logger->info($this->module->getName() . ':: Invoice: ' . $invoice->id . ' - Success Message: ' . $message);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
$invoice_url = $this->getInvoiceUrl($invoice);
|
||||
|
||||
if ($force_redirect || ($this->type == 'redirect')) {
|
||||
return redirect($invoice_url);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => $message,
|
||||
'redirect' => $invoice_url,
|
||||
'success' => true,
|
||||
'data' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getInvoiceShow(Invoice $invoice, $view = 'show')
|
||||
{
|
||||
$this->setContactFirstLastName($invoice);
|
||||
|
||||
$confirm_url = $this->getConfirmUrl($invoice);
|
||||
|
||||
$html = view('partials.portal.payment_method.' . $this->type . '.' . $view, [
|
||||
'setting' => $this->setting,
|
||||
'invoice' => $invoice,
|
||||
'confirm_url' => $confirm_url,
|
||||
])->render();
|
||||
|
||||
return response()->json([
|
||||
'code' => $this->setting['code'],
|
||||
'name' => $this->setting['name'],
|
||||
'description' => trans($this->alias . '::general.description'),
|
||||
'redirect' => false,
|
||||
'html' => $html,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getInvoiceUrl($invoice)
|
||||
{
|
||||
return $this->user
|
||||
? route('portal.invoices.show', $invoice->id)
|
||||
: URL::signedRoute('signed.invoices.show', [$invoice->id, 'company_id' => $invoice->company_id]);
|
||||
}
|
||||
|
||||
public function getConfirmUrl($invoice)
|
||||
{
|
||||
return $this->getModuleUrl($invoice, 'confirm');
|
||||
}
|
||||
|
||||
public function getReturnUrl($invoice)
|
||||
{
|
||||
return $this->getModuleUrl($invoice, 'return');
|
||||
}
|
||||
|
||||
public function getCancelUrl($invoice)
|
||||
{
|
||||
return $this->getModuleUrl($invoice, 'cancel');
|
||||
}
|
||||
|
||||
public function getNotifyUrl($invoice)
|
||||
{
|
||||
return route('portal.invoices.' . $this->alias . '.notify', $invoice->id);
|
||||
}
|
||||
|
||||
public function getModuleUrl($invoice, $suffix)
|
||||
{
|
||||
return $this->user
|
||||
? route('portal.invoices.' . $this->alias . '.' . $suffix, $invoice->id)
|
||||
: URL::signedRoute('signed.invoices.' . $this->alias . '.' . $suffix, [$invoice->id, 'company_id' => $invoice->company_id]);
|
||||
}
|
||||
|
||||
public function getLogger()
|
||||
{
|
||||
$log = new Logger($this->alias);
|
||||
$log->pushHandler(new StreamHandler(storage_path('logs/' . $this->alias . '.log')), Logger::INFO);
|
||||
|
||||
return $log;
|
||||
}
|
||||
|
||||
public function dispatchPaidEvent($invoice, $request)
|
||||
{
|
||||
$request['company_id'] = $invoice->company_id;
|
||||
$request['amount'] = $invoice->amount;
|
||||
$request['payment_method'] = $this->alias;
|
||||
$request['reference'] = $this->getReference($invoice);
|
||||
|
||||
event(new \App\Events\Income\PaymentReceived($invoice, $request));
|
||||
}
|
||||
|
||||
public function setReference($invoice, $reference)
|
||||
{
|
||||
session([
|
||||
$this->alias . '_' . $invoice->id . '_reference' => $reference
|
||||
]);
|
||||
}
|
||||
|
||||
public function getReference($invoice)
|
||||
{
|
||||
return session($this->alias . '_' . $invoice->id . '_reference');
|
||||
}
|
||||
|
||||
public function forgetReference($invoice)
|
||||
{
|
||||
session()->forget($this->alias . '_' . $invoice->id . '_reference');
|
||||
}
|
||||
|
||||
public function setContactFirstLastName(&$invoice)
|
||||
{
|
||||
$contact = explode(" ", $invoice->contact_name);
|
||||
|
||||
$last_name = array_pop($contact);
|
||||
$first_name = implode(" ", $contact);
|
||||
|
||||
$invoice->first_name = $first_name;
|
||||
$invoice->last_name = $last_name;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user