feat: Implement payment proof upload for orders, add payment method selection API, and seed bank transfer payment options.

This commit is contained in:
pbao9
2026-03-11 16:57:26 +07:00
parent 74cbe2fe1a
commit 7f6264c87f
19 changed files with 706 additions and 49 deletions
@@ -22,11 +22,13 @@ use App\Repositories\Module\Vmware\VmwareRepositoryInterface;
use App\Repositories\Order\OrderRepositoryInterface;
use App\Repositories\Os\OsRepositoryInterface;
use App\Repositories\VPS\VPSRepositoryInterface;
use App\Models\Customer;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Yajra\DataTables\Facades\DataTables;
@@ -95,6 +97,14 @@ class OrderController extends Controller
{
$request->validated();
$customer = Customer::find($request->customer_id);
if (!$customer || (int) $customer->status !== 1) {
return response()->json([
'error' => true,
'message' => 'Tài khoản khách hàng không hoạt động. Không thể tạo đơn hàng.',
], 422);
}
$total = 0;
$product_item = [];
$relid_data = [];
@@ -167,28 +177,32 @@ class OrderController extends Controller
$total += $item['quantity'] * ($item['price_custom'] ?? ($product->price_sale ?? $product->price)) * $billingDetails['billing'];
}
$paymentRecord = \App\Models\Payment::find((int) $request->payment_method);
$isBankTransfer = $paymentRecord && (int) $paymentRecord->type === 1;
$order_item = [
'order_num' => Helper::generateUniqueCode(),
'customer_id' => $request->customer_id,
'invoice_id' => 0,
'discount_id' => $request->discount_id ?: null,
'amount' => $total + ($total * (10 / 100)),
'payment_method' => $request->payment_method,
'user_id' => Auth::guard('admin')->id(),
'status' => $request->status,
'client_ip' => $request->getClientIp()
'order_num' => Helper::generateUniqueCode(),
'customer_id' => $request->customer_id,
'invoice_id' => 0,
'discount_id' => $request->discount_id ?: null,
'amount' => $total + ($total * (10 / 100)),
'payment_id' => $paymentRecord?->id,
'payment_proof' => $request->payment_proof ?? null,
'user_id' => Auth::guard('admin')->id(),
'status' => $request->status,
'client_ip' => $request->getClientIp(),
];
$invoice_data = [
'date' => Carbon::today()->toDateString(),
'due_date' => Carbon::today()->toDateString(),
'date_paid' => (int)$request->status === 1 ? Carbon::today()->toDateString() : null,
'sub_total' => $total,
'tax' => $total * (10 / 100),
'total' => $total + ($total * (10 / 100)),
'payment_method' => $request->payment_method,
'date' => Carbon::today()->toDateString(),
'due_date' => Carbon::today()->toDateString(),
'date_paid' => (int)$request->status === 1 ? Carbon::today()->toDateString() : null,
'sub_total' => $total,
'tax' => $total * (10 / 100),
'total' => $total + ($total * (10 / 100)),
'payment_id' => $paymentRecord?->id,
'customer_id' => $request->create_invoice ? $request->customer_id : 0,
'status' => $request->status
'status' => $request->status
];
DB::beginTransaction();
@@ -242,30 +256,39 @@ class OrderController extends Controller
$this->invoiceItemRepository->insert($invoiceItemData);
$now = now();
\App\Models\OrderItem::insert(array_map(fn($item) => [
'order_id' => $order->id,
'product_id' => $item['product_id'],
'billing_cycle' => $item['billing_cycle'],
'months' => $item['months'],
'os_id' => $item['os_id'],
'quantity' => $item['quantity'],
'server_name' => $item['server_name'],
'price' => $item['price'],
'created_at' => $now,
'updated_at' => $now,
], $product_item));
\App\Models\OrderItem::insert(array_map(function ($item) use ($now) {
$expiryDate = $now->copy()->addMonths($item['months']);
return [
'order_id' => $order->id,
'product_id' => $item['product_id'],
'billing_cycle' => $item['billing_cycle'],
'months' => $item['months'],
'expiry_date' => $expiryDate,
'os_id' => $item['os_id'],
'quantity' => $item['quantity'],
'server_name' => $item['server_name'],
'price' => $item['price'],
'created_at' => $now,
'updated_at' => $now,
];
}, $product_item));
// Dispatch VM creation job for VPS/cloud products
$vpsTypes = ['vps', 'cloud'];
$createdOrderItems = \App\Models\OrderItem::where('order_id', $order->id)->get();
foreach ($createdOrderItems as $orderItem) {
$pt = $orderItem->product?->product_type ?? $orderItem->billing_cycle;
if (in_array($orderItem->product?->product_type, $vpsTypes)) {
\App\Jobs\VirtualMachine\Create\CreateVirtualMachineJob::dispatch($orderItem)
->delay(now()->addSeconds(10));
}
}
// Deduct customer balance for non-bank-transfer payments
if (!$isBankTransfer) {
Customer::where('id', $request->customer_id)
->decrement('total', (int) ($total + ($total * (10 / 100))));
}
DB::commit();
return response()->json([
@@ -284,10 +307,15 @@ class OrderController extends Controller
public function show(int $id): JsonResponse
{
$order = $this->orderRepository->findWithRelationships(
$id,
['orderItem', 'user', 'customer.invoiceItem.type', 'payment', 'discount', 'invoice.invoiceItem']
);
$order = Order::with([
'orderItems.product',
'orderItems.os',
'user',
'customer',
'payment',
'discount',
'invoice'
])->find($id);
if (!$order) {
return response()->json([
@@ -501,6 +529,19 @@ class OrderController extends Controller
}
}
public function uploadProof(Request $request): JsonResponse
{
$request->validate(['file' => 'required|image|max:5120']);
$path = $request->file('file')->store('payment-proofs', 'public');
return response()->json([
'error' => false,
'url' => Storage::disk('public')->url($path),
'path' => $path,
]);
}
private function calculateBillingDetails($billing): array
{
$months = match ($billing) {