invoiceninja/app/Http/Controllers/ClientPortal/PaymentController.php

184 lines
6.2 KiB
PHP
Raw Normal View History

2019-08-16 15:20:28 +10:00
<?php
2019-08-16 15:20:28 +10:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-08-16 15:20:28 +10:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers\ClientPortal;
use App\Filters\PaymentFilters;
use App\Http\Controllers\Controller;
use App\Jobs\Invoice\InjectSignature;
2019-09-13 08:33:48 +10:00
use App\Models\CompanyGateway;
2019-09-25 12:07:33 +10:00
use App\Models\Invoice;
2019-08-16 15:20:28 +10:00
use App\Models\Payment;
2019-09-25 12:07:33 +10:00
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
2019-08-16 15:20:28 +10:00
use App\Utils\Traits\MakesHash;
2019-09-25 12:07:33 +10:00
use Cache;
2019-08-16 15:20:28 +10:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Yajra\DataTables\Facades\DataTables;
2019-08-16 15:20:28 +10:00
/**
2019-09-25 12:07:33 +10:00
* Class PaymentController
* @package App\Http\Controllers\ClientPortal\PaymentController
2019-08-16 15:20:28 +10:00
*/
class PaymentController extends Controller
{
use MakesHash;
2019-09-25 12:07:33 +10:00
use MakesDates;
2019-08-16 15:20:28 +10:00
/**
* Show the list of payments.
2019-08-16 15:20:28 +10:00
*
2020-03-23 18:10:42 +01:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2019-08-16 15:20:28 +10:00
*/
public function index()
2019-08-16 15:20:28 +10:00
{
return $this->render('payments.index');
2019-08-16 15:20:28 +10: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 15:20:28 +10:00
*/
public function show(Request $request, Payment $payment)
2019-08-16 15:20:28 +10:00
{
$payment->load('invoices');
2019-08-16 15:20:28 +10:00
2020-03-23 18:10:42 +01:00
return $this->render('payments.show', [
'payment' => $payment,
]);
2019-08-16 15:20:28 +10:00
}
2019-09-12 16:10:21 +10:00
/**
* Presents the payment screen for a given
* gateway and payment method.
* The request will also contain the amount
2019-09-12 21:46:09 +10:00
* and invoice ids for reference.
*
* @return \Illuminate\Http\RedirectResponse|mixed
2019-09-12 16:10:21 +10:00
*/
2019-09-25 12:07:33 +10:00
public function process()
{
2020-08-25 23:06:38 +10:00
//REFACTOR - Here the request will contain an array of invoices and the amount to be charged for the invoice
//REFACTOR - At this point, we will also need to modify the invoice to include a line item for a gateway fee if applicable
// This is tagged with a type_id of 3 which is for a pending gateway fee.
//REFACTOR - In order to preserve state we should save the array of invoices and amounts and store it in db/cache and use a HASH
// to rehydrate these values in the payment response.
// [
// 'invoices' =>
// [
// 'invoice_id' => 'xx',
// 'amount' => 'yy',
// ]
// ]
//old
// $invoices = Invoice::whereIn('id', $this->transformKeys(request()->invoices))
// ->where('company_id', auth('contact')->user()->company->id)
// ->get();
2020-08-25 23:18:17 +10:00
/*find invoices*/
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column(request()->invoices, 'invoice_id')))->get();
2020-08-25 23:06:38 +10:00
//old
// $amount = $invoices->sum('balance');
2019-09-12 16:10:21 +10:00
2020-08-25 23:18:17 +10:00
/*filter only payable invoices*/
$invoices = $invoices->filter(function ($invoice) {
2019-09-25 12:07:33 +10:00
return $invoice->isPayable();
});
2020-08-25 23:18:17 +10:00
/*return early if no invoices*/
if ($invoices->count() == 0) {
return redirect()
->route('client.invoices.index')
->with(['warning' => 'No payable invoices selected.']);
}
2020-03-23 18:10:42 +01:00
2020-08-25 23:18:17 +10:00
/*iterate through invoices and add gateway fees*/
2020-08-25 23:06:38 +10:00
foreach(request()->invoices as $payable_invoice)
{
2020-08-25 23:18:17 +10:00
$invoice = $invoices->first(function ($inv) use($payable_invoice) {
return $payable_invoice['invoice_id'] == $inv->hashed_id;
});
2020-08-25 23:06:38 +10:00
2020-08-25 23:18:17 +10:00
if($invoice)
$invoice->service()->addGatewayFee($payable_invoice['amount'])->save();
2020-08-25 23:06:38 +10:00
}
/*Format invoices we need to use fresh() here to bring in the gateway fees*/
2020-08-25 23:18:17 +10:00
$invoices->fresh()->map(function ($invoice) {
2019-09-25 12:07:33 +10:00
$invoice->balance = Number::formatMoney($invoice->balance, $invoice->client);
$invoice->due_date = $this->formatDate($invoice->due_date, $invoice->client->date_format());
2019-09-25 12:07:33 +10:00
return $invoice;
});
2019-09-12 21:46:09 +10:00
2020-06-01 14:29:41 +02:00
if ((bool) request()->signature) {
$invoices->each(function ($invoice) {
InjectSignature::dispatch($invoice, request()->signature);
});
}
2019-09-25 12:07:33 +10: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 16:10:21 +10:00
// Place to calculate gateway fee.
2020-03-23 18:10:42 +01:00
2019-09-12 16:10:21 +10:00
$data = [
2019-09-12 21:46:09 +10:00
'invoices' => $invoices,
'amount' => $amount,
2019-09-13 08:33:48 +10:00
'fee' => $gateway->calcGatewayFee($amount),
2019-09-25 12:07:33 +10: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,
'hashed_ids' => request()->invoices,
2019-09-12 16:10:21 +10:00
];
2020-03-23 18:10:42 +01:00
2020-06-01 14:29:41 +02:00
return $gateway
->driver(auth()->user()->client)
2020-06-15 21:42:46 +10:00
->setPaymentMethod($payment_method_id)
2020-06-01 14:29:41 +02:00
->processPaymentView($data);
2019-09-12 16:10:21 +10:00
}
public function response(Request $request)
{
$gateway = CompanyGateway::find($request->input('company_gateway_id'));
2020-08-25 23:06:38 +10:00
//REFACTOR - Entry point for the gateway response - we don't need to do anything at this point.
//
// - Inside each gateway driver, we should use have a generic code path (in BaseDriver.php)for successful/failed payment
//
// Success workflow
//
// - Rehydrate the hash and iterate through the invoices and update the balances
// - Update the type_id of the gateway fee to type_id 4
// - Link invoices to payment
//
// Failure workflow
//
// - Rehydrate hash, iterate through invoices and remove type_id 3's
// - Recalcuate invoice totals
2020-06-01 16:19:03 +02:00
return $gateway
->driver(auth()->user()->client)
2020-06-16 10:21:40 +10:00
->setPaymentMethod($request->input('payment_method_id'))
2020-06-01 16:19:03 +02:00
->processPaymentResponse($request);
}
2019-08-16 15:20:28 +10:00
}