<?php
// PayPal webhook 接收端 — 持久化到 /var/log/paypal-events.jsonl（恢复版，行为与 09-14 原版一致）
header('Content-Type: application/json; charset=utf-8');
$raw = file_get_contents('php://input');
$event = json_decode($raw, true);
if (!is_array($event) || !isset($event['event_type'])) {
    http_response_code(400); echo json_encode(['ok' => false, 'error' => 'bad event']); exit;
}
// 升级路径（TODO）：用 PAYPAL-TRANSMISSION-SIG 调
// POST https://api-m.paypal.com/v1/notifications/verify-webhook-signature 验签后再持久化
// （需要 webhook_id + client_id/secret，存思源《账号密码库》）。当前恢复版先保证事件不丢。
file_put_contents('/var/log/paypal-events.jsonl', json_encode([
    'ts'      => time(),
    'headers' => array_change_key_case(function_exists('getallheaders') ? (getallheaders() ?: []) : [], CASE_LOWER),
    'body'    => $event,
], JSON_UNESCAPED_UNICODE) . "\n", FILE_APPEND | LOCK_EX);
http_response_code(200);
echo json_encode(['ok' => true]);
