mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-05 01:54:36 -04:00
Fixes for payment request
This commit is contained in:
parent
f3947b104a
commit
5f51ea9002
@ -54,7 +54,10 @@ class StorePaymentRequest extends Request
|
|||||||
if (isset($input['invoices']) && is_array($input['invoices']) !== false) {
|
if (isset($input['invoices']) && is_array($input['invoices']) !== false) {
|
||||||
foreach ($input['invoices'] as $key => $value) {
|
foreach ($input['invoices'] as $key => $value) {
|
||||||
$input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
|
$input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
|
||||||
|
|
||||||
|
if(array_key_exists('amount', $value))
|
||||||
$invoices_total += $value['amount'];
|
$invoices_total += $value['amount'];
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,12 +94,12 @@ class StorePaymentRequest extends Request
|
|||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
$rules = [
|
$rules = [
|
||||||
'amount' => 'numeric|required',
|
'amount' => 'sometimes|numeric',
|
||||||
'amount' => [new PaymentAmountsBalanceRule(), new ValidCreditsPresentRule()],
|
'amount' => [new PaymentAmountsBalanceRule(), new ValidCreditsPresentRule()],
|
||||||
'client_id' => 'bail|required|exists:clients,id',
|
'client_id' => 'bail|required|exists:clients,id',
|
||||||
'invoices.*.invoice_id' => 'bail|required|distinct|exists:invoices,id',
|
'invoices.*.invoice_id' => 'bail|required|distinct|exists:invoices,id',
|
||||||
|
'invoices.*.amount' => 'bail|required',
|
||||||
'invoices.*.invoice_id' => new ValidInvoicesRules($this->all()),
|
'invoices.*.invoice_id' => new ValidInvoicesRules($this->all()),
|
||||||
'invoices.*.amount' => 'required',
|
|
||||||
'credits.*.credit_id' => 'bail|required|exists:credits,id',
|
'credits.*.credit_id' => 'bail|required|exists:credits,id',
|
||||||
'credits.*.credit_id' => new ValidCreditsRules($this->all()),
|
'credits.*.credit_id' => new ValidCreditsRules($this->all()),
|
||||||
'credits.*.amount' => ['required', new CreditsSumRule($this->all())],
|
'credits.*.amount' => ['required', new CreditsSumRule($this->all())],
|
||||||
|
@ -57,6 +57,11 @@ class ValidInvoicesRules implements Rule
|
|||||||
|
|
||||||
$unique_array[] = $invoice['invoice_id'];
|
$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();
|
$inv = Invoice::whereId($invoice['invoice_id'])->first();
|
||||||
|
|
||||||
if (! $inv) {
|
if (! $inv) {
|
||||||
|
@ -79,6 +79,11 @@ class ValidRefundableRequest implements Rule
|
|||||||
{
|
{
|
||||||
$invoice = Invoice::whereId($invoice['invoice_id'])->whereCompanyId($payment->company_id)->withTrashed()->first();
|
$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()) {
|
if ($payment->invoices()->exists()) {
|
||||||
$paymentable_invoice = $payment->invoices->where('id', $invoice->id)->first();
|
$paymentable_invoice = $payment->invoices->where('id', $invoice->id)->first();
|
||||||
|
|
||||||
|
@ -59,12 +59,16 @@ class PaymentAmountsBalanceRule implements Rule
|
|||||||
|
|
||||||
if (request()->input('credits') && is_array(request()->input('credits'))) {
|
if (request()->input('credits') && is_array(request()->input('credits'))) {
|
||||||
foreach (request()->input('credits') as $credit) {
|
foreach (request()->input('credits') as $credit) {
|
||||||
|
|
||||||
|
if(array_key_exists('amount', $credit))
|
||||||
$payment_amounts += $credit['amount'];
|
$payment_amounts += $credit['amount'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request()->input('invoices') && is_array(request()->input('invoices'))) {
|
if (request()->input('invoices') && is_array(request()->input('invoices'))) {
|
||||||
foreach (request()->input('invoices') as $invoice) {
|
foreach (request()->input('invoices') as $invoice) {
|
||||||
|
|
||||||
|
if(array_key_exists('amount', $invoice))
|
||||||
$invoice_amounts += $invoice['amount'];
|
$invoice_amounts += $invoice['amount'];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
167
tests/Feature/Payments/StorePaymentValidationTest.php
Normal file
167
tests/Feature/Payments/StorePaymentValidationTest.php
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://opensource.org/licenses/AAL
|
||||||
|
*/
|
||||||
|
namespace Tests\Feature\Payments;
|
||||||
|
|
||||||
|
use App\DataMapper\ClientSettings;
|
||||||
|
use App\Factory\ClientFactory;
|
||||||
|
use App\Factory\CreditFactory;
|
||||||
|
use App\Factory\InvoiceFactory;
|
||||||
|
use App\Factory\InvoiceItemFactory;
|
||||||
|
use App\Factory\PaymentFactory;
|
||||||
|
use App\Helpers\Invoice\InvoiceSum;
|
||||||
|
use App\Models\Client;
|
||||||
|
use App\Models\ClientContact;
|
||||||
|
use App\Models\Credit;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use App\Models\Payment;
|
||||||
|
use App\Utils\Traits\MakesHash;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Foundation\Testing\WithoutEvents;
|
||||||
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||||
|
use Illuminate\Support\Facades\Session;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Tests\MockAccountData;
|
||||||
|
use Tests\MockUnitData;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
class StorePaymentValidationTest extends TestCase
|
||||||
|
{
|
||||||
|
use MakesHash;
|
||||||
|
use DatabaseTransactions;
|
||||||
|
use MockAccountData;
|
||||||
|
use WithoutEvents;
|
||||||
|
|
||||||
|
public function setUp() :void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Session::start();
|
||||||
|
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user