Stripe live operatiu i descompte d stock al Google Sheet

This commit is contained in:
2026-04-09 09:46:07 +02:00
parent 70a20938ac
commit cd9633dfab
4 changed files with 87 additions and 4 deletions
+72
View File
@@ -295,6 +295,78 @@ function kapvoe_create_checkout_session(array $config, array $payload): array {
return $decoded;
}
function kapvoe_decrement_sheet_stock(array $config, array $order, string $sessionId): array
{
$url = (string)($config['stock_sync_url'] ?? '');
$token = (string)($config['stock_sync_token'] ?? '');
if ($url === '' || $token === '') {
throw new RuntimeException('Falta configurar stock_sync_url o stock_sync_token');
}
$payload = [
'action' => 'decrement_stock',
'token' => $token,
'product_code' => (string)($order['product_code'] ?? ''),
'quantity' => max(1, (int)($order['quantity'] ?? 1)),
'order_id' => (string)($order['order_id'] ?? ''),
'session_id' => $sessionId,
];
if ($payload['product_code'] === '') {
throw new RuntimeException('La comanda no té product_code');
}
if ($payload['order_id'] === '') {
throw new RuntimeException('La comanda no té order_id');
}
if (!function_exists('curl_init')) {
throw new RuntimeException('cURL no està disponible al PHP');
}
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
],
CURLOPT_TIMEOUT => 20,
CURLOPT_CONNECTTIMEOUT => 10,
]);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
throw new RuntimeException("Error cURL stock sync: {$error}");
}
$statusCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statusCode < 200 || $statusCode >= 300) {
throw new RuntimeException("Stock sync HTTP {$statusCode}: {$response}");
}
$json = json_decode($response, true);
if (!is_array($json)) {
throw new RuntimeException('Resposta no JSON del stock sync: ' . $response);
}
if (!($json['ok'] ?? false)) {
throw new RuntimeException('Stock sync error: ' . ($json['error'] ?? 'error desconegut'));
}
return $json;
}
function kapvoe_verify_stripe_signature(string $payload, string $header, string $secret, int $tolerance = 300): bool {
if (!$header || !$secret) {
return false;