2020-01-20 11:10:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-20 11:10:33 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-01-20 11:10:33 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Payment;
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
2020-01-29 03:03:47 +01:00
|
|
|
use App\Http\ValidationRules\Payment\ValidRefundableRequest;
|
2020-01-20 11:10:33 +01:00
|
|
|
use App\Http\ValidationRules\ValidRefundableInvoices;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
|
|
|
|
class RefundPaymentRequest extends Request
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use MakesHash;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize() : bool
|
|
|
|
{
|
|
|
|
return auth()->user()->isAdmin();
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
protected function prepareForValidation()
|
|
|
|
{
|
|
|
|
$input = $this->all();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
|
|
|
if (! isset($input['gateway_refund'])) {
|
2020-03-21 06:37:30 +01:00
|
|
|
$input['gateway_refund'] = false;
|
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! isset($input['send_email'])) {
|
2020-01-29 03:03:47 +01:00
|
|
|
$input['send_email'] = false;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (isset($input['id'])) {
|
|
|
|
$input['id'] = $this->decodePrimaryKey($input['id']);
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (isset($input['invoices'])) {
|
|
|
|
foreach ($input['invoices'] as $key => $invoice) {
|
2020-01-29 03:03:47 +01:00
|
|
|
$input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($invoice['invoice_id']);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (isset($input['credits'])) {
|
2020-02-01 21:45:23 +01:00
|
|
|
unset($input['credits']);
|
2020-03-21 06:37:30 +01:00
|
|
|
// foreach($input['credits'] as $key => $credit)
|
2020-02-01 21:45:23 +01:00
|
|
|
// $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($credit['credit_id']);
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->replace($input);
|
2020-01-20 11:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
2020-02-05 22:54:20 +01:00
|
|
|
$input = $this->all();
|
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
$rules = [
|
2020-04-11 13:19:05 +02:00
|
|
|
'id' => 'bail|required',
|
2020-02-05 22:54:20 +01:00
|
|
|
'id' => new ValidRefundableRequest($input),
|
2020-01-30 05:50:45 +01:00
|
|
|
'amount' => 'numeric',
|
2020-01-20 11:10:33 +01:00
|
|
|
'date' => 'required',
|
|
|
|
'invoices.*.invoice_id' => 'required',
|
2020-01-30 05:50:45 +01:00
|
|
|
'invoices.*.amount' => 'required',
|
2020-02-05 22:54:20 +01:00
|
|
|
'invoices' => new ValidRefundableInvoices($input),
|
2020-01-20 11:10:33 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
|
|
|
public function payment() :?Payment
|
|
|
|
{
|
2020-02-05 22:54:20 +01:00
|
|
|
$input = $this->all();
|
|
|
|
|
|
|
|
return Payment::whereId($input['id'])->first();
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
}
|