fix(core): harden settlement and wallet integration
This commit is contained in:
@@ -3,19 +3,20 @@
|
||||
namespace App\Services\Player;
|
||||
|
||||
use App\Models\Player;
|
||||
use App\Services\Agent\AgentUsedCreditSyncService;
|
||||
use App\Support\AgentOverdueGuard;
|
||||
use App\Support\CreditAmountScale;
|
||||
use App\Support\PlayerFundingMode;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Agent\AgentUsedCreditSyncService;
|
||||
|
||||
final class PlayerCreditService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AgentUsedCreditSyncService $usedCreditSync,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{credit_limit?: int} $payload
|
||||
*/
|
||||
@@ -163,8 +164,12 @@ final class PlayerCreditService
|
||||
$this->syncAgentUsedCredit($player);
|
||||
}
|
||||
|
||||
public function applySettledLoss(Player $player, int $amountMinor, int $ticketItemId): void
|
||||
{
|
||||
public function applySettledLoss(
|
||||
Player $player,
|
||||
int $amountMinor,
|
||||
int $ticketItemId,
|
||||
int $settlementVersion = 0,
|
||||
): void {
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
@@ -187,6 +192,7 @@ final class PlayerCreditService
|
||||
'reason' => 'game_settlement_loss',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'settlement_version' => $settlementVersion,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
@@ -218,8 +224,12 @@ final class PlayerCreditService
|
||||
$this->syncAgentUsedCredit($player);
|
||||
}
|
||||
|
||||
public function applySettledWin(Player $player, int $amountMinor, int $ticketItemId): void
|
||||
{
|
||||
public function applySettledWin(
|
||||
Player $player,
|
||||
int $amountMinor,
|
||||
int $ticketItemId,
|
||||
int $settlementVersion = 0,
|
||||
): void {
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
@@ -228,30 +238,55 @@ final class PlayerCreditService
|
||||
return;
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$applied = DB::transaction(function () use ($player, $amountMinor, $ticketItemId, $settlementVersion): bool {
|
||||
$now = now();
|
||||
$currency = (string) $player->default_currency;
|
||||
$requestedMajor = CreditAmountScale::minorToMajor($amountMinor, $currency);
|
||||
$account = DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
$appliedMajor = $account === null
|
||||
? 0
|
||||
: min((int) $account->used_credit, $requestedMajor);
|
||||
$appliedMinor = CreditAmountScale::majorToMinor($appliedMajor, $currency);
|
||||
|
||||
// 先写 credit_ledger:以 (ref_type, ref_id, reason) partial unique 索引为幂等闸门。
|
||||
// 已存在同 (ticket_item, game_settlement_win) 的流水则直接返回,避免并发/重入场景下重复扣减 used_credit。
|
||||
try {
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => $amountMinor,
|
||||
'reason' => 'game_settlement_win',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
} catch (QueryException $e) {
|
||||
if ($this->isUniqueViolation($e)) {
|
||||
return;
|
||||
// 流水记录实际释放的额度,而非可能超过 used_credit 的名义中奖额。
|
||||
// 结算被驳回时必须按这个实际值恢复,否则会凭空增加玩家已用额度。
|
||||
try {
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => $appliedMinor,
|
||||
'reason' => 'game_settlement_win',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'settlement_version' => $settlementVersion,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
} catch (QueryException $e) {
|
||||
if ($this->isUniqueViolation($e)) {
|
||||
return false;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->decreaseUsedCredit($player, $amountMinor);
|
||||
$this->syncAgentUsedCredit($player);
|
||||
if ($account !== null && $appliedMajor > 0) {
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->update([
|
||||
'used_credit' => (int) $account->used_credit - $appliedMajor,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
if ($applied) {
|
||||
$this->syncAgentUsedCredit($player);
|
||||
}
|
||||
}
|
||||
|
||||
public function assertMayPlaceBet(Player $player, int $amountMinor): void
|
||||
@@ -290,8 +325,12 @@ final class PlayerCreditService
|
||||
}
|
||||
}
|
||||
|
||||
public function releaseBetHold(Player $player, int $amountMinor, int $ticketItemId): void
|
||||
{
|
||||
public function releaseBetHold(
|
||||
Player $player,
|
||||
int $amountMinor,
|
||||
int $ticketItemId,
|
||||
int $settlementVersion = 0,
|
||||
): void {
|
||||
if ($amountMinor <= 0 || ! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
@@ -308,6 +347,7 @@ final class PlayerCreditService
|
||||
'reason' => 'bet_hold_release',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'settlement_version' => $settlementVersion,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
@@ -354,14 +394,33 @@ final class PlayerCreditService
|
||||
$this->syncAgentUsedCredit($player);
|
||||
}
|
||||
|
||||
public function reverseGameSettlement(Player $player, int $gameWinLossSigned, int $ticketItemId): void
|
||||
{
|
||||
public function reverseGameSettlement(
|
||||
Player $player,
|
||||
int $gameWinLossSigned,
|
||||
int $ticketItemId,
|
||||
int $settlementVersion = 0,
|
||||
): void {
|
||||
if ($gameWinLossSigned === 0 || ! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$amountMinor = abs($gameWinLossSigned);
|
||||
if ($gameWinLossSigned < 0) {
|
||||
// 中奖释放 used_credit 时可能受 0 下限截断;反转只能恢复当时实际释放的数额。
|
||||
// 旧流水或人工修复数据可能不存在,保留名义值兜底以兼容历史记录。
|
||||
$recordedAmount = DB::table('credit_ledger')
|
||||
->where('owner_type', 'player')
|
||||
->where('owner_id', $player->id)
|
||||
->where('ref_type', 'ticket_item')
|
||||
->where('ref_id', $ticketItemId)
|
||||
->where('reason', 'game_settlement_win')
|
||||
->where('settlement_version', $settlementVersion)
|
||||
->value('amount');
|
||||
if ($recordedAmount !== null) {
|
||||
$amountMinor = abs((int) $recordedAmount);
|
||||
}
|
||||
}
|
||||
|
||||
// 先写 credit_ledger:以 (ref_type, ref_id, reason) partial unique 索引为幂等闸门。
|
||||
try {
|
||||
@@ -372,6 +431,7 @@ final class PlayerCreditService
|
||||
'reason' => 'game_settlement_reversal',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'settlement_version' => $settlementVersion,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
@@ -410,6 +470,56 @@ final class PlayerCreditService
|
||||
$this->syncAgentUsedCredit($player);
|
||||
}
|
||||
|
||||
/** 驳回结算后恢复原注占额,使票回到 pending_draw 时额度状态与结算前一致。 */
|
||||
public function restoreBetHoldAfterSettlementReversal(
|
||||
Player $player,
|
||||
int $amountMinor,
|
||||
int $ticketItemId,
|
||||
int $settlementVersion = 0,
|
||||
): void {
|
||||
if ($amountMinor <= 0 || ! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$now = now();
|
||||
try {
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => -$amountMinor,
|
||||
'reason' => 'bet_hold_restore',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'settlement_version' => $settlementVersion,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
} catch (QueryException $e) {
|
||||
if ($this->isUniqueViolation($e)) {
|
||||
return;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$majorDelta = CreditAmountScale::minorToMajor($amountMinor, (string) $player->default_currency);
|
||||
$row = DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
if ($row === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->update([
|
||||
'used_credit' => (int) $row->used_credit + $majorDelta,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
$this->syncAgentUsedCredit($player);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $cumulativePaidMinor 账单累计已登记收付(minor),支持部分收付多笔递增。
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user