110 lines
3.8 KiB
PHP
110 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\Wallet;
|
|
use App\Models\Transaction;
|
|
use App\Models\AuditLog;
|
|
|
|
class WalletHelper
|
|
{
|
|
/**
|
|
* Process a wallet transaction (e.g., deposit, bet) with atomic updates and audit logging.
|
|
*
|
|
* @param int $userId
|
|
* @param float $amount
|
|
* @param string $type (deposit, bet, win, withdrawal)
|
|
* @param string|null $description
|
|
* @param string|null $ipAddress
|
|
* @return Transaction
|
|
* @throws \Exception
|
|
*/
|
|
public static function processTransaction($userId, $amount, $type, $description = null, $ipAddress = null)
|
|
{
|
|
return DB::transaction(function () use ($userId, $amount, $type, $description, $ipAddress) {
|
|
// Lock wallet for atomic update
|
|
$wallet = Wallet::where('user_id', $userId)->lockForUpdate()->firstOrFail();
|
|
|
|
// Validate balance for bets/withdrawals
|
|
if (in_array($type, ['bet', 'withdrawal']) && $wallet->balance < $amount) {
|
|
throw new \Exception('Insufficient Skunk Coins');
|
|
}
|
|
|
|
// Update balance
|
|
$newBalance = in_array($type, ['bet', 'withdrawal']) ? $wallet->balance - $amount : $wallet->balance + $amount;
|
|
$wallet->update(['balance' => $newBalance]);
|
|
|
|
// Create transaction with SHA-256 hash
|
|
$hash = hash('sha256', $userId . $amount . $type . time());
|
|
$transaction = Transaction::create([
|
|
'wallet_id' => $wallet->id,
|
|
'amount' => $amount,
|
|
'type' => $type,
|
|
'description' => $description,
|
|
'hash' => $hash,
|
|
]);
|
|
|
|
// Log action in audit_logs
|
|
AuditLog::create([
|
|
'user_id' => $userId,
|
|
'action' => 'wallet_' . $type,
|
|
'details' => json_encode([
|
|
'wallet_id' => $wallet->id,
|
|
'amount' => $amount,
|
|
'new_balance' => $newBalance,
|
|
'transaction_id' => $transaction->id,
|
|
]),
|
|
'ip_address' => $ipAddress,
|
|
]);
|
|
|
|
return $transaction;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Redeem a promo code for Skunk Coins.
|
|
*
|
|
* @param int $userId
|
|
* @param string $code
|
|
* @param string|null $ipAddress
|
|
* @return Transaction
|
|
* @throws \Exception
|
|
*/
|
|
public static function redeemPromoCode($userId, $code, $ipAddress = null)
|
|
{
|
|
return DB::transaction(function () use ($userId, $code, $ipAddress) {
|
|
$promo = \App\Models\PromoCode::where('code', $code)
|
|
->where('is_active', true)
|
|
->where(function ($query) {
|
|
$query->where('uses_remaining', -1)->orWhere('uses_remaining', '>', 0);
|
|
})
|
|
->where(function ($query) {
|
|
$query->whereNull('expires_at')->orWhere('expires_at', '>', now());
|
|
})
|
|
->firstOrFail();
|
|
|
|
// Check if already redeemed
|
|
if (\App\Models\PromoCodeRedemption::where('user_id', $userId)->where('promo_code_id', $promo->id)->exists()) {
|
|
throw new \Exception('Promo code already redeemed');
|
|
}
|
|
|
|
// Process deposit
|
|
$transaction = self::processTransaction($userId, $promo->value, 'deposit', "Promo code: $code", $ipAddress);
|
|
|
|
// Record redemption
|
|
\App\Models\PromoCodeRedemption::create([
|
|
'promo_code_id' => $promo->id,
|
|
'user_id' => $userId,
|
|
'redeemed_at' => now(),
|
|
]);
|
|
|
|
// Decrement uses if limited
|
|
if ($promo->uses_remaining !== -1) {
|
|
$promo->decrement('uses_remaining');
|
|
}
|
|
|
|
return $transaction;
|
|
});
|
|
}
|
|
} |