Add new entry point for livewire component payments

This commit is contained in:
David Bomba 2024-06-28 11:12:56 +10:00
parent 7189c494ea
commit 54af9cc667
2 changed files with 373 additions and 29 deletions

View File

@ -0,0 +1,344 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\ClientPortal;
use App\Exceptions\PaymentFailed;
use App\Jobs\Invoice\CheckGatewayFee;
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;
use App\Utils\Ninja;
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;
/**
* LivewireInstantPayment
*
* New entry point for livewire component
* payments.
*/
class LivewireInstantPayment
{
use MakesHash;
use MakesDates;
/**
* (bool) success
* (string) error - "displayed back to the user, either in error div, or in with() on redirect"
* (string) redirect - ie client.invoices.index
* (array) payload - the data needed to complete the payment
* (string) component - the payment component to be displayed
*
* @var array $responder
*/
private array $responder = [
'success' => true,
'error' => '',
'redirect' => '',
'payload' => [],
'component' => '',
];
/**
* is_credit_payment
*
* Indicates whether this is a credit payment
* @var bool
*/
private $is_credit_payment = false;
/**
* __construct
*
* contact() guard
* company_gateway_id
* payable_invoices[] ['invoice_id' => '', 'amount' => 0]
* ?signature
* ?signature_ip
* payment_method_id
* ?pre_payment
* ?frequency_id
* ?remaining_cycles
* ?is_recurring
* ?hash
*
* @param array $data
* @return void
*/
public function __construct(public array $data)
{
}
public function run()
{
$company_gateway = CompanyGateway::query()->find($this->data['company_gateway_id']);
if ($this->data['company_gateway_id'] == CompanyGateway::GATEWAY_CREDIT) {
$this->is_credit_payment = true;
}
$payable_invoices = collect($this->data['payable_invoices']);
$tokens = [];
$invoices = Invoice::query()
->whereIn('id', $this->transformKeys($payable_invoices->pluck('invoice_id')->toArray()))
->withTrashed()
->get()
->filter(function ($invoice){
$invoice =
$invoice->service()
->markSent()
->removeUnpaidGatewayFees()
->save();
return $invoice->isPayable();
});
/* 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();
});
/* return early */
if ($payable_invoices->count() == 0) {
$this->mergeResponder(['success' => false, 'error' => ctrans('texts.no_payable_invoices_selected')]);
return $this->getResponder();
}
/** Logic Loops for Under/Overpayments */
$invoices = Invoice::query()->whereIn('id', $this->transformKeys($payable_invoices->pluck('invoice_id')->toArray()))->withTrashed()->get();
$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
*/
$payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), $client->currency()->precision);
$invoice_balance = Number::roundValue(($invoice->partial > 0 ? $invoice->partial : $invoice->balance), $client->currency()->precision);
/*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) {
$this->mergeResponder(['success' => false, 'error' => ctrans('texts.minimum_required_payment', ['amount' => $invoice_balance]), 'redirect' => 'client.invoices.index']);
return $this->getResponder();
}
if ($settings->client_portal_allow_under_payment) {
if ($invoice_balance < $settings->client_portal_under_payment_minimum && $payable_amount < $invoice_balance) {
$this->mergeResponder(['success' => false, 'error' => ctrans('texts.minimum_required_payment', ['amount' => $invoice_balance]), 'redirect' => 'client.invoices.index']);
return $this->getResponder();
}
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) {
$this->mergeResponder(['success' => false, 'error' => ctrans('texts.minimum_required_payment', ['amount' => $settings->client_portal_under_payment_minimum]), 'redirect' => 'client.invoices.index']);
return $this->getResponder();
}
}
/* If we don't allow over payments and the amount exceeds the balance */
if (! $settings->client_portal_allow_over_payment && $payable_amount > $invoice_balance) {
$this->mergeResponder(['success' => false, 'error' => ctrans('texts.over_payments_disabled'), 'redirect' => 'client.invoices.index']);
return $this->getResponder();
}
}
/*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;
});
$payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), $client->currency()->precision);
$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 (isset($this->data['signature']) && $this->data['signature']) {
$contact_id = auth()->guard('contact')->user() ? auth()->guard('contact')->user()->id : null;
$invoices->each(function ($invoice) use ($contact_id) {
InjectSignature::dispatch($invoice, $contact_id, $this->data['signature'], $this->data['signature_ip']);
});
}
$payable_invoices = $payable_invoice_collection;
$payment_method_id = $this->data['payment_method_id'];
$invoice_totals = $payable_invoices->sum('amount');
$first_invoice = $invoices->first();
$credit_totals = in_array($first_invoice->client->getSetting('use_credits_payment'), ['always', 'option']) ? $first_invoice->client->service()->getCreditBalance() : 0;
$starting_invoice_amount = $first_invoice->balance;
if ($company_gateway) {
$first_invoice->service()->addGatewayFee($company_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 ($company_gateway) {
$tokens = $client->gateway_tokens()
->whereCompanyGatewayId($company_gateway->id)
->whereGatewayTypeId($payment_method_id)
->get();
}
if (! $this->is_credit_payment) {
$credit_totals = 0;
}
/** $hash_data = mixed[] */
$hash_data = [
'invoices' => $payable_invoices->toArray(),
'credits' => $credit_totals,
'amount_with_fee' => max(0, (($invoice_totals + $fee_totals) - $credit_totals)),
'pre_payment' => $this->data['pre_payment'],
'frequency_id' => $this->data['frequency_id'],
'remaining_cycles' => $this->data['remaining_cycles'],
'is_recurring' => $this->data['is_recurring'],
];
if (isset($this->data['hash'])) {
$hash_data['billing_context'] = Cache::get($this->data['hash']);
} elseif ($this->data['hash']) {
$hash_data['billing_context'] = Cache::get($this->data['hash']);
} elseif ($old_hash = PaymentHash::query()->where('fee_invoice_id', $first_invoice->id)->whereNull('payment_id')->orderBy('id', 'desc')->first()) {
if (isset($old_hash->data->billing_context)) {
$hash_data['billing_context'] = $old_hash->data->billing_context;
}
}
$payment_hash = new PaymentHash();
$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 ($this->is_credit_payment) {
$amount_with_fee = max(0, (($invoice_totals + $fee_totals) - $credit_totals));
} else {
$credit_totals = 0;
$amount_with_fee = max(0, $invoice_totals + $fee_totals);
}
$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,
'pre_payment' => $this->data['pre_payment'],
'is_recurring' => $this->data['is_recurring'],
'company_gateway' => $company_gateway,
];
if ($this->is_credit_payment) {
$this->mergeResponder(['success' => true, 'component' => 'CreditPaymentComponent', 'payload' => $data]);
return $this->getResponder();
}
$this->mergeResponder(['success' => true, 'payload' => $data]);
return $this->getResponder();
}
private function getResponder(): array
{
return $this->responder;
}
private function mergeResponder(array $data): self
{
$this->responder = array_merge($this->responder, $data);
return $this;
}
}

58
composer.lock generated
View File

@ -535,16 +535,16 @@
},
{
"name": "aws/aws-sdk-php",
"version": "3.315.0",
"version": "3.315.1",
"source": {
"type": "git",
"url": "https://github.com/aws/aws-sdk-php.git",
"reference": "a7f6026f00771025c32548dac321541face0dedc"
"reference": "13871330833e167d098240dab74b8b069b9b07e3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/a7f6026f00771025c32548dac321541face0dedc",
"reference": "a7f6026f00771025c32548dac321541face0dedc",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/13871330833e167d098240dab74b8b069b9b07e3",
"reference": "13871330833e167d098240dab74b8b069b9b07e3",
"shasum": ""
},
"require": {
@ -624,9 +624,9 @@
"support": {
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sdk-php/issues",
"source": "https://github.com/aws/aws-sdk-php/tree/3.315.0"
"source": "https://github.com/aws/aws-sdk-php/tree/3.315.1"
},
"time": "2024-06-26T18:08:22+00:00"
"time": "2024-06-27T18:03:53+00:00"
},
{
"name": "bacon/bacon-qr-code",
@ -4609,16 +4609,16 @@
},
{
"name": "laravel/framework",
"version": "v11.12.0",
"version": "v11.13.0",
"source": {
"type": "git",
"url": "https://github.com/laravel/framework.git",
"reference": "9a6d9cea83cfa6b9e8eda05c89741d0411d8ebe8"
"reference": "92deaa4f037ff100e36809443811301819a8cf84"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/framework/zipball/9a6d9cea83cfa6b9e8eda05c89741d0411d8ebe8",
"reference": "9a6d9cea83cfa6b9e8eda05c89741d0411d8ebe8",
"url": "https://api.github.com/repos/laravel/framework/zipball/92deaa4f037ff100e36809443811301819a8cf84",
"reference": "92deaa4f037ff100e36809443811301819a8cf84",
"shasum": ""
},
"require": {
@ -4810,7 +4810,7 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2024-06-25T19:33:56+00:00"
"time": "2024-06-27T09:04:50+00:00"
},
{
"name": "laravel/pint",
@ -11010,16 +11010,16 @@
},
{
"name": "sprain/swiss-qr-bill",
"version": "v4.12.1",
"version": "v4.13",
"source": {
"type": "git",
"url": "https://github.com/sprain/php-swiss-qr-bill.git",
"reference": "3728cd1366ac631a0587c0997a4878c37923e55b"
"reference": "5490e9139c4050d18533440cd9ff51a64955c035"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sprain/php-swiss-qr-bill/zipball/3728cd1366ac631a0587c0997a4878c37923e55b",
"reference": "3728cd1366ac631a0587c0997a4878c37923e55b",
"url": "https://api.github.com/repos/sprain/php-swiss-qr-bill/zipball/5490e9139c4050d18533440cd9ff51a64955c035",
"reference": "5490e9139c4050d18533440cd9ff51a64955c035",
"shasum": ""
},
"require": {
@ -11067,7 +11067,7 @@
"description": "A PHP library to create Swiss QR bills",
"support": {
"issues": "https://github.com/sprain/php-swiss-qr-bill/issues",
"source": "https://github.com/sprain/php-swiss-qr-bill/tree/v4.12.1"
"source": "https://github.com/sprain/php-swiss-qr-bill/tree/v4.13"
},
"funding": [
{
@ -11075,7 +11075,7 @@
"type": "github"
}
],
"time": "2024-05-16T07:19:59+00:00"
"time": "2024-06-27T11:17:56+00:00"
},
{
"name": "square/square",
@ -14820,16 +14820,16 @@
},
{
"name": "turbo124/beacon",
"version": "v2.0.0",
"version": "v2.0.2",
"source": {
"type": "git",
"url": "https://github.com/turbo124/beacon.git",
"reference": "6397c3fa575e9b670718b6f31f04bdf20aa83a72"
"reference": "95f3de3bdcbb786329cd7050f319520588920466"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/turbo124/beacon/zipball/6397c3fa575e9b670718b6f31f04bdf20aa83a72",
"reference": "6397c3fa575e9b670718b6f31f04bdf20aa83a72",
"url": "https://api.github.com/repos/turbo124/beacon/zipball/95f3de3bdcbb786329cd7050f319520588920466",
"reference": "95f3de3bdcbb786329cd7050f319520588920466",
"shasum": ""
},
"require": {
@ -14876,9 +14876,9 @@
"turbo124"
],
"support": {
"source": "https://github.com/turbo124/beacon/tree/v2.0.0"
"source": "https://github.com/turbo124/beacon/tree/v2.0.2"
},
"time": "2024-05-31T23:01:02+00:00"
"time": "2024-06-27T01:23:05+00:00"
},
{
"name": "twig/intl-extra",
@ -19006,16 +19006,16 @@
},
{
"name": "spatie/error-solutions",
"version": "1.0.2",
"version": "1.0.3",
"source": {
"type": "git",
"url": "https://github.com/spatie/error-solutions.git",
"reference": "9782ba6e25cb026cc653619e01ca695d428b3f03"
"reference": "55ea4117e0fde89d520883734ab9b71064c48876"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/error-solutions/zipball/9782ba6e25cb026cc653619e01ca695d428b3f03",
"reference": "9782ba6e25cb026cc653619e01ca695d428b3f03",
"url": "https://api.github.com/repos/spatie/error-solutions/zipball/55ea4117e0fde89d520883734ab9b71064c48876",
"reference": "55ea4117e0fde89d520883734ab9b71064c48876",
"shasum": ""
},
"require": {
@ -19068,7 +19068,7 @@
],
"support": {
"issues": "https://github.com/spatie/error-solutions/issues",
"source": "https://github.com/spatie/error-solutions/tree/1.0.2"
"source": "https://github.com/spatie/error-solutions/tree/1.0.3"
},
"funding": [
{
@ -19076,7 +19076,7 @@
"type": "github"
}
],
"time": "2024-06-26T13:09:17+00:00"
"time": "2024-06-27T12:22:48+00:00"
},
{
"name": "spatie/flare-client-php",