1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Http/Requests/CreatePaymentAPIRequest.php
David Bomba 0ceeefb10b Improve error reporting from API (#1194)
* apply API response across all requests

* apply API response across all requests
2016-12-13 17:52:09 +11:00

60 lines
1.2 KiB
PHP

<?php namespace App\Http\Requests;
use App\Models\Invoice;
class CreatePaymentAPIRequest extends PaymentRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->user()->can('create', ENTITY_PAYMENT);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
if ( ! $this->invoice_id || ! $this->amount) {
return [
'invoice_id' => 'required|numeric|min:1',
'amount' => 'required|numeric|min:0.01',
];
}
$invoice = Invoice::scope($this->invoice_id)
->invoices()
->whereIsPublic(true)
->firstOrFail();
$this->merge([
'invoice_id' => $invoice->id,
'client_id' => $invoice->client->id,
]);
$rules = [
'amount' => "required|numeric|between:0.01,{$invoice->balance}",
];
if ($this->payment_type_id == PAYMENT_TYPE_CREDIT) {
$rules['payment_type_id'] = 'has_credit:' . $invoice->client->public_id . ',' . $this->amount;
}
return $rules;
}
}