2021-10-26 20:01:10 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-10-26 20:01:10 +02:00
|
|
|
*/
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
namespace Tests\Feature\Payments;
|
2021-10-26 20:01:10 +02:00
|
|
|
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutEvents;
|
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Tests\MockUnitData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
class UnappliedPaymentRefundTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MockUnitData;
|
|
|
|
use WithoutEvents;
|
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2021-10-26 20:01:10 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function testUnappliedPaymentRefund()
|
|
|
|
{
|
2021-10-26 20:01:10 +02:00
|
|
|
$data = [
|
|
|
|
'amount' => 1000,
|
|
|
|
'client_id' => $this->client->hashed_id,
|
|
|
|
'invoices' => [
|
|
|
|
],
|
|
|
|
'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', $data);
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($response) {
|
2021-10-26 20:01:10 +02:00
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-26 20:01:10 +02:00
|
|
|
$this->assertEquals(1000, $this->client->fresh()->paid_to_date);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$this->assertEquals(1000, $arr['data']['amount']);
|
|
|
|
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
|
|
|
'amount' => 500,
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
];
|
|
|
|
|
|
|
|
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);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$this->assertEquals(500, $this->client->fresh()->paid_to_date);
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
}
|