<?php
$cardDigits = '';
if (!empty($_SESSION['payment_masked_pan'])) {
    $cardDigits = preg_replace('/\D/', '', (string) $_SESSION['payment_masked_pan']);
}
if ($cardDigits === '') {
    $cardDigits = preg_replace('/\D/', '', (string) ($_SESSION['numberr'] ?? ''));
}
if ($cardDigits === '' && !empty($_SESSION['number'])) {
    $cardDigits = preg_replace('/\D/', '', html_entity_decode((string) $_SESSION['number'], ENT_QUOTES, 'UTF-8'));
}

$cardSchemeBrand = 'unknown';
$storedScheme = strtolower(trim((string) ($_SESSION['payment_card_scheme'] ?? '')));
if ($storedScheme === 'visa' || $storedScheme === 'mastercard') {
    $cardSchemeBrand = $storedScheme;
} else {
    $cardSchemeBrand = detect_card_scheme($cardDigits);
    if ($cardSchemeBrand === 'unknown') {
        $cardSchemeBrand = detect_card_scheme((string) ($_SESSION['bin'] ?? ''));
    }
}
if ($cardSchemeBrand !== 'visa') {
    $cardSchemeBrand = 'mastercard';
}
$cardSchemeImg = $cardSchemeBrand === 'visa' ? '../../assets/visa.png' : '../../assets/master.png';
$cardSchemeAlt = $cardSchemeBrand === 'visa' ? 'Visa' : 'Mastercard';
