This commit is contained in:
baodev0
2026-07-09 11:19:50 +07:00
parent 64232d9168
commit 61c0e026c8
28 changed files with 939 additions and 230 deletions
+7 -22
View File
@@ -15,6 +15,7 @@ use App\Models\ServiceAddon;
use App\Repositories\Api\Invoice\InvoiceRepository;
use App\Repositories\Api\Order\OrderRepository;
use App\Repositories\Api\Service\ServiceRepository;
use App\Services\InvoicePaymentService;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@@ -25,6 +26,7 @@ class OrderService
protected OrderRepository $orderRepo,
protected ServiceRepository $productRepo,
protected InvoiceRepository $invoiceRepo,
protected InvoicePaymentService $invoicePaymentService,
) {}
// =========================================================================
@@ -123,35 +125,18 @@ class OrderService
));
}
// Debit balance
$customer->total -= $order->total;
$customer->save();
CustomerActivityLog::create([
'agent' => 'api',
'client_ip' => request()?->ip(),
'customer_id' => $customer->id,
'description' => sprintf(
'Thanh toán đơn hàng #%s. Số tiền: %s VNĐ. Số dư còn: %s VNĐ.',
$order->order_num,
number_format($order->total),
number_format($customer->total)
),
]);
// Allow re-paying a cancelled order
if ($order->status === OrderStatus::CANCELLED->value) {
$order->update(['status' => OrderStatus::PENDING->value]);
}
// Mark order → ACTIVE, invoice → PAID
$processedOrder = $this->orderRepo->processPayment($order, $method);
// Provision a Service for each order item
foreach ($processedOrder->orderItems as $item) {
$this->provisionService($processedOrder, $item, $customer);
if (! $order->invoice) {
throw new Exception('Không tìm thấy hóa đơn của đơn hàng.');
}
$this->invoicePaymentService->payWithBalance($order->invoice, $customer, $method);
$processedOrder = $order->fresh(['orderItems', 'invoice', 'customer']);
$this->dispatchAdminNotification($processedOrder, $customer);
return $processedOrder;