From fa6337c6c477b9cf06053f7f9a1db864d2ff347a Mon Sep 17 00:00:00 2001 From: Kwesi Banson Jnr Date: Tue, 4 Aug 2026 18:00:47 +0000 Subject: [PATCH] added a new file for dealer api --- app/Http/Controllers/DealerAoiController.php | 112 +++++++++++++++++++ routes/api.php | 2 +- 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/DealerAoiController.php diff --git a/app/Http/Controllers/DealerAoiController.php b/app/Http/Controllers/DealerAoiController.php new file mode 100644 index 0000000..dcb1f2e --- /dev/null +++ b/app/Http/Controllers/DealerAoiController.php @@ -0,0 +1,112 @@ +all(), [ + 'merchant_id' => 'required|integer|exists:merchants,id', + 'amount' => 'required|numeric|min:1', + 'dealer_id' => 'required|integer|exists:org_users,id' + ]); + + if ($validator->fails()) { + return response()->json([ + 'code' => 3, + 'status' => 'failed', + 'message' => 'Validation error.', + 'errors' => $validator->errors() + ], 422); + } + + + $dealer_id = $request->dealer_id; + $merchant_id = $request->merchant_id; + + $amount = (float) $request->amount; + + $dealer = Models\OrgUser::findOrFail($dealer_id); + $merchant = Models\Merchant::findOrFail($merchant_id); + + if ($amount > $dealer->balance) { + return response()->json([ + 'code' => 3, + 'status' => 'failed', + 'message' => 'Insufficient Balance.', + 'balance' => $dealer->balance + ], 400); + } + + DB::beginTransaction(); + + try { + Models\MerchantTopUp::create([ + 'merchant_id' => $merchant_id, + 'amount' => $amount, + 'transaction_id' => 'MCH-' . Str::uuid(), + 'org_user_id' => $dealer_id, + 'created_by' => $dealer_id, + 'payment_method' => 'cash' + ]); + + $merchant->increment('balance', $amount); + $dealer->decrement('balance', $amount); + + DB::commit(); + + } catch (\Exception $e) { + DB::rollBack(); + \Log::error($e); + + return response()->json([ + 'code' => 3, + 'status' => 'failed', + 'message' => 'Merchant Top Up failed.' + ], 500); + } + + ActivityLog::create([ + 'user_id' => $dealer_id, + 'user_type' => 'Dealer', + 'description' => json_encode([ + 'title' => 'Dealer to Merchant Top Up', + 'data' => 'MWK ' . number_format($amount, 2) . ' for ' . $merchant->fullname, + 'type' => 'dealer topup' + ]) + ]); + + $merchant->refresh(); + + try { + if (!app()->environment('local')) { + (new Smsgateway())->sendSmsNew( + $merchant->phone, + "Hello {$merchant->fullname}, your account has been credited with MWK {$amount}. Your new balance is MWK {$merchant->balance}." + ); + } + } catch (\Exception $e) { + \Log::error($e); + } + + return response()->json([ + 'code' => 1, + 'status' => 'success', + 'message' => 'Merchant Top Up Successfully completed.' + ]); + } +} diff --git a/routes/api.php b/routes/api.php index ca33bc0..67fc9cf 100644 --- a/routes/api.php +++ b/routes/api.php @@ -45,7 +45,7 @@ Route::middleware('auth:sanctum')->group(function () { Route::post('dealer/balance', [ApiDealersController::class, 'getBalance']); Route::post('dealer/transactions', [ApiDealersController::class, 'getTransactionsJson']); Route::post('dealer/activities', [ApiDealersController::class, 'getActivity']); - Route::post('dealer/topupmerchant', [ApiDealersController::class, 'merchantTopupStore']); + Route::post('dealer/topupmerchant', [DealerAoiController::class, 'merchantTopupStore']); /*