feat: 支持开奖重开与风险池原子扣减,完善投注部分成功流程

This commit is contained in:
2026-05-18 11:28:11 +08:00
parent 4f143c7cb1
commit 9157dcb6a1
14 changed files with 526 additions and 103 deletions

View File

@@ -436,6 +436,16 @@ test('admin can reopen cooldown draw for a replacement result batch', function (
expect($draw->is_reopened)->toBeTrue();
expect($draw->cooling_end_time)->toBeNull();
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson("/api/v1/admin/draws/{$draw->id}/rng")
->assertOk()
->assertJsonPath('data.batch.result_version', 2)
->assertJsonPath('data.batch.items_count', 23);
$draw->refresh();
expect($draw->current_result_version)->toBe(2);
expect(DrawResultBatch::query()->where('draw_id', $draw->id)->count())->toBe(2);
Carbon::setTestNow();
});

View File

@@ -471,7 +471,7 @@ test('§14.5 jackpot contributes on place and stays in pool when no first-prize
expect((int) $item->jackpot_win_amount)->toBe(0);
});
test('§14.5 placement rollback returns stake when mid-order risk acquire fails (退本)', function (): void {
test('§14.5 placement partial failure only deducts successful lines when mid-order risk acquire fails', function (): void {
$player = p145_player(500_000);
$drawNo = p145_next_draw_no();
$draw = p145_draw($drawNo, random_int(1, 99_999));
@@ -495,14 +495,24 @@ test('§14.5 placement rollback returns stake when mid-order risk acquire fails
['number' => '1234', 'play_code' => 'big', 'amount' => 120],
],
])
->assertStatus(400)
->assertJsonPath('code', ErrorCode::RiskPoolSoldOut->value);
->assertOk()
->assertJsonPath('data.summary.success_count', 1)
->assertJsonPath('data.summary.failure_count', 1)
->assertJsonPath('data.items.1.fail_reason_code', (string) ErrorCode::RiskPoolSoldOut->value);
expect(TicketOrder::query()->count())->toBe(0);
expect(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(0);
expect(TicketOrder::query()->count())->toBe(1);
expect(TicketOrder::query()->firstOrFail()->status)->toBe('partial_failed');
expect(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(1);
$wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
expect((int) $wallet->balance)->toBe(500_000);
expect((int) $wallet->balance)->toBe(500_000 - 120);
$pool = RiskPool::query()
->where('draw_id', $draw->id)
->where('normalized_number', '1234')
->firstOrFail();
expect((int) $pool->remaining_amount)->toBe(2000);
expect((int) $pool->locked_amount)->toBe(3000);
});
/**

View File

@@ -236,6 +236,24 @@ test('ticket place rejects closed draw', function (): void {
->assertJsonPath('code', ErrorCode::DrawClosed->value);
});
test('ticket preview and place reject open draw after server close time', function (): void {
$player = ticketPlayerWithWallet();
$draw = ticketOpenDraw();
$draw->update(['close_time' => now()->subSecond()]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->postJson('/api/v1/ticket/preview', ticketPreviewPayload())
->assertStatus(400)
->assertJsonPath('code', ErrorCode::DrawClosed->value);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->postJson('/api/v1/ticket/place', ticketPreviewPayload())
->assertStatus(400)
->assertJsonPath('code', ErrorCode::DrawClosed->value);
expect(TicketOrder::query()->count())->toBe(0);
});
test('ticket place rejects insufficient balance', function (): void {
$player = ticketPlayerWithWallet(1_000);
ticketOpenDraw();
@@ -324,6 +342,58 @@ test('ticket place rejects sold out risk pool', function (): void {
expect(TicketOrder::query()->count())->toBe(0);
});
test('ticket place can return mixed success and failed risk results', function (): void {
$player = ticketPlayerWithWallet(500_000);
$draw = ticketOpenDraw();
RiskPool::query()->create([
'draw_id' => $draw->id,
'normalized_number' => '1234',
'total_cap_amount' => 5000,
'locked_amount' => 0,
'remaining_amount' => 5000,
'sold_out_status' => 0,
'version' => 0,
]);
RiskPool::query()->create([
'draw_id' => $draw->id,
'normalized_number' => '5678',
'total_cap_amount' => 100,
'locked_amount' => 100,
'remaining_amount' => 0,
'sold_out_status' => 1,
'version' => 1,
]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->postJson('/api/v1/ticket/place', [
'draw_id' => '20260511-001',
'currency_code' => 'NPR',
'client_trace_id' => 'trace-mixed-risk',
'lines' => [
['number' => '1234', 'play_code' => 'big', 'amount' => 120],
['number' => '5678', 'play_code' => 'big', 'amount' => 120],
],
])
->assertOk()
->assertJsonPath('code', ErrorCode::Success->value)
->assertJsonPath('data.summary.success_count', 1)
->assertJsonPath('data.summary.failure_count', 1)
->assertJsonPath('data.items.0.status', 'success')
->assertJsonPath('data.items.1.status', 'failed')
->assertJsonPath('data.items.1.fail_reason_code', (string) ErrorCode::RiskPoolSoldOut->value);
$order = TicketOrder::query()->firstOrFail();
expect($order->status)->toBe('partial_failed')
->and((int) $order->total_actual_deduct)->toBe(120)
->and(TicketItem::query()->where('status', 'success')->count())->toBe(1)
->and(TicketItem::query()->where('status', 'failed')->count())->toBe(1);
$wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
expect((int) $wallet->balance)->toBe(500_000 - 120);
});
test('ticket place rejects disabled play from active catalog', function (): void {
$player = ticketPlayerWithWallet();
ticketOpenDraw();
@@ -424,10 +494,10 @@ test('ticket preview rejects invalid line amount per validation rules', function
});
/**
* §13.5 风险占用回滚同一订单两行共享号码时preview 各自通过;首条 acquire 后剩余不足则第二条失败,整单事务回滚
* §10.1.2 混合成功失败:同一订单两行共享号码时,首条占用成功,第二条额度不足则该注项失败
* big + amount 120 estimated_payout 3000maxOdds 250000 / 10000 = 25)。
*/
test('ticket place rolls back order wallet and risk locks when mid-order acquire fails', function (): void {
test('ticket place records partial failed when mid-order acquire fails', function (): void {
$player = ticketPlayerWithWallet(500_000);
$draw = ticketOpenDraw();
@@ -453,21 +523,24 @@ test('ticket place rolls back order wallet and risk locks when mid-order acquire
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->postJson('/api/v1/ticket/place', $payload)
->assertStatus(400)
->assertJsonPath('code', ErrorCode::RiskPoolSoldOut->value);
->assertOk()
->assertJsonPath('code', ErrorCode::Success->value)
->assertJsonPath('data.summary.success_count', 1)
->assertJsonPath('data.summary.failure_count', 1);
expect(TicketOrder::query()->count())->toBe(0);
expect(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(0);
expect(TicketOrder::query()->count())->toBe(1);
expect(TicketOrder::query()->firstOrFail()->status)->toBe('partial_failed');
expect(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(1);
$wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
expect((int) $wallet->balance)->toBe(500_000);
expect((int) $wallet->balance)->toBe(500_000 - 120);
$pool = RiskPool::query()
->where('draw_id', $draw->id)
->where('normalized_number', '1234')
->firstOrFail();
expect((int) $pool->remaining_amount)->toBe(5000);
expect((int) $pool->locked_amount)->toBe(0);
expect((int) $pool->remaining_amount)->toBe(2000);
expect((int) $pool->locked_amount)->toBe(3000);
});
/** §13.5 并发下注(顺序挤出):先成功者占用额度,后一盘同一号码收到售罄。 */

View File

@@ -251,6 +251,12 @@ test('ticket items index filters by status number and date range', function ():
->assertJsonPath('data.total', 1)
->assertJsonPath('data.items.0.draw_no', '20260512-780');
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/ticket/items?status=settled_win')
->assertOk()
->assertJsonPath('data.total', 1)
->assertJsonPath('data.items.0.draw_no', '20260512-780');
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/ticket/items?number=1234')
->assertOk()
@@ -309,7 +315,7 @@ test('ticket item show returns match result and timeline', function (): void {
->assertJsonPath('data.timeline.4.code', 'settled');
});
test('my-match returns hit numbers when draw published', function (): void {
test('my-match returns hit numbers when draw settled with winning ticket', function (): void {
$uniq = bin2hex(random_bytes(4));
$player = Player::query()->create([
'site_code' => 'test',
@@ -355,11 +361,39 @@ test('my-match returns hit numbers when draw published', function (): void {
])
->assertOk();
ticketItemsPublishAndSettle($draw, '1234');
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/ticket/draws/20260511-778/my-match')
->assertOk()
->assertJsonPath('data.has_bets', true)
->assertJsonPath('data.winning_ticket_count', 1)
->assertJsonPath('data.hit_numbers_4d', ['1234']);
});
test('my-match only highlights settled winning tickets', function (): void {
$player = ticketItemsPlayer();
$draw = Draw::query()->create([
'draw_no' => '20260514-779',
'business_date' => '2026-05-14',
'sequence_no' => 779,
'status' => DrawStatus::Cooldown->value,
'start_time' => now()->subMinutes(20),
'close_time' => now()->subMinutes(10),
'draw_time' => now()->subMinutes(5),
'cooling_end_time' => now()->addMinutes(5),
'result_source' => 'rng',
'current_result_version' => 1,
'settle_version' => 0,
'is_reopened' => false,
]);
$batch = DrawResultBatch::query()->create([
'draw_id' => $draw->id,
'result_version' => 1,
'source_type' => 'rng',
'rng_seed_hash' => 'test',
'rng_seed_hash' => 'pending-match',
'raw_seed_encrypted' => null,
'status' => DrawResultBatchStatus::Published->value,
'created_by' => null,
@@ -368,28 +402,71 @@ test('my-match returns hit numbers when draw published', function (): void {
]);
foreach (DrawPrizeLayout::slots() as $slot) {
$num = $slot['prize_type'] === 'first' ? '1234' : '5678';
DrawResultItem::query()->create([
'draw_id' => $draw->id,
'result_batch_id' => $batch->id,
'prize_type' => $slot['prize_type'],
'prize_index' => $slot['prize_index'],
'number_4d' => $num,
'suffix_3d' => substr($num, -3),
'suffix_2d' => substr($num, -2),
'head_digit' => (int) substr($num, 0, 1),
'tail_digit' => (int) substr($num, 3, 1),
'number_4d' => '1234',
'suffix_3d' => '234',
'suffix_2d' => '34',
'head_digit' => 1,
'tail_digit' => 4,
]);
}
$draw->forceFill([
'status' => DrawStatus::Cooldown->value,
'current_result_version' => 1,
])->save();
$order = TicketOrder::query()->create([
'order_no' => 'ORD-PENDING-MATCH',
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 10_000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 10_000,
'total_estimated_payout' => 20_000,
'status' => 'placed',
'submit_source' => 'h5',
'client_trace_id' => 'pending-match',
]);
$item = \App\Models\TicketItem::query()->create([
'ticket_no' => 'TKPENDINGMATCH',
'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' => 'single',
'unit_bet_amount' => 10_000,
'total_bet_amount' => 10_000,
'rebate_rate_snapshot' => '0.0000',
'commission_rate_snapshot' => '0.0000',
'actual_deduct_amount' => 10_000,
'odds_snapshot_json' => [],
'rule_snapshot_json' => [],
'combination_count' => 1,
'estimated_max_payout' => 20_000,
'risk_locked_amount' => 20_000,
'status' => 'success',
'win_amount' => 0,
'jackpot_win_amount' => 0,
]);
\App\Models\TicketCombination::query()->create([
'ticket_item_id' => $item->id,
'combination_no' => 0,
'number_4d' => '1234',
'bet_amount' => 10_000,
'estimated_payout' => 20_000,
]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/ticket/draws/20260511-778/my-match')
->getJson('/api/v1/ticket/draws/20260514-779/my-match')
->assertOk()
->assertJsonPath('data.has_bets', true)
->assertJsonPath('data.hit_numbers_4d', ['1234']);
->assertJsonPath('data.winning_ticket_count', 0)
->assertJsonPath('data.hit_numbers_4d', []);
});