diff --git a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php index 158e51e6761e..13791b49cd3a 100644 --- a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php +++ b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php @@ -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; } diff --git a/app/Http/ValidationRules/ValidRefundableInvoices.php b/app/Http/ValidationRules/ValidRefundableInvoices.php index a3c41572947b..d24525b46d20 100644 --- a/app/Http/ValidationRules/ValidRefundableInvoices.php +++ b/app/Http/ValidationRules/ValidRefundableInvoices.php @@ -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; } diff --git a/tests/Feature/RefundTest.php b/tests/Feature/RefundTest.php index e65b8de6df73..3b3355454c81 100644 --- a/tests/Feature/RefundTest.php +++ b/tests/Feature/RefundTest.php @@ -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); + + } + + + + }