mirror of
https://github.com/myronblair/tomsjavajive
synced 2026-07-28 09:12:36 -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:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Tom's Java Jive - Apply Wallet Credit / Gift Card at Checkout
|
||||
*
|
||||
* Validates and quotes an amount to apply toward the current order total.
|
||||
* Does NOT deduct wallet balance here - that only happens once the Square
|
||||
* payment actually succeeds (see markSquarePaymentResult() in
|
||||
* includes/square.php), so an abandoned checkout never loses a customer's
|
||||
* real wallet money. A gift card code, if given, is redeemed into wallet
|
||||
* balance immediately (same as the Wallet page does) since that step is
|
||||
* safe/reversible-in-spirit - only the *spending* of wallet balance at
|
||||
* checkout is deferred to payment success.
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../includes/functions.php';
|
||||
require_once __DIR__ . '/../includes/auth.php';
|
||||
require_once __DIR__ . '/../includes/loyalty.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
jsonResponse(['error' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
if (!CustomerAuth::isLoggedIn()) {
|
||||
jsonResponse(['error' => 'Please log in to use wallet balance or a gift card'], 401);
|
||||
}
|
||||
|
||||
$customer = CustomerAuth::getFullUser();
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$orderTotal = (float) ($input['order_total'] ?? 0);
|
||||
if ($orderTotal <= 0) {
|
||||
jsonResponse(['error' => 'Invalid order total'], 400);
|
||||
}
|
||||
|
||||
$giftCardCode = trim($input['gift_card_code'] ?? '');
|
||||
|
||||
if (!empty($giftCardCode)) {
|
||||
$redeemResult = loyalty()->redeemGiftCardToWallet($customer['customer_id'], $giftCardCode);
|
||||
if (!$redeemResult['success']) {
|
||||
jsonResponse(['error' => $redeemResult['error']], 400);
|
||||
}
|
||||
$walletBalance = $redeemResult['new_balance'];
|
||||
} else {
|
||||
$fresh = db()->fetch("SELECT wallet_balance FROM customers WHERE customer_id = :id", ['id' => $customer['customer_id']]);
|
||||
$walletBalance = (float) ($fresh['wallet_balance'] ?? 0);
|
||||
}
|
||||
|
||||
$requested = isset($input['amount']) ? (float) $input['amount'] : $walletBalance;
|
||||
$applyAmount = max(0, round(min($requested, $walletBalance, $orderTotal), 2));
|
||||
|
||||
jsonResponse([
|
||||
'success' => true,
|
||||
'apply_amount' => $applyAmount,
|
||||
'wallet_balance' => $walletBalance,
|
||||
'new_total' => round($orderTotal - $applyAmount, 2)
|
||||
]);
|
||||
Reference in New Issue
Block a user