loadConfig(); $ipServer = $config['ipServer'] ?? ''; $baseUrl = 'https://' . $ipServer; $apiUcrmKey = $config['apiTokenUcrm'] ?? ''; $stripeApiKey = $config['apiTokenStripe'] ?? ''; $service = new PaymentIntentService($baseUrl, $apiUcrmKey, $stripeApiKey); // --- 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); if ($event->type == 'payment_intent.succeeded') { $paymentIntent = $event->data->object; $clientId = $paymentIntent->metadata->clientId ?? null; $amount = $paymentIntent->amount / 100; $currency = $paymentIntent->currency; if ($clientId) { $notes = "Pago procesado via Stripe PaymentIntent: " . $paymentIntent->id; $res = $service->registerPayment($clientId, $amount, $currency, $notes); $log->appendLog("Payment Register UCRM ($clientId): " . ($res ? 'Success' : 'Fail')); } } elseif ($event->type == 'customer_cash_balance_transaction.created') { $txn = $event->data->object; // 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; $paymentIntent = $service->getPaymentIntent($piId); if ($paymentIntent) { $clientId = $paymentIntent->metadata->clientId ?? null; $amount = abs($txn->net_amount) / 100; $currency = $txn->currency; if ($clientId) { $notes = "Pago via Transferencia (Cash Balance) aplicado a Intent: " . $piId; $res = $service->registerPayment($clientId, $amount, $currency, $notes); $log->appendLog("Cash Balance Payment Register UCRM ($clientId): " . ($res ? 'Success' : 'Fail')); } } } } http_response_code(200); exit; } // --- VIEW (HTML) --- ?>
Busca un cliente para generar un cobro vía Stripe