Files
HotLunches/payp/index.php
T
2026-10-19 15:22:52 -07:00

183 lines
9.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
$config = require __DIR__ . '/config.php';
require __DIR__ . '/lib/Storage.php';
$storage = new Storage($config['storage_path']);
$merchants = $storage->get('merchants', []);
// Backfill: if pending_onboarding has connected entries with merchant_id, ensure they exist in merchants.json
$pending = $storage->get('pending_onboarding', []);
$changed = false;
foreach ($pending as $trackId => $entry) {
if (!is_array($entry)) continue;
$schoolName = $entry['school'] ?? null;
$mid = $entry['merchant_id'] ?? null;
$status = $entry['status'] ?? null;
if ($schoolName && $mid && strtolower((string)$status) === 'connected') {
if (!isset($merchants[$schoolName]) || empty($merchants[$schoolName]['merchant_id'])) {
$merchants[$schoolName] = [
'merchant_id' => $mid,
'tracking_id' => $trackId,
'connected_at' => $entry['connected_at'] ?? time(),
];
$changed = true;
}
}
}
if ($changed) {
$storage->set('merchants', $merchants);
}
?><!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>School Lunch PayPal Demo</title>
<style>
body{font-family:system-ui,Segoe UI,Arial,sans-serif; margin:2rem;}
.box{border:1px solid #ddd; padding:1rem; border-radius:.5rem; margin-bottom:1rem;}
label{display:block; margin:.5rem 0 .25rem;}
input,select,button{padding:.5rem; font-size:1rem;}
table{border-collapse:collapse; width:100%;}
th,td{border:1px solid #eee; padding:.5rem;}
</style>
</head>
<body>
<h1>School Lunch Payments (Demo)</h1>
<div class="box">
<h2>Connect a School's PayPal (Merchant Onboarding)</h2>
<form method="post" action="/payp/onboard.php">
<label>School Name</label>
<input name="school_name" placeholder="e.g., Green Valley Elementary" required />
<button type="submit">Connect with PayPal</button>
<div class="box">
<h2>Pending Onboarding</h2>
<?php
// Show entries that do not yet have a merchant_id saved
$pendingList = [];
$incompleteList = [];
foreach ($pending as $trackId => $entry) {
if (!is_array($entry)) continue;
$status = $entry['status'] ?? null;
if ($status === 'incomplete_onboarding') {
$incompleteList[$trackId] = $entry;
} elseif (empty($entry['merchant_id'])) {
$pendingList[$trackId] = $entry;
}
}
?>
<?php if ($incompleteList): ?>
<div style="background:#fff3cd; padding:1rem; margin-bottom:1rem; border-radius:.5rem;">
<h3 style="margin-top:0; color:#856404;">⚠️ Incomplete Onboarding</h3>
<p style="color:#856404;">The following merchants started but did NOT complete PayPal onboarding. They need to complete the signup process at PayPal.</p>
<table>
<thead>
<tr>
<th>School</th>
<th>Status</th>
<th>Note</th>
<th>Last Update</th>
</tr>
</thead>
<tbody>
<?php foreach ($incompleteList as $trackId => $entry): ?>
<tr style="background:#fff;">
<td><?= htmlspecialchars($entry['school'] ?? '') ?></td>
<td><span style="color:#856404;">❌ Incomplete</span></td>
<td><small><?= htmlspecialchars($entry['note'] ?? 'Onboarding not completed') ?></small></td>
<td><?= isset($entry['last_return_at']) ? date('Y-m-d H:i:s', (int)$entry['last_return_at']) : (isset($entry['created_at']) ? date('Y-m-d H:i:s', (int)$entry['created_at']) : '') ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
<?php if (!$pendingList): ?>
<p><?= $incompleteList ? 'No other pending onboardings.' : 'No pending onboardings.' ?></p>
<?php else: ?>
<p>Sometimes PayPal takes a short time to propagate onboarding. Click Sync to resolve merchant IDs.</p>
<p>
<a href="/payp/sync_merchants.php" style="padding:.5rem 1rem; background:#0070ba; color:white; text-decoration:none; border-radius:.25rem;">🔄 Sync Now</a>
</p>
<table>
<thead>
<tr>
<th>School</th>
<th>Tracking ID</th>
<th>Partner Referral</th>
<th>Created</th>
</tr>
</thead>
<tbody>
<?php foreach ($pendingList as $trackId => $entry): ?>
<tr>
<td><?= htmlspecialchars($entry['school'] ?? '') ?></td>
<td><code><?= htmlspecialchars($trackId) ?></code></td>
<td><code><?= htmlspecialchars($entry['partner_referral_id'] ?? ($entry['referral_token'] ?? '')) ?></code></td>
<td><?= isset($entry['created_at']) ? date('Y-m-d H:i:s', (int)$entry['created_at']) : '' ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</form>
<p>This uses PayPal Partner Referrals to onboard merchants and store their merchantId for routing payments.</p>
<p><small>After connecting, you'll be redirected back here.</small></p>
<p><a href="https://developer.paypal.com/docs/platforms/partner-referrals/" target="_blank">Docs</a></p>
<?php if (isset($_GET['sync'])): ?>
<p style="color:green">Sync completed. Updated <?= (int)$_GET['sync']; ?> record(s).</p>
<?php endif; ?>
<p><a href="/payp/partner_diag.php" target="_blank">Partner diagnostics</a></p>
<?php if (isset($_GET['onboard']) && $_GET['onboard']==='ok'): ?>
<p style="color:green;">✅ Onboarding completed and merchant saved.</p>
<?php endif; ?>
<?php if (isset($_GET['onboard']) && $_GET['onboard']==='incomplete'): ?>
<p style="color:#856404; background:#fff3cd; padding:1rem; border-radius:.5rem;">
⚠️ <strong>Onboarding Incomplete:</strong> The merchant started the PayPal signup process but did not complete it.
Please finish the entire PayPal business account setup to get your merchant ID. You can try connecting again.
</p>
<?php endif; ?>
<?php if (isset($_GET['onboard']) && $_GET['onboard']==='pending'): ?>
<p style="color:#0070ba; background:#e7f3ff; padding:1rem; border-radius:.5rem;">
ℹ️ <strong>Onboarding Pending:</strong> Your merchant ID is not available yet. This might take a few moments after completing PayPal signup.
Try clicking "Sync Now" in a minute or refresh this page.
</p>
<?php endif; ?>
<?php if (isset($_GET['captured']) && $_GET['captured'] === '1'): ?>
<p style="color:green">Payment captured successfully. Order <?= htmlspecialchars($_GET['order'] ?? '') ?> (status: <?= htmlspecialchars($_GET['status'] ?? 'CAPTURED') ?>)</p>
<?php endif; ?>
</div>
<div class="box">
<h2>Connected Schools</h2>
<?php if (!$merchants): ?>
<p>No schools connected yet.</p>
<?php else: ?>
<table>
<thead><tr><th>School</th><th>Merchant ID</th><th>Actions</th></tr></thead>
<tbody>
<?php foreach ($merchants as $school => $info): ?>
<tr>
<td><?= htmlspecialchars($school) ?></td>
<td><code><?= htmlspecialchars($info['merchant_id'] ?? 'n/a') ?></code></td>
<td>
<form method="post" action="/payp/create_order.php" style="display:inline">
<input type="hidden" name="school" value="<?= htmlspecialchars($school) ?>" />
<input type="number" step="0.01" name="amount" value="5.00" style="width:7rem" />
<button type="submit">Create Order</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</body>
</html>