- Add Enums: TransactionType, TransactionStatus, PaymentGateway - Migrations: wallets, wallet_transactions, wallet_activity_logs - Models: Wallet, WalletTransaction, WalletActivityLog - Services: WalletService with complete business logic - Controllers: WalletController, PaymentController - Filament Resources: WalletResource, WalletTransactionResource - Artisan Command: ifnex:token for API token generation - Sanctum integration with statefulApi and exception handling - Full API endpoints: balance, transactions, admin-adjust, freeze/unfreeze
112 lines
2.8 KiB
PHP
112 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Wallet extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'balance',
|
|
'total_deposited',
|
|
'total_withdrawn',
|
|
'is_frozen',
|
|
'freeze_reason',
|
|
'frozen_at',
|
|
'frozen_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'balance' => 'decimal:2',
|
|
'total_deposited' => 'decimal:2',
|
|
'total_withdrawn' => 'decimal:2',
|
|
'is_frozen' => 'boolean',
|
|
'frozen_at' => 'datetime',
|
|
];
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function transactions(): HasMany
|
|
{
|
|
return $this->hasMany(WalletTransaction::class)->orderByDesc('created_at');
|
|
}
|
|
|
|
public function completedTransactions(): HasMany
|
|
{
|
|
return $this->transactions()->where('status', 'completed');
|
|
}
|
|
|
|
public function pendingTransactions(): HasMany
|
|
{
|
|
return $this->transactions()->where('status', 'pending');
|
|
}
|
|
|
|
public function activityLogs(): HasMany
|
|
{
|
|
return $this->hasMany(WalletActivityLog::class)->orderByDesc('created_at');
|
|
}
|
|
|
|
public function frozenByUser(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'frozen_by');
|
|
}
|
|
|
|
// Helper methods
|
|
public function isFrozen(): bool
|
|
{
|
|
return $this->is_frozen;
|
|
}
|
|
|
|
public function hasSufficientBalance(float $amount): bool
|
|
{
|
|
return $this->balance >= $amount && !$this->is_frozen;
|
|
}
|
|
|
|
public function freeze(string $reason, ?User $admin = null): bool
|
|
{
|
|
return $this->update([
|
|
'is_frozen' => true,
|
|
'freeze_reason' => $reason,
|
|
'frozen_at' => now(),
|
|
'frozen_by' => $admin?->id,
|
|
]);
|
|
}
|
|
|
|
public function unfreeze(?User $admin = null): bool
|
|
{
|
|
return $this->update([
|
|
'is_frozen' => false,
|
|
'freeze_reason' => null,
|
|
'frozen_at' => null,
|
|
'frozen_by' => null,
|
|
]);
|
|
}
|
|
|
|
public function refreshBalance(): self
|
|
{
|
|
$balance = $this->completedTransactions()->sum('amount');
|
|
$totalDeposited = $this->completedTransactions()
|
|
->where('type', 'deposit')
|
|
->sum('amount');
|
|
$totalWithdrawn = $this->completedTransactions()
|
|
->whereIn('type', ['withdrawal', 'order_payment'])
|
|
->sum('amount');
|
|
|
|
$this->update([
|
|
'balance' => $balance,
|
|
'total_deposited' => $totalDeposited,
|
|
'total_withdrawn' => abs($totalWithdrawn),
|
|
]);
|
|
|
|
return $this->fresh();
|
|
}
|
|
} |