feat: 增强奖池与钱包管理功能
更新 AdminJackpotPoolUpdateController 校验规则,禁止传入 current_amount。 优化 AdminRiskPoolManualStatusController:更新奖池状态后同步 Redis 状态。 在 TransferOrderReconcileController 中新增 completeCredit 方法,用于处理卡住的转账订单对账。 调整 TransferOrderListController:优化转账订单处理条件。 在 TicketItemsIndexController 中实现支持时区的日期筛选,提升日期处理准确性。 扩展 JackpotPool 模型,新增 adjustments 关联关系。 改进票据与钱包相关服务中的错误处理和事务管理。
This commit is contained in:
@@ -333,6 +333,93 @@ test('ticket place is idempotent by player draw and client trace id', function (
|
||||
expect((int) $wallet->balance)->toBe(200_000 - 12_400);
|
||||
});
|
||||
|
||||
test('ticket place idempotency is scoped per draw not global trace', function (): void {
|
||||
$player = ticketPlayerWithWallet();
|
||||
ticketOpenDraw('20260511-001');
|
||||
|
||||
Draw::query()->create([
|
||||
'draw_no' => '20260511-002',
|
||||
'business_date' => '2026-05-11',
|
||||
'sequence_no' => 2,
|
||||
'status' => DrawStatus::Open->value,
|
||||
'start_time' => now()->subMinutes(2),
|
||||
'close_time' => now()->addMinutes(5),
|
||||
'draw_time' => now()->addMinutes(6),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => null,
|
||||
'current_result_version' => 0,
|
||||
'settle_version' => 0,
|
||||
'is_reopened' => false,
|
||||
]);
|
||||
|
||||
$trace = 'shared-trace-two-draws';
|
||||
$payloadA = array_merge(ticketPreviewPayload('20260511-001'), ['client_trace_id' => $trace]);
|
||||
$payloadB = array_merge(ticketPreviewPayload('20260511-002'), ['client_trace_id' => $trace]);
|
||||
|
||||
$first = $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
||||
->postJson('/api/v1/ticket/place', $payloadA)
|
||||
->assertOk()
|
||||
->json('data.order_no');
|
||||
|
||||
$second = $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
||||
->postJson('/api/v1/ticket/place', $payloadB)
|
||||
->assertOk()
|
||||
->json('data.order_no');
|
||||
|
||||
expect($second)->not->toBe($first)
|
||||
->and(TicketOrder::query()->count())->toBe(2);
|
||||
});
|
||||
|
||||
test('ticket place accepts draw pending in db when hall rules show open', function (): void {
|
||||
$player = ticketPlayerWithWallet();
|
||||
Draw::query()->create([
|
||||
'draw_no' => '20260511-pending-hall',
|
||||
'business_date' => '2026-05-11',
|
||||
'sequence_no' => 99,
|
||||
'status' => DrawStatus::Pending->value,
|
||||
'start_time' => now()->subMinute(),
|
||||
'close_time' => now()->addMinutes(5),
|
||||
'draw_time' => now()->addMinutes(6),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => null,
|
||||
'current_result_version' => 0,
|
||||
'settle_version' => 0,
|
||||
'is_reopened' => false,
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
||||
->postJson('/api/v1/ticket/place', [
|
||||
'draw_id' => '20260511-pending-hall',
|
||||
'currency_code' => 'NPR',
|
||||
'client_trace_id' => 'pending-hall-open-place',
|
||||
'lines' => [
|
||||
['number' => '1234', 'play_code' => 'big', 'amount' => 10_000],
|
||||
],
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('code', ErrorCode::Success->value);
|
||||
|
||||
expect(TicketOrder::query()->where('client_trace_id', 'pending-hall-open-place')->exists())->toBeTrue();
|
||||
});
|
||||
|
||||
test('ticket place rejects bet when only frozen balance would cover stake', function (): void {
|
||||
$player = ticketPlayerWithWallet(20_000);
|
||||
ticketOpenDraw();
|
||||
|
||||
$wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
|
||||
$wallet->forceFill(['balance' => 20_000, 'frozen_balance' => 19_000])->save();
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
||||
->postJson('/api/v1/ticket/place', array_merge(ticketPreviewPayload(), [
|
||||
'client_trace_id' => 'frozen-balance-guard',
|
||||
'lines' => [
|
||||
['number' => '1234', 'play_code' => 'big', 'amount' => 10_000],
|
||||
],
|
||||
]))
|
||||
->assertStatus(400)
|
||||
->assertJsonPath('code', ErrorCode::BetInsufficientBalance->value);
|
||||
});
|
||||
|
||||
test('box family estimated max payout is the sum of every expanded combination payout', function (): void {
|
||||
$player = ticketPlayerWithWallet(500_000);
|
||||
ticketOpenDraw();
|
||||
@@ -1043,3 +1130,143 @@ test('ticket place reverses wallet and releases risk when post deduction confirm
|
||||
->and(WalletTxn::query()->where('biz_no', $order->order_no)->where('biz_type', 'bet_reverse')->count())->toBe(1)
|
||||
->and((int) RiskPool::query()->where('draw_id', $draw->id)->where('normalized_number', '1234')->value('locked_amount'))->toBe(0);
|
||||
});
|
||||
|
||||
test('ticket place idempotency replays refunded order for same trace', function (): void {
|
||||
$player = ticketPlayerWithWallet();
|
||||
$draw = ticketOpenDraw();
|
||||
$trace = 'trace-refunded-replay';
|
||||
|
||||
$order = TicketOrder::query()->create([
|
||||
'order_no' => 'TO-REFUNDED-IDEM',
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'currency_code' => 'NPR',
|
||||
'total_bet_amount' => 100,
|
||||
'total_rebate_amount' => 0,
|
||||
'total_actual_deduct' => 100,
|
||||
'total_estimated_payout' => 3000,
|
||||
'status' => 'refunded',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => $trace,
|
||||
]);
|
||||
|
||||
$payload = array_merge(ticketPreviewPayload(), ['client_trace_id' => $trace]);
|
||||
|
||||
$replay = $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
||||
->postJson('/api/v1/ticket/place', $payload)
|
||||
->assertOk()
|
||||
->assertJsonPath('code', ErrorCode::Success->value)
|
||||
->json('data');
|
||||
|
||||
expect($replay['order_no'])->toBe($order->order_no)
|
||||
->and(TicketOrder::query()->count())->toBe(1)
|
||||
->and(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('ticket pending confirmation reconcile refunds when draw no longer accepts bets', function (): void {
|
||||
$draw = ticketOpenDraw();
|
||||
$draw->forceFill([
|
||||
'status' => DrawStatus::Closed->value,
|
||||
'close_time' => now()->subMinute(),
|
||||
'draw_time' => now()->subMinute(),
|
||||
])->save();
|
||||
|
||||
$player = ticketPlayerWithWallet(10_000);
|
||||
$wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
|
||||
|
||||
$order = TicketOrder::query()->create([
|
||||
'order_no' => 'TO-PENDING-CLOSED',
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'currency_code' => 'NPR',
|
||||
'total_bet_amount' => 100,
|
||||
'total_rebate_amount' => 0,
|
||||
'total_actual_deduct' => 100,
|
||||
'total_estimated_payout' => 3000,
|
||||
'status' => 'pending_confirm',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => 'pending-on-closed-draw',
|
||||
'created_at' => now()->subMinutes(20),
|
||||
'updated_at' => now()->subMinutes(20),
|
||||
]);
|
||||
TicketOrder::query()->whereKey($order->id)->update(['updated_at' => now()->subMinutes(20)]);
|
||||
|
||||
$item = TicketItem::query()->create([
|
||||
'ticket_no' => 'TK-PENDING-CLOSED',
|
||||
'order_id' => $order->id,
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'original_number' => '1234',
|
||||
'normalized_number' => '1234',
|
||||
'play_code' => 'big',
|
||||
'dimension' => 4,
|
||||
'digit_slot' => null,
|
||||
'bet_mode' => 'straight',
|
||||
'unit_bet_amount' => 100,
|
||||
'total_bet_amount' => 100,
|
||||
'rebate_rate_snapshot' => 0,
|
||||
'commission_rate_snapshot' => 0,
|
||||
'actual_deduct_amount' => 100,
|
||||
'odds_snapshot_json' => [],
|
||||
'rule_snapshot_json' => [],
|
||||
'combination_count' => 1,
|
||||
'estimated_max_payout' => 3000,
|
||||
'risk_locked_amount' => 3000,
|
||||
'status' => 'pending_confirm',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'win_amount' => 0,
|
||||
'jackpot_win_amount' => 0,
|
||||
'settled_at' => null,
|
||||
'created_at' => now()->subMinutes(20),
|
||||
'updated_at' => now()->subMinutes(20),
|
||||
]);
|
||||
|
||||
TicketCombination::query()->create([
|
||||
'ticket_item_id' => $item->id,
|
||||
'combination_no' => 1,
|
||||
'number_4d' => '1234',
|
||||
'bet_amount' => 100,
|
||||
'estimated_payout' => 3000,
|
||||
'created_at' => now()->subMinutes(20),
|
||||
]);
|
||||
|
||||
RiskPool::query()->create([
|
||||
'draw_id' => $draw->id,
|
||||
'normalized_number' => '1234',
|
||||
'total_cap_amount' => 5000,
|
||||
'locked_amount' => 3000,
|
||||
'remaining_amount' => 2000,
|
||||
'sold_out_status' => 0,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
$wallet->forceFill(['balance' => 9_900])->save();
|
||||
|
||||
WalletTxn::query()->create([
|
||||
'txn_no' => 'WL-PENDING-CLOSED',
|
||||
'player_id' => $player->id,
|
||||
'wallet_id' => $wallet->id,
|
||||
'biz_type' => 'bet_deduct',
|
||||
'biz_no' => 'TO-PENDING-CLOSED',
|
||||
'direction' => 2,
|
||||
'amount' => 100,
|
||||
'balance_before' => 10_000,
|
||||
'balance_after' => 9_900,
|
||||
'status' => 'posted',
|
||||
'external_ref_no' => null,
|
||||
'idempotent_key' => 'bet_deduct:TO-PENDING-CLOSED',
|
||||
'remark' => null,
|
||||
]);
|
||||
|
||||
$this->artisan('lottery:ticket-pending-confirm-reconcile --stale-minutes=15 --limit=100')
|
||||
->expectsOutputToContain('refunded: 1')
|
||||
->assertExitCode(0);
|
||||
|
||||
expect($order->fresh()->status)->toBe('refunded')
|
||||
->and($item->fresh()->status)->toBe('refunded')
|
||||
->and($item->fresh()->fail_reason_code)->toBe('draw_no_longer_open')
|
||||
->and((int) PlayerWallet::query()->where('player_id', $player->id)->value('balance'))->toBe(10_000)
|
||||
->and(WalletTxn::query()->where('biz_type', 'bet_reverse')->where('biz_no', 'TO-PENDING-CLOSED')->count())->toBe(1)
|
||||
->and((int) RiskPool::query()->where('draw_id', $draw->id)->where('normalized_number', '1234')->value('locked_amount'))->toBe(0);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user