'Springfield Elementary', // required * 'amount' => '25.00', // required (string, major units) * 'currency' => 'CAD', // optional, default from onboarding/CAD * 'order_api_url' => '/square/order-api.php', // URL to order-api.php * 'success_url' => '/payment-success', // redirect after a successful charge (Phase 4) * 'buyer_email' => 'parent@school.com', // optional, for receipts (Phase 4) * ]; * require_once '/path/to/square/square-checkout-widget.php'; * * The widget resolves the school's Square connection server-side; if the school * is not connected / not ready, it renders a small muted notice and stops. */ if (!isset($sqCheckout) || !is_array($sqCheckout)) { throw new RuntimeException('$sqCheckout array must be set before including square-checkout-widget.php'); } require_once __DIR__ . '/lib/SquareClient.php'; require_once __DIR__ . '/lib/MerchantStore.php'; $_sqConfig = require __DIR__ . '/config.php'; $_sqSchool = (string)($sqCheckout['school_name'] ?? ''); $_sqAmount = (string)($sqCheckout['amount'] ?? ''); $_sqSchoolNum = (int)($sqCheckout['school_num'] ?? 0); // ── Resolve the school's Square connection server-side ──────────────────────── $_sqAppId = $_sqConfig['web_payments_app_id'] ?: $_sqConfig['client_id']; $_sqEnv = $_sqConfig['env'] ?? 'sandbox'; $_sqStore = new MerchantStore($_sqConfig); $_sqClient = new SquareClient($_sqConfig); $_sqMeta = $_sqSchoolNum > 0 ? $_sqStore->getMetaByNum($_sqSchoolNum) : ($_sqSchool !== '' ? $_sqStore->getMeta($_sqSchool) : []); $_sqMerchantId = $_sqMeta['merchant_id'] ?? null; $_sqLocationId = ($_sqSchool !== '' || $_sqSchoolNum > 0) ? $_sqStore->locationId($_sqSchool, $_sqClient, $_sqSchoolNum > 0 ? $_sqSchoolNum : null) : null; $_sqReady = !isset($_sqMeta['square_ready']) || (bool)$_sqMeta['square_ready']; $_sqRevoked = !empty($_sqMeta['authorization_revoked']); $_sqCurrency = strtoupper((string)($sqCheckout['currency'] ?? ($_sqMeta['currency'] ?? 'CAD'))); // Helper to render a muted, non-blocking notice and bail out. $_sqNotice = function (string $msg): void { echo '
' . htmlspecialchars($msg) . '
'; }; if (!$_sqAppId || strpos($_sqAppId, 'REPLACE') === 0) { $_sqNotice('Card payment is temporarily unavailable.'); return; } if (!$_sqMerchantId || $_sqRevoked) { $_sqNotice('This school is not set up to accept card payments yet.'); return; } if (!$_sqLocationId) { $_sqNotice('Card payment for this school is being set up and is not available yet.'); return; } if (!is_numeric($_sqAmount) || (float)$_sqAmount <= 0) { $_sqNotice('Invalid payment amount.'); return; } $_sqAmountFmt = number_format((float)$_sqAmount, 2, '.', ''); $_sqApiUrl = (string)($sqCheckout['order_api_url'] ?? './order-api.php'); $_sqSuccessUrl = (string)($sqCheckout['success_url'] ?? ''); $_sqBuyerEmail = (string)($sqCheckout['buyer_email'] ?? ''); $_sqUserNum = (int)($sqCheckout['user_num'] ?? 0); // Public embed key (preferred over school_name when this widget is loaded from // an external site via pay-frame.php). Resolved server-side; safe to expose. $_sqMerchantKey = (string)($sqCheckout['merchant_key'] ?? ($_sqMeta['merchant_key'] ?? '')); // Unique id so multiple widgets can coexist on one page. Alphanumeric only. $_sqWidgetId = 'sqwidget' . substr(md5($_sqSchool . $_sqAmountFmt . microtime()), 0, 8); // SDK URL by environment. $_sqSdkUrl = $_sqEnv === 'production' ? 'https://web.squarecdn.com/v1/square.js' : 'https://sandbox.web.squarecdn.com/v1/square.js'; // Load the SDK once per page. $_sqSdkAlreadyLoaded = $GLOBALS['_sq_sdk_loaded'] ?? false; if (!$_sqSdkAlreadyLoaded) { $GLOBALS['_sq_sdk_loaded'] = true; } ?>
Pay with card
Card details are encrypted by Square and never touch our servers.