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
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-08-16 07:20:28 +02:00
|
|
|
*
|
|
|
|
* @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;
|
2020-08-27 14:12:39 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2020-05-28 15:59:45 +02:00
|
|
|
use App\Jobs\Invoice\InjectSignature;
|
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;
|
2020-08-26 02:47:50 +02:00
|
|
|
use App\Models\PaymentHash;
|
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;
|
2020-08-26 02:47:50 +02:00
|
|
|
use Illuminate\Support\Str;
|
2020-04-09 04:18:07 +02:00
|
|
|
use Yajra\DataTables\Facades\DataTables;
|
2020-04-15 02:30:52 +02:00
|
|
|
|
2019-08-16 07:20:28 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class 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-08-25 15:06:38 +02: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.
|
2020-08-30 00:00:57 +02:00
|
|
|
// dd(request()->all());
|
2020-08-25 15:06:38 +02:00
|
|
|
|
2020-08-26 02:47:50 +02:00
|
|
|
$gateway = CompanyGateway::find(request()->input('company_gateway_id'));
|
2020-08-25 15:18:17 +02:00
|
|
|
/*find invoices*/
|
2020-08-26 02:47:50 +02:00
|
|
|
$payable_invoices = request()->payable_invoices;
|
|
|
|
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payable_invoices, 'invoice_id')))->get();
|
2019-09-12 08:10:21 +02:00
|
|
|
|
2020-08-25 15:18:17 +02:00
|
|
|
/*filter only payable invoices*/
|
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
|
|
|
});
|
|
|
|
|
2020-08-25 15:18:17 +02:00
|
|
|
/*return early if no invoices*/
|
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
|
|
|
|
2020-08-26 02:47:50 +02:00
|
|
|
/*iterate through invoices and add gateway fees and other payment metadata*/
|
2020-08-26 02:53:11 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
foreach ($payable_invoices as $key => $payable_invoice) {
|
2020-08-26 03:14:15 +02:00
|
|
|
$payable_invoices[$key]['amount'] = Number::parseFloat($payable_invoice['amount']);
|
|
|
|
$payable_invoice['amount'] = $payable_invoices[$key]['amount'];
|
2020-08-26 02:47:50 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice = $invoices->first(function ($inv) use ($payable_invoice) {
|
|
|
|
return $payable_invoice['invoice_id'] == $inv->hashed_id;
|
|
|
|
});
|
|
|
|
|
2020-08-26 03:14:15 +02:00
|
|
|
$payable_invoices[$key]['due_date'] = $this->formatDate($invoice->due_date, $invoice->client->date_format());
|
|
|
|
$payable_invoices[$key]['invoice_number'] = $invoice->number;
|
2020-08-26 02:47:50 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (isset($invoice->po_number)) {
|
2020-08-26 02:47:50 +02:00
|
|
|
$additional_info = $invoice->po_number;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif (isset($invoice->public_notes)) {
|
2020-08-26 02:47:50 +02:00
|
|
|
$additional_info = $invoice->public_notes;
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-08-26 02:47:50 +02:00
|
|
|
$additional_info = $invoice->date;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-26 02:47:50 +02:00
|
|
|
|
2020-08-26 03:14:15 +02:00
|
|
|
$payable_invoices[$key]['additional_info'] = $additional_info;
|
2020-08-26 02:47:50 +02:00
|
|
|
}
|
2019-09-12 13:46:09 +02:00
|
|
|
|
2020-06-01 14:29:41 +02:00
|
|
|
if ((bool) request()->signature) {
|
|
|
|
$invoices->each(function ($invoice) {
|
|
|
|
InjectSignature::dispatch($invoice, request()->signature);
|
|
|
|
});
|
|
|
|
}
|
2020-05-28 15:59:45 +02:00
|
|
|
|
2020-08-26 03:14:15 +02:00
|
|
|
$payment_methods = auth()->user()->client->getPaymentMethods(array_sum(array_column($payable_invoices, 'amount_with_fee')));
|
2019-09-25 04:07:33 +02:00
|
|
|
$payment_method_id = request()->input('payment_method_id');
|
2019-09-12 08:10:21 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice_totals = array_sum(array_column($payable_invoices, 'amount'));
|
2020-08-31 04:00:43 +02:00
|
|
|
|
|
|
|
$first_invoice = $invoices->first();
|
|
|
|
$fee_totals = round($gateway->calcGatewayFee($invoice_totals, true), $first_invoice->client->currency()->precision);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $first_invoice->uses_inclusive_taxes) {
|
2020-08-31 04:00:43 +02:00
|
|
|
$fee_tax = 0;
|
2020-09-06 11:38:10 +02:00
|
|
|
$fee_tax += round(($first_invoice->tax_rate1 / 100) * $fee_totals, $first_invoice->client->currency()->precision);
|
|
|
|
$fee_tax += round(($first_invoice->tax_rate2 / 100) * $fee_totals, $first_invoice->client->currency()->precision);
|
|
|
|
$fee_tax += round(($first_invoice->tax_rate3 / 100) * $fee_totals, $first_invoice->client->currency()->precision);
|
2020-08-31 04:00:43 +02:00
|
|
|
|
|
|
|
$fee_totals += $fee_tax;
|
|
|
|
}
|
|
|
|
|
|
|
|
$first_invoice->service()->addGatewayFee($gateway, $invoice_totals)->save();
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-08-26 02:47:50 +02:00
|
|
|
$payment_hash = new PaymentHash;
|
|
|
|
$payment_hash->hash = Str::random(128);
|
|
|
|
$payment_hash->data = $payable_invoices;
|
2020-08-30 14:00:19 +02:00
|
|
|
$payment_hash->fee_total = $fee_totals;
|
2020-08-31 06:27:47 +02:00
|
|
|
$payment_hash->fee_invoice_id = $first_invoice->id;
|
2020-08-26 02:47:50 +02:00
|
|
|
$payment_hash->save();
|
|
|
|
|
|
|
|
$totals = [
|
2020-08-26 03:14:15 +02:00
|
|
|
'invoice_totals' => $invoice_totals,
|
2020-08-27 14:12:39 +02:00
|
|
|
'fee_total' => $fee_totals,
|
2020-08-26 03:14:15 +02:00
|
|
|
'amount_with_fee' => $invoice_totals + $fee_totals,
|
2020-08-26 02:47:50 +02:00
|
|
|
];
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2019-09-12 08:10:21 +02:00
|
|
|
$data = [
|
2020-08-26 02:47:50 +02:00
|
|
|
'payment_hash' => $payment_hash->hash,
|
|
|
|
'total' => $totals,
|
|
|
|
'invoices' => $payable_invoices,
|
2019-09-25 04:07:33 +02:00
|
|
|
'token' => auth()->user()->client->gateway_token($gateway->id, $payment_method_id),
|
|
|
|
'payment_method_id' => $payment_method_id,
|
2020-08-30 14:00:19 +02:00
|
|
|
'amount_with_fee' => $invoice_totals + $fee_totals,
|
2019-09-12 08:10:21 +02: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 13:42:46 +02:00
|
|
|
->setPaymentMethod($payment_method_id)
|
2020-06-01 14:29:41 +02:00
|
|
|
->processPaymentView($data);
|
2019-09-12 08:10:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 14:12:39 +02:00
|
|
|
public function response(PaymentResponseRequest $request)
|
2019-09-25 06:03:28 +02:00
|
|
|
{
|
2020-08-30 00:00:57 +02:00
|
|
|
/*Payment Gateway*/
|
2020-08-27 14:12:39 +02:00
|
|
|
$gateway = CompanyGateway::find($request->input('company_gateway_id'))->firstOrFail();
|
|
|
|
|
2020-08-25 15:06:38 +02: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
|
2020-09-06 11:38:10 +02:00
|
|
|
//
|
2020-08-25 15:06:38 +02:00
|
|
|
// Success workflow
|
2020-09-06 11:38:10 +02:00
|
|
|
//
|
2020-08-25 15:06:38 +02:00
|
|
|
// - 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
|
2020-09-06 11:38:10 +02:00
|
|
|
//
|
2020-08-25 15:06:38 +02:00
|
|
|
// Failure workflow
|
2020-09-06 11:38:10 +02:00
|
|
|
//
|
2020-08-25 15:06:38 +02:00
|
|
|
// - Rehydrate hash, iterate through invoices and remove type_id 3's
|
|
|
|
// - Recalcuate invoice totals
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-01 16:19:03 +02:00
|
|
|
return $gateway
|
|
|
|
->driver(auth()->user()->client)
|
2020-06-16 02:21:40 +02:00
|
|
|
->setPaymentMethod($request->input('payment_method_id'))
|
2020-06-01 16:19:03 +02:00
|
|
|
->processPaymentResponse($request);
|
2019-09-25 06:03:28 +02:00
|
|
|
}
|
2019-08-16 07:20:28 +02:00
|
|
|
}
|