Files
2026-05-13 09:43:32 -04:00

153 lines
5.4 KiB
PHP

<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/php/inc_fonctions.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/paypal_advanced/PaypalWebhook.class.php');
// affaire trouver une solution pour centraliser les infos
$strDatabaseHost = 'localhost';
$strDatabaseUser = 'ms1incription_bd';
$strDatabasePass = '3+~=,y=eV)M;';
$strDatabaseMain = 'ms1incription_prod';
$strDatabaseProd = 'ms1incription_prod';
$strDatabasePreProd = 'ms1incription_preprod';
$arrConDatabaseMain = array(
'host' => $strDatabaseHost,
'user' => $strDatabaseUser,
'pass' => $strDatabasePass,
'db' => $strDatabaseMain
);
$objDatabase = new clsMysql($arrConDatabaseMain);
$__raw = file_get_contents('php://input');
$__hdr = function_exists('getallheaders') ? getallheaders() : [];
file_put_contents(__DIR__ . '/paypal_incoming.json', ($__raw ?: "{}") . "\n", FILE_APPEND);
file_put_contents(__DIR__ . '/paypal_incoming_headers.log', print_r($__hdr, true) . "\n", FILE_APPEND);
$data = json_decode($__raw, true);
file_put_contents(__DIR__ . '/paypal_decoded.log', print_r($data, true) . "\n", FILE_APPEND);
$eventType = $data['event_type'] ?? '';
$resource = $data['resource'] ?? [];
$summary = $data['summary'] ?? '';
$status = $resource['status'] ?? ($resource['state'] ?? null);
// Déduire un “resource type” à partir du event_type (ex: PAYMENT.CAPTURE.COMPLETED -> capture)
$resourceType = null;
if (is_string($eventType) && strpos($eventType, '.') !== false) {
$partsET = explode('.', strtolower($eventType)); // ex: ['payment','capture','completed']
// On prend le 2e segment si disponible comme type “métier”
$resourceType = $partsET[1] ?? $partsET[0];
}
// IDs génériques potentiels
$resourceId = $resource['id'] ?? null;
$orderId = null;
$captureId = null;
$authorizationId = null;
$invoiceId = $resource['invoice_id'] ?? ($resource['supplementary_data']['related_ids']['invoice_id'] ?? null);
$customId = $resource['custom_id'] ?? null;
// Montant/devise si présents
$amountValue = $resource['amount']['value'] ?? ($resource['seller_receivable_breakdown']['gross_amount']['value'] ?? null);
$amountCurrency = $resource['amount']['currency_code'] ?? ($resource['seller_receivable_breakdown']['gross_amount']['currency_code'] ?? null);
$createTime = $resource['create_time'] ?? null;
$updateTime = $resource['update_time'] ?? null;
// Extraire IDs à partir des liens, quand dispo
if (!empty($resource['links']) && is_array($resource['links'])) {
foreach ($resource['links'] as $lnk) {
$rel = $lnk['rel'] ?? '';
$href = $lnk['href'] ?? '';
// Cherche des patterns connus
if (!$orderId && strpos($href, '/orders/') !== false) {
$orderId = basename(parse_url($href, PHP_URL_PATH)); // dernier segment
}
if (!$captureId && strpos($href, '/captures/') !== false) {
$captureId = basename(parse_url($href, PHP_URL_PATH));
}
if (!$authorizationId && strpos($href, '/authorizations/') !== false) {
$authorizationId = basename(parse_url($href, PHP_URL_PATH));
}
}
}
// Certains events embed l'order/capture directement
if (!$orderId) {
$orderId = $resource['supplementary_data']['related_ids']['order_id'] ?? ($resource['order_id'] ?? null);
}
if (!$captureId) {
$captureId = $resource['supplementary_data']['related_ids']['capture_id'] ?? ($resource['capture_id'] ?? null);
}
if (!$authorizationId) {
$authorizationId = $resource['supplementary_data']['related_ids']['authorization_id'] ?? ($resource['authorization_id'] ?? null);
}
$refundId = $resourceId; // pour cohérence avec ta suite (si c'est un refund, id = refund_id)
// Log générique, une seule ligne compacte (lisible)
$line = sprintf(
"[%s] type=%s resType=%s resId=%s status=%s amt=%s %s order=%s capture=%s auth=%s invoice=%s custom=%s summary=%s",
date('c'),
$eventType ?: '-',
$resourceType ?: '-',
$resourceId ?: '-',
$status ?: '-',
$amountValue ?: '-',
$amountCurrency ?: '',
$orderId ?: '-',
$captureId ?: '-',
$authorizationId ?: '-',
$invoiceId ?: '-',
$customId ?: '-',
$summary ? preg_replace('/\s+/', ' ', $summary) : '-'
);
file_put_contents(__DIR__ . '/paypal_event_detected.log', $line . "\n", FILE_APPEND);
$refundId = $data['resource']['id'] ?? null;
$status = $data['resource']['status'] ?? null;
$eventType = $data['event_type'] ?? null;
/* $captureId est déjà construit plus haut dans ton script */
$Q = function($v){ return $v === null ? "NULL" : "'" . addslashes($v) . "'"; };
if ($refundId && $status) {
$sql = "
INSERT INTO inscriptions_panier_remboursement (
refund_id,
status,
status_interne,
create_time,
update_time
) VALUES (
{$Q($refundId)},
{$Q($status)},
'WEBHOOK',
NOW(),
NOW()
)
ON DUPLICATE KEY UPDATE
status = VALUES(status),
status_interne = 'WEBHOOK',
update_time = NOW()
";
$ok = $objDatabase->fxQuery($sql);
file_put_contents(__DIR__.'/paypal_db_update.log',
"[".date('c')."] UPSERT refund_id={$refundId} cap={$captureId} ok=".$ok."\n",
FILE_APPEND
);
} else {
file_put_contents(__DIR__.'/paypal_db_update.log',
"[".date('c')."] SKIP UPSERT: refundId/status manquants (refundId="
.var_export($refundId,true).", status=".var_export($status,true).")\n",
FILE_APPEND
);
}
exit;