2019-08-16 07:20:28 +02:00
|
|
|
<?php
|
2020-05-09 00:20:37 +02:00
|
|
|
|
2019-08-16 07:20:28 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-08-16 07:20:28 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\ClientPortal;
|
|
|
|
|
|
|
|
use App\Filters\PaymentFilters;
|
|
|
|
use App\Http\Controllers\Controller;
|
2019-09-13 00:33:48 +02:00
|
|
|
use App\Models\CompanyGateway;
|
2019-09-25 04:07:33 +02:00
|
|
|
use App\Models\Invoice;
|
2019-08-16 07:20:28 +02:00
|
|
|
use App\Models\Payment;
|
2019-09-25 04:07:33 +02:00
|
|
|
use App\Utils\Number;
|
|
|
|
use App\Utils\Traits\MakesDates;
|
2019-08-16 07:20:28 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-09-25 04:07:33 +02:00
|
|
|
use Cache;
|
2019-08-16 07:20:28 +02:00
|
|
|
use Illuminate\Http\Request;
|
2020-04-09 04:18:07 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Yajra\DataTables\Facades\DataTables;
|
2020-04-15 02:30:52 +02:00
|
|
|
|
2019-08-16 07:20:28 +02:00
|
|
|
/**
|
2019-09-25 04:07:33 +02:00
|
|
|
* Class PaymentController
|
|
|
|
* @package App\Http\Controllers\ClientPortal\PaymentController
|
2019-08-16 07:20:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
class PaymentController extends Controller
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2019-09-25 04:07:33 +02:00
|
|
|
use MakesDates;
|
2019-08-16 07:20:28 +02:00
|
|
|
|
|
|
|
/**
|
2020-04-23 00:49:23 +02:00
|
|
|
* Show the list of payments.
|
2019-08-16 07:20:28 +02:00
|
|
|
*
|
2020-03-23 18:10:42 +01:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2019-08-16 07:20:28 +02:00
|
|
|
*/
|
2020-04-23 00:49:23 +02:00
|
|
|
public function index()
|
2019-08-16 07:20:28 +02:00
|
|
|
{
|
2020-04-23 00:49:23 +02:00
|
|
|
return $this->render('payments.index');
|
2019-08-16 07:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
2020-03-23 18:10:42 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Payment $payment
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2019-08-16 07:20:28 +02:00
|
|
|
*/
|
2019-09-25 08:23:51 +02:00
|
|
|
public function show(Request $request, Payment $payment)
|
2019-08-16 07:20:28 +02:00
|
|
|
{
|
2019-09-25 08:23:51 +02:00
|
|
|
$payment->load('invoices');
|
2019-08-16 07:20:28 +02:00
|
|
|
|
2020-03-23 18:10:42 +01:00
|
|
|
return $this->render('payments.show', [
|
|
|
|
'payment' => $payment,
|
|
|
|
]);
|
2019-08-16 07:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 08:10:21 +02:00
|
|
|
/**
|
|
|
|
* Presents the payment screen for a given
|
|
|
|
* gateway and payment method.
|
|
|
|
* The request will also contain the amount
|
2019-09-12 13:46:09 +02:00
|
|
|
* and invoice ids for reference.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2020-03-24 23:13:47 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|mixed
|
2019-09-12 08:10:21 +02:00
|
|
|
*/
|
2019-09-25 04:07:33 +02:00
|
|
|
public function process()
|
2020-03-26 04:23:57 +01:00
|
|
|
{
|
2020-03-24 23:13:47 +01:00
|
|
|
$invoices = Invoice::whereIn('id', $this->transformKeys(request()->invoices))
|
2020-05-09 00:20:37 +02:00
|
|
|
->whereClientId(auth('contact')->user()->company()->id)
|
|
|
|
->get();
|
2019-09-12 13:46:09 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$amount = $invoices->sum('balance');
|
2019-09-12 08:10:21 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoices = $invoices->filter(function ($invoice) {
|
2019-09-25 04:07:33 +02:00
|
|
|
return $invoice->isPayable();
|
2019-10-08 05:06:27 +02:00
|
|
|
});
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($invoices->count() == 0) {
|
2020-03-24 23:13:47 +01:00
|
|
|
return redirect()
|
|
|
|
->route('client.invoices.index')
|
|
|
|
->with(['warning' => 'No payable invoices selected.']);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoices->map(function ($invoice) {
|
2019-09-25 04:07:33 +02:00
|
|
|
$invoice->balance = Number::formatMoney($invoice->balance, $invoice->client);
|
|
|
|
$invoice->due_date = $this->formatDate($invoice->due_date, $invoice->client->date_format());
|
|
|
|
return $invoice;
|
|
|
|
});
|
2019-09-12 13:46:09 +02:00
|
|
|
|
2019-09-25 04:07:33 +02:00
|
|
|
$payment_methods = auth()->user()->client->getPaymentMethods($amount);
|
|
|
|
$gateway = CompanyGateway::find(request()->input('company_gateway_id'));
|
|
|
|
|
|
|
|
$payment_method_id = request()->input('payment_method_id');
|
2019-09-12 08:10:21 +02:00
|
|
|
|
2020-05-09 00:20:37 +02:00
|
|
|
// Place to calculate gateway fee.
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2019-09-12 08:10:21 +02:00
|
|
|
$data = [
|
2019-09-12 13:46:09 +02:00
|
|
|
'invoices' => $invoices,
|
|
|
|
'amount' => $amount,
|
2019-09-13 00:33:48 +02:00
|
|
|
'fee' => $gateway->calcGatewayFee($amount),
|
2019-09-25 04:07:33 +02:00
|
|
|
'amount_with_fee' => $amount + $gateway->calcGatewayFee($amount),
|
|
|
|
'token' => auth()->user()->client->gateway_token($gateway->id, $payment_method_id),
|
|
|
|
'payment_method_id' => $payment_method_id,
|
2020-05-09 00:20:37 +02:00
|
|
|
'hashed_ids' => request()->invoices,
|
2019-09-12 08:10:21 +02:00
|
|
|
];
|
2020-03-23 18:10:42 +01:00
|
|
|
|
|
|
|
|
2019-09-25 06:03:28 +02:00
|
|
|
return $gateway->driver(auth()->user()->client)->processPaymentView($data);
|
2019-09-12 08:10:21 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 06:03:28 +02:00
|
|
|
public function response(Request $request)
|
|
|
|
{
|
|
|
|
$gateway = CompanyGateway::find($request->input('company_gateway_id'));
|
|
|
|
|
|
|
|
return $gateway->driver(auth()->user()->client)->processPaymentResponse($request);
|
|
|
|
}
|
2019-08-16 07:20:28 +02:00
|
|
|
}
|