feat: add idempotency support for settlement adjustments and payments, fix rebate handling
- Added idempotency_key parameter to settlement bill adjustment and payment endpoints to prevent duplicate operations - Fixed player rebate profile updates to preserve existing values when only one field is modified - Moved wallet player validation earlier in AdminPlayerStoreController to prevent unnecessary processing - Enhanced extra rebate calculation to include 'in_bill' and 'settled' statuses in AgentP
This commit is contained in:
@@ -21,6 +21,7 @@ final class AgentSettlementBillAdjustmentService
|
||||
string $adjustmentType,
|
||||
?string $reason,
|
||||
int $adminUserId,
|
||||
?string $idempotencyKey = null,
|
||||
): int {
|
||||
$original = DB::table('settlement_bills')->where('id', $originalBillId)->first();
|
||||
if ($original === null) {
|
||||
@@ -45,11 +46,38 @@ final class AgentSettlementBillAdjustmentService
|
||||
]);
|
||||
}
|
||||
|
||||
if ($idempotencyKey !== null && $idempotencyKey !== '') {
|
||||
$existing = DB::table('settlement_adjustments')
|
||||
->where('original_bill_id', $originalBillId)
|
||||
->where('idempotency_key', $idempotencyKey)
|
||||
->first();
|
||||
if ($existing !== null) {
|
||||
return (int) DB::table('settlement_bills')
|
||||
->where('reversed_bill_id', $originalBillId)
|
||||
->where('bill_type', 'adjustment')
|
||||
->value('id');
|
||||
}
|
||||
}
|
||||
|
||||
$type = in_array($adjustmentType, ['adjustment', 'reversal'], true)
|
||||
? $adjustmentType
|
||||
: 'adjustment';
|
||||
|
||||
return (int) DB::transaction(function () use ($original, $amount, $type, $reason, $adminUserId): int {
|
||||
return (int) DB::transaction(function () use ($original, $amount, $type, $reason, $adminUserId, $idempotencyKey): int {
|
||||
if ($idempotencyKey !== null && $idempotencyKey !== '') {
|
||||
$existing = DB::table('settlement_adjustments')
|
||||
->where('original_bill_id', (int) $original->id)
|
||||
->where('idempotency_key', $idempotencyKey)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
if ($existing !== null) {
|
||||
return (int) DB::table('settlement_bills')
|
||||
->where('reversed_bill_id', (int) $original->id)
|
||||
->where('bill_type', 'adjustment')
|
||||
->value('id');
|
||||
}
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$newBillId = (int) DB::table('settlement_bills')->insertGetId([
|
||||
'settlement_period_id' => (int) $original->settlement_period_id,
|
||||
@@ -81,6 +109,7 @@ final class AgentSettlementBillAdjustmentService
|
||||
'adjustment_type' => $type,
|
||||
'amount' => $amount,
|
||||
'reason' => $reason,
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'created_by' => $adminUserId > 0 ? $adminUserId : null,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
|
||||
Reference in New Issue
Block a user