From 1ba1b1b1a3f3605c2e18dc0f8183003749069401 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 7 Nov 2023 17:15:50 +1100 Subject: [PATCH] Improvements for payment idempotency --- app/Http/Requests/Payment/StorePaymentRequest.php | 6 ++++-- app/Jobs/Ledger/UpdateLedger.php | 2 +- app/Models/Payment.php | 1 + 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index b110abcf38ec..c57a3e0c37ab 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -96,6 +96,10 @@ class StorePaymentRequest extends Request $input['date'] = now()->addSeconds($user->company()->timezone()->utc_offset)->format('Y-m-d'); } + if (! isset($input['idempotency_key'])) { + $input['idempotency_key'] = substr(sha1(json_encode($input)).time()."{$input['date']}{$input['amount']}{$input['client_id']}{$user->id}",0,64); + } + $this->replace($input); } @@ -106,7 +110,6 @@ class StorePaymentRequest extends Request $rules = [ 'amount' => ['numeric', 'bail', new PaymentAmountsBalanceRule(), new ValidCreditsPresentRule($this->all())], - // 'client_id' => 'bail|required|exists:clients,id', 'client_id' => 'bail|required|exists:clients,id,company_id,'.$user->company()->id.',is_deleted,0', 'invoices.*.invoice_id' => 'bail|required|distinct|exists:invoices,id', 'invoices.*.amount' => 'bail|required', @@ -117,7 +120,6 @@ class StorePaymentRequest extends Request 'invoices' => new ValidPayableInvoicesRule(), 'number' => ['nullable', 'bail', Rule::unique('payments')->where('company_id', $user->company()->id)], 'idempotency_key' => ['nullable', 'bail', 'string','max:64', Rule::unique('payments')->where('company_id', $user->company()->id)], - ]; if ($this->file('documents') && is_array($this->file('documents'))) { diff --git a/app/Jobs/Ledger/UpdateLedger.php b/app/Jobs/Ledger/UpdateLedger.php index 54cda67b14f9..aa870bde4b68 100644 --- a/app/Jobs/Ledger/UpdateLedger.php +++ b/app/Jobs/Ledger/UpdateLedger.php @@ -40,7 +40,7 @@ class UpdateLedger implements ShouldQueue */ public function handle() :void { - nlog("Updating company ledger for client ". $this->company_ledger_id); + // nlog("Updating company ledger for client ". $this->company_ledger_id); MultiDB::setDb($this->db); diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 1f80aaed0a91..45e9f6bb3212 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -158,6 +158,7 @@ class Payment extends BaseModel 'custom_value3', 'custom_value4', 'category_id', + 'idempotency_key', ]; protected $casts = [