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;
|
|
|
|
|
2020-10-13 14:28:30 +02:00
|
|
|
use App\Factory\PaymentFactory;
|
2019-08-16 07:20:28 +02:00
|
|
|
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;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2019-08-16 07:20:28 +02:00
|
|
|
use Illuminate\Http\Request;
|
2020-08-26 02:47:50 +02:00
|
|
|
use Illuminate\Support\Str;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\View\View;
|
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-10-28 11:10:49 +01:00
|
|
|
* @return Factory|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
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Factory|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-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @return RedirectResponse|mixed
|
2019-09-12 08:10:21 +02:00
|
|
|
*/
|
2020-10-15 02:37:16 +02:00
|
|
|
public function process(Request $request)
|
2020-03-26 04:23:57 +01:00
|
|
|
{
|
2020-10-15 05:35:35 +02:00
|
|
|
$is_credit_payment = false;
|
|
|
|
$token = false;
|
|
|
|
|
2020-10-15 02:37:16 +02:00
|
|
|
if($request->input('company_gateway_id') == CompanyGateway::GATEWAY_CREDIT)
|
2020-10-15 05:35:35 +02:00
|
|
|
$is_credit_payment = true;
|
2020-10-15 02:37:16 +02:00
|
|
|
|
2020-10-15 05:35:35 +02:00
|
|
|
$gateway = CompanyGateway::find($request->input('company_gateway_id'));
|
2020-09-24 11:29:47 +02:00
|
|
|
|
2020-10-15 02:37:16 +02:00
|
|
|
//refactor from here!
|
|
|
|
|
2020-10-13 12:32:15 +02:00
|
|
|
/**
|
|
|
|
* find invoices
|
|
|
|
*
|
|
|
|
* ['invoice_id' => xxx, 'amount' => 22.00]
|
|
|
|
*/
|
|
|
|
|
2020-10-15 05:35:35 +02:00
|
|
|
$payable_invoices = collect($request->payable_invoices);
|
2020-10-13 12:32:15 +02:00
|
|
|
$invoices = Invoice::whereIn('id', $this->transformKeys($payable_invoices->pluck('invoice_id')->toArray()))->get();
|
2020-09-24 11:29:47 +02:00
|
|
|
|
2020-10-13 12:32:15 +02:00
|
|
|
/* pop non payable invoice from the $payable_invoices array */
|
|
|
|
$payable_invoices = $payable_invoices->filter(function ($payable_invoice) use ($invoices){
|
|
|
|
|
|
|
|
return $invoices->where('hashed_id', $payable_invoice['invoice_id'])->first()->isPayable();
|
2019-09-12 08:10:21 +02:00
|
|
|
|
2019-10-08 05:06:27 +02:00
|
|
|
});
|
|
|
|
|
2020-08-25 15:18:17 +02:00
|
|
|
/*return early if no invoices*/
|
2020-10-13 12:32:15 +02:00
|
|
|
if ($payable_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-09-24 11:29:47 +02:00
|
|
|
$settings = auth()->user()->client->getMergedSettings();
|
2020-08-26 02:53:11 +02:00
|
|
|
|
2020-09-24 11:29:47 +02:00
|
|
|
/*iterate through invoices and add gateway fees and other payment metadata*/
|
2020-10-13 12:32:15 +02:00
|
|
|
$payable_invoices = $payable_invoices->map(function($payable_invoice) use($invoices, $settings){
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2020-10-13 12:32:15 +02:00
|
|
|
$payable_invoice['amount'] = Number::parseFloat($payable_invoice['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-09-24 11:29:47 +02:00
|
|
|
// Check if company supports over & under payments.
|
|
|
|
// In case it doesn't this is where process should stop.
|
|
|
|
|
|
|
|
$payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), auth()->user()->client->currency()->precision);
|
2020-10-09 16:08:12 +02:00
|
|
|
$invoice_balance = Number::roundValue($invoice->balance, auth()->user()->client->currency()->precision);
|
2020-09-24 11:29:47 +02:00
|
|
|
|
|
|
|
if ($settings->client_portal_allow_under_payment == false && $settings->client_portal_allow_over_payment == false) {
|
2020-09-29 14:16:01 +02:00
|
|
|
$payable_invoice['amount'] = Number::roundValue(($invoice->partial > 0 ? $invoice->partial : $invoice->balance), auth()->user()->client->currency()->precision);
|
2020-09-24 11:29:47 +02:00
|
|
|
} // We don't allow either of these, reset the amount to default invoice (to prevent inspect element payments).
|
|
|
|
|
|
|
|
if ($settings->client_portal_allow_under_payment) {
|
|
|
|
if ($payable_invoice['amount'] < $settings->client_portal_under_payment_minimum) {
|
|
|
|
return redirect()
|
|
|
|
->route('client.invoices.index')
|
|
|
|
->with('message', ctrans('texts.minimum_required_payment', ['amount' => $settings->client_portal_under_payment_minimum]));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), auth()->user()->client->currency()->precision);
|
2020-10-09 16:08:12 +02:00
|
|
|
$invoice_balance = Number::roundValue($invoice->balance, auth()->user()->client->currency()->precision);
|
2020-09-24 11:29:47 +02:00
|
|
|
|
2020-10-09 16:08:12 +02:00
|
|
|
if ($payable_amount < $invoice_balance) {
|
2020-09-24 11:29:47 +02:00
|
|
|
return redirect()
|
|
|
|
->route('client.invoices.index')
|
|
|
|
->with('message', ctrans('texts.under_payments_disabled'));
|
|
|
|
}
|
|
|
|
} // Make sure 'amount' from form is not lower than 'amount' from invoice.
|
|
|
|
|
|
|
|
if ($settings->client_portal_allow_over_payment == false) {
|
2020-10-09 16:08:12 +02:00
|
|
|
if ($payable_amount > $invoice_balance) {
|
2020-09-24 11:29:47 +02:00
|
|
|
return redirect()
|
|
|
|
->route('client.invoices.index')
|
|
|
|
->with('message', ctrans('texts.over_payments_disabled'));
|
|
|
|
}
|
|
|
|
} // Make sure 'amount' from form is not higher than 'amount' from invoice.
|
|
|
|
|
2020-10-13 12:32:15 +02:00
|
|
|
$payable_invoice['due_date'] = $this->formatDate($invoice->due_date, $invoice->client->date_format());
|
|
|
|
$payable_invoice['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-10-13 12:32:15 +02:00
|
|
|
$payable_invoice['additional_info'] = $additional_info;
|
|
|
|
|
|
|
|
return $payable_invoice;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2020-10-15 05:35:35 +02:00
|
|
|
if ((bool) $request->signature) {
|
2020-06-01 14:29:41 +02:00
|
|
|
$invoices->each(function ($invoice) {
|
2020-10-15 05:35:35 +02:00
|
|
|
InjectSignature::dispatch($invoice, $request->signature);
|
2020-06-01 14:29:41 +02:00
|
|
|
});
|
|
|
|
}
|
2020-05-28 15:59:45 +02:00
|
|
|
|
2020-10-15 05:35:35 +02:00
|
|
|
$payment_method_id = $request->input('payment_method_id');
|
2020-10-13 12:32:15 +02:00
|
|
|
$invoice_totals = $payable_invoices->sum('amount');
|
2020-08-31 04:00:43 +02:00
|
|
|
$first_invoice = $invoices->first();
|
2020-10-26 20:10:04 +01:00
|
|
|
$credit_totals = $first_invoice->client->getSetting('use_credits_payment') == 'off' ? 0 : $first_invoice->client->service()->getCreditBalance();
|
2020-10-13 05:25:51 +02:00
|
|
|
$starting_invoice_amount = $first_invoice->amount;
|
2020-08-31 04:00:43 +02:00
|
|
|
|
2020-10-15 05:35:35 +02:00
|
|
|
if($gateway)
|
|
|
|
$first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save();
|
2020-10-10 23:31:50 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* Gateway fee is calculated
|
|
|
|
* by adding it as a line item, and then subtract
|
2020-10-15 02:37:16 +02:00
|
|
|
* the starting and finishing amounts of the invoice.
|
2020-10-10 23:31:50 +02:00
|
|
|
*/
|
|
|
|
$fee_totals = $first_invoice->amount - $starting_invoice_amount;
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-10-15 05:35:35 +02:00
|
|
|
if($gateway)
|
|
|
|
$token = auth()->user()->client->gateway_token($gateway->id, $payment_method_id);
|
|
|
|
|
2020-08-26 02:47:50 +02:00
|
|
|
$payment_hash = new PaymentHash;
|
|
|
|
$payment_hash->hash = Str::random(128);
|
2020-10-22 15:24:18 +02:00
|
|
|
$payment_hash->data = ['invoices' => $payable_invoices->toArray()];
|
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-10-13 05:25:51 +02:00
|
|
|
'credit_totals' => $credit_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-10-13 05:25:51 +02:00
|
|
|
'amount_with_fee' => max(0, (($invoice_totals + $fee_totals) - $credit_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,
|
2020-10-15 05:35:35 +02:00
|
|
|
'token' => $token,
|
2019-09-25 04:07:33 +02:00
|
|
|
'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-11-02 16:20:38 +01:00
|
|
|
if($is_credit_payment) {
|
2020-10-15 05:35:35 +02:00
|
|
|
return $this->processCreditPayment($request, $data);
|
2020-11-02 16:20:38 +01:00
|
|
|
}
|
2020-10-15 05:35:35 +02: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-10-27 12:53:35 +01:00
|
|
|
->setPaymentHash($payment_hash)
|
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-27 14:12:39 +02:00
|
|
|
$gateway = CompanyGateway::find($request->input('company_gateway_id'))->firstOrFail();
|
2020-10-27 12:53:35 +01:00
|
|
|
$payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->payment_hash])->first();
|
2020-08-27 14:12:39 +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-10-27 12:53:35 +01:00
|
|
|
->setPaymentHash($payment_hash)
|
2020-06-01 16:19:03 +02:00
|
|
|
->processPaymentResponse($request);
|
2019-09-25 06:03:28 +02:00
|
|
|
}
|
2020-10-13 12:32:15 +02:00
|
|
|
|
2020-10-14 01:53:20 +02:00
|
|
|
/**
|
|
|
|
* Pay for invoice/s using credits only.
|
2020-10-28 11:10:49 +01:00
|
|
|
*
|
2020-10-14 01:53:20 +02:00
|
|
|
* @param Request $request The request object
|
|
|
|
* @return Response The response view
|
|
|
|
*/
|
2020-10-13 12:32:15 +02:00
|
|
|
public function credit_response(Request $request)
|
|
|
|
{
|
2020-10-14 01:53:20 +02:00
|
|
|
$payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->input('payment_hash')])->first();
|
2020-10-13 14:28:30 +02:00
|
|
|
|
2020-10-14 01:53:20 +02:00
|
|
|
/* Hydrate the $payment */
|
|
|
|
if($payment_hash->payment()->exists())
|
2020-10-13 14:28:30 +02:00
|
|
|
$payment = $payment_hash->payment;
|
|
|
|
else {
|
2020-10-14 01:53:20 +02:00
|
|
|
$payment = PaymentFactory::create($payment_hash->fee_invoice->company_id, $payment_hash->fee_invoice->user_id);
|
|
|
|
$payment->client_id = $payment_hash->fee_invoice->client_id;
|
|
|
|
$payment->save();
|
|
|
|
|
2020-10-13 14:28:30 +02:00
|
|
|
$payment_hash->payment_id = $payment->id;
|
|
|
|
$payment_hash->save();
|
|
|
|
}
|
|
|
|
|
2020-10-14 01:53:20 +02:00
|
|
|
/* Iterate through the invoices and apply credits to them */
|
2020-10-13 14:28:30 +02:00
|
|
|
collect($payment_hash->invoices())->each(function ($payable_invoice) use ($payment, $payment_hash){
|
|
|
|
|
2020-10-14 01:53:20 +02:00
|
|
|
$invoice = Invoice::find($this->decodePrimaryKey($payable_invoice->invoice_id));
|
|
|
|
$amount = $payable_invoice->amount;
|
2020-10-13 14:28:30 +02:00
|
|
|
|
|
|
|
$credits = $payment_hash->fee_invoice
|
|
|
|
->client
|
|
|
|
->service()
|
|
|
|
->getCredits();
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2020-10-13 14:28:30 +02:00
|
|
|
foreach($credits as $credit)
|
2020-10-28 11:10:49 +01:00
|
|
|
{
|
2020-10-13 14:28:30 +02:00
|
|
|
//starting invoice balance
|
|
|
|
$invoice_balance = $invoice->balance;
|
|
|
|
|
|
|
|
//credit payment applied
|
2020-10-14 01:53:20 +02:00
|
|
|
$credit->service()->applyPayment($invoice, $amount, $payment);
|
2020-10-13 14:28:30 +02:00
|
|
|
|
|
|
|
//amount paid from invoice calculated
|
2020-10-14 01:53:20 +02:00
|
|
|
$remaining_balance = ($invoice_balance - $invoice->fresh()->balance);
|
2020-10-13 14:28:30 +02:00
|
|
|
|
|
|
|
//reduce the amount to be paid on the invoice from the NEXT credit
|
|
|
|
$amount -= $remaining_balance;
|
|
|
|
|
|
|
|
//break if the invoice is no longer PAYABLE OR there is no more amount to be applied
|
|
|
|
if(!$invoice->isPayable() || (int)$amount == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2020-10-14 01:53:20 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
|
2020-10-13 14:28:30 +02:00
|
|
|
|
2020-10-13 12:32:15 +02:00
|
|
|
}
|
2020-10-15 02:37:16 +02:00
|
|
|
|
2020-10-15 05:35:35 +02:00
|
|
|
public function processCreditPayment(Request $request, array $data)
|
2020-10-15 02:37:16 +02:00
|
|
|
{
|
|
|
|
|
2020-10-15 05:35:35 +02:00
|
|
|
return render('gateways.credit.index', $data);
|
|
|
|
|
2020-10-15 02:37:16 +02:00
|
|
|
}
|
2019-08-16 07:20:28 +02:00
|
|
|
}
|