diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index 8e40f0b3478f..152a8aecff43 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -45,12 +45,12 @@ class StorePaymentRequest extends Request $rules = [ 'client_id' => ['bail','required',Rule::exists('clients','id')->where('company_id',$user->company()->id)->where('is_deleted', 0)], - 'amount' => ['bail', 'numeric', new PaymentAmountsBalanceRule()], + 'invoices' => ['bail','sometimes', 'nullable', 'array', new ValidPayableInvoicesRule()], 'invoices.*.amount' => ['bail','required'], 'invoices.*.invoice_id' => ['bail','required','distinct', new ValidInvoicesRules($this->all()),Rule::exists('invoices','id')->where('company_id', $user->company()->id)->where('client_id', $this->client_id)], 'credits.*.credit_id' => ['bail','required','distinct', new ValidCreditsRules($this->all()),Rule::exists('credits','id')->where('company_id', $user->company()->id)->where('client_id', $this->client_id)], 'credits.*.amount' => ['bail','required', new CreditsSumRule($this->all())], - 'invoices' => ['bail','sometimes', 'nullable', 'array', new ValidPayableInvoicesRule()], + 'amount' => ['bail', 'numeric', new PaymentAmountsBalanceRule()], 'number' => ['bail', 'nullable', 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)], ]; @@ -94,7 +94,7 @@ class StorePaymentRequest extends Request if (isset($input['invoices']) && is_array($input['invoices']) !== false) { foreach ($input['invoices'] as $key => $value) { - if (is_string($value['invoice_id'])) { + if (isset($value['invoice_id']) && is_string($value['invoice_id'])) { $input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']); } @@ -110,7 +110,7 @@ class StorePaymentRequest extends Request if (isset($input['credits']) && is_array($input['credits']) !== false) { foreach ($input['credits'] as $key => $value) { - if (array_key_exists('credit_id', $input['credits'][$key])) { + if (isset($value['credit_id']) && is_string($value['credit_id'])) { $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); $credits_total += $value['amount']; } diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 3dd9813def58..cf5816191f4d 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -62,6 +62,31 @@ class PaymentTest extends TestCase ); } + public function testInvoicesValidationProp() + { + + $data = [ + 'amount' => 5, + 'client_id' => $this->client->hashed_id, + 'invoices' => [ + [ + 'invoice_id:' => $this->invoice->hashed_id, + 'amount' => 5, + ], + ], + 'date' => '2020/12/11', + 'idempotency_key' => \Illuminate\Support\Str::uuid()->toString() + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments/', $data); + + $response->assertStatus(422); + + } + public function testClientIdValidation() { $p = Payment::factory()->create([