2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Abstracts\Http;
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Events\Document\PaymentReceived;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Http\Requests\Portal\InvoicePayment as PaymentRequest;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Models\Document\Document;
|
2019-11-16 10:21:14 +03:00
|
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
|
|
use Illuminate\Support\Facades\URL;
|
2023-08-24 11:46:16 +03:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-11-16 10:21:14 +03:00
|
|
|
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();
|
2020-06-27 12:25:44 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$this->module = module($this->alias);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
public function show(Document $invoice, PaymentRequest $request, $cards = [])
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-01-19 12:52:57 +03:00
|
|
|
$this->setContactFirstLastName($invoice);
|
2020-01-02 13:15:49 +03:00
|
|
|
|
2021-01-19 12:52:57 +03:00
|
|
|
$confirm_url = $this->getConfirmUrl($invoice);
|
2020-01-02 13:15:49 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
$html = view('components.payment_method.' . $this->type, [
|
2020-01-02 13:15:49 +03:00
|
|
|
'setting' => $this->setting,
|
2021-01-19 12:52:57 +03:00
|
|
|
'invoice' => $invoice,
|
2020-01-02 13:15:49 +03:00
|
|
|
'confirm_url' => $confirm_url,
|
2022-06-01 10:15:55 +03:00
|
|
|
'store_card' => !empty($this->setting['store_card']) ? true : false,
|
|
|
|
'cards' => $cards,
|
2020-01-02 13:15:49 +03:00
|
|
|
])->render();
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'code' => $this->setting['code'],
|
|
|
|
'name' => $this->setting['name'],
|
|
|
|
'description' => trans($this->alias . '::general.description'),
|
|
|
|
'redirect' => false,
|
|
|
|
'html' => $html,
|
|
|
|
]);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 12:52:57 +03:00
|
|
|
public function signed(Document $invoice, PaymentRequest $request)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-01-19 12:52:57 +03:00
|
|
|
return $this->show($invoice, $request);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
public function cancel(Document $invoice, $force_redirect = false)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$message = trans('messages.warning.payment_cancel', ['method' => setting($this->alias . '.name')]);
|
|
|
|
|
|
|
|
$this->logger->info($this->module->getName() . ':: Invoice: ' . $invoice->id . ' - Cancel Message: ' . $message);
|
|
|
|
|
2021-02-12 19:26:38 +03:00
|
|
|
flash($message)->warning()->important();
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
$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();
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
$finish_url = $this->getFinishUrl($invoice);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
if ($force_redirect || ($this->type == 'redirect')) {
|
2022-06-01 10:15:55 +03:00
|
|
|
return redirect($finish_url);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'error' => $message,
|
2022-06-01 10:15:55 +03:00
|
|
|
'redirect' => $finish_url,
|
2019-11-16 10:21:14 +03:00
|
|
|
'success' => true,
|
|
|
|
'data' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInvoiceUrl($invoice)
|
|
|
|
{
|
2021-04-21 09:59:56 +03:00
|
|
|
return request()->isPortal($invoice->company_id)
|
2019-11-16 10:21:14 +03:00
|
|
|
? route('portal.invoices.show', $invoice->id)
|
2021-04-21 09:59:56 +03:00
|
|
|
: URL::signedRoute('signed.invoices.show', [$invoice->id]);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
public function getFinishUrl($invoice)
|
|
|
|
{
|
|
|
|
return request()->isPortal($invoice->company_id)
|
|
|
|
? route('portal.invoices.finish', $invoice->id)
|
|
|
|
: URL::signedRoute('signed.invoices.finish', [$invoice->id]);
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
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)
|
|
|
|
{
|
2021-04-17 01:29:18 +03:00
|
|
|
return route('portal.' . $this->alias . '.invoices.notify', $invoice->id);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getModuleUrl($invoice, $suffix)
|
|
|
|
{
|
2021-04-21 09:59:56 +03:00
|
|
|
return request()->isPortal($invoice->company_id)
|
2021-04-17 01:29:18 +03:00
|
|
|
? route('portal.' . $this->alias . '.invoices.' . $suffix, $invoice->id)
|
|
|
|
: URL::signedRoute('signed.' . $this->alias . '.invoices.' . $suffix, [$invoice->id]);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getLogger()
|
|
|
|
{
|
|
|
|
$log = new Logger($this->alias);
|
2023-08-24 11:46:16 +03:00
|
|
|
$log->pushHandler(new StreamHandler(Storage::path('logs/' . $this->alias . '.log')), Logger::INFO);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dispatchPaidEvent($invoice, $request)
|
|
|
|
{
|
|
|
|
$request['company_id'] = $invoice->company_id;
|
2020-06-27 12:25:44 +03:00
|
|
|
$request['account_id'] = setting($this->alias . '.account_id', setting('default.account'));
|
2019-11-16 10:21:14 +03:00
|
|
|
$request['amount'] = $invoice->amount;
|
|
|
|
$request['payment_method'] = $this->alias;
|
|
|
|
$request['reference'] = $this->getReference($invoice);
|
2020-12-25 18:19:16 +03:00
|
|
|
$request['type'] = 'income';
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2019-12-21 13:05:33 +03:00
|
|
|
event(new PaymentReceived($invoice, $request));
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|