Tot funcionant al 100% i amb Looker
This commit is contained in:
@@ -34,6 +34,8 @@ if (!is_array($event)) {
|
||||
$type = (string)($event['type'] ?? '');
|
||||
$object = $event['data']['object'] ?? null;
|
||||
|
||||
error_log('[KAPVOE WEBHOOK] event_type=' . $type);
|
||||
|
||||
if (!is_array($object)) {
|
||||
http_response_code(400);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
@@ -51,6 +53,8 @@ try {
|
||||
$paymentIntentId = (string)($object['payment_intent'] ?? '');
|
||||
$paymentStatus = (string)($object['payment_status'] ?? '');
|
||||
|
||||
error_log('[KAPVOE WEBHOOK] checkout.session.completed session_id=' . $sessionId . ' payment_status=' . $paymentStatus);
|
||||
|
||||
if ($sessionId === '') {
|
||||
throw new RuntimeException('Falta session id');
|
||||
}
|
||||
@@ -70,6 +74,8 @@ try {
|
||||
|
||||
$alreadyUpdated = (string)($order['stock_updated'] ?? '') === '1';
|
||||
$stockResult = null;
|
||||
$mailResult = null;
|
||||
$analyticsResult = null;
|
||||
|
||||
if ($paymentStatus === 'paid' && !$alreadyUpdated) {
|
||||
$stockResult = kapvoe_decrement_sheet_stock($config, $order, $sessionId);
|
||||
@@ -82,6 +88,15 @@ try {
|
||||
$alreadyUpdated = true;
|
||||
}
|
||||
|
||||
if ($paymentStatus === 'paid') {
|
||||
$order = kapvoe_get_order_by_session_id($config, $sessionId) ?? $order;
|
||||
error_log('[KAPVOE WEBHOOK] sending_order_notifications session_id=' . $sessionId . ' customer_email=' . (string)($order['email'] ?? ''));
|
||||
$mailResult = kapvoe_send_order_notifications($config, $order, $sessionId);
|
||||
error_log('[KAPVOE WEBHOOK] mail_result session_id=' . $sessionId . ' result=' . json_encode($mailResult, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
$analyticsResult = kapvoe_track_analytics_event($config, kapvoe_build_payment_success_event($order, $sessionId));
|
||||
error_log('[KAPVOE WEBHOOK] analytics_result session_id=' . $sessionId . ' result=' . ($analyticsResult ? '1' : '0'));
|
||||
}
|
||||
|
||||
http_response_code(200);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
@@ -90,7 +105,9 @@ try {
|
||||
'session_id' => $sessionId,
|
||||
'payment_intent_id' => $paymentIntentId,
|
||||
'already_updated' => $alreadyUpdated,
|
||||
'stock_result' => $stockResult
|
||||
'stock_result' => $stockResult,
|
||||
'mail_result' => $mailResult,
|
||||
'analytics_result' => $analyticsResult,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
|
||||
@@ -104,6 +121,7 @@ try {
|
||||
exit;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
error_log('[KAPVOE WEBHOOK] ERROR ' . $e->getMessage());
|
||||
http_response_code(500);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
@@ -111,4 +129,4 @@ try {
|
||||
'error' => $e->getMessage()
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user