add fixes for invoice balance

This commit is contained in:
cnohall 2025-10-17 14:55:36 +09:00
parent efaceba367
commit 8615d5bbc3
2 changed files with 21 additions and 1 deletions

View File

@ -16,6 +16,8 @@ use App\Models\Payment;
use App\Models\SystemLog;
use App\Models\GatewayType;
use App\Models\PaymentType;
use App\Models\PaymentHash;
use App\Models\Invoice;
use App\Jobs\Util\SystemLogger;
use App\Utils\Traits\MakesHash;
use App\Exceptions\PaymentFailed;
@ -143,6 +145,8 @@ class Blockonomics implements LivewireMethodInterface
// 'status' => ['required'],
]);
$this->payment_hash = PaymentHash::where('hash', $request->payment_hash)->firstOrFail();
try {
// Satoshis is the smallest unit of Bitcoin
$amount_received_satoshis = $request->btc_amount;

View File

@ -121,7 +121,7 @@ class BlockonomicsPaymentDriver extends BaseDriver
return response()->json(['message' => 'Payment not found'], 200);
}
// If payment is already completed, no need to process again
// // If payment is already completed, no need to process again
if ($payment->status_id == Payment::STATUS_COMPLETED) {
return response()->json(['message' => 'Payment already completed'], 200);
}
@ -135,6 +135,22 @@ class BlockonomicsPaymentDriver extends BaseDriver
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->save();
$this->payment_hash = PaymentHash::where('payment_id', $payment->id)->firstOrFail();
$invoices_data = $this->payment_hash->invoices();
$fiat_amount = $payment->amount;
// How about recurring invoices?
if (is_array($invoices_data)) {
foreach ($invoices_data as $invoice_data) {
$invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($invoice_data->invoice_id));
// Do I need to loop through each payment for the invoice?
if ($invoice) {
$invoice->balance = $invoice->amount - $fiat_amount;
$invoice->save();
}
}
}
return response()->json([
'message' => 'Payment confirmed successfully',
], 200);