diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index 2deffed782ec..c74f91d78ab4 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -54,7 +54,10 @@ class StorePaymentRequest extends Request if (isset($input['invoices']) && is_array($input['invoices']) !== false) { foreach ($input['invoices'] as $key => $value) { $input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']); - $invoices_total += $value['amount']; + + if(array_key_exists('amount', $value)) + $invoices_total += $value['amount']; + } } @@ -91,12 +94,12 @@ class StorePaymentRequest extends Request public function rules() { $rules = [ - 'amount' => 'numeric|required', + 'amount' => 'sometimes|numeric', 'amount' => [new PaymentAmountsBalanceRule(), new ValidCreditsPresentRule()], 'client_id' => 'bail|required|exists:clients,id', 'invoices.*.invoice_id' => 'bail|required|distinct|exists:invoices,id', + 'invoices.*.amount' => 'bail|required', 'invoices.*.invoice_id' => new ValidInvoicesRules($this->all()), - 'invoices.*.amount' => 'required', 'credits.*.credit_id' => 'bail|required|exists:credits,id', 'credits.*.credit_id' => new ValidCreditsRules($this->all()), 'credits.*.amount' => ['required', new CreditsSumRule($this->all())], diff --git a/app/Http/ValidationRules/Payment/ValidInvoicesRules.php b/app/Http/ValidationRules/Payment/ValidInvoicesRules.php index 0bc63580ebc7..f3a333a091c7 100644 --- a/app/Http/ValidationRules/Payment/ValidInvoicesRules.php +++ b/app/Http/ValidationRules/Payment/ValidInvoicesRules.php @@ -57,6 +57,11 @@ class ValidInvoicesRules implements Rule $unique_array[] = $invoice['invoice_id']; + if(!array_key_exists('amount', $invoice)){ + $this->error_msg = ctrans('texts.amount') . " required"; + return false; + } + $inv = Invoice::whereId($invoice['invoice_id'])->first(); if (! $inv) { diff --git a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php index b29c6ab06a18..7abba2b7870e 100644 --- a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php +++ b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php @@ -79,6 +79,11 @@ class ValidRefundableRequest implements Rule { $invoice = Invoice::whereId($invoice['invoice_id'])->whereCompanyId($payment->company_id)->withTrashed()->first(); + if(!$invoice){ + $this->error_msg = "Invoice not found for refund"; + return false; + } + if ($payment->invoices()->exists()) { $paymentable_invoice = $payment->invoices->where('id', $invoice->id)->first(); diff --git a/app/Http/ValidationRules/PaymentAmountsBalanceRule.php b/app/Http/ValidationRules/PaymentAmountsBalanceRule.php index 2097b1b77c1d..be8f0d60ec98 100644 --- a/app/Http/ValidationRules/PaymentAmountsBalanceRule.php +++ b/app/Http/ValidationRules/PaymentAmountsBalanceRule.php @@ -59,13 +59,17 @@ class PaymentAmountsBalanceRule implements Rule if (request()->input('credits') && is_array(request()->input('credits'))) { foreach (request()->input('credits') as $credit) { - $payment_amounts += $credit['amount']; + + if(array_key_exists('amount', $credit)) + $payment_amounts += $credit['amount']; } } if (request()->input('invoices') && is_array(request()->input('invoices'))) { foreach (request()->input('invoices') as $invoice) { - $invoice_amounts += $invoice['amount']; + + if(array_key_exists('amount', $invoice)) + $invoice_amounts += $invoice['amount']; } } else { return true; diff --git a/tests/Feature/Payments/StorePaymentValidationTest.php b/tests/Feature/Payments/StorePaymentValidationTest.php new file mode 100644 index 000000000000..7a4d11a44d58 --- /dev/null +++ b/tests/Feature/Payments/StorePaymentValidationTest.php @@ -0,0 +1,167 @@ +faker = \Faker\Factory::create(); + + Model::reguard(); + + $this->makeTestData(); + + + $this->withoutMiddleware( + ThrottleRequests::class + ); + } + + + public function testValidPayment() + { + + $data = [ + 'amount' => 0, + 'client_id' => $this->client->hashed_id, + 'invoices' => [ + ], + 'date' => '2019/12/12', + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments/', $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + nlog($e->validator->getMessageBag()); + } + + $response->assertStatus(200); + + } + + + public function testValidPaymentWithAmount() + { + + $data = [ + 'amount' => 0, + 'client_id' => $this->client->hashed_id, + 'invoices' => [ + [ + 'invoice_id' => $this->invoice->hashed_id, + 'amount' => 10, + ], + ], + 'credits' => [ + [ + 'credit_id' => $this->credit->hashed_id, + 'amount' => 5 + ] + ], + 'date' => '2019/12/12', + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments/', $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + nlog($e->validator->getMessageBag()); + } + + $response->assertStatus(200); + + } + + public function testValidPaymentWithInvalidData() + { + + + $data = [ + 'amount' => 0, + 'client_id' => $this->client->hashed_id, + 'invoices' => [ + [ + 'invoice_id' => $this->invoice->hashed_id, + ], + ], + 'credits' => [ + [ + 'credit_id' => $this->credit->hashed_id, + 'amount' => 5 + ] + ], + 'date' => '2019/12/12', + ]; + + $response = false; + + try{ + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments/', $data); + }catch(ValidationException $e){ + $response->assertStatus(302); + } + + } + + + +} +