22
33namespace Fleetbase \Ledger \Http \Controllers \Internal \v1 ;
44
5+ use Fleetbase \Ledger \DTO \RefundRequest ;
56use Fleetbase \Ledger \Http \Controllers \LedgerResourceController ;
67use Fleetbase \Ledger \Http \Resources \v1 \Invoice as InvoiceResource ;
78use Fleetbase \Ledger \Http \Resources \v1 \Transaction as TransactionResource ;
8- use Fleetbase \Ledger \DTO \RefundRequest ;
99use Fleetbase \Ledger \Models \GatewayTransaction ;
1010use Fleetbase \Ledger \Models \Invoice ;
1111use Fleetbase \Ledger \Models \InvoiceItem ;
@@ -156,13 +156,13 @@ public function refundOptions(string $id, Request $request): JsonResponse
156156
157157 return response ()->json ([
158158 'invoice ' => [
159- 'id ' => $ invoice ->public_id ,
160- 'uuid ' => $ invoice ->uuid ,
161- 'number ' => $ invoice ->number ,
162- 'currency ' => $ invoice ->currency ,
163- 'total_amount ' => (int ) $ invoice ->total_amount ,
164- 'amount_paid ' => (int ) $ invoice ->amount_paid ,
165- 'refunded_amount ' => $ this ->invoiceRefundedAmount ($ invoice ),
159+ 'id ' => $ invoice ->public_id ,
160+ 'uuid ' => $ invoice ->uuid ,
161+ 'number ' => $ invoice ->number ,
162+ 'currency ' => $ invoice ->currency ,
163+ 'total_amount ' => (int ) $ invoice ->total_amount ,
164+ 'amount_paid ' => (int ) $ invoice ->amount_paid ,
165+ 'refunded_amount ' => $ this ->invoiceRefundedAmount ($ invoice ),
166166 'remaining_refundable_amount ' => $ this ->invoiceRemainingRefundableAmount ($ invoice ),
167167 ],
168168 'options ' => $ options ,
@@ -182,21 +182,21 @@ public function refund(string $id, Request $request, PaymentService $paymentServ
182182 'reason ' => 'nullable|string|max:500 ' ,
183183 ]);
184184
185- $ options = collect ($ this ->refundableGatewayTransactions ($ invoice ));
185+ $ options = collect ($ this ->refundableGatewayTransactions ($ invoice ));
186186 $ selected = $ options ->firstWhere ('gateway_transaction_id ' , $ request ->input ('gateway_transaction_id ' ));
187187
188188 if (!$ selected ) {
189189 return response ()->json (['error ' => 'The selected payment cannot be refunded. ' ], 422 );
190190 }
191191
192- $ amount = $ request ->integer ('amount ' );
192+ $ amount = $ request ->integer ('amount ' );
193193 $ remainingInvoiceAmount = $ this ->invoiceRemainingRefundableAmount ($ invoice );
194194 $ remainingPaymentAmount = (int ) data_get ($ selected , 'refundable_amount ' , 0 );
195- $ maxRefundableAmount = min ($ remainingInvoiceAmount , $ remainingPaymentAmount );
195+ $ maxRefundableAmount = min ($ remainingInvoiceAmount , $ remainingPaymentAmount );
196196
197197 if ($ amount > $ maxRefundableAmount ) {
198198 return response ()->json ([
199- 'error ' => 'Refund amount exceeds the remaining refundable amount. ' ,
199+ 'error ' => 'Refund amount exceeds the remaining refundable amount. ' ,
200200 'remaining_refundable_amount ' => $ maxRefundableAmount ,
201201 ], 422 );
202202 }
@@ -210,8 +210,8 @@ public function refund(string $id, Request $request, PaymentService $paymentServ
210210 reason: $ request ->input ('reason ' ),
211211 invoiceUuid: $ invoice ->uuid ,
212212 metadata: [
213- 'refund_kind ' => $ refundKind ,
214- 'invoice_uuid ' => $ invoice ->uuid ,
213+ 'refund_kind ' => $ refundKind ,
214+ 'invoice_uuid ' => $ invoice ->uuid ,
215215 'invoice_public_id ' => $ invoice ->public_id ,
216216 ],
217217 ));
@@ -399,32 +399,32 @@ private function refundableGatewayTransactions(Invoice $invoice): array
399399
400400 return $ gatewayTransactions
401401 ->map (function (GatewayTransaction $ transaction ) use ($ invoice , $ remainingInvoiceAmount ) {
402- $ paidAmount = (int ) ($ transaction ->amount ?: $ invoice ->amount_paid );
402+ $ paidAmount = (int ) ($ transaction ->amount ?: $ invoice ->amount_paid );
403403 $ refundedAmount = (int ) GatewayTransaction::where ('company_uuid ' , $ invoice ->company_uuid )
404404 ->where ('gateway_uuid ' , $ transaction ->gateway_uuid )
405405 ->where ('gateway_reference_id ' , $ transaction ->gateway_reference_id )
406406 ->where ('type ' , 'refund ' )
407407 ->whereNotIn ('status ' , ['failed ' ])
408408 ->sum ('amount ' );
409409 $ remainingPaymentAmount = max (0 , $ paidAmount - $ refundedAmount );
410- $ refundableAmount = min ($ remainingInvoiceAmount , $ remainingPaymentAmount );
410+ $ refundableAmount = min ($ remainingInvoiceAmount , $ remainingPaymentAmount );
411411
412412 if (!$ transaction ->gateway || $ refundableAmount <= 0 ) {
413413 return null ;
414414 }
415415
416416 return [
417- 'id ' => $ transaction ->public_id ,
418- 'uuid ' => $ transaction ->uuid ,
419- 'gateway_transaction_id ' => $ transaction ->gateway_reference_id ,
420- 'amount ' => $ paidAmount ,
421- 'currency ' => $ transaction ->currency ?? $ invoice ->currency ,
422- 'refunded_amount ' => $ refundedAmount ,
423- 'refundable_amount ' => $ refundableAmount ,
424- 'status ' => $ transaction ->status ,
425- 'created_at ' => optional ($ transaction ->created_at )->toISOString (),
417+ 'id ' => $ transaction ->public_id ,
418+ 'uuid ' => $ transaction ->uuid ,
419+ 'gateway_transaction_id ' => $ transaction ->gateway_reference_id ,
420+ 'amount ' => $ paidAmount ,
421+ 'currency ' => $ transaction ->currency ?? $ invoice ->currency ,
422+ 'refunded_amount ' => $ refundedAmount ,
423+ 'refundable_amount ' => $ refundableAmount ,
424+ 'status ' => $ transaction ->status ,
425+ 'created_at ' => optional ($ transaction ->created_at )->toISOString (),
426426 'requires_customer_action ' => $ transaction ->gateway ->driver === 'taler ' ,
427- 'gateway ' => [
427+ 'gateway ' => [
428428 'id ' => $ transaction ->gateway ->public_id ?? $ transaction ->gateway ->uuid ,
429429 'uuid ' => $ transaction ->gateway ->uuid ,
430430 'public_id ' => $ transaction ->gateway ->public_id ,
0 commit comments