mirror of
https://github.com/myronblair/tomsjavajive
synced 2026-07-28 01:02:35 -05:00
Wire up wallet balance and gift cards at checkout; fix Square gift card option; fix account/rewards.php CSS; fix real PDO/schema bugs found along the way
Feature: checkout.php now lets logged-in customers apply existing wallet balance or redeem a gift card code (which tops up wallet first, then applies) toward their order total. Deduction is deferred to payment success (markSquarePaymentResult in includes/square.php), never at order creation, so an abandoned checkout never loses real wallet money - mirrors how loyalty points already work here, unlike stock which is decremented eagerly. payment.php gains a second Square Gift Card tab (payments.giftCard() SDK method) alongside the card form, both hitting the same create-square-payment.php endpoint since Square treats both source types identically. New api/apply-wallet-credit.php validates/quotes an amount without writing anything - actual spend happens only via markSquarePaymentResult(). The gift-card-to-wallet transaction logic was extracted out of api/redeem-gift-card.php into a shared loyalty()->redeemGiftCardToWallet() so the Wallet page and checkout both call the same code. Also fixed three unrelated pre-existing bugs surfaced while testing this: - loyalty.php awardPoints() reused the same named PDO parameter (:points) twice in one UPDATE - fails under real prepared statements, meaning loyalty points (and the email sent right after them) were silently never awarded for any order tied to a logged-in customer. - redeemGiftCardToWallet (formerly inline in redeem-gift-card.php) referenced a gift_cards.updated_at column that does not exist in the schema, and used invalid enum values (gift_card_transactions.type=redeem, wallet_transactions.type=gift_card) that do not match the actual enum definitions - gift card redemption has likely never worked at all. - account/rewards.php was missing the line that loads account.css, unlike every other account/*.php page, so its sidebar/layout rendered unstyled.
This commit is contained in:
+82
-2
@@ -150,10 +150,10 @@ class LoyaltyProgram {
|
||||
db()->query(
|
||||
"UPDATE customers SET
|
||||
reward_points = reward_points + :points,
|
||||
lifetime_points = COALESCE(lifetime_points, 0) + :points,
|
||||
lifetime_points = COALESCE(lifetime_points, 0) + :points2,
|
||||
updated_at = NOW()
|
||||
WHERE customer_id = :id",
|
||||
['points' => $totalPoints, 'id' => $customerId]
|
||||
['points' => $totalPoints, 'points2' => $totalPoints, 'id' => $customerId]
|
||||
);
|
||||
|
||||
$newBalance = db()->fetch(
|
||||
@@ -417,6 +417,86 @@ class LoyaltyProgram {
|
||||
'new_customer_bonus' => $newCustomerBonus
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Redeem a gift card code into wallet balance. Shared by the Wallet page
|
||||
* and checkout so both go through the same transaction logic instead of
|
||||
* duplicating it.
|
||||
*/
|
||||
public function redeemGiftCardToWallet(string $customerId, string $rawCode): array {
|
||||
$code = strtoupper(str_replace(['-', ' '], '', trim($rawCode)));
|
||||
|
||||
if (empty($code) || strlen($code) < 8) {
|
||||
return ['success' => false, 'error' => 'Invalid gift card code'];
|
||||
}
|
||||
|
||||
$giftCard = db()->fetch(
|
||||
"SELECT * FROM gift_cards WHERE code = :code AND is_active = 1",
|
||||
['code' => $code]
|
||||
);
|
||||
|
||||
if (!$giftCard) {
|
||||
return ['success' => false, 'error' => 'Gift card not found or already used'];
|
||||
}
|
||||
|
||||
if ($giftCard['current_balance'] <= 0) {
|
||||
return ['success' => false, 'error' => 'This gift card has no remaining balance'];
|
||||
}
|
||||
|
||||
if ($giftCard['expires_at'] && strtotime($giftCard['expires_at']) < time()) {
|
||||
return ['success' => false, 'error' => 'This gift card has expired'];
|
||||
}
|
||||
|
||||
$customer = db()->fetch("SELECT wallet_balance FROM customers WHERE customer_id = :id", ['id' => $customerId]);
|
||||
if (!$customer) {
|
||||
return ['success' => false, 'error' => 'Customer not found'];
|
||||
}
|
||||
|
||||
$amount = $giftCard['current_balance'];
|
||||
|
||||
try {
|
||||
db()->query("START TRANSACTION");
|
||||
|
||||
db()->query(
|
||||
"UPDATE gift_cards SET current_balance = 0, is_active = 0 WHERE gift_card_id = :id",
|
||||
['id' => $giftCard['gift_card_id']]
|
||||
);
|
||||
|
||||
db()->insert('gift_card_transactions', [
|
||||
'gift_card_id' => $giftCard['gift_card_id'],
|
||||
'amount' => -$amount,
|
||||
'balance_after' => 0,
|
||||
'type' => 'redemption',
|
||||
]);
|
||||
|
||||
$newWalletBalance = (float) $customer['wallet_balance'] + $amount;
|
||||
|
||||
db()->query(
|
||||
"UPDATE customers SET wallet_balance = :balance, updated_at = NOW() WHERE customer_id = :id",
|
||||
['balance' => $newWalletBalance, 'id' => $customerId]
|
||||
);
|
||||
|
||||
db()->insert('wallet_transactions', [
|
||||
'transaction_id' => generateId('wt_'),
|
||||
'customer_id' => $customerId,
|
||||
'amount' => $amount,
|
||||
'balance_after' => $newWalletBalance,
|
||||
'type' => 'deposit',
|
||||
'description' => 'Gift card redeemed: ' . $code
|
||||
]);
|
||||
|
||||
db()->query("COMMIT");
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'amount' => $amount,
|
||||
'new_balance' => $newWalletBalance
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
db()->query("ROLLBACK");
|
||||
return ['success' => false, 'error' => 'Failed to redeem gift card. Please try again.'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function
|
||||
|
||||
Reference in New Issue
Block a user