2019-05-03 09:57:55 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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-09-14 13:11:46 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-05-03 09:57:55 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\DataMapper\ClientSettings;
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Factory\ClientFactory;
|
2020-04-08 12:48:31 +02:00
|
|
|
use App\Factory\CreditFactory;
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Factory\InvoiceFactory;
|
2021-04-22 01:35:31 +02:00
|
|
|
use App\Factory\InvoiceItemFactory;
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Factory\PaymentFactory;
|
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
2019-05-03 09:57:55 +02:00
|
|
|
use App\Models\Client;
|
2020-10-01 13:34:05 +02:00
|
|
|
use App\Models\ClientContact;
|
2020-04-08 12:48:31 +02:00
|
|
|
use App\Models\Credit;
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Models\Invoice;
|
2019-05-03 09:57:55 +02:00
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2020-04-08 12:48:31 +02:00
|
|
|
use Illuminate\Foundation\Testing\WithoutEvents;
|
2020-06-01 09:04:07 +02:00
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
2019-05-03 09:57:55 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2019-11-18 11:46:01 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Tests\MockAccountData;
|
2019-05-03 09:57:55 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Http\Controllers\PaymentController
|
|
|
|
*/
|
|
|
|
class PaymentTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
2019-11-18 11:46:01 +01:00
|
|
|
use MockAccountData;
|
2020-04-08 12:48:31 +02:00
|
|
|
use WithoutEvents;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2019-05-03 09:57:55 +02:00
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->makeTestData();
|
|
|
|
$this->withoutExceptionHandling();
|
2020-06-01 09:04:07 +02:00
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
2019-05-03 09:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPaymentList()
|
|
|
|
{
|
2020-10-01 13:34:05 +02:00
|
|
|
Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {
|
|
|
|
ClientContact::factory()->create([
|
2019-11-18 11:46:01 +01:00
|
|
|
'user_id' => $this->user->id,
|
2019-05-03 09:57:55 +02:00
|
|
|
'client_id' => $c->id,
|
2019-11-18 11:46:01 +01:00
|
|
|
'company_id' => $this->company->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'is_primary' => 1,
|
2019-05-03 09:57:55 +02:00
|
|
|
]);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
ClientContact::factory()->create([
|
2019-11-18 11:46:01 +01:00
|
|
|
'user_id' => $this->user->id,
|
2019-05-03 09:57:55 +02:00
|
|
|
'client_id' => $c->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'company_id' => $this->company->id,
|
2019-05-03 09:57:55 +02:00
|
|
|
]);
|
|
|
|
});
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2019-05-03 09:57:55 +02:00
|
|
|
$client = Client::all()->first();
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
Payment::factory()->create(
|
2020-01-09 21:15:10 +01:00
|
|
|
['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $client->id]
|
|
|
|
);
|
2019-05-03 09:57:55 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-11-18 11:46:01 +01:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2019-05-03 09:57:55 +02:00
|
|
|
])->get('/api/v1/payments');
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPaymentRESTEndPoints()
|
|
|
|
{
|
2020-10-01 13:34:05 +02:00
|
|
|
Payment::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]);
|
2019-11-18 11:46:01 +01:00
|
|
|
|
|
|
|
$Payment = Payment::all()->last();
|
2019-05-03 09:57:55 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-11-18 11:46:01 +01:00
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->get('/api/v1/payments/'.$this->encodePrimaryKey($Payment->id));
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$response->assertStatus(200);
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2021-03-20 00:10:45 +01:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/payments/'.$this->encodePrimaryKey($Payment->id), $Payment->toArray());
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->get('/api/v1/payments/'.$this->encodePrimaryKey($Payment->id).'/edit');
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
public function testStorePaymentWithoutClientId()
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_Taxes = false;
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice->save();
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$data = [
|
|
|
|
'amount' => $this->invoice->amount,
|
2019-12-01 12:23:24 +01:00
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2019-12-01 12:23:24 +01:00
|
|
|
],
|
|
|
|
],
|
2019-12-16 12:34:38 +01:00
|
|
|
'date' => '2020/12/11',
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
2019-05-03 09:57:55 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-11-18 11:46:01 +01:00
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->assertTrue(array_key_exists('client_id', $message));
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
public function testStorePaymentWithClientId()
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
$this->invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_Taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => $this->invoice->amount,
|
|
|
|
'client_id' => $client->hashed_id,
|
2019-12-01 12:23:24 +01:00
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2019-12-01 12:23:24 +01:00
|
|
|
],
|
|
|
|
],
|
2019-12-16 12:34:38 +01:00
|
|
|
'date' => '2020/12/12',
|
2019-11-18 11:46:01 +01:00
|
|
|
|
|
|
|
];
|
|
|
|
|
2019-12-01 12:23:24 +01:00
|
|
|
$response = null;
|
2019-11-18 11:46:01 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
2019-05-03 09:57:55 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-11-18 11:46:01 +01:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-06-22 05:07:58 +02:00
|
|
|
])->post('/api/v1/payments?include=invoices,paymentables', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2019-11-18 11:46:01 +01:00
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
2019-12-01 12:23:24 +01:00
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$payment = Payment::find($this->decodePrimaryKey($payment_id))->first();
|
2020-06-22 05:07:58 +02:00
|
|
|
$payment->load('invoices');
|
2019-12-01 12:23:24 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
}
|
2019-11-18 11:46:01 +01:00
|
|
|
}
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
public function testStorePaymentWithNoInvoiecs()
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
$this->invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
2019-12-01 12:23:24 +01:00
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
2019-11-18 11:46:01 +01:00
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => $this->invoice->amount,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => '',
|
2019-12-16 12:34:38 +01:00
|
|
|
'date' => '2020/12/12',
|
2019-11-18 11:46:01 +01:00
|
|
|
|
|
|
|
];
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
$response = false;
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
2019-05-03 09:57:55 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-11-18 11:46:01 +01:00
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2019-12-01 12:23:24 +01:00
|
|
|
$response->assertStatus(200);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-12-01 12:23:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPartialPaymentAmount()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
ClientContact::factory()->create([
|
2020-02-10 10:53:02 +01:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' =>$this->company->id,
|
2020-03-21 06:37:30 +01:00
|
|
|
'is_primary' => true,
|
2020-02-10 10:53:02 +01:00
|
|
|
]);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2019-12-01 12:23:24 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->partial = 2.0;
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-26 08:48:22 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2019-12-01 12:23:24 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 2.0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 2.0,
|
2019-12-01 12:23:24 +01:00
|
|
|
],
|
|
|
|
],
|
2019-12-16 12:34:38 +01:00
|
|
|
'date' => '2019/12/12',
|
2019-12-01 12:23:24 +01:00
|
|
|
];
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
$response = false;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
try {
|
2019-12-01 12:23:24 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2019-12-16 12:34:38 +01:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2019-12-01 12:23:24 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
$arr = $response->json();
|
|
|
|
|
2019-12-01 12:23:24 +01:00
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2019-12-01 12:23:24 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
|
|
|
|
$pivot_invoice = $payment->invoices()->first();
|
|
|
|
$this->assertEquals($pivot_invoice->pivot->amount, 2);
|
|
|
|
$this->assertEquals($pivot_invoice->partial, 0);
|
|
|
|
$this->assertEquals($pivot_invoice->amount, 10.0000);
|
|
|
|
$this->assertEquals($pivot_invoice->balance, 8.0000);
|
2019-12-16 12:34:38 +01:00
|
|
|
}
|
2019-05-03 09:57:55 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 07:22:20 +01:00
|
|
|
public function testPaymentGreaterThanPartial()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
2020-02-17 10:37:44 +01:00
|
|
|
$client->setRelation('company', $this->company);
|
2019-12-05 07:22:20 +01:00
|
|
|
$client->save();
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
$client_contact = ClientContact::factory()->create([
|
2020-02-17 10:37:44 +01:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $this->company->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'is_primary' => 1,
|
2020-02-17 10:37:44 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$client->setRelation('contacts', $client_contact);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2019-12-05 07:22:20 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->partial = 5.0;
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->invoice->company->setRelation('company', $this->company);
|
|
|
|
$this->invoice->company->setRelation('client', $client);
|
2019-12-05 07:22:20 +01:00
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-15 10:43:40 +01:00
|
|
|
$this->invoice->is_deleted = false;
|
2019-12-05 07:22:20 +01:00
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 6.0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 6.0,
|
2019-12-05 07:22:20 +01:00
|
|
|
],
|
|
|
|
],
|
2019-12-16 12:34:38 +01:00
|
|
|
'date' => '2019/12/12',
|
2019-12-05 07:22:20 +01:00
|
|
|
];
|
|
|
|
|
2020-01-15 10:43:40 +01:00
|
|
|
$response = false;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
try {
|
2019-12-05 07:22:20 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
}
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment_id = $arr['data']['id'];
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$invoice = $payment->invoices()->first();
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($invoice->partial, 0);
|
|
|
|
$this->assertEquals($invoice->balance, 4);
|
2019-12-05 07:22:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPaymentLessThanPartialAmount()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
ClientContact::factory()->create([
|
2020-04-08 12:48:31 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
]);
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
ClientContact::factory()->create([
|
2020-04-08 12:48:31 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $this->company->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'send_email' => true,
|
2020-04-08 12:48:31 +02:00
|
|
|
]);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2019-12-05 07:22:20 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->partial = 5.0;
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-26 22:11:42 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2019-12-05 07:22:20 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 2.0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 2.0,
|
2019-12-05 07:22:20 +01:00
|
|
|
],
|
|
|
|
],
|
2019-12-16 12:34:38 +01:00
|
|
|
'date' => '2019/12/12',
|
2019-12-05 07:22:20 +01:00
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-12-05 07:22:20 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment_id = $arr['data']['id'];
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$invoice = $payment->invoices()->first();
|
2019-12-05 07:22:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($invoice->partial, 3);
|
|
|
|
$this->assertEquals($invoice->balance, 8);
|
2019-12-05 07:22:20 +01:00
|
|
|
}
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
public function testPaymentValidationAmount()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
$contact = ClientContact::factory()->create([
|
2020-04-08 12:48:31 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
]);
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
ClientContact::factory()->create([
|
2020-04-08 12:48:31 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'send_email' => true,
|
2020-04-08 12:48:31 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$client->setRelation('contact', $contact);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-09 21:15:10 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->partial = 5.0;
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-04-08 12:48:31 +02:00
|
|
|
$this->invoice->setRelation('client', $client);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 1.0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 2.0,
|
2020-01-09 21:15:10 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2019/12/12',
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('amount', $message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPaymentChangesBalancesCorrectly()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-09 21:15:10 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 2.0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 2.0,
|
2020-01-09 21:15:10 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2019/12/12',
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = false;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('amount', $message));
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2020-01-09 21:15:10 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$invoice = Invoice::find($this->decodePrimaryKey($this->invoice->hashed_id));
|
|
|
|
|
|
|
|
$this->assertEquals($invoice->balance, 8);
|
|
|
|
|
|
|
|
$payment = $invoice->payments()->first();
|
|
|
|
|
|
|
|
$this->assertEquals($payment->applied, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatePaymentValidationWorks()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-09 21:15:10 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$payment = PaymentFactory::create($this->company->id, $this->user->id);
|
|
|
|
$payment->amount = 10;
|
|
|
|
$payment->client_id = $client->id;
|
|
|
|
$payment->date = now();
|
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 2.0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [],
|
|
|
|
'date' => '2019/12/12',
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = false;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('invoices', $message));
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2020-06-22 05:07:58 +02:00
|
|
|
$response->assertStatus(200);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdatePaymentValidationPasses()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-09 21:15:10 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$payment = PaymentFactory::create($this->company->id, $this->user->id);
|
|
|
|
$payment->amount = 10;
|
|
|
|
$payment->client_id = $client->id;
|
|
|
|
$payment->date = now();
|
2021-10-17 05:21:13 +02:00
|
|
|
$payment->number = $client->getNextPaymentNumber($client, $payment);
|
2020-01-09 21:15:10 +01:00
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 10.0,
|
|
|
|
'client_id' => $this->encodePrimaryKey($client->id),
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
2020-01-09 21:15:10 +01:00
|
|
|
'amount' => 10,
|
2020-09-06 11:38:10 +02:00
|
|
|
],
|
2020-01-09 21:15:10 +01:00
|
|
|
],
|
|
|
|
'date' => '2019/12/12',
|
|
|
|
];
|
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
$response = false;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
\Log::error(print_r($e->validator->getMessageBag(), 1));
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('invoices', $message));
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2020-01-09 21:15:10 +01:00
|
|
|
$response->assertStatus(200);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoublePaymentTestWithInvalidAmounts()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-09 21:15:10 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 15.0,
|
|
|
|
'client_id' => $this->encodePrimaryKey($client->id),
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
2020-01-09 21:15:10 +01:00
|
|
|
'amount' => 10,
|
2020-09-06 11:38:10 +02:00
|
|
|
],
|
2020-01-09 21:15:10 +01:00
|
|
|
],
|
|
|
|
'date' => '2019/12/12',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = false;
|
|
|
|
|
|
|
|
try {
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2020-01-09 21:15:10 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
\Log::error(print_r($e->validator->getMessageBag(), 1));
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$arr = $response->json();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment_id = $arr['data']['id'];
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($payment->amount, 15);
|
|
|
|
$this->assertEquals($payment->applied, 10);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->invoice = null;
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-09 21:15:10 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 15.0,
|
|
|
|
'client_id' => $this->encodePrimaryKey($client->id),
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
2020-01-09 21:15:10 +01:00
|
|
|
'amount' => 10,
|
2020-09-06 11:38:10 +02:00
|
|
|
],
|
2020-01-09 21:15:10 +01:00
|
|
|
],
|
|
|
|
'date' => '2019/12/12',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('invoices', $message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoublePaymentTestWithValidAmounts()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-09 21:15:10 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 20.0,
|
|
|
|
'client_id' => $this->encodePrimaryKey($client->id),
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
2020-01-09 21:15:10 +01:00
|
|
|
'amount' => 10,
|
2020-09-06 11:38:10 +02:00
|
|
|
],
|
2020-01-09 21:15:10 +01:00
|
|
|
],
|
|
|
|
'date' => '2019/12/12',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/', $data);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-09 21:15:10 +01:00
|
|
|
$response->assertStatus(200);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$arr = $response->json();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment_id = $arr['data']['id'];
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($payment->amount, 20);
|
|
|
|
$this->assertEquals($payment->applied, 10);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $this->invoice = null;
|
|
|
|
// $this->invoice = InvoiceFactory::create($this->company->id, $this->user->id);//stub the company and user_id
|
|
|
|
// $this->invoice->client_id = $client->id;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $this->invoice->line_items = $this->buildLineItems();
|
|
|
|
// $this->invoice->uses_inclusive_taxes = false;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $this->invoice->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
// $this->invoice_calc->build();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
// $this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
// $this->invoice->service()->markSent()->createInvitations()->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $data = [
|
|
|
|
// 'amount' => 20.0,
|
|
|
|
// 'client_id' => $this->encodePrimaryKey($client->id),
|
|
|
|
// 'invoices' => [
|
|
|
|
// [
|
|
|
|
// 'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
|
|
|
// 'amount' => 10,
|
|
|
|
// ]
|
|
|
|
// ],
|
|
|
|
// 'date' => '2019/12/12',
|
|
|
|
// ];
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $response = false;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// try {
|
|
|
|
// $response = $this->withHeaders([
|
|
|
|
// 'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
// 'X-API-TOKEN' => $this->token,
|
|
|
|
// ])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data);
|
|
|
|
// } catch (ValidationException $e) {
|
|
|
|
// $message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
// \Log::error(print_r($e->validator->getMessageBag(), 1));
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $this->assertTrue(array_key_exists('invoices', $message));
|
|
|
|
// }
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $response->assertStatus(200);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $arr = $response->json();
|
2020-01-30 21:53:14 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
// $this->assertEquals(20, $arr['data']['applied']);
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
2020-01-19 04:02:02 +01:00
|
|
|
|
|
|
|
public function testStorePaymentWithNoAmountField()
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-19 04:02:02 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
$this->invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_Taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2020-01-19 04:02:02 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-19 04:02:02 +01:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2020-01-19 04:02:02 +01:00
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$this->assertEquals($this->invoice->amount, $arr['data']['amount']);
|
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2020-01-19 04:02:02 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStorePaymentWithZeroAmountField()
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-01-19 04:02:02 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
$this->invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_Taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2020-01-19 04:02:02 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-19 04:02:02 +01:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-30 22:15:13 +01:00
|
|
|
$response->assertStatus(200);
|
2020-02-02 08:54:52 +01:00
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals(round($payment->amount, 2), $this->invoice->amount);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals(round($payment->applied, 2), $this->invoice->amount);
|
2020-01-19 04:02:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-28 02:58:49 +01:00
|
|
|
public function testPaymentForInvoicesFromDifferentClients()
|
|
|
|
{
|
|
|
|
$client1 = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client1->save();
|
|
|
|
|
|
|
|
$client2 = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client2->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice1 = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-02-28 02:58:49 +01:00
|
|
|
$invoice1->client_id = $client1->id;
|
|
|
|
$invoice1->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$invoice1->line_items = $this->buildLineItems();
|
|
|
|
$invoice1->uses_inclusive_Taxes = false;
|
|
|
|
|
|
|
|
$invoice1->save();
|
|
|
|
|
|
|
|
$invoice_calc = new InvoiceSum($invoice1);
|
|
|
|
$invoice_calc->build();
|
|
|
|
|
|
|
|
$invoice1 = $invoice_calc->getInvoice();
|
|
|
|
$invoice1->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice2 = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-02-28 02:58:49 +01:00
|
|
|
$invoice2->client_id = $client2->id;
|
|
|
|
$invoice2->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$invoice2->line_items = $this->buildLineItems();
|
|
|
|
$invoice2->uses_inclusive_Taxes = false;
|
|
|
|
|
|
|
|
$invoice2->save();
|
|
|
|
|
|
|
|
$invoice_calc = new InvoiceSum($invoice2);
|
|
|
|
$invoice_calc->build();
|
|
|
|
|
|
|
|
$invoice2 = $invoice_calc->getInvoice();
|
|
|
|
$invoice2->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => $invoice1->amount + $invoice2->amount,
|
|
|
|
'client_id' => $client1->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $invoice1->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $invoice1->amount,
|
2020-02-28 02:58:49 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'invoice_id' => $invoice2->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $invoice2->amount,
|
|
|
|
],
|
2020-02-28 02:58:49 +01:00
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-02-28 02:58:49 +01:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 09:18:34 +01:00
|
|
|
public function testPaymentWithSameInvoiceMultipleTimes()
|
|
|
|
{
|
|
|
|
$client1 = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client1->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice1 = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-02-28 09:18:34 +01:00
|
|
|
$invoice1->client_id = $client1->id;
|
|
|
|
$invoice1->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$invoice1->line_items = $this->buildLineItems();
|
|
|
|
$invoice1->uses_inclusive_Taxes = false;
|
|
|
|
|
|
|
|
$invoice1->save();
|
|
|
|
|
|
|
|
$invoice_calc = new InvoiceSum($invoice1);
|
|
|
|
$invoice_calc->build();
|
|
|
|
|
|
|
|
$invoice1 = $invoice_calc->getInvoice();
|
|
|
|
$invoice1->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => $invoice1->amount,
|
|
|
|
'client_id' => $client1->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $invoice1->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 1,
|
2020-02-28 09:18:34 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'invoice_id' => $invoice1->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 1,
|
|
|
|
],
|
2020-02-28 09:18:34 +01:00
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-02-28 09:18:34 +01:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertNull($response);
|
|
|
|
}
|
2020-04-08 12:48:31 +02:00
|
|
|
|
|
|
|
public function testStorePaymentWithCredits()
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-04-08 12:48:31 +02:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
$this->invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$credit = CreditFactory::create($this->company->id, $this->user->id);
|
|
|
|
$credit->client_id = $client->id;
|
|
|
|
$credit->status_id = Credit::STATUS_SENT;
|
|
|
|
|
|
|
|
$credit->line_items = $this->buildLineItems();
|
|
|
|
$credit->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$credit->save();
|
|
|
|
|
|
|
|
$credit_calc = new InvoiceSum($credit);
|
|
|
|
$credit_calc->build();
|
|
|
|
|
|
|
|
$credit = $this->credit_calc->getCredit();
|
|
|
|
$credit->save(); //$10 credit
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => $this->invoice->amount,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 5,
|
2020-04-08 12:48:31 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'credits' => [
|
|
|
|
[
|
|
|
|
'credit_id' => $credit->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 5,
|
2020-04-08 12:48:31 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-04-08 12:48:31 +02:00
|
|
|
if ($response) {
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$payment = Payment::find($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 12:29:58 +02:00
|
|
|
|
|
|
|
public function testStorePaymentExchangeRate()
|
|
|
|
{
|
|
|
|
$settings = ClientSettings::defaults();
|
2020-09-06 11:38:10 +02:00
|
|
|
$settings->currency_id = '2';
|
2020-04-19 12:29:58 +02:00
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->settings = $settings;
|
|
|
|
$client->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
2020-04-19 12:29:58 +02:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
$this->invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_Taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => $this->invoice->amount,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2020-04-19 12:29:58 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-04-19 12:29:58 +02:00
|
|
|
if ($response) {
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$payment = Payment::find($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
2020-12-29 22:10:03 +01:00
|
|
|
// nlog($payment);
|
2020-04-19 12:29:58 +02:00
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 22:18:02 +01:00
|
|
|
|
|
|
|
public function testPaymentActionArchive()
|
|
|
|
{
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2020-11-02 22:18:02 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 20.0,
|
|
|
|
'client_id' => $this->encodePrimaryKey($client->id),
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
|
|
|
'amount' => 10,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2019/12/12',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/', $data);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'ids' => [$this->encodePrimaryKey($payment->id)],
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/bulk?action=archive', $data);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertGreaterThan(0, $arr['data'][0]['archived_at']);
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/bulk?action=restore', $data);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $arr['data'][0]['archived_at']);
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/bulk?action=delete', $data);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
2020-11-03 06:44:50 +01:00
|
|
|
|
2020-11-02 22:18:02 +01:00
|
|
|
$this->assertEquals(1, $arr['data'][0]['is_deleted']);
|
|
|
|
}
|
2021-04-22 01:35:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
public function testDeleteRefundedPayment()
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->invoice = null;
|
|
|
|
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
|
|
|
|
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 10;
|
|
|
|
$item->product_key = 'test';
|
|
|
|
$item->notes = 'test';
|
|
|
|
$item->custom_value1 = '';
|
|
|
|
$item->custom_value2 = '';
|
|
|
|
$item->custom_value3 = '';
|
|
|
|
$item->custom_value4 = '';
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $line_items;
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice_calc->getInvoice();
|
|
|
|
$this->invoice->save();
|
2022-02-27 01:50:18 +01:00
|
|
|
$this->invoice->service()->markSent()->createInvitations()->save();
|
2021-04-22 01:35:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(10, $this->invoice->balance);
|
2022-04-22 14:01:32 +02:00
|
|
|
$this->assertEquals(10, $this->invoice->client->fresh()->balance);
|
2021-04-22 01:35:31 +02:00
|
|
|
|
|
|
|
$this->invoice->service()->markPaid()->save();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $this->invoice->balance);
|
|
|
|
$this->assertEquals(0, $this->invoice->client->balance);
|
|
|
|
|
|
|
|
$this->assertTrue($this->invoice->payments()->exists());
|
|
|
|
|
|
|
|
$payment = $this->invoice->payments()->first();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
|
|
|
'amount' => 10,
|
|
|
|
'date' => '2021/12/12',
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
|
|
|
'amount' => 10,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/refund', $data);
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
nlog($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$this->assertEquals(10, $this->invoice->fresh()->balance);
|
|
|
|
$this->assertEquals(10, $this->invoice->fresh()->balance);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'ids' => [$this->encodePrimaryKey($payment->id)],
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/bulk?action=delete', $data);
|
|
|
|
|
|
|
|
$this->assertEquals(10, $this->invoice->fresh()->balance);
|
|
|
|
$this->assertEquals(10, $this->invoice->fresh()->balance);
|
|
|
|
}
|
2021-12-10 11:50:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
public function testUniquePaymentNumbers()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'amount' => $this->invoice->amount,
|
|
|
|
'client_id' => $this->client->hashed_id,
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
'number' => 'duplicate',
|
|
|
|
];
|
|
|
|
|
|
|
|
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($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$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($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($response)
|
|
|
|
$response->assertStatus(302);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-03 09:57:55 +02:00
|
|
|
}
|
2021-04-22 01:35:31 +02:00
|
|
|
|