2019-05-03 09:57:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\DataMapper\ClientSettings;
|
|
|
|
use App\DataMapper\CompanySettings;
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Factory\ClientFactory;
|
|
|
|
use App\Factory\InvoiceFactory;
|
|
|
|
use App\Factory\PaymentFactory;
|
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
2019-05-03 09:57:55 +02:00
|
|
|
use App\Models\Account;
|
2020-01-29 03:03:47 +01:00
|
|
|
use App\Models\Activity;
|
2019-05-03 09:57:55 +02:00
|
|
|
use App\Models\Client;
|
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;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
2019-07-05 00:36:40 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
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-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();
|
2019-05-03 09:57:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPaymentList()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
factory(\App\Models\Client::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {
|
2019-05-03 09:57:55 +02:00
|
|
|
|
|
|
|
factory(\App\Models\ClientContact::class,1)->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,
|
2019-05-03 09:57:55 +02:00
|
|
|
'is_primary' => 1
|
|
|
|
]);
|
|
|
|
|
|
|
|
factory(\App\Models\ClientContact::class,1)->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
|
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-01-09 21:15:10 +01:00
|
|
|
factory(\App\Models\Payment::class, 1)->create(
|
|
|
|
['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()
|
|
|
|
{
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
factory(\App\Models\Payment::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]);
|
|
|
|
|
|
|
|
$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
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id
|
|
|
|
$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,
|
2019-12-01 12:23:24 +01:00
|
|
|
'amount' => $this->invoice->amount
|
|
|
|
],
|
|
|
|
],
|
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);
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$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
|
|
|
}
|
2019-05-03 09:57:55 +02:00
|
|
|
|
|
|
|
|
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 testStorePaymentWithClientId()
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($this->company->id, $this->user->id);
|
|
|
|
$client->save();
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id
|
|
|
|
$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,
|
2019-12-01 12:23:24 +01:00
|
|
|
'amount' => $this->invoice->amount
|
|
|
|
],
|
|
|
|
],
|
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,
|
|
|
|
])->post('/api/v1/payments?include=invoices', $data);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
2019-11-25 10:38:55 +01:00
|
|
|
// \Log::error('in the validator');
|
2019-11-18 11:46:01 +01:00
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
2019-11-25 10:38:55 +01:00
|
|
|
// \Log::error($message);
|
2019-11-18 11:46:01 +01:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
|
|
|
|
}
|
2019-12-01 12:23:24 +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();
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id
|
|
|
|
$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);
|
2019-05-03 09:57:55 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
|
2019-12-01 12:23:24 +01:00
|
|
|
if($response)
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPartialPaymentAmount()
|
|
|
|
{
|
|
|
|
$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->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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 2.0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2019-12-01 12:23:24 +01:00
|
|
|
'amount' => 2.0
|
|
|
|
],
|
|
|
|
],
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
$this->assertNotNull($message);
|
2020-01-09 21:15:10 +01:00
|
|
|
//\Log::error($message);
|
2019-12-16 12:34:38 +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-12-01 12:23:24 +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);
|
|
|
|
$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->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();
|
|
|
|
$this->invoice->markSent();
|
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,
|
2019-12-05 07:22:20 +01:00
|
|
|
'amount' => 6.0
|
|
|
|
],
|
|
|
|
],
|
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;
|
|
|
|
|
|
|
|
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-01-15 10:43:40 +01:00
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
|
|
|
|
\Log::error($message);
|
|
|
|
}
|
2019-12-05 07:22:20 +01:00
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2019-12-05 07:22:20 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
|
|
|
|
$invoice = $payment->invoices()->first();
|
|
|
|
|
|
|
|
$this->assertEquals($invoice->partial, 0);
|
|
|
|
$this->assertEquals($invoice->balance, 4);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPaymentLessThanPartialAmount()
|
|
|
|
{
|
|
|
|
|
|
|
|
$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->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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 2.0,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2020-01-11 23:01:28 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2019-12-05 07:22:20 +01:00
|
|
|
'amount' => 2.0
|
|
|
|
],
|
|
|
|
],
|
2019-12-16 12:34:38 +01:00
|
|
|
'date' => '2019/12/12',
|
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);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2019-12-05 07:22:20 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
|
|
|
|
$invoice = $payment->invoices()->first();
|
|
|
|
|
|
|
|
$this->assertEquals($invoice->partial, 3);
|
|
|
|
$this->assertEquals($invoice->balance, 8);
|
|
|
|
|
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id
|
|
|
|
$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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
|
|
|
|
$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-01-09 21:15:10 +01:00
|
|
|
'amount' => 2.0
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'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);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('amount', $message));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// if($response)
|
|
|
|
// $response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testPaymentChangesBalancesCorrectly()
|
|
|
|
{
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
|
|
|
|
$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-01-09 21:15:10 +01:00
|
|
|
'amount' => 2.0
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'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?include=invoices', $data);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('amount', $message));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if($response){
|
|
|
|
$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();
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$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',
|
|
|
|
];
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('invoices', $message));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if($response)
|
|
|
|
$response->assertStatus(302);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testUpdatePaymentValidationPasses()
|
|
|
|
{
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
$payment = PaymentFactory::create($this->company->id, $this->user->id);
|
|
|
|
$payment->amount = 10;
|
|
|
|
$payment->client_id = $client->id;
|
|
|
|
$payment->date = now();
|
|
|
|
$payment->number = $client->getNextPaymentNumber($client);
|
|
|
|
$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,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'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);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
\Log::error(print_r($e->validator->getMessageBag(),1));
|
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('invoices', $message));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if($response)
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testDoublePaymentTestWithInvalidAmounts()
|
|
|
|
{
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
|
|
|
|
$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,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'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);
|
|
|
|
\Log::error(print_r($e->validator->getMessageBag(),1));
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->assertEquals($payment->amount, 15);
|
|
|
|
$this->assertEquals($payment->applied, 10);
|
|
|
|
|
|
|
|
$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;
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
|
|
|
|
|
|
|
|
$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,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'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);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
2020-01-15 10:43:40 +01:00
|
|
|
// \Log::error(print_r($e->validator->getMessageBag(),1));
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('invoices', $message));
|
2020-01-15 10:43:40 +01:00
|
|
|
// \Log::error('hit error');
|
2020-01-09 21:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//$response->assertStatus(302);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testDoublePaymentTestWithValidAmounts()
|
|
|
|
{
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
|
|
|
|
$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,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'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'];
|
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$this->assertEquals($payment->amount, 20);
|
|
|
|
$this->assertEquals($payment->applied, 10);
|
|
|
|
|
|
|
|
$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;
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->invoice->markSent();
|
|
|
|
|
|
|
|
|
|
|
|
$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,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'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);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
\Log::error(print_r($e->validator->getMessageBag(),1));
|
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('invoices', $message));
|
|
|
|
\Log::error('hit error');
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
}
|
2020-01-19 04:02:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
public function testStorePaymentWithNoAmountField()
|
|
|
|
{
|
|
|
|
$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->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,
|
|
|
|
'amount' => $this->invoice->amount
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'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) {
|
|
|
|
// \Log::error('in the validator');
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
// \Log::error($message);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if($response){
|
|
|
|
$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();
|
|
|
|
|
|
|
|
$this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id
|
|
|
|
$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,
|
|
|
|
'amount' => $this->invoice->amount
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'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);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(ValidationException $e) {
|
|
|
|
// \Log::error('in the validator');
|
|
|
|
$message = json_decode($e->validator->getMessageBag(),1);
|
|
|
|
// \Log::error($message);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-03 09:57:55 +02:00
|
|
|
}
|