Add Square payment processor, gated behind PAYMENT_PROCESSOR flag (default: stripe)

Consolidates payment processing onto the same Square account already used by
tomtomgames.com and parkerslingshotrentals.com. Collapses the two prior parallel
Stripe flows (hosted Checkout + embedded Elements) into a single Square Web
Payments SDK flow, since Payments API is synchronous and removes the original
reason for two paths.

- includes/square.php: squareApi() cURL helper (mirrors the pattern already
  used on parkerslingshotrentals.com), markSquarePaymentResult() as the single
  source of truth for order completion shared by the sync response, webhook,
  and reconciliation poll - fixes a pre-existing bug where loyalty points were
  only ever awarded from the polling endpoint, never from the webhook.
- api/create-square-payment.php replaces api/create-payment-intent.php;
  api/create-checkout-session.php deleted (no Square equivalent - single flow).
- api/webhook.php rewritten for Squares signature scheme and event types.
- api/payment-status.php repurposed to reconciliation-only fallback.
- payment.php branches on PAYMENT_PROCESSOR so Stripe and Square code coexist
  deployed while dormant - flipping one config constant is the cutover/rollback.
- admin/payments.php: added a Square settings card alongside the existing
  (now legacy-labeled) Stripe card.
- db/schema.sql + live DB: added square_payment_id/square_order_id columns,
  stripe_* columns kept for historical orders.

Not yet cut over - PAYMENT_PROCESSOR still defaults to stripe in
config-secrets.php (outside this repo). Sandbox testing still needed before
flipping to square/production.
This commit is contained in:
Myron Blair
2026-07-05 05:27:19 +00:00
parent a0b8bf2a09
commit 11edf3394f
9 changed files with 610 additions and 540 deletions
+46 -110
View File
@@ -1,157 +1,93 @@
<?php
/**
* Tom's Java Jive - Check Payment Status API
* Polls Stripe for payment/checkout session status
* Reconciliation-only fallback: Square payments complete synchronously in
* create-square-payment.php, so this endpoint only matters if the browser
* lost that response after Square had already accepted the charge. It shares
* markSquarePaymentResult() with the webhook and the synchronous path so
* loyalty points/emails are never duplicated no matter which path resolves first.
*/
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/stripe.php';
require_once __DIR__ . '/../includes/square.php';
require_once __DIR__ . '/../includes/loyalty.php';
require_once __DIR__ . '/../includes/email.php';
header('Content-Type: application/json');
// Only accept GET
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
jsonResponse(['error' => 'Method not allowed'], 405);
}
$orderId = $_GET['order_id'] ?? '';
$sessionId = $_GET['session_id'] ?? '';
if (empty($orderId) && empty($sessionId)) {
jsonResponse(['error' => 'Order ID or Session ID required'], 400);
if (empty($orderId)) {
jsonResponse(['error' => 'Order ID required'], 400);
}
// Get order by ID or session
if (!empty($orderId)) {
$order = db()->fetch(
"SELECT * FROM orders WHERE order_id = :id",
['id' => $orderId]
);
} else {
$order = db()->fetch(
"SELECT * FROM orders WHERE stripe_session_id = :session OR stripe_payment_intent = :session",
['session' => $sessionId]
);
}
$order = db()->fetch(
"SELECT * FROM orders WHERE order_id = :id",
['id' => $orderId]
);
if (!$order) {
jsonResponse(['error' => 'Order not found'], 404);
}
// If already marked as paid, return success
if ($order['payment_status'] === 'paid') {
jsonResponse([
'status' => 'complete',
'status' => 'complete',
'payment_status' => 'paid',
'order_id' => $order['order_id'],
'order_number' => $order['order_number'],
'redirect' => '/order-confirmation.php?order=' . $order['order_id']
'order_id' => $order['order_id'],
'order_number' => $order['order_number'],
'redirect' => '/order-confirmation.php?order=' . $order['order_id']
]);
}
// Check if Stripe is configured
if (!isStripeConfigured()) {
if (!isSquareConfigured()) {
jsonResponse([
'status' => 'demo_mode',
'status' => 'demo_mode',
'payment_status' => $order['payment_status'],
'message' => 'Stripe not configured - running in demo mode'
'message' => 'Square not configured - running in demo mode'
]);
}
if (empty($order['square_payment_id'])) {
jsonResponse([
'status' => 'pending',
'payment_status' => $order['payment_status']
]);
}
try {
// Check with Stripe
if (!empty($order['stripe_session_id'])) {
// Check checkout session status
$session = stripe()->getCheckoutSession($order['stripe_session_id']);
if ($session['payment_status'] === 'paid') {
// Update order
db()->update('orders',
[
'payment_status' => 'paid',
'order_status' => 'confirmed',
'stripe_payment_intent' => $session['payment_intent'] ?? null
],
'order_id = :id',
['id' => $order['order_id']]
);
$result = squareGetPayment($order['square_payment_id']);
$payment = $result['payment'] ?? [];
$status = $payment['status'] ?? '';
// Award loyalty points
if (!empty($order['customer_id'])) {
loyalty()->awardPoints(
$order['customer_id'],
(float) $order['total'],
'Order #' . $order['order_number'],
$order['order_id']
);
}
markSquarePaymentResult($order['square_payment_id'], $order['order_id'], $status);
jsonResponse([
'status' => 'complete',
'payment_status' => 'paid',
'order_id' => $order['order_id'],
'order_number' => $order['order_number'],
'redirect' => '/order-confirmation.php?order=' . $order['order_id']
]);
}
$updated = db()->fetch("SELECT * FROM orders WHERE order_id = :id", ['id' => $order['order_id']]);
if ($updated['payment_status'] === 'paid') {
jsonResponse([
'status' => $session['status'],
'payment_status' => $session['payment_status']
]);
} elseif (!empty($order['stripe_payment_intent'])) {
// Check payment intent status
$paymentIntent = stripe()->getPaymentIntent($order['stripe_payment_intent']);
if ($paymentIntent['status'] === 'succeeded') {
// Update order
db()->update('orders',
[
'payment_status' => 'paid',
'order_status' => 'confirmed'
],
'order_id = :id',
['id' => $order['order_id']]
);
// Award loyalty points
if (!empty($order['customer_id'])) {
loyalty()->awardPoints(
$order['customer_id'],
(float) $order['total'],
'Order #' . $order['order_number'],
$order['order_id']
);
}
jsonResponse([
'status' => 'complete',
'payment_status' => 'paid',
'order_id' => $order['order_id'],
'order_number' => $order['order_number'],
'redirect' => '/order-confirmation.php?order=' . $order['order_id']
]);
}
jsonResponse([
'status' => $paymentIntent['status'],
'payment_status' => 'pending'
'status' => 'complete',
'payment_status' => 'paid',
'order_id' => $updated['order_id'],
'order_number' => $updated['order_number'],
'redirect' => '/order-confirmation.php?order=' . $updated['order_id']
]);
}
// No Stripe reference found
jsonResponse([
'status' => 'pending',
'payment_status' => $order['payment_status']
'status' => $status,
'payment_status' => $updated['payment_status']
]);
} catch (Exception $e) {
error_log('Payment status check error: ' . $e->getMessage());
error_log('Square payment status check error: ' . $e->getMessage());
jsonResponse([
'status' => 'error',
'status' => 'error',
'payment_status' => $order['payment_status'],
'error' => 'Failed to check payment status'
'error' => 'Failed to check payment status'
]);
}