feat: 增强奖池与钱包管理功能
更新 AdminJackpotPoolUpdateController 校验规则,禁止传入 current_amount。 优化 AdminRiskPoolManualStatusController:更新奖池状态后同步 Redis 状态。 在 TransferOrderReconcileController 中新增 completeCredit 方法,用于处理卡住的转账订单对账。 调整 TransferOrderListController:优化转账订单处理条件。 在 TicketItemsIndexController 中实现支持时区的日期筛选,提升日期处理准确性。 扩展 JackpotPool 模型,新增 adjustments 关联关系。 改进票据与钱包相关服务中的错误处理和事务管理。
This commit is contained in:
243
tests/Feature/AdminBusinessLogicGuardsTest.php
Normal file
243
tests/Feature/AdminBusinessLogicGuardsTest.php
Normal file
@@ -0,0 +1,243 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Models\Player;
|
||||
use App\Models\AdminUser;
|
||||
use App\Lottery\DrawStatus;
|
||||
use App\Models\PlayerWallet;
|
||||
use App\Models\TransferOrder;
|
||||
use App\Models\DrawResultBatch;
|
||||
use App\Models\DrawResultItem;
|
||||
use App\Models\SettlementBatch;
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Lottery\SettlementBatchStatus;
|
||||
use App\Services\Draw\DrawPrizeLayout;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
function guardsAdminToken(): string
|
||||
{
|
||||
$admin = AdminUser::query()->create([
|
||||
'username' => 'guards_admin_'.uniqid(),
|
||||
'name' => 'Guards Admin',
|
||||
'email' => null,
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
test('cannot publish result batch while settlement batch is pending review', function (): void {
|
||||
$token = guardsAdminToken();
|
||||
|
||||
$draw = Draw::query()->create([
|
||||
'draw_no' => '20260526-guard-1',
|
||||
'business_date' => '2026-05-26',
|
||||
'sequence_no' => 1,
|
||||
'status' => DrawStatus::Settling->value,
|
||||
'start_time' => now()->subHour(),
|
||||
'close_time' => now()->subMinutes(30),
|
||||
'draw_time' => now()->subMinutes(20),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => 'manual',
|
||||
'current_result_version' => 1,
|
||||
'settle_version' => 1,
|
||||
'is_reopened' => false,
|
||||
]);
|
||||
|
||||
$published = DrawResultBatch::query()->create([
|
||||
'draw_id' => $draw->id,
|
||||
'result_version' => 1,
|
||||
'source_type' => 'manual',
|
||||
'rng_seed_hash' => null,
|
||||
'raw_seed_encrypted' => null,
|
||||
'status' => DrawResultBatchStatus::Published->value,
|
||||
'created_by' => null,
|
||||
'confirmed_by' => null,
|
||||
'confirmed_at' => now(),
|
||||
]);
|
||||
|
||||
$pending = DrawResultBatch::query()->create([
|
||||
'draw_id' => $draw->id,
|
||||
'result_version' => 2,
|
||||
'source_type' => 'manual',
|
||||
'rng_seed_hash' => null,
|
||||
'raw_seed_encrypted' => null,
|
||||
'status' => DrawResultBatchStatus::PendingReview->value,
|
||||
'created_by' => null,
|
||||
'confirmed_by' => null,
|
||||
'confirmed_at' => null,
|
||||
]);
|
||||
|
||||
SettlementBatch::query()->create([
|
||||
'draw_id' => $draw->id,
|
||||
'result_batch_id' => $published->id,
|
||||
'settle_version' => 1,
|
||||
'status' => SettlementBatchStatus::PendingReview->value,
|
||||
'review_status' => 'pending',
|
||||
'started_at' => now(),
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson("/api/v1/admin/draws/{$draw->id}/result-batches/{$pending->id}/publish")
|
||||
->assertStatus(409);
|
||||
});
|
||||
|
||||
test('cannot create second pending result batch for same draw', function (): void {
|
||||
$token = guardsAdminToken();
|
||||
|
||||
$draw = Draw::query()->create([
|
||||
'draw_no' => '20260526-guard-2',
|
||||
'business_date' => '2026-05-26',
|
||||
'sequence_no' => 2,
|
||||
'status' => DrawStatus::Review->value,
|
||||
'start_time' => now()->subHour(),
|
||||
'close_time' => now()->subMinutes(30),
|
||||
'draw_time' => now()->subMinutes(20),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => 'manual',
|
||||
'current_result_version' => 0,
|
||||
'settle_version' => 0,
|
||||
'is_reopened' => false,
|
||||
]);
|
||||
|
||||
$items = [];
|
||||
foreach (DrawPrizeLayout::slots() as $i => $slot) {
|
||||
$items[] = [
|
||||
'prize_type' => $slot['prize_type'],
|
||||
'prize_index' => $slot['prize_index'],
|
||||
'number_4d' => str_pad((string) ($i + 1), 4, '0', STR_PAD_LEFT),
|
||||
];
|
||||
}
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson("/api/v1/admin/draws/{$draw->id}/result-batches", ['items' => $items])
|
||||
->assertOk();
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson("/api/v1/admin/draws/{$draw->id}/result-batches", ['items' => $items])
|
||||
->assertStatus(409);
|
||||
});
|
||||
|
||||
test('admin cannot manually process transfer out pending reconcile order', function (): void {
|
||||
$token = guardsAdminToken();
|
||||
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'main',
|
||||
'site_player_id' => 'out-manual-block',
|
||||
'username' => null,
|
||||
'nickname' => null,
|
||||
'default_currency' => 'NPR',
|
||||
'status' => 0,
|
||||
]);
|
||||
|
||||
TransferOrder::query()->create([
|
||||
'transfer_no' => 'TO_manual_block',
|
||||
'player_id' => $player->id,
|
||||
'direction' => 'out',
|
||||
'currency_code' => 'NPR',
|
||||
'amount' => 300,
|
||||
'idempotent_key' => 'out-manual-block-key',
|
||||
'status' => 'pending_reconcile',
|
||||
'external_request_payload' => null,
|
||||
'external_response_payload' => null,
|
||||
'external_ref_no' => null,
|
||||
'fail_reason' => 'main_site_timeout',
|
||||
'finished_at' => null,
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/v1/admin/wallet/transfer-orders/TO_manual_block/manually-process')
|
||||
->assertStatus(422);
|
||||
|
||||
expect(TransferOrder::query()->where('transfer_no', 'TO_manual_block')->value('status'))
|
||||
->toBe('pending_reconcile');
|
||||
});
|
||||
|
||||
test('admin cannot complete credit for main site timeout transfer in order', function (): void {
|
||||
$token = guardsAdminToken();
|
||||
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'main',
|
||||
'site_player_id' => 'in-complete-block',
|
||||
'username' => null,
|
||||
'nickname' => null,
|
||||
'default_currency' => 'NPR',
|
||||
'status' => 0,
|
||||
]);
|
||||
|
||||
PlayerWallet::query()->create([
|
||||
'player_id' => $player->id,
|
||||
'wallet_type' => 'lottery',
|
||||
'currency_code' => 'NPR',
|
||||
'balance' => 100,
|
||||
'frozen_balance' => 0,
|
||||
'status' => 0,
|
||||
'version' => 0,
|
||||
]);
|
||||
|
||||
TransferOrder::query()->create([
|
||||
'transfer_no' => 'TI_complete_block',
|
||||
'player_id' => $player->id,
|
||||
'direction' => 'in',
|
||||
'currency_code' => 'NPR',
|
||||
'amount' => 500,
|
||||
'idempotent_key' => 'in-complete-block-key',
|
||||
'status' => 'pending_reconcile',
|
||||
'external_request_payload' => null,
|
||||
'external_response_payload' => null,
|
||||
'external_ref_no' => null,
|
||||
'fail_reason' => 'main_site_timeout',
|
||||
'finished_at' => null,
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/v1/admin/wallet/transfer-orders/TI_complete_block/complete-credit')
|
||||
->assertStatus(422);
|
||||
|
||||
expect((int) PlayerWallet::query()->where('player_id', $player->id)->value('balance'))->toBe(100);
|
||||
});
|
||||
|
||||
test('transfer order list hides manual process for out pending reconcile', function (): void {
|
||||
$token = guardsAdminToken();
|
||||
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'main',
|
||||
'site_player_id' => 'list-flags',
|
||||
'username' => null,
|
||||
'nickname' => null,
|
||||
'default_currency' => 'NPR',
|
||||
'status' => 0,
|
||||
]);
|
||||
|
||||
TransferOrder::query()->create([
|
||||
'transfer_no' => 'TO_list_flags',
|
||||
'player_id' => $player->id,
|
||||
'direction' => 'out',
|
||||
'currency_code' => 'NPR',
|
||||
'amount' => 100,
|
||||
'idempotent_key' => 'list-flags-key',
|
||||
'status' => 'pending_reconcile',
|
||||
'external_request_payload' => null,
|
||||
'external_response_payload' => null,
|
||||
'external_ref_no' => null,
|
||||
'fail_reason' => 'main_site_timeout',
|
||||
'finished_at' => null,
|
||||
]);
|
||||
|
||||
$items = $this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/wallet/transfer-orders?player_id='.$player->id)
|
||||
->assertOk()
|
||||
->json('data.items');
|
||||
|
||||
$item = collect($items)->firstWhere('transfer_no', 'TO_list_flags');
|
||||
expect($item)->not->toBeNull();
|
||||
|
||||
expect($item['can_reverse'])->toBeTrue()
|
||||
->and($item['can_manually_process'])->toBeFalse()
|
||||
->and($item['can_complete_credit'])->toBeFalse();
|
||||
});
|
||||
110
tests/Feature/AdminJackpotPoolAdjustmentTest.php
Normal file
110
tests/Feature/AdminJackpotPoolAdjustmentTest.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\JackpotPool;
|
||||
use App\Models\JackpotPoolAdjustment;
|
||||
use Database\Seeders\CurrencySeeder;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->seed(CurrencySeeder::class);
|
||||
});
|
||||
|
||||
function jackpotAdjustAdminToken(): string
|
||||
{
|
||||
$admin = AdminUser::query()->create([
|
||||
'username' => 'jackpot_adjust_admin',
|
||||
'name' => 'Jackpot Adjust',
|
||||
'email' => null,
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
test('admin cannot set current_amount via pool update', function (): void {
|
||||
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
|
||||
$token = jackpotAdjustAdminToken();
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->putJson('/api/v1/admin/jackpot/pools/'.$pool->id, [
|
||||
'current_amount' => 9_999_999,
|
||||
])
|
||||
->assertStatus(422);
|
||||
});
|
||||
|
||||
test('admin can apply jackpot pool balance adjustment with ledger row', function (): void {
|
||||
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
|
||||
$pool->forceFill(['current_amount' => 1_000])->save();
|
||||
$token = jackpotAdjustAdminToken();
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/v1/admin/jackpot/pools/'.$pool->id.'/adjustments', [
|
||||
'amount_delta' => 500,
|
||||
'reason' => 'manual top-up after reconciliation',
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.pool.current_amount', 1_500)
|
||||
->assertJsonPath('data.adjustment.amount_delta', 500)
|
||||
->assertJsonPath('data.adjustment.balance_before', 1_000)
|
||||
->assertJsonPath('data.adjustment.balance_after', 1_500);
|
||||
|
||||
expect(JackpotPoolAdjustment::query()->where('jackpot_pool_id', $pool->id)->count())->toBe(1);
|
||||
|
||||
expect(
|
||||
AuditLog::query()
|
||||
->where('module_code', 'jackpot')
|
||||
->where('action_code', 'adjust_balance')
|
||||
->exists(),
|
||||
)->toBeTrue();
|
||||
});
|
||||
|
||||
test('admin jackpot adjustment rejects negative balance', function (): void {
|
||||
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
|
||||
$pool->forceFill(['current_amount' => 100])->save();
|
||||
$token = jackpotAdjustAdminToken();
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/v1/admin/jackpot/pools/'.$pool->id.'/adjustments', [
|
||||
'amount_delta' => -200,
|
||||
'reason' => 'correction',
|
||||
])
|
||||
->assertStatus(422);
|
||||
|
||||
expect((int) $pool->fresh()->current_amount)->toBe(100);
|
||||
});
|
||||
|
||||
test('admin can list jackpot pool adjustments', function (): void {
|
||||
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
|
||||
$admin = AdminUser::query()->create([
|
||||
'username' => 'adj_list_admin',
|
||||
'name' => 'List',
|
||||
'email' => null,
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
|
||||
JackpotPoolAdjustment::query()->create([
|
||||
'adjustment_no' => 'JA_TEST_1',
|
||||
'jackpot_pool_id' => $pool->id,
|
||||
'admin_user_id' => $admin->id,
|
||||
'amount_delta' => 50,
|
||||
'balance_before' => 0,
|
||||
'balance_after' => 50,
|
||||
'reason' => 'seed',
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/jackpot/pools/'.$pool->id.'/adjustments')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.total', 1)
|
||||
->assertJsonPath('data.items.0.adjustment_no', 'JA_TEST_1');
|
||||
});
|
||||
@@ -159,6 +159,7 @@ test('admin transfer order list exposes available reconcile actions by status',
|
||||
->and($byNo['TI_failed']['can_manually_process'])->toBeTrue()
|
||||
->and($byNo['TI_wait']['can_reverse'])->toBeTrue()
|
||||
->and($byNo['TI_wait']['can_manually_process'])->toBeTrue()
|
||||
->and($byNo['TI_wait']['can_complete_credit'])->toBeFalse()
|
||||
->and($byNo['TI_done']['can_reverse'])->toBeFalse()
|
||||
->and($byNo['TI_done']['can_manually_process'])->toBeFalse();
|
||||
});
|
||||
@@ -326,6 +327,56 @@ test('admin transfer reverse is idempotent under concurrent reconcile', function
|
||||
->and(WalletTxn::query()->where('biz_type', 'reversal')->count())->toBe(1);
|
||||
});
|
||||
|
||||
test('admin can complete stuck transfer in credit for pending reconcile order', function (): void {
|
||||
$token = makeAdminToken();
|
||||
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'main',
|
||||
'site_player_id' => 'complete-credit-player',
|
||||
'username' => null,
|
||||
'nickname' => null,
|
||||
'default_currency' => 'NPR',
|
||||
'status' => 0,
|
||||
]);
|
||||
|
||||
$wallet = PlayerWallet::query()->create([
|
||||
'player_id' => $player->id,
|
||||
'wallet_type' => 'lottery',
|
||||
'currency_code' => 'NPR',
|
||||
'balance' => 500,
|
||||
'frozen_balance' => 0,
|
||||
'status' => 0,
|
||||
'version' => 0,
|
||||
]);
|
||||
|
||||
TransferOrder::query()->create([
|
||||
'transfer_no' => 'TI_complete_credit',
|
||||
'player_id' => $player->id,
|
||||
'direction' => 'in',
|
||||
'currency_code' => 'NPR',
|
||||
'amount' => 2_000,
|
||||
'idempotent_key' => 'complete-credit-key',
|
||||
'status' => 'pending_reconcile',
|
||||
'external_request_payload' => ['ok' => true],
|
||||
'external_response_payload' => ['ok' => true],
|
||||
'external_ref_no' => 'main-ref-1',
|
||||
'fail_reason' => 'lottery_credit_failed',
|
||||
'finished_at' => null,
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/v1/admin/wallet/transfer-orders/TI_complete_credit/complete-credit', [
|
||||
'remark' => 'manual complete',
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', 'success');
|
||||
|
||||
$wallet->refresh();
|
||||
expect((int) $wallet->balance)->toBe(2_500)
|
||||
->and(TransferOrder::query()->where('transfer_no', 'TI_complete_credit')->value('status'))->toBe('success')
|
||||
->and(WalletTxn::query()->where('biz_type', 'transfer_in')->count())->toBe(1);
|
||||
});
|
||||
|
||||
test('admin shows player wallets', function (): void {
|
||||
$token = makeAdminToken();
|
||||
|
||||
|
||||
@@ -2,8 +2,14 @@
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Draw;
|
||||
use App\Models\Player;
|
||||
use App\Models\AdminRole;
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\TicketItem;
|
||||
use App\Models\TicketOrder;
|
||||
use App\Models\PlayerWallet;
|
||||
use App\Models\WalletTxn;
|
||||
use App\Models\TicketCombination;
|
||||
use App\Lottery\DrawStatus;
|
||||
use App\Models\DrawResultItem;
|
||||
use App\Models\DrawResultBatch;
|
||||
@@ -286,6 +292,132 @@ test('admin can cancel draw before results exist', function (): void {
|
||||
Carbon::setTestNow();
|
||||
});
|
||||
|
||||
test('admin cancel draw refunds open bets and releases risk', function (): void {
|
||||
Carbon::setTestNow(Carbon::parse('2026-05-09 12:16:00', 'UTC'));
|
||||
|
||||
$draw = Draw::query()->create([
|
||||
'draw_no' => '20260509-121b',
|
||||
'business_date' => '2026-05-09',
|
||||
'sequence_no' => 121,
|
||||
'status' => DrawStatus::Open->value,
|
||||
'start_time' => now()->copy()->subMinute(),
|
||||
'close_time' => now()->copy()->addMinutes(10),
|
||||
'draw_time' => now()->copy()->addMinutes(15),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => null,
|
||||
'current_result_version' => 0,
|
||||
'settle_version' => 0,
|
||||
'is_reopened' => false,
|
||||
]);
|
||||
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'test',
|
||||
'site_player_id' => 'cancel-refund-player',
|
||||
'username' => 'cancel_refund',
|
||||
'nickname' => null,
|
||||
'default_currency' => 'NPR',
|
||||
'status' => 0,
|
||||
]);
|
||||
|
||||
$wallet = PlayerWallet::query()->create([
|
||||
'player_id' => $player->id,
|
||||
'wallet_type' => 'lottery',
|
||||
'currency_code' => 'NPR',
|
||||
'balance' => 49_900,
|
||||
'frozen_balance' => 0,
|
||||
'status' => 0,
|
||||
'version' => 0,
|
||||
]);
|
||||
|
||||
$order = TicketOrder::query()->create([
|
||||
'order_no' => 'TO-CANCEL-REFUND',
|
||||
'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' => 'placed',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => 'cancel-refund-trace',
|
||||
]);
|
||||
|
||||
$item = TicketItem::query()->create([
|
||||
'ticket_no' => 'TK-CANCEL-REFUND',
|
||||
'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_draw',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'win_amount' => 0,
|
||||
'jackpot_win_amount' => 0,
|
||||
'settled_at' => null,
|
||||
]);
|
||||
|
||||
TicketCombination::query()->create([
|
||||
'ticket_item_id' => $item->id,
|
||||
'combination_no' => 1,
|
||||
'number_4d' => '1234',
|
||||
'bet_amount' => 100,
|
||||
'estimated_payout' => 3000,
|
||||
]);
|
||||
|
||||
WalletTxn::query()->create([
|
||||
'txn_no' => 'WT-CANCEL-REFUND',
|
||||
'player_id' => $player->id,
|
||||
'wallet_id' => $wallet->id,
|
||||
'biz_type' => 'bet_deduct',
|
||||
'biz_no' => $order->order_no,
|
||||
'direction' => 2,
|
||||
'amount' => 100,
|
||||
'balance_before' => 50_000,
|
||||
'balance_after' => 49_900,
|
||||
'status' => 'posted',
|
||||
'external_ref_no' => null,
|
||||
'idempotent_key' => 'bet:'.$order->order_no,
|
||||
'remark' => null,
|
||||
]);
|
||||
|
||||
$admin = AdminUser::query()->create([
|
||||
'username' => 'draw_cancel_refund_admin',
|
||||
'name' => 'Draw Cancel Refund Admin',
|
||||
'email' => null,
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson("/api/v1/admin/draws/{$draw->id}/cancel")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', DrawStatus::Cancelled->value);
|
||||
|
||||
expect($order->fresh()->status)->toBe('refunded')
|
||||
->and($item->fresh()->status)->toBe('refunded')
|
||||
->and((int) PlayerWallet::query()->where('player_id', $player->id)->value('balance'))->toBe(50_000);
|
||||
|
||||
Carbon::setTestNow();
|
||||
});
|
||||
|
||||
test('admin can manually trigger rng for closed draw', function (): void {
|
||||
config(['lottery.draw.require_manual_review' => true]);
|
||||
Carbon::setTestNow(Carbon::parse('2026-05-09 12:20:00', 'UTC'));
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -122,6 +122,48 @@ test('transfer out debits lottery and matches stub credit', function () {
|
||||
expect((int) PlayerWallet::query()->where('player_id', $player->id)->first()?->balance)->toBe(600);
|
||||
});
|
||||
|
||||
test('transfer out respects frozen balance when checking available funds', function () {
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'main',
|
||||
'site_player_id' => 'u-frozen',
|
||||
'username' => null,
|
||||
'nickname' => null,
|
||||
'default_currency' => 'NPR',
|
||||
'status' => 0,
|
||||
]);
|
||||
|
||||
PlayerWallet::query()->create([
|
||||
'player_id' => $player->id,
|
||||
'wallet_type' => 'lottery',
|
||||
'currency_code' => 'NPR',
|
||||
'balance' => 10_000,
|
||||
'frozen_balance' => 9_500,
|
||||
'status' => 0,
|
||||
'version' => 0,
|
||||
]);
|
||||
|
||||
$code = ErrorCode::WalletInsufficientBalance->value;
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
||||
->postJson('/api/v1/wallet/transfer-out', [
|
||||
'amount' => 1_000,
|
||||
'idempotent_key' => 'idem-out-frozen-too-much',
|
||||
])
|
||||
->assertStatus(400)
|
||||
->assertJsonPath('code', $code);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
||||
->postJson('/api/v1/wallet/transfer-out', [
|
||||
'amount' => 400,
|
||||
'idempotent_key' => 'idem-out-frozen-ok',
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.lottery_balance_after', 9_600);
|
||||
|
||||
expect((int) PlayerWallet::query()->where('player_id', $player->id)->value('balance'))->toBe(9_600)
|
||||
->and((int) PlayerWallet::query()->where('player_id', $player->id)->value('frozen_balance'))->toBe(9_500);
|
||||
});
|
||||
|
||||
test('transfer out insufficient balance fails with 1001', function () {
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'main',
|
||||
|
||||
Reference in New Issue
Block a user