1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Services/ClientPortal/InstantPayment.php

315 lines
12 KiB
PHP
Raw Normal View History

2021-10-07 08:52:29 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2021-10-07 08:52:29 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\ClientPortal;
use App\Exceptions\PaymentFailed;
2023-01-11 05:43:54 +01:00
use App\Jobs\Invoice\CheckGatewayFee;
2021-10-07 08:52:29 +02:00
use App\Jobs\Invoice\InjectSignature;
use App\Jobs\Util\SystemLogger;
use App\Models\CompanyGateway;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\SystemLog;
2023-01-13 10:23:47 +01:00
use App\Utils\Ninja;
2021-10-07 08:52:29 +02:00
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
class InstantPayment
{
use MakesHash;
use MakesDates;
2023-03-17 02:49:08 +01:00
/** $request mixed */
2021-10-07 08:52:29 +02:00
public Request $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function run()
{
2024-02-20 06:05:00 +01:00
/** @var \App\Models\ClientContact $cc */
$cc = auth()->guard('contact')->user();
$cc->first_name = $this->request->contact_first_name;
$cc->last_name = $this->request->contact_last_name;
$cc->email = $this->request->contact_email;
2024-07-27 07:18:21 +02:00
$cc->client->postal_code = strlen($cc->client->postal_code ?? '') > 1 ? $cc->client->postal_code : $this->request->client_postal_code;
$cc->client->city = strlen($cc->client->city ?? '') > 1 ? $cc->client->city : $this->request->client_city;
2024-08-22 08:45:06 +02:00
$cc->client->shipping_postal_code = strlen($cc->client->shipping_postal_code ?? '') > 1 ? $cc->client->shipping_postal_code : $cc->client->postal_code;
2024-07-27 07:18:21 +02:00
$cc->client->shipping_city = strlen($cc->client->shipping_city ?? '') > 1 ? $cc->client->shipping_city : $cc->client->city;
$cc->pushQuietly();
2021-10-07 08:52:29 +02:00
$is_credit_payment = false;
$tokens = [];
if ($this->request->input('company_gateway_id') == CompanyGateway::GATEWAY_CREDIT) {
$is_credit_payment = true;
}
2023-08-08 10:56:31 +02:00
$gateway = CompanyGateway::query()->find($this->request->input('company_gateway_id'));
2021-10-07 08:52:29 +02:00
/**
* find invoices
*
* ['invoice_id' => xxx, 'amount' => 22.00]
*/
$payable_invoices = collect($this->request->payable_invoices);
2024-02-20 07:29:38 +01:00
2023-08-06 09:03:12 +02:00
$invoices = Invoice::query()->whereIn('id', $this->transformKeys($payable_invoices->pluck('invoice_id')->toArray()))->withTrashed()->get();
2021-10-07 08:52:29 +02:00
$invoices->each(function ($invoice) {
$invoice->service()
2021-10-22 11:56:47 +02:00
->markSent()
->removeUnpaidGatewayFees()
->save();
2021-10-07 08:52:29 +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();
});
/*return early if no invoices*/
if ($payable_invoices->count() == 0) {
return redirect()
->route('client.invoices.index')
->with(['message' => 'No payable invoices selected.']);
}
2023-11-20 05:31:40 +01:00
$invoices = Invoice::query()->whereIn('id', $this->transformKeys($payable_invoices->pluck('invoice_id')->toArray()))->withTrashed()->get();
2021-10-07 08:52:29 +02:00
$client = $invoices->first()->client;
$settings = $client->getMergedSettings();
/* This loop checks for under / over payments and returns the user if a check fails */
foreach ($payable_invoices as $payable_invoice) {
/*Match the payable invoice to the Model Invoice*/
$invoice = $invoices->first(function ($inv) use ($payable_invoice) {
return $payable_invoice['invoice_id'] == $inv->hashed_id;
});
/*
* Check if company supports over & under payments.
* Determine the payable amount and the max payable. ie either partial or invoice balance
*/
2023-08-08 11:44:52 +02:00
$payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), $client->currency()->precision);
2021-10-07 08:52:29 +02:00
$invoice_balance = Number::roundValue(($invoice->partial > 0 ? $invoice->partial : $invoice->balance), $client->currency()->precision);
2021-10-07 08:52:29 +02:00
/*If we don't allow under/over payments force the payable amount - prevents inspect element adjustments in JS*/
if ($settings->client_portal_allow_under_payment == false && $settings->client_portal_allow_over_payment == false) {
$payable_invoice['amount'] = Number::roundValue(($invoice->partial > 0 ? $invoice->partial : $invoice->balance), $client->currency()->precision);
}
if (! $settings->client_portal_allow_under_payment && $payable_amount < $invoice_balance) {
2021-10-07 08:52:29 +02:00
return redirect()
->route('client.invoices.index')
->with('message', ctrans('texts.minimum_required_payment', ['amount' => $invoice_balance]));
}
if ($settings->client_portal_allow_under_payment) {
if ($invoice_balance < $settings->client_portal_under_payment_minimum && $payable_amount < $invoice_balance) {
return redirect()
->route('client.invoices.index')
->with('message', ctrans('texts.minimum_required_payment', ['amount' => $invoice_balance]));
}
if ($invoice_balance < $settings->client_portal_under_payment_minimum) {
// Skip the under payment rule.
}
if ($invoice_balance >= $settings->client_portal_under_payment_minimum && $payable_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]));
}
}
/* If we don't allow over payments and the amount exceeds the balance */
if (! $settings->client_portal_allow_over_payment && $payable_amount > $invoice_balance) {
2021-10-07 08:52:29 +02:00
return redirect()
->route('client.invoices.index')
->with('message', ctrans('texts.over_payments_disabled'));
}
}
/*Iterate through invoices and add gateway fees and other payment metadata*/
//$payable_invoices = $payable_invoices->map(function ($payable_invoice) use ($invoices, $settings) {
$payable_invoice_collection = collect();
foreach ($payable_invoices as $payable_invoice) {
$payable_invoice['amount'] = Number::parseFloat($payable_invoice['amount']);
$invoice = $invoices->first(function ($inv) use ($payable_invoice) {
return $payable_invoice['invoice_id'] == $inv->hashed_id;
});
2023-08-08 11:44:52 +02:00
$payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), $client->currency()->precision);
2021-10-07 08:52:29 +02:00
$invoice_balance = Number::roundValue($invoice->balance, $client->currency()->precision);
$payable_invoice['due_date'] = $this->formatDate($invoice->due_date, $invoice->client->date_format());
$payable_invoice['invoice_number'] = $invoice->number;
if (isset($invoice->po_number)) {
$additional_info = $invoice->po_number;
} elseif (isset($invoice->public_notes)) {
$additional_info = $invoice->public_notes;
} else {
$additional_info = $invoice->date;
}
$payable_invoice['additional_info'] = $additional_info;
$payable_invoice_collection->push($payable_invoice);
}
if ($this->request->has('signature') && ! is_null($this->request->signature) && ! empty($this->request->signature)) {
2024-01-14 05:05:00 +01:00
2023-07-26 04:18:00 +02:00
$contact_id = auth()->guard('contact')->user() ? auth()->guard('contact')->user()->id : null;
2023-10-26 04:57:44 +02:00
$invoices->each(function ($invoice) use ($contact_id) {
InjectSignature::dispatch($invoice, $contact_id, $this->request->signature, request()->getClientIp());
2021-10-07 08:52:29 +02:00
});
}
$payable_invoices = $payable_invoice_collection;
$payment_method_id = $this->request->input('payment_method_id');
$invoice_totals = $payable_invoices->sum('amount');
$first_invoice = $invoices->first();
2023-02-04 07:09:04 +01:00
$credit_totals = in_array($first_invoice->client->getSetting('use_credits_payment'), ['always', 'option']) ? $first_invoice->client->service()->getCreditBalance() : 0;
2021-10-07 08:52:29 +02:00
$starting_invoice_amount = $first_invoice->balance;
if ($gateway) {
$first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save();
}
/**
* Gateway fee is calculated
* by adding it as a line item, and then subtract
* the starting and finishing amounts of the invoice.
*/
$fee_totals = $first_invoice->balance - $starting_invoice_amount;
if ($gateway) {
$tokens = $client->gateway_tokens()
->whereCompanyGatewayId($gateway->id)
->whereGatewayTypeId($payment_method_id)
->get();
}
if (! $is_credit_payment) {
2021-10-07 08:52:29 +02:00
$credit_totals = 0;
}
2023-03-17 02:49:08 +01:00
/** $hash_data = mixed[] */
$hash_data = [
2023-03-18 08:24:56 +01:00
'invoices' => $payable_invoices->toArray(),
'credits' => $credit_totals,
'amount_with_fee' => max(0, (($invoice_totals + $fee_totals) - $credit_totals)),
2023-03-17 02:49:08 +01:00
'pre_payment' => $this->request->pre_payment,
'frequency_id' => $this->request->frequency_id,
'remaining_cycles' => $this->request->remaining_cycles,
'is_recurring' => $this->request->is_recurring,
];
2021-10-07 08:52:29 +02:00
if ($this->request->query('hash')) {
$hash_data['billing_context'] = Cache::get($this->request->query('hash'));
2023-02-16 02:36:09 +01:00
} elseif ($this->request->hash) {
$hash_data['billing_context'] = Cache::get($this->request->hash);
2024-08-22 08:45:06 +02:00
} elseif ($old_hash = PaymentHash::query()->where('fee_invoice_id', $first_invoice->id)->whereNull('payment_id')->orderBy('id', 'desc')->first()) {
2023-02-16 02:36:09 +01:00
if (isset($old_hash->data->billing_context)) {
$hash_data['billing_context'] = $old_hash->data->billing_context;
}
}
2021-10-07 08:52:29 +02:00
2024-01-14 05:05:00 +01:00
$payment_hash = new PaymentHash();
2021-10-07 08:52:29 +02:00
$payment_hash->hash = Str::random(32);
$payment_hash->data = $hash_data;
$payment_hash->fee_total = $fee_totals;
$payment_hash->fee_invoice_id = $first_invoice->id;
$payment_hash->save();
if ($is_credit_payment) {
2021-10-07 08:52:29 +02:00
$amount_with_fee = max(0, (($invoice_totals + $fee_totals) - $credit_totals));
} else {
2021-10-07 08:52:29 +02:00
$credit_totals = 0;
$amount_with_fee = max(0, $invoice_totals + $fee_totals);
2021-10-07 08:52:29 +02:00
}
$totals = [
'credit_totals' => $credit_totals,
'invoice_totals' => $invoice_totals,
'fee_total' => $fee_totals,
'amount_with_fee' => $amount_with_fee,
];
$data = [
'payment_hash' => $payment_hash->hash,
'total' => $totals,
'invoices' => $payable_invoices,
'tokens' => $tokens,
'payment_method_id' => $payment_method_id,
'amount_with_fee' => $invoice_totals + $fee_totals,
'client' => $client,
2023-03-17 03:55:46 +01:00
'pre_payment' => $this->request->pre_payment,
2023-03-17 04:38:00 +01:00
'is_recurring' => $this->request->is_recurring,
2021-10-07 08:52:29 +02:00
];
2023-08-08 11:11:59 +02:00
if ($is_credit_payment) {
2021-10-07 08:52:29 +02:00
return $this->processCreditPayment($this->request, $data);
}
try {
return $gateway
->driver($client)
->setPaymentMethod($payment_method_id)
->setPaymentHash($payment_hash)
->checkRequirements()
->processPaymentView($data);
} catch (\Exception $e) {
SystemLogger::dispatch(
$e->getMessage(),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_FAILURE,
$client,
$client->company
);
throw new PaymentFailed($e->getMessage());
}
}
public function processCreditPayment(Request $request, array $data)
2023-02-16 02:36:09 +01:00
{
2021-10-07 08:52:29 +02:00
return render('gateways.credit.index', $data);
}
}