Files
actiewebsitedinges/webhook.php
T
2026-10-10 21:32:00 +02:00

66 lines
2.1 KiB
PHP

<?php
require_once 'config.php';
require_once 'vendor/autoload.php';
// Mollie stuurt een POST met 'id' van de betaling
$paymentId = $_POST['id'] ?? null;
if (!$paymentId) {
http_response_code(400);
exit('Geen payment ID ontvangen.');
}
try {
$mollie = new \Mollie\Api\MollieApiClient();
$mollie->setApiKey(MOLLIE_API_KEY);
$payment = $mollie->payments->get($paymentId);
// Alleen verwerken als betaling geslaagd is
if ($payment->isPaid()) {
$meta = $payment->metadata;
$rij = [
'payment_id' => $payment->id,
'datum' => $meta->datum ?? date('Y-m-d H:i:s'),
'groep' => $meta->groep ?? '',
'naam_verkoper' => $meta->naam_verkoper ?? '',
'naam_ouder' => $meta->naam_ouder ?? '',
'aantal' => $meta->aantal ?? '',
'totaal' => $payment->amount->value,
'status' => 'betaald',
];
stuurNaarGoogleSheets($rij);
}
http_response_code(200);
exit('OK');
} catch (\Mollie\Api\Exceptions\ApiException $e) {
http_response_code(500);
exit('Fout: ' . $e->getMessage());
}
// ─────────────────────────────────────────────
// Stuurt een rij naar Google Sheets via Apps Script Web App
// ─────────────────────────────────────────────
function stuurNaarGoogleSheets(array $data): void
{
$ch = curl_init(GOOGLE_SCRIPT_URL);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true, // Apps Script redirect volgen
CURLOPT_TIMEOUT => 15,
]);
$response = curl_exec($ch);
curl_close($ch);
// Optioneel: log fouten naar bestand
if ($response === false) {
error_log('[webhook] Google Sheets fout: ' . curl_error($ch));
}
}