<?php
/**
 * WebEngine CMS - PayPal Gateway (PAQUETES + IP en custom + ROUTER)
 * custom = userid|SITE|PACKID|IP · Estilo Nexora (tema oscuro).
 */

if (!isLoggedIn()) {
	redirect(1, 'login');
}
if (!mconfig('active')) {
	throw new Exception(lang('error_47'));
}

$pageTitle = lang('module_titles_txt_21', true) ?: 'Buy Credits';

echo '<div class="nexora-donation-page nexora-paypal-page">';
echo '<header class="nexora-donation-header">';
echo '<h1 class="nexora-donation-title">' . htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') . '</h1>';
echo '<p class="nexora-donation-subtitle">' . htmlspecialchars(nexora_lang('nexora_paypal_subtitle', 'PayPal — paquetes'), ENT_QUOTES, 'UTF-8') . '</p>';
echo '</header>';

echo '<span id="paypal_conversion_rate_value" style="display:none;">' . (int) mconfig('paypal_conversion_rate') . '</span>';

/**
 * Obtener IP real del usuario (Cloudflare / proxy / normal)
 */
$userIp = class_exists('NexoraSecurity') ? NexoraSecurity::clientIp() : ($_SERVER['REMOTE_ADDR'] ?? '0.0.0.0');

$creditLabel = lang('donation_txt_2');
$creditLabelJs = json_encode($creditLabel, JSON_UNESCAPED_UNICODE);

$packages = [
	'P100'  => ['usd' => 1.00,  'base' => 1000,  'bonus' => 0,    'total' => 1000,  'label' => '1.000 WC — $1.00 — sin bonus — Total: 1.000 WC'],
	'P200'  => ['usd' => 1.99,  'base' => 2000,  'bonus' => 0,    'total' => 2000,  'label' => '2.000 WC — $1.99 — sin bonus — Total: 2.000 WC'],
	'P300'  => ['usd' => 2.99,  'base' => 3000,  'bonus' => 0,    'total' => 3000,  'label' => '3.000 WC — $2.99 — sin bonus — Total: 3.000 WC'],
	'P400'  => ['usd' => 3.99,  'base' => 4000,  'bonus' => 0,    'total' => 4000,  'label' => '4.000 WC — $3.99 — sin bonus — Total: 4.000 WC'],
	'P550'  => ['usd' => 4.99,  'base' => 5000,  'bonus' => 500,  'total' => 5500,  'label' => '5.000 WC — $4.99 — 10% (500 WC) — Total: 5.500 WC'],
	'P781'  => ['usd' => 6.99,  'base' => 7000,  'bonus' => 810,  'total' => 7810,  'label' => '7.000 WC — $6.99 — 11.5% (810 WC) — Total: 7.810 WC'],
	'P1125' => ['usd' => 9.99,  'base' => 10000, 'bonus' => 1250, 'total' => 11250, 'label' => '10.000 WC — $9.99 — 12.5% (1.250 WC) — Total: 11.250 WC'],
	'P1703' => ['usd' => 14.99, 'base' => 15000, 'bonus' => 2030, 'total' => 17030, 'label' => '15.000 WC — $14.99 — 13.5% (2.030 WC) — Total: 17.030 WC'],
	'P2290' => ['usd' => 19.99, 'base' => 20000, 'bonus' => 2900, 'total' => 22900, 'label' => '20.000 WC — $19.99 — 14.5% (2.900 WC) — Total: 22.900 WC'],
	'P2888' => ['usd' => 24.99, 'base' => 25000, 'bonus' => 3880, 'total' => 28880, 'label' => '25.000 WC — $24.99 — 15.5% (3.880 WC) — Total: 28.880 WC'],
	'P3495' => ['usd' => 29.99, 'base' => 30000, 'bonus' => 4950, 'total' => 34950, 'label' => '30.000 WC — $29.99 — 16.5% (4.950 WC) — Total: 34.950 WC'],
	'P5925' => ['usd' => 49.99, 'base' => 50000, 'bonus' => 9250, 'total' => 59250, 'label' => '50.000 WC — $49.99 — 18.5% (9.250 WC) — Total: 59.250 WC'],
];

reset($packages);
$defaultPackId = key($packages);
$defaultUsd    = number_format($packages[$defaultPackId]['usd'], 2, '.', '');
$defaultTotal  = (int) $packages[$defaultPackId]['total'];

$formAction = (mconfig('paypal_enable_sandbox') ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr');

$order_id = hash('sha256', uniqid((string) $_SESSION['userid'] . '_', true));

$routerUrl = 'https://s6.nexoravenetworks.com/api/ipn-router.php';
$siteCode = 'S6';

echo '<div class="nex-paypal-gateway">';
echo '<div class="nexora-donation-panel nex-paypal-gateway-inner">';
$ppImgFs = __PATH_TEMPLATE_ROOT__ . 'img/donation/paypal.jpg';
if (is_file($ppImgFs)) {
	echo '<div class="nex-paypal-brand"><img src="' . htmlspecialchars(__PATH_TEMPLATE_IMG__ . 'donation/paypal.jpg', ENT_QUOTES, 'UTF-8') . '" alt="PayPal" loading="lazy"></div>';
}

echo '<p class="nex-paypal-pack-heading">' . htmlspecialchars(nexora_lang('nexora_paypal_pick_pack', 'Selecciona tu paquete'), ENT_QUOTES, 'UTF-8') . ':</p>';

echo '<form action="' . htmlspecialchars($formAction, ENT_QUOTES, 'UTF-8') . '" method="post" id="paypalPackForm" class="nex-paypal-pack-form">';
echo '<input type="hidden" name="cmd" value="_xclick">';
echo '<input type="hidden" name="business" value="' . htmlspecialchars(mconfig('paypal_email'), ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="item_name" value="' . htmlspecialchars(mconfig('paypal_title'), ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="item_number" value="' . htmlspecialchars($order_id, ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="currency_code" value="' . htmlspecialchars(mconfig('paypal_currency'), ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="no_shipping" value="1">';
echo '<input type="hidden" name="shipping" value="0.00">';
echo '<input type="hidden" name="return" value="' . htmlspecialchars(mconfig('paypal_return_url'), ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="cancel_return" value="' . htmlspecialchars(mconfig('paypal_return_url'), ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="notify_url" value="' . htmlspecialchars($routerUrl, ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="no_note" value="1">';
echo '<input type="hidden" name="tax" value="0.00">';
echo '<input type="hidden" name="amount" id="paypal_amount" value="' . htmlspecialchars($defaultUsd, ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="hidden" name="custom" id="paypal_custom" value="' . (int) $_SESSION['userid'] . '|' . htmlspecialchars($siteCode, ENT_QUOTES, 'UTF-8') . '|' . htmlspecialchars($defaultPackId, ENT_QUOTES, 'UTF-8') . '|' . htmlspecialchars($userIp, ENT_QUOTES, 'UTF-8') . '">';

echo '<ul class="nex-paypal-pack-list" id="paypalPackList">';
foreach ($packages as $packId => $p) {
	$usd     = number_format((float) $p['usd'], 2, '.', '');
	$total   = (int) $p['total'];
	$checked = ($packId === $defaultPackId) ? ' checked' : '';
	$safeLabel = htmlspecialchars($p['label'], ENT_QUOTES, 'UTF-8');
	echo '<li class="nex-paypal-pack-item" data-pack="' . htmlspecialchars($packId, ENT_QUOTES, 'UTF-8') . '" data-usd="' . htmlspecialchars($usd, ENT_QUOTES, 'UTF-8') . '" data-total="' . (int) $total . '">';
	echo '<input type="radio" name="pack_radio"' . $checked . '>';
	echo '<div class="nex-paypal-pack-text">' . $safeLabel . '</div>';
	echo '<div class="nex-paypal-pack-badges">';
	echo '<span class="nex-paypal-badge nex-paypal-badge--usd">$' . htmlspecialchars($usd, ENT_QUOTES, 'UTF-8') . ' ' . htmlspecialchars(mconfig('paypal_currency'), ENT_QUOTES, 'UTF-8') . '</span>';
	echo '<span class="nex-paypal-badge nex-paypal-badge--total">' . htmlspecialchars(nexora_lang('nexora_paypal_total', 'Total'), ENT_QUOTES, 'UTF-8') . ': ' . (int) $total . ' ' . htmlspecialchars((string) $creditLabel, ENT_QUOTES, 'UTF-8') . '</span>';
	echo '</div></li>';
}
echo '</ul>';

echo '<div class="nex-paypal-summary">';
echo '<div class="nex-paypal-summary-copy">';
echo '<strong>' . htmlspecialchars(nexora_lang('nexora_paypal_selected', 'Seleccionado'), ENT_QUOTES, 'UTF-8') . ':</strong> ';
echo '<span id="paypal_selected_text">$' . htmlspecialchars($defaultUsd, ENT_QUOTES, 'UTF-8') . ' = ' . $defaultTotal . ' ' . htmlspecialchars((string) $creditLabel, ENT_QUOTES, 'UTF-8') . '</span><br>';
echo '<span class="nex-paypal-muted">' . htmlspecialchars(nexora_lang('nexora_paypal_ipn_note', 'El crédito se acredita cuando PayPal confirme el pago (IPN).'), ENT_QUOTES, 'UTF-8') . '</span>';
echo '</div>';
echo '<button type="submit" class="nexora-usercp-btn nex-paypal-submit">' . htmlspecialchars(lang('donation_txt_5', true) ?: 'Continue to PayPal', ENT_QUOTES, 'UTF-8') . '</button>';
echo '</div>';
echo '</form>';
echo '</div></div>';
echo '</div>';

echo '<script>
(function(){
  var creditLabel = ' . $creditLabelJs . ';
  var list = document.getElementById("paypalPackList");
  if (!list) return;
  var amountEl = document.getElementById("paypal_amount");
  var customEl = document.getElementById("paypal_custom");
  var selectedText = document.getElementById("paypal_selected_text");
  function setSelected(li){
    var packId = li.getAttribute("data-pack");
    var usd = li.getAttribute("data-usd");
    var total = li.getAttribute("data-total");
    var radios = list.querySelectorAll("input[type=radio]");
    radios.forEach(function(r){ r.checked = false; });
    var rin = li.querySelector("input[type=radio]");
    if (rin) rin.checked = true;
    amountEl.value = usd;
    var parts = customEl.value.split("|");
    var userId = parts[0] || "";
    var site   = parts[1] || "S6";
    var userIp = parts[3] || "";
    customEl.value = userId + "|" + site + "|" + packId + "|" + userIp;
    selectedText.textContent = "$" + usd + " = " + total + " " + creditLabel;
  }
  list.querySelectorAll(".nex-paypal-pack-item").forEach(function(li){
    li.addEventListener("click", function(){ setSelected(li); });
  });
})();
</script>';
