loadConfig(); $ipServer = $config['ipServer'] ?? ''; $baseUrl = 'https://' . $ipServer . '/crm'; $apiUcrmKey = $config['apiTokenUcrm'] ?? ''; $stripeApiKey = $config['apiTokenStripe'] ?? ''; $log->appendLog("Plugin Config Loaded: Server=$ipServer, UCRM Token=" . substr($apiUcrmKey, 0, 5) . "..., Stripe Token=" . substr($stripeApiKey, 0, 7) . "..."); $service = new PaymentIntentService($baseUrl, $apiUcrmKey, $stripeApiKey, $log); // --- ROUTER --- $action = $_GET['action'] ?? 'view'; // 1. Image Handler if ($action === 'image') { $filename = basename($_GET['file'] ?? ''); $path = __DIR__ . '/img/' . $filename; if ($filename && file_exists($path)) { $mime = mime_content_type($path); header("Content-Type: $mime"); readfile($path); } else { http_response_code(404); echo "Image not found"; } exit; } // 2. Search Handler (AJAX) if ($action === 'search') { $query = $_GET['q'] ?? ''; if (strlen($query) < 2) { echo json_encode([]); exit; } $results = $service->searchClients($query); header('Content-Type: application/json'); echo json_encode($results); exit; } // 3. Get Details Handler (AJAX) if ($action === 'details') { $clientId = $_GET['id'] ?? 0; if (!$clientId) { echo json_encode(['error' => 'No ID provided']); exit; } $details = $service->getClientDetails($clientId); header('Content-Type: application/json'); echo json_encode($details); exit; } // 4. Create Intent Handler (AJAX) if ($action === 'create_intent') { $input = json_decode(file_get_contents('php://input'), true); $clientId = $input['clientId'] ?? 0; $amount = filter_var($input['amount'], FILTER_VALIDATE_FLOAT); $stripeCustomerId = $input['stripeCustomerId'] ?? ''; // Hardcoded admin ID as per user context in previous script (fallback) // In a real plugin, we might want to get this from a mapping or config, but for now specific ID or 1 $adminId = 5472; if (!$amount || $amount < 10) { echo json_encode(['success' => false, 'error' => 'El monto debe ser mayor a $10.00 MXN']); exit; } if (!$stripeCustomerId) { echo json_encode(['success' => false, 'error' => 'El cliente no tiene Stripe Customer ID']); exit; } $result = $service->createPaymentIntent($clientId, $amount, $stripeCustomerId, $adminId); // Logging for debug $log->appendLog("Create Intent Result: " . json_encode($result)); header('Content-Type: application/json'); echo json_encode($result); exit; } // 5. Webhook Handler if ($action === 'webhook') { $payload = @file_get_contents('php://input'); $event = null; try { $event = \Stripe\Event::constructFrom( json_decode($payload, true) ); } catch(\UnexpectedValueException $e) { http_response_code(400); exit(); } $log->appendLog("Webhook Received: " . $event->type); $log->appendLog("Webhook Payload (Raw): " . $payload); try { if ($event->type == 'payment_intent.succeeded') { $paymentIntent = $event->data->object; $clientId = $paymentIntent->metadata->clientId ?? null; $amount = $paymentIntent->amount / 100; $currency = $paymentIntent->currency; if ($clientId) { $log->appendLog("Info: PI {$paymentIntent->id} succeeded for Client $clientId. UCRM native integration should handle registration."); } else { $log->appendLog("Warning: No clientId found in metadata for PI: " . $paymentIntent->id); } } elseif ($event->type == 'customer_cash_balance_transaction.created') { $txn = $event->data->object; $log->appendLog("Processing Cash Balance Transaction: " . $txn->id); // Check if money was applied to a payment object if (isset($txn->applied_to_payment) && isset($txn->applied_to_payment->payment_intent)) { $piId = $txn->applied_to_payment->payment_intent; $log->appendLog("Transaction applied to PI: " . $piId . ". UCRM native integration will handle PI success."); } else { $log->appendLog("Info: Cash Balance Transaction " . $txn->id . " not directly applied to a PaymentIntent yet."); } } } catch (\Exception $e) { $log->appendLog("CRITICAL ERROR in Webhook Exception: " . $e->getMessage()); } http_response_code(200); exit; } // --- VIEW (HTML) --- ?>
Busca un cliente para generar un cobro vía Stripe