From 09d5b7c38f5198022b721d5f46c907ea496314ca Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 11 Jul 2022 18:24:49 +1000 Subject: [PATCH] Wrap paid to date in transaction --- app/Exceptions/PaymentFailed.php | 2 +- .../ValidationRules/ValidCreditsPresentRule.php | 2 +- app/Repositories/PaymentRepository.php | 2 +- app/Services/Payment/UpdateInvoicePayment.php | 15 ++++++++++----- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/Exceptions/PaymentFailed.php b/app/Exceptions/PaymentFailed.php index 7c0f2cb67711..b509df02fa77 100644 --- a/app/Exceptions/PaymentFailed.php +++ b/app/Exceptions/PaymentFailed.php @@ -13,7 +13,7 @@ class PaymentFailed extends Exception public function render($request) { - if (auth()->user() || ($request->has('cko-session-id') && $request->query('cko-session-id') )) { + if (auth()->guard('contact')->user() || ($request->has('cko-session-id') && $request->query('cko-session-id') )) { return render('gateways.unsuccessful', [ 'message' => $this->getMessage(), 'code' => $this->getCode(), diff --git a/app/Http/ValidationRules/ValidCreditsPresentRule.php b/app/Http/ValidationRules/ValidCreditsPresentRule.php index d2ad2f7fb72b..3cc5dab68028 100644 --- a/app/Http/ValidationRules/ValidCreditsPresentRule.php +++ b/app/Http/ValidationRules/ValidCreditsPresentRule.php @@ -44,7 +44,7 @@ class ValidCreditsPresentRule implements Rule { //todo need to ensure the clients credits are here not random ones! - if (request()->input('credits') && is_array(request()->input('credits'))) { + if (request()->input('credits') && is_array(request()->input('credits')) && count(request()->input('credits')) > 0) { $credit_collection = Credit::whereIn('id', $this->transformKeys(array_column(request()->input('credits'), 'credit_id'))) ->count(); diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 53170d76e718..1bdb4942949b 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -83,7 +83,7 @@ class PaymentRepository extends BaseRepository { $client->service()->updatePaidToDate($data['amount'])->save(); } - // elseif($data['amount'] >0){ + else{ //this fixes an edge case with unapplied payments $client->service()->updatePaidToDate($data['amount'])->save(); diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 12eecad1d1a2..d07308730158 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -14,6 +14,7 @@ namespace App\Services\Payment; use App\Events\Invoice\InvoiceWasUpdated; use App\Jobs\Invoice\InvoiceWorkflowSettings; use App\Jobs\Ninja\TransactionLog; +use App\Models\Client; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; @@ -48,8 +49,6 @@ class UpdateInvoicePayment collect($paid_invoices)->each(function ($paid_invoice) use ($invoices, $client) { - $client = $client->fresh(); - $invoice = $invoices->first(function ($inv) use ($paid_invoice) { return $paid_invoice->invoice_id == $inv->hashed_id; }); @@ -63,9 +62,15 @@ class UpdateInvoicePayment $paid_amount = $paid_invoice->amount; } - $client->paid_to_date += $paid_amount; - $client->balance -= $paid_amount; - $client->save(); + \DB::connection(config('database.default'))->transaction(function () use($client, $paid_amount){ + + $update_client = Client::withTrashed()->where('id', $client->id)->lockForUpdate()->first(); + + $update_client->paid_to_date += $paid_amount; + $update_client->balance -= $paid_amount; + $update_client->save(); + + }, 1); /* Need to determine here is we have an OVER payment - if YES only apply the max invoice amount */ if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance)