To implement a feature where users can use their wallet balance to purchase airtime and data in Laravel, you'll need to create several components:
- User Wallet Model
- Airtime and Data Purchase Controller
- Routes
- Migrations and Database Tables
- Blade Views
Here's a simplified example of how you can implement this feature:
- User Wallet Model:
Create a UserWallet model using the following command:
php artisan make:model UserWallet -m
Update the migration file to include the necessary columns:
public function up()
{
Schema::create('user_wallets', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->decimal('balance', 10, 2);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
- Airtime and Data Purchase Controller:
Create a PurchaseController using the following command:
php artisan make:controller PurchaseController
Add the following methods to the PurchaseController:
use App\Models\UserWallet;
use Illuminate\Http\Request;
public function purchaseAirtime(Request $request)
{
$request->validate([
'amount' => 'required|numeric|min:1',
]);
$user = $request->user();
$wallet = UserWallet::where('user_id', $user->id)->first();
if ($wallet->balance < $request->amount) {
return back()->withErrors(['Insufficient balance.']);
}
// Process the airtime purchase here
$wallet->decrement('balance', $request->amount);
$wallet->save();
return redirect()->route('purchase.success');
}
public function purchaseData(Request $request)
{
$request->validate([
'amount' => 'required|numeric|min:1',
]);
$user = $request->user();
$wallet = UserWallet::where('user_id', $user->id)->first();
if ($wallet->balance < $request->amount) {
return back()->withErrors(['Insufficient balance.']);
}
// Process the data purchase here
$wallet->decrement('balance', $request->amount);
$wallet->save();
return redirect()->route('purchase.success');
}
- Routes:
Add the following routes to your routes/web.php file:
Route::middleware(['auth'])->group(function () {
Route::get('/purchase/airtime', 'PurchaseController@purchaseAirtimeForm')->name('purchase.airtime.form');
Route::post('/purchase/airtime', 'PurchaseController@purchaseAirtime')->name('purchase.airtime');
Route::get('/purchase/data', 'PurchaseController@purchaseDataForm')->name('purchase.data.form');
Route::post('/purchase/data', 'PurchaseController@purchaseData')->name('purchase.data');
Route::get('/purchase/success', 'PurchaseController@purchaseSuccess')->name('purchase.success');
});
- Migrations and Database Tables:
Run the migration to create the user_wallets table:
php artisan migrate
- Blade Views:
Create the necessary Blade views for the purchase forms and success page.
This example assumes you have a User model with an authenticated user. You'll need to integrate with a third-party API or service to process the airtime and data purchases. Replace the comments in the PurchaseController with the appropriate code to handle these transactions.