2019-09-23 07:59:01 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-09-23 07:59:01 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-09-23 07:59:01 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-09-23 07:59:01 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\ClientPortal;
|
|
|
|
|
2020-08-12 04:02:21 +02:00
|
|
|
use App\Events\Credit\CreditWasViewed;
|
2020-07-17 03:18:40 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasViewed;
|
2020-03-05 08:14:57 +01:00
|
|
|
use App\Events\Misc\InvitationWasViewed;
|
2020-08-12 04:02:21 +02:00
|
|
|
use App\Events\Quote\QuoteWasViewed;
|
2019-09-23 07:59:01 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
2021-09-27 03:40:17 +02:00
|
|
|
use App\Jobs\Entity\CreateRawPdf;
|
2021-08-30 14:04:51 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
2021-10-05 12:29:32 +02:00
|
|
|
use App\Models\InvoiceInvitation;
|
2021-08-30 14:04:51 +02:00
|
|
|
use App\Models\Payment;
|
2021-10-06 05:47:17 +02:00
|
|
|
use App\Services\ClientPortal\InstantPayment;
|
|
|
|
use App\Utils\CurlUtils;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2019-09-23 07:59:01 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2020-10-09 03:59:59 +02:00
|
|
|
use Illuminate\Support\Str;
|
2019-09-23 07:59:01 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class InvitationController.
|
2019-09-23 07:59:01 +02:00
|
|
|
*/
|
|
|
|
class InvitationController extends Controller
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use MakesDates;
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
public function router(string $entity, string $invitation_key)
|
2021-01-30 02:13:32 +01:00
|
|
|
{
|
|
|
|
Auth::logout();
|
|
|
|
|
2021-01-03 08:56:42 +01:00
|
|
|
return $this->genericRouter($entity, $invitation_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function recurringRouter(string $invitation_key)
|
|
|
|
{
|
|
|
|
return $this->genericRouter('recurring_invoice', $invitation_key);
|
|
|
|
}
|
|
|
|
|
2021-09-05 12:41:00 +02:00
|
|
|
public function invoiceRouter(string $invitation_key)
|
|
|
|
{
|
|
|
|
return $this->genericRouter('invoice', $invitation_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function quoteRouter(string $invitation_key)
|
|
|
|
{
|
|
|
|
return $this->genericRouter('quote', $invitation_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function creditRouter(string $invitation_key)
|
|
|
|
{
|
|
|
|
return $this->genericRouter('credit', $invitation_key);
|
|
|
|
}
|
|
|
|
|
2021-01-03 08:56:42 +01:00
|
|
|
private function genericRouter(string $entity, string $invitation_key)
|
2019-09-23 07:59:01 +02:00
|
|
|
{
|
2021-01-30 02:13:32 +01:00
|
|
|
|
2021-10-05 06:20:44 +02:00
|
|
|
if(!in_array($entity, ['invoice', 'credit', 'quote', 'recurring_invoice']))
|
|
|
|
return response()->json(['message' => 'Invalid resource request']);
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
$key = $entity.'_id';
|
2020-03-06 14:41:15 +01:00
|
|
|
|
2020-11-11 01:13:39 +01:00
|
|
|
$entity_obj = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
|
2019-09-23 07:59:01 +02:00
|
|
|
|
2020-09-07 06:49:57 +02:00
|
|
|
$invitation = $entity_obj::whereRaw('BINARY `key`= ?', [$invitation_key])
|
|
|
|
->with('contact.client')
|
|
|
|
->firstOrFail();
|
2019-09-23 07:59:01 +02:00
|
|
|
|
2020-09-07 06:49:57 +02:00
|
|
|
/* Return early if we have the correct client_hash embedded */
|
2021-06-08 13:45:23 +02:00
|
|
|
$client_contact = $invitation->contact;
|
|
|
|
|
|
|
|
if(empty($client_contact->email))
|
|
|
|
$client_contact->email = Str::random(15) . "@example.com"; $client_contact->save();
|
2020-07-14 07:37:49 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (request()->has('client_hash') && request()->input('client_hash') == $invitation->contact->client->client_hash) {
|
2021-06-08 13:45:23 +02:00
|
|
|
auth()->guard('contact')->login($client_contact, true);
|
2021-01-30 02:13:32 +01:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ((bool) $invitation->contact->client->getSetting('enable_client_portal_password') !== false) {
|
2020-09-07 06:49:57 +02:00
|
|
|
$this->middleware('auth:contact');
|
2021-01-30 02:13:32 +01:00
|
|
|
return redirect()->route('client.login');
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
} else {
|
2021-06-08 13:45:23 +02:00
|
|
|
nlog("else - default - login contact");
|
|
|
|
auth()->guard('contact')->login($client_contact, true);
|
2020-09-07 06:49:57 +02:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-01-30 02:13:32 +01:00
|
|
|
|
2020-09-07 06:49:57 +02:00
|
|
|
if (auth()->guard('contact') && ! request()->has('silent') && ! $invitation->viewed_date) {
|
|
|
|
$invitation->markViewed();
|
2020-07-17 03:18:40 +02:00
|
|
|
|
2020-09-07 06:49:57 +02:00
|
|
|
event(new InvitationWasViewed($invitation->{$entity}, $invitation, $invitation->{$entity}->company, Ninja::eventVars()));
|
2019-11-04 01:22:59 +01:00
|
|
|
|
2020-09-07 06:49:57 +02:00
|
|
|
$this->fireEntityViewedEvent($invitation, $entity);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-09-07 06:49:57 +02:00
|
|
|
|
|
|
|
return redirect()->route('client.'.$entity.'.show', [$entity => $this->encodePrimaryKey($invitation->{$key})]);
|
2020-03-06 14:41:15 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 03:18:40 +02:00
|
|
|
private function fireEntityViewedEvent($invitation, $entity_string)
|
|
|
|
{
|
|
|
|
switch ($entity_string) {
|
|
|
|
case 'invoice':
|
|
|
|
event(new InvoiceWasViewed($invitation, $invitation->company, Ninja::eventVars()));
|
|
|
|
break;
|
2020-08-12 04:02:21 +02:00
|
|
|
case 'quote':
|
|
|
|
event(new QuoteWasViewed($invitation, $invitation->company, Ninja::eventVars()));
|
|
|
|
break;
|
|
|
|
case 'credit':
|
|
|
|
event(new CreditWasViewed($invitation, $invitation->company, Ninja::eventVars()));
|
2020-09-06 11:38:10 +02:00
|
|
|
break;
|
2020-07-17 03:18:40 +02:00
|
|
|
default:
|
2020-09-06 11:38:10 +02:00
|
|
|
// code...
|
2020-07-17 03:18:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 14:41:15 +01:00
|
|
|
public function routerForDownload(string $entity, string $invitation_key)
|
|
|
|
{
|
2021-09-27 03:40:17 +02:00
|
|
|
|
|
|
|
if(Ninja::isHosted())
|
|
|
|
return $this->returnRawPdf($entity, $invitation_key);
|
|
|
|
|
2020-03-11 12:05:05 +01:00
|
|
|
return redirect('client/'.$entity.'/'.$invitation_key.'/download_pdf');
|
2019-09-23 07:59:01 +02:00
|
|
|
}
|
2019-11-06 23:57:09 +01:00
|
|
|
|
2021-09-27 03:40:17 +02:00
|
|
|
private function returnRawPdf(string $entity, string $invitation_key)
|
|
|
|
{
|
|
|
|
|
2021-10-05 06:20:44 +02:00
|
|
|
if(!in_array($entity, ['invoice', 'credit', 'quote', 'recurring_invoice']))
|
|
|
|
return response()->json(['message' => 'Invalid resource request']);
|
|
|
|
|
2021-09-27 03:40:17 +02:00
|
|
|
$key = $entity.'_id';
|
|
|
|
|
|
|
|
$entity_obj = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
|
|
|
|
|
2021-10-04 00:36:30 +02:00
|
|
|
// $invitation = $entity_obj::whereRaw('BINARY `key`= ?', [$invitation_key])
|
|
|
|
// ->with('contact.client')
|
|
|
|
// ->firstOrFail();
|
|
|
|
|
|
|
|
$invitation = $entity_obj::where('key', $invitation_key)
|
2021-09-27 03:40:17 +02:00
|
|
|
->with('contact.client')
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
if(!$invitation)
|
|
|
|
return response()->json(["message" => "no record found"], 400);
|
|
|
|
|
|
|
|
$file_name = $invitation->{$entity}->numberFormatter().'.pdf';
|
|
|
|
nlog($file_name);
|
|
|
|
|
|
|
|
$file = CreateRawPdf::dispatchNow($invitation, $invitation->company->db);
|
|
|
|
|
|
|
|
$headers = ['Content-Type' => 'application/pdf'];
|
|
|
|
|
|
|
|
if(request()->input('inline') == 'true')
|
|
|
|
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
|
|
|
|
|
|
|
|
return response()->streamDownload(function () use($file) {
|
|
|
|
echo $file;
|
|
|
|
}, $file_name, $headers);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
public function routerForIframe(string $entity, string $client_hash, string $invitation_key)
|
|
|
|
{
|
|
|
|
}
|
2021-08-30 14:04:51 +02:00
|
|
|
|
|
|
|
public function paymentRouter(string $contact_key, string $payment_id)
|
|
|
|
{
|
|
|
|
$contact = ClientContact::where('contact_key', $contact_key)->firstOrFail();
|
|
|
|
$payment = Payment::find($this->decodePrimaryKey($payment_id));
|
|
|
|
|
|
|
|
if($payment->client_id != $contact->client_id)
|
|
|
|
abort(403, 'You are not authorized to view this resource');
|
|
|
|
|
|
|
|
auth()->guard('contact')->login($contact, true);
|
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', $payment->hashed_id);
|
|
|
|
|
|
|
|
}
|
2021-10-05 12:29:32 +02:00
|
|
|
|
2021-10-06 05:47:17 +02:00
|
|
|
public function payInvoice(Request $request, string $invitation_key)
|
2021-10-05 12:29:32 +02:00
|
|
|
{
|
|
|
|
$invitation = InvoiceInvitation::where('key', $invitation_key)
|
|
|
|
->with('contact.client')
|
|
|
|
->firstOrFail();
|
|
|
|
|
2021-10-06 05:47:17 +02:00
|
|
|
auth()->guard('contact')->login($invitation->contact, true);
|
|
|
|
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
|
|
|
|
if($invoice->partial > 0)
|
|
|
|
$amount = round($invoice->partial, (int)$invoice->client->currency()->precision);
|
|
|
|
else
|
|
|
|
$amount = round($invoice->balance, (int)$invoice->client->currency()->precision);
|
|
|
|
|
|
|
|
$gateways = $invitation->contact->client->service()->getPaymentMethods($amount);
|
|
|
|
|
|
|
|
if(is_array($gateways))
|
|
|
|
{
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'company_gateway_id' => $gateways[0]['company_gateway_id'],
|
|
|
|
'payment_method_id' => $gateways[0]['gateway_type_id'],
|
|
|
|
'payable_invoices' => [
|
|
|
|
['invoice_id' => $invitation->invoice->hashed_id, 'amount' => $amount],
|
|
|
|
],
|
|
|
|
'signature' => false
|
|
|
|
];
|
|
|
|
|
|
|
|
$request->replace($data);
|
|
|
|
|
|
|
|
return (new InstantPayment($request))->run();
|
|
|
|
}
|
|
|
|
|
2021-10-06 06:05:16 +02:00
|
|
|
abort(404, "Invoice not found");
|
2021-10-05 12:29:32 +02:00
|
|
|
}
|
2019-09-23 07:59:01 +02:00
|
|
|
}
|