mirror of
https://github.com/myronblair/tomtomgames
synced 2026-06-30 17:51:08 -05:00
Auto-debit platform credits when purchase is approved
When a pending purchase is resolved as completed: - Inserts a debit row into platform_credits for the matching platform (joins token_purchases.platform_id slug → platforms.id) - Debit notes include purchase #, player name, username, token count, amount, method - Total shown in credit modal now subtracts debits from credits (net balance) Credit history table updates: - CREDIT/DEBIT type badges, debit rows tinted red with − prefix - Debit rows show "Purchase #X ↗" button that closes modal, jumps to the Purchases section (all tab), and highlights that purchase row - Edit/delete buttons hidden on auto-generated debit rows Also fixes: resolve_purchase was echoing $sent (undefined variable bug) Also fixes: purchaseCard div now has id="pr-N" so jump-highlight works Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+20
-4
@@ -104,17 +104,33 @@ switch ($action) {
|
||||
db()->beginTransaction();
|
||||
try {
|
||||
if ($status === 'completed') {
|
||||
// Credit tokens to user
|
||||
logAdminAction('TOKENS_ADJUSTED', $adminId, 'user', isset($targetId)?(int)$targetId:0, 'Manual token adjustment: '.($data['tokens']??0).' tokens', '', ($data['tokens']??''), 'critical');
|
||||
db()->prepare("UPDATE users SET tokens=tokens+? WHERE id=?")->execute([$purchase['tokens'], $purchase['user_id']]);
|
||||
}
|
||||
db()->prepare("UPDATE token_purchases SET status=?,admin_note=? WHERE id=?")->execute([$status, $note, $id]);
|
||||
db()->commit();
|
||||
echo json_encode($sent ? ['success'=>true] : ['success'=>false,'error'=>'Failed to send reset email. Please try again.']);
|
||||
} catch (Exception $e) {
|
||||
db()->rollBack();
|
||||
echo json_encode(['success'=>false,'error'=>'DB error']);
|
||||
echo json_encode(['success'=>false,'error'=>'DB error']); exit;
|
||||
}
|
||||
|
||||
// Insert debit entry into platform_credits when approved
|
||||
if ($status === 'completed' && !empty($purchase['platform_id'])) {
|
||||
$platRow = db()->prepare("SELECT id FROM platforms WHERE slug=?");
|
||||
$platRow->execute([$purchase['platform_id']]);
|
||||
$platNumId = (int)$platRow->fetchColumn();
|
||||
if ($platNumId) {
|
||||
$userRow = db()->prepare("SELECT username FROM users WHERE id=?");
|
||||
$userRow->execute([$purchase['user_id']]);
|
||||
$username = $userRow->fetchColumn() ?: 'User#'.$purchase['user_id'];
|
||||
$amtDollars = number_format($purchase['amount_cents'] / 100, 2);
|
||||
$playerLabel = trim($purchase['player_name'] ?: $purchase['game_alias'] ?: $username);
|
||||
$debitNotes = "Purchase #{$id} · {$playerLabel} ({$username}) · {$purchase['tokens']} tokens · \${$amtDollars} via {$purchase['payment_method']}";
|
||||
db()->prepare("INSERT INTO platform_credits (platform_id, credits_purchased, credit_date, payment_method, notes, type, purchase_ref_id) VALUES (?,?,CURDATE(),?,?,?,?)")
|
||||
->execute([$platNumId, $purchase['tokens'], $purchase['payment_method'], $debitNotes, 'debit', $id]);
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode(['success'=>true]);
|
||||
break;
|
||||
|
||||
// ─── CASHOUTS ─────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user