Payment tests (#3224)

* Auto Calculate Payment amount if parameter is not supplied

* Tests for payments

* Flag logs to be sent with support messages
This commit is contained in:
David Bomba 2020-01-19 13:02:02 +10:00 committed by GitHub
parent 770f0763f4
commit 27d06a2ae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 147 additions and 4 deletions

View File

@ -69,8 +69,13 @@ class SendingController extends Controller
'message' => ['required'],
]);
$send_logs = false;
if($request->has('send_logs'));
$send_logs = $request->input('send_logs');
Mail::to(config('ninja.contact.ninja_official_contact'))
->send(new SupportMessageSent($request->message));
->send(new SupportMessageSent($request->message, $send_logs));
return response()->json([
'success' => true

View File

@ -37,15 +37,21 @@ class StorePaymentRequest extends Request
{
$input = $this->all();
$invoices_total = 0;
$credits_total = 0;
if (isset($input['client_id'])) {
$input['client_id'] = $this->decodePrimaryKey($input['client_id']);
}
if (isset($input['invoices']) && is_array($input['invoices']) !== false) {
foreach ($input['invoices'] as $key => $value) {
$input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
$invoices_total += $value['amount'];
}
//if(!isset($input['amount']) || )
}
if (isset($input['invoices']) && is_array($input['invoices']) === false) {
@ -55,6 +61,7 @@ class StorePaymentRequest extends Request
if (isset($input['credits']) && is_array($input['credits']) !== false) {
foreach ($input['credits'] as $key => $value) {
$input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']);
$credits_total += $value['amount'];
}
}
@ -62,6 +69,10 @@ class StorePaymentRequest extends Request
$input['credits'] = null;
}
if(!isset($input['amount'])){
$input['amount'] = $invoices_total - $credits_total;
}
$this->replace($input);
}
@ -72,6 +83,10 @@ class StorePaymentRequest extends Request
'amount' => [new PaymentAmountsBalanceRule(),new ValidCreditsPresentRule()],
'date' => 'required',
'client_id' => 'required',
'invoices.*.invoice_id' => 'required',
'invoices.*.amount' => 'required',
'credits.*.credit_id' => 'required',
'credits.*.amount' => 'required',
'invoices' => new ValidPayableInvoicesRule(),
'number' => 'nullable',
];

View File

@ -14,9 +14,12 @@ class SupportMessageSent extends Mailable
public $message;
public function __construct($message)
public $send_logs;
public function __construct($message, $send_logs)
{
$this->message = $message;
$this->send_logs = $send_logs;
}
/**
@ -34,7 +37,7 @@ class SupportMessageSent extends Mailable
* we are going to bundle system-level info
* and last 10 lines of laravel.log file.
*/
if (Ninja::isSelfHost()) {
if (Ninja::isSelfHost() && $this->send_logs !== false) {
$system_info = Ninja::getDebugInfo();
$log_file = new \SplFileObject(sprintf('%s/laravel.log', base_path('storage/logs')));

View File

@ -946,4 +946,124 @@ class PaymentTest extends TestCase
$response->assertStatus(200);
}
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']);
$payment = Payment::find($this->decodePrimaryKey($payment_id))->first();
$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);
}
}
}