First Commit
This commit is contained in:
1 parent
5eb9dd037d
commit
2e62c0e60b
11877 files changed
+2035312
No files matched your search
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
$config = require __DIR__ . '/config.php';
|
||||
require __DIR__ . '/lib/PayPalClient.php';
|
||||
require __DIR__ . '/lib/Storage.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo 'Method Not Allowed';
|
||||
exit;
|
||||
}
|
||||
|
||||
$schoolName = trim($_POST['school_name'] ?? '');
|
||||
if ($schoolName === '') {
|
||||
http_response_code(400);
|
||||
echo 'Missing school_name';
|
||||
exit;
|
||||
}
|
||||
|
||||
$client = new PayPalClient($config);
|
||||
$storage = new Storage($config['storage_path']);
|
||||
|
||||
$trackingId = 'school_' . substr(md5($schoolName . '|' . time()), 0, 10);
|
||||
|
||||
// IMPORTANT: Change return_url to be more explicit
|
||||
$returnUrl = rtrim($config['base_url'], '/') . '/return.php?school=' . urlencode($schoolName) . '&track=' . urlencode($trackingId);
|
||||
$cancelUrl = rtrim($config['base_url'], '/') . '/index.php';
|
||||
|
||||
$payload = [
|
||||
'tracking_id' => $trackingId,
|
||||
'operations' => [[
|
||||
'operation' => 'API_INTEGRATION',
|
||||
'api_integration_preference' => [
|
||||
'rest_api_integration' => [
|
||||
'integration_method' => 'PAYPAL',
|
||||
'integration_type' => 'THIRD_PARTY',
|
||||
'third_party_details' => [
|
||||
'features' => [
|
||||
'PAYMENT', 'REFUND'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]],
|
||||
'products' => ['PPCP'],
|
||||
'legal_consents' => [[
|
||||
'type' => 'SHARE_DATA_CONSENT',
|
||||
'granted' => true
|
||||
]],
|
||||
'partner_config_override' => [
|
||||
'return_url' => $returnUrl,
|
||||
'return_url_description' => 'Return to School Lunch App',
|
||||
// Add this to ensure PayPal shows the return button
|
||||
'show_add_credit_card' => false,
|
||||
],
|
||||
'requested_capabilities' => [[
|
||||
'capability' => 'PAYMENTS_RECEIVABLE'
|
||||
]]
|
||||
];
|
||||
|
||||
// ...existing code...
|
||||
$resp = $client->api('POST', '/v2/customer/partner-referrals', $payload);
|
||||
if ($resp['status'] < 200 || $resp['status'] >= 300) {
|
||||
http_response_code(500);
|
||||
echo 'Partner referral failed: ' . htmlspecialchars(json_encode($resp));
|
||||
exit;
|
||||
}
|
||||
|
||||
$body = $resp['body'];
|
||||
$partnerReferralId = $body['partner_referral_id'] ?? ($body['referral_id'] ?? null);
|
||||
$referralToken = null;
|
||||
$links = $body['links'] ?? [];
|
||||
$href = null;
|
||||
|
||||
$debugReferral = $storage->get('referral_debug', []);
|
||||
$debugReferral[] = [
|
||||
'ts' => time(),
|
||||
'school' => $schoolName,
|
||||
'tracking_id' => $trackingId,
|
||||
'status' => $resp['status'],
|
||||
'body' => $body,
|
||||
];
|
||||
$storage->set('referral_debug', $debugReferral);
|
||||
|
||||
foreach ($links as $lnk) {
|
||||
if (($lnk['rel'] ?? '') === 'action_url') {
|
||||
$href = $lnk['href'];
|
||||
if (!$partnerReferralId) {
|
||||
$u = parse_url($href);
|
||||
if (!empty($u['query'])) {
|
||||
parse_str($u['query'], $q);
|
||||
if (!empty($q['partner_referral_id'])) {
|
||||
$partnerReferralId = $q['partner_referral_id'];
|
||||
} elseif (!empty($q['referral_id'])) {
|
||||
$partnerReferralId = $q['referral_id'];
|
||||
} elseif (!empty($q['referralToken'])) {
|
||||
$referralToken = $q['referralToken'];
|
||||
$partnerReferralId = $partnerReferralId ?? $referralToken;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$partnerReferralId) {
|
||||
foreach ($links as $lnk) {
|
||||
if (($lnk['rel'] ?? '') === 'self' && !empty($lnk['href'])) {
|
||||
$parts = explode('/partner-referrals/', $lnk['href']);
|
||||
if (count($parts) === 2 && $parts[1]) {
|
||||
$partnerReferralId = $parts[1];
|
||||
$referralToken = $referralToken ?? $partnerReferralId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$href) {
|
||||
http_response_code(500);
|
||||
echo 'Did not receive onboarding URL';
|
||||
exit;
|
||||
}
|
||||
|
||||
$pending = $storage->get('pending_onboarding', []);
|
||||
$pending[$trackingId] = [
|
||||
'school' => $schoolName,
|
||||
'created_at' => time(),
|
||||
'partner_referral_id' => $partnerReferralId,
|
||||
'referral_token' => $referralToken,
|
||||
];
|
||||
$storage->set('pending_onboarding', $pending);
|
||||
|
||||
if ($partnerReferralId && strpos($href, 'partner_referral_id') === false) {
|
||||
$sep = (strpos($href, '?') === false) ? '?' : '&';
|
||||
$href .= $sep . 'partner_referral_id=' . urlencode($partnerReferralId);
|
||||
}
|
||||
|
||||
header('Location: ' . $href);
|
||||
exit;
|
||||
Reference in new issue
Block a user