Files
2026-10-19 15:22:52 -07:00

58 lines
1.8 KiB
PHP

<?php
/**
* Demo helper
* ───────────
* Resolves which *connected* school the demo checkout pages should run as.
*
* The PayPal widget (paypal-checkout-widget.php) only renders buttons for a
* school that has a saved merchant_id and is PayPal-ready. The demo pages used
* to hardcode "Demo School", which is never connected — so the buttons silently
* never appeared. These helpers let the demos auto-pick a school that can
* actually accept PayPal, so the page is testable out of the box.
*/
/** All onboarded schools keyed by school name. */
function pp_demo_schools(): array
{
$file = __DIR__ . '/data/school_merchants.json';
if (!file_exists($file)) {
return [];
}
return json_decode(file_get_contents($file), true) ?: [];
}
/**
* Decide which school the demo should check out as.
*
* Priority:
* 1. An explicit ?school= value, if that school is actually connected.
* 2. Otherwise the first connected school that is PayPal-ready.
* 3. null when no school can accept PayPal yet.
*/
function pp_demo_resolve_school(?string $requested = null): ?string
{
$merchants = pp_demo_schools();
// Honour an explicit request only if that school is connected.
if ($requested !== null && $requested !== '' && isset($merchants[$requested])) {
return $requested;
}
// Auto-pick the first school that can actually receive payments.
foreach ($merchants as $name => $rec) {
if (empty($rec['merchant_id'])) {
continue;
}
// paypal_ready may be absent (status never checked) — treat absence as ready.
if (array_key_exists('paypal_ready', $rec) && !$rec['paypal_ready']) {
continue;
}
if (!empty($rec['consent_revoked'])) {
continue;
}
return $name;
}
return null;
}