mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-05 18:52:44 +01:00
Working on refund tests (#3261)
This commit is contained in:
parent
61b3385102
commit
133e56dd0b
@ -56,12 +56,14 @@ class ValidRefundableRequest implements Rule
|
||||
foreach($request_credits as $key => $value)
|
||||
$request_credits[$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']);
|
||||
|
||||
|
||||
if($payment->invoices()->exists())
|
||||
{
|
||||
foreach($payment->invoices as $paymentable_invoice)
|
||||
$this->checkInvoice($paymentable_invoice, $request_invoices);
|
||||
}
|
||||
|
||||
|
||||
if($payment->credits()->exists())
|
||||
{
|
||||
foreach($payment->credits as $paymentable_credit)
|
||||
@ -72,10 +74,14 @@ class ValidRefundableRequest implements Rule
|
||||
foreach($request_invoices as $request_invoice)
|
||||
$this->checkInvoiceIsPaymentable($request_invoice, $payment);
|
||||
|
||||
|
||||
foreach($request_credits as $request_credit)
|
||||
$this->checkCreditIsPaymentable($request_credit, $payment);
|
||||
|
||||
|
||||
if(strlen($this->error_msg) > 0 )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ class ValidRefundableInvoices implements Rule
|
||||
{
|
||||
$payment = Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first();
|
||||
|
||||
if($request->has('refunded') && ($request->input('refunded') > ($payment->amount - $payment->refunded))){
|
||||
if(request()->has('refunded') && (request()->input('refunded') > ($payment->amount - $payment->refunded))){
|
||||
$this->error_msg = "Attempting to refunded more than payment amount, enter a value equal to or lower than the payment amount of ". $payment->amount;
|
||||
return false;
|
||||
}
|
||||
|
@ -231,4 +231,92 @@ class RefundTest extends TestCase
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testRefundValidationWithValidInvoiceProvided()
|
||||
{
|
||||
$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' => 50,
|
||||
'client_id' => $client->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => $this->invoice->amount
|
||||
],
|
||||
],
|
||||
'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),
|
||||
'refunded' => 50,
|
||||
'invoices' => [
|
||||
[
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => $this->invoice->amount
|
||||
],
|
||||
],
|
||||
'date' => '2020/12/12',
|
||||
];
|
||||
|
||||
$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);
|
||||
|
||||
\Log::error($message);
|
||||
}
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user