2020-01-29 03:03:47 +01: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
|
|
|
|
*/
|
2020-01-29 03:03:47 +01:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\Factory\ClientFactory;
|
2020-02-01 21:45:23 +01:00
|
|
|
use App\Factory\CreditFactory;
|
2020-01-29 03:03:47 +01:00
|
|
|
use App\Factory\InvoiceFactory;
|
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2020-02-01 21:45:23 +01:00
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
2020-01-29 03:03:47 +01:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Utils\Traits\Payment\Refundable
|
|
|
|
*/
|
|
|
|
class RefundTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MockAccountData;
|
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-02-01 21:45:23 +01:00
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
}
|
|
|
|
|
2020-01-30 22:15:13 +01:00
|
|
|
/**
|
|
|
|
* Test that a simple payment of $50
|
|
|
|
* is able to be refunded.
|
|
|
|
*/
|
2020-01-29 03:03:47 +01:00
|
|
|
public function testBasicRefundValidation()
|
|
|
|
{
|
|
|
|
$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-29 03:03:47 +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' => 50,
|
|
|
|
'client_id' => $client->hashed_id,
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments', $data);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
$this->assertEquals(50, $arr['data']['amount']);
|
|
|
|
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
2020-01-30 05:50:45 +01:00
|
|
|
'amount' => 50,
|
2020-01-29 03:03:47 +01:00
|
|
|
'date' => '2020/12/12',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = false;
|
|
|
|
|
|
|
|
try {
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2020-01-29 03:03:47 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/refund', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$this->assertEquals(50, $arr['data']['refunded']);
|
|
|
|
$this->assertEquals(Payment::STATUS_REFUNDED, $arr['data']['status_id']);
|
|
|
|
}
|
|
|
|
|
2020-01-30 22:15:13 +01:00
|
|
|
/**
|
|
|
|
* Test that a payment with Invoices
|
|
|
|
* requires a refund with invoices specified.
|
|
|
|
*
|
|
|
|
* Should produce a validation error if
|
|
|
|
* no invoices are specified in the refund
|
|
|
|
*/
|
2020-01-29 03:03:47 +01:00
|
|
|
public function testRefundValidationNoInvoicesProvided()
|
|
|
|
{
|
|
|
|
$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-29 03:03:47 +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();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-12 08:48:09 +02:00
|
|
|
$this->invoice->setRelation('client', $this->client);
|
|
|
|
$this->invoice->setRelation('company', $this->company);
|
|
|
|
|
2020-08-12 12:12:55 +02:00
|
|
|
$this->invoice->service()->createInvitations()->markSent()->save();
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-08-12 12:12:55 +02:00
|
|
|
$this->assertNotNull($this->invoice->invitations);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-13 02:57:06 +02:00
|
|
|
$this->assertNotNull($this->invoice->invitations->first()->contact);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
$data = [
|
|
|
|
'amount' => 50,
|
|
|
|
'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-29 03:03:47 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments', $data);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$this->assertEquals(50, $arr['data']['amount']);
|
|
|
|
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
2020-01-30 05:50:45 +01:00
|
|
|
'amount' => 50,
|
2020-01-29 03:03:47 +01:00
|
|
|
'date' => '2020/12/12',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = false;
|
|
|
|
|
|
|
|
try {
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2020-01-29 03:03:47 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/refund', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-29 03:03:47 +01:00
|
|
|
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
\Log::error($message);
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2020-01-29 03:03:47 +01:00
|
|
|
$response->assertStatus(302);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:15:13 +01:00
|
|
|
/**
|
|
|
|
* Test that a refund with invoices provided
|
|
|
|
* passes.
|
|
|
|
*/
|
2020-01-29 03:45:20 +01:00
|
|
|
public function testRefundValidationWithValidInvoiceProvided()
|
|
|
|
{
|
|
|
|
$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-29 03:45:20 +01:00
|
|
|
$this->invoice->client_id = $client->id;
|
|
|
|
$this->invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
2020-01-29 05:25:08 +01:00
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
2020-01-29 03:45:20 +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' => 50,
|
|
|
|
'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-29 03:45:20 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments', $data);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$this->assertEquals(50, $arr['data']['amount']);
|
|
|
|
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
2020-01-30 05:50:45 +01:00
|
|
|
'amount' => 50,
|
2020-01-29 05:25:08 +01:00
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2020-01-29 05:25:08 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = false;
|
2020-01-29 05:25:08 +01:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/refund', $data);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2020-01-29 05:25:08 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:15:13 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Test Validation with incorrect invoice refund amounts.
|
2020-01-30 22:15:13 +01:00
|
|
|
*/
|
2020-01-30 05:50:45 +01:00
|
|
|
public function testRefundValidationWithInValidInvoiceRefundedAmount()
|
|
|
|
{
|
|
|
|
$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-30 05:50:45 +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' => 50,
|
|
|
|
'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-30 05:50:45 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments', $data);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$this->assertEquals(50, $arr['data']['amount']);
|
|
|
|
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
|
|
|
'amount' => 50,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => 100,
|
2020-01-30 05:50:45 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = false;
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
2020-01-30 05:50:45 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/refund', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
\Log::error($message);
|
2020-01-30 05:50:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2020-01-30 05:50:45 +01:00
|
|
|
$response->assertStatus(302);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-30 22:15:13 +01:00
|
|
|
/**
|
|
|
|
* Tests refund when providing an invoice
|
2020-09-06 11:38:10 +02:00
|
|
|
* not related to the payment.
|
2020-01-30 22:15:13 +01:00
|
|
|
*/
|
2020-01-29 05:25:08 +01:00
|
|
|
public function testRefundValidationWithInValidInvoiceProvided()
|
|
|
|
{
|
|
|
|
$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-29 05:25:08 +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' => 50,
|
|
|
|
'client_id' => $client->hashed_id,
|
2020-01-29 03:45:20 +01:00
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2020-01-29 03:45:20 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
2020-01-29 05:25:08 +01:00
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments', $data);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$this->assertEquals(50, $arr['data']['amount']);
|
|
|
|
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
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-29 05:25:08 +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 = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
2020-01-30 05:50:45 +01:00
|
|
|
'amount' => 50,
|
2020-01-29 05:25:08 +01:00
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2020-01-29 05:25:08 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
2020-01-29 03:45:20 +01:00
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = false;
|
2020-01-29 03:45:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
2020-01-29 03:45:20 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/refund', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-01-29 03:45:20 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
\Log::error($message);
|
2020-01-29 03:45:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2020-01-29 05:25:08 +01:00
|
|
|
$response->assertStatus(302);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-29 03:45:20 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 21:45:23 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Test refunds where payments include credits.
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2020-02-01 21:45:23 +01:00
|
|
|
* $10 invoice
|
|
|
|
* $10 credit
|
|
|
|
* $50 credit card payment
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* result should be
|
|
|
|
*
|
|
|
|
* payment.applied = 10
|
|
|
|
* credit.balance = 0
|
|
|
|
*/
|
|
|
|
public function testRefundWhereCreditsArePresent()
|
|
|
|
{
|
|
|
|
$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-02-01 21:45:23 +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;
|
2020-04-08 12:48:31 +02:00
|
|
|
$this->invoice_client_id = $client->id;
|
2020-02-01 21:45:23 +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();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->credit = CreditFactory::create($this->company->id, $this->user->id);
|
2020-04-08 12:48:31 +02:00
|
|
|
$this->credit->client_id = $client->id;
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->credit->status_id = 2;
|
2020-02-01 21:45:23 +01:00
|
|
|
|
|
|
|
$this->credit->line_items = $this->buildLineItems();
|
|
|
|
$this->credit->amount = 10;
|
|
|
|
$this->credit->balance = 10;
|
|
|
|
|
|
|
|
$this->credit->uses_inclusive_taxes = false;
|
|
|
|
$this->credit->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'amount' => 50,
|
|
|
|
'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-02-01 21:45:23 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'credits' => [
|
|
|
|
[
|
|
|
|
'credit_id' => $this->credit->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->credit->amount,
|
2020-02-01 21:45:23 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = false;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
try {
|
2020-02-01 21:45:23 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'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);
|
2020-09-06 11:38:10 +02:00
|
|
|
\Log::error('this should not hit');
|
2020-02-01 21:45:23 +01:00
|
|
|
\Log::error($message);
|
|
|
|
}
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-02-01 21:45:23 +01:00
|
|
|
$arr = $response->json();
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$payment_id = $arr['data']['id'];
|
|
|
|
|
|
|
|
$this->assertEquals(50, $arr['data']['amount']);
|
|
|
|
|
|
|
|
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($payment);
|
|
|
|
$this->assertNotNull($payment->invoices());
|
|
|
|
$this->assertEquals(1, $payment->invoices()->count());
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
|
|
|
'amount' => 50,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $this->invoice->amount,
|
2020-02-01 21:45:23 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/12',
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = false;
|
2020-02-01 21:45:23 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
2020-02-01 21:45:23 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/refund', $data);
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2020-09-06 11:38:10 +02:00
|
|
|
\Log::error('refund message error');
|
2020-02-01 21:45:23 +01:00
|
|
|
\Log::error($message);
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
$arr = $response->json();
|
2020-02-01 21:45:23 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment = Payment::find($this->decodePrimaryKey($arr['data']['id']));
|
2020-02-01 21:45:23 +01:00
|
|
|
}
|
2020-01-30 22:15:13 +01:00
|
|
|
|
|
|
|
/*Additional scenarios*/
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|