Files
html/api/stripe-webhook.php
2026-04-12 22:57:03 +02:00

43 lines
1.5 KiB
PHP

<?php
// WEVAL Stripe Webhook Handler
header('Content-Type: application/json');
$payload = file_get_contents('php://input');
$sig = $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? '';
// Log all events
$logDir = '/var/www/weval/logs';
@mkdir($logDir, 0755, true);
file_put_contents("$logDir/stripe-webhook.log", date('Y-m-d H:i:s')." $sig\n".substr($payload,0,500)."\n\n", FILE_APPEND);
$event = json_decode($payload, true);
if(!$event || !isset($event['type'])) {
http_response_code(400);
die(json_encode(['error'=>'invalid payload']));
}
$type = $event['type'];
$data = $event['data']['object'] ?? [];
switch($type) {
case 'checkout.session.completed':
$email = $data['customer_email'] ?? '';
$amount = ($data['amount_total'] ?? 0) / 100;
file_put_contents("$logDir/stripe-payments.log", date('Y-m-d H:i:s')." PAYMENT $email {$amount}EUR\n", FILE_APPEND);
break;
case 'customer.subscription.created':
case 'customer.subscription.updated':
$status = $data['status'] ?? '';
$cid = $data['customer'] ?? '';
file_put_contents("$logDir/stripe-subs.log", date('Y-m-d H:i:s')." SUB $cid $status\n", FILE_APPEND);
break;
case 'invoice.paid':
$email = $data['customer_email'] ?? '';
$total = ($data['total'] ?? 0) / 100;
file_put_contents("$logDir/stripe-invoices.log", date('Y-m-d H:i:s')." INVOICE $email {$total}EUR\n", FILE_APPEND);
break;
}
http_response_code(200);
echo json_encode(['received'=>true,'type'=>$type]);