<?php
// Tu API Key de Coinbase Commerce
$apiKey = "4c514d38-f187-41c7-ba1e-9bf449d5345e"; // 🔑 Reemplaza con tu clave real

// Endpoint de Coinbase Commerce
$url = "https://api.commerce.coinbase.com/charges";

$orderId = 'ORD-' . time(); // ID único de orden demo

// Datos del body
$data = [
    "name"        => "Producto Demo",
    "description" => "Acceso premium (licencia 1 año)",
    "local_price" => ["amount" => "1.00", "currency" => "USD"],
    "pricing_type"=> "fixed_price",
    "metadata"    => ["order_id" => $orderId],
];

// Inicializar cURL
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "X-CC-Api-Key: $apiKey",
    "X-CC-Version: 2018-03-22"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

// Ejecutar y obtener respuesta
$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo json_encode(["success" => false, "error" => curl_error($ch)]);
    exit;
}

curl_close($ch);

// Decodificar JSON
$result = json_decode($response, true);

// Devolver solo el hosted_url si existe
if (isset($result['data']['hosted_url'])) {
    echo json_encode([
        "success" => true,
        "checkout_url" => $result['data']['hosted_url']
    ]);
} else {
    echo json_encode([
        "success" => false,
        "response" => $result
    ]);
}