295 lines
11 KiB
PHP
295 lines
11 KiB
PHP
|
||
<?php
|
||
|
||
// MSIN-3679 nouveau tableau
|
||
|
||
ini_set('display_errors', true);
|
||
error_reporting(E_ALL);
|
||
require_once $_SERVER["DOCUMENT_ROOT"] . "/superadm/php/inc_fx_paypal.php";
|
||
global $objDatabase, $vDomaine, $strLangue;
|
||
$intEveId = 0;
|
||
$blnDon = false;
|
||
|
||
if ($strLangue == 'fr') {
|
||
$strPage = '/compte';
|
||
} else {
|
||
$strPage = '/account';
|
||
}
|
||
if($strLangue == 'fr')
|
||
$strCode = 'compte/';
|
||
else
|
||
$strCode = 'account/';
|
||
|
||
$strBibCode = $strCode;
|
||
|
||
if (!empty($_GET['code'])) {
|
||
$code = $_GET['code'];
|
||
} else {
|
||
$code = 'bib_assignment';
|
||
}
|
||
|
||
if (isset($_GET['promoteur_eve_id'])) {
|
||
$intEveId = base64_decode(urldecode($_GET['promoteur_eve_id']));
|
||
$arrEvenement = fxGetEvenementsId($intEveId);
|
||
//$tabEvenement['general'] = $arrEvenement;
|
||
}
|
||
// a metre dimamique
|
||
|
||
$sqlCommandes = "SELECT *, IF(a.com_id <> 0, (SELECT c.com_login FROM inscriptions_comptes c WHERE a.com_id = c.com_id), '') AS usager FROM inscriptions_panier_acheteurs a, inscriptions_panier_statuts s WHERE s.sta_id = a.sta_id AND a.no_panier = '" . $_GET['no_panier']."'";
|
||
$recCommandes = $objDatabase->fxGetRow($sqlCommandes);
|
||
|
||
|
||
|
||
?>
|
||
<a class="btn btn-primary rounded-pill" id="link_back" href="<?php echo $vDomaine . $strPage . "/inc_tableau_inscriptions?promoteur_eve_id=" . urlencode(base64_encode($intEveId)); ?>">
|
||
<i class="fa fa-chevron-left mr-2" aria-hidden="true"></i>
|
||
<?php afficheTexte('promoteur_back'); ?>
|
||
</a>
|
||
<br>
|
||
|
||
<h3><img src="images_blue/ico_commandes_big.png" alt="">Détails de la commande</h3>
|
||
<?php echo fxShowPanier($recCommandes['no_panier'], 0, 'web', 1, 'fr'); ?>
|
||
<p> </p>
|
||
|
||
<br>
|
||
|
||
|
||
<h3><img src="images_blue/ico_commandes_big.png" alt="">PAYPAL</h3>
|
||
|
||
<?php
|
||
|
||
// ************************************************ rembourcement
|
||
|
||
/****************************************************
|
||
* Page: remboursement.php
|
||
* Rôle : affiche le formulaire de remboursement + prépare
|
||
* les variables serveur nécessaires au front (AJAX).
|
||
****************************************************/
|
||
|
||
// Démarrer la session
|
||
//session_start();
|
||
|
||
// Jeton anti double clic / re-soumission
|
||
if (empty($_SESSION['refund_token'])) {
|
||
$_SESSION['refund_token'] = bin2hex(random_bytes(16));
|
||
}
|
||
$refundToken = $_SESSION['refund_token'];
|
||
|
||
// Récupération des infos commande (présentes dans ton contexte)
|
||
$CAPTURE_ID = $recCommandes["TransactionID"] ?? null;
|
||
|
||
if (!isset($CAPTURE_ID) || !is_string($CAPTURE_ID) || $CAPTURE_ID === '') {
|
||
//echo '<div style="background:#feecec;border:1px solid #f2b8b5;padding:10px;border-radius:8px;">
|
||
// ⚠️ CAPTURE_ID non fourni côté serveur.
|
||
// </div>';
|
||
return;
|
||
}
|
||
|
||
// Charger ta classe PayPal
|
||
require_once $_SERVER["DOCUMENT_ROOT"] . "/paypal_advanced/PaypalCheckout.class.php";
|
||
$paypal = new PayPalCheckout(); // adapte si nécessaire
|
||
|
||
// Préparation
|
||
$errors = [];
|
||
$successMsg = null;
|
||
|
||
// Devise par défaut si jamais les détails PayPal n’en donnent pas
|
||
$currencyUI = 'CAD';
|
||
|
||
// ====== Calculs initiaux côté serveur ======
|
||
|
||
// Solde dispo promoteur (ton helper existant)
|
||
$solde_dispo_ms1 = statut_payement($recCommandes["eve_id"]);
|
||
|
||
// Récupérer les détails PayPal de la capture
|
||
$paypaldetails = $paypal->paypaldetails($CAPTURE_ID);
|
||
|
||
// Harmoniser avec tes remboursements locaux si besoin
|
||
$paypaldetails = reconcileRefundsWithLocal($paypaldetails);
|
||
|
||
// Déterminer le max remboursable PayPal
|
||
$max_remboursement_paypal = 0.0;
|
||
|
||
// Cas habituel : clé remaining
|
||
if (!empty($paypaldetails['refunds']['totals']['remaining'])) {
|
||
$max_remboursement_paypal = (float)$paypaldetails['refunds']['totals']['remaining'];
|
||
}
|
||
// Fallback : montant total capturé (certains retours utilisent "details", d’autres ont eu une coquille "delails")
|
||
elseif (!empty($paypaldetails['details']['amount']['value'])) {
|
||
$max_remboursement_paypal = (float)$paypaldetails['details']['amount']['value'];
|
||
} elseif (!empty($paypaldetails['delails']['amount']['value'])) { // compat. si ton code historique renvoie "delails"
|
||
$max_remboursement_paypal = (float)$paypaldetails['delails']['amount']['value'];
|
||
}
|
||
|
||
// Déterminer la devise d’affichage si fournie par PayPal
|
||
if (!empty($paypaldetails['refunds']['totals']['currency'])) {
|
||
$currencyUI = (string)$paypaldetails['refunds']['totals']['currency'];
|
||
} elseif (!empty($paypaldetails['details']['amount']['currency_code'])) {
|
||
$currencyUI = (string)$paypaldetails['details']['amount']['currency_code'];
|
||
} elseif (!empty($paypaldetails['delails']['amount']['currency_code'])) {
|
||
$currencyUI = (string)$paypaldetails['delails']['amount']['currency_code'];
|
||
}
|
||
|
||
// Frais MS1 (selon ta structure)
|
||
$frais_ms1 = (float)($recCommandes['fra_total'] ?? 0);
|
||
|
||
// Max remboursable à l’écran (ta logique actuelle)
|
||
$maxUI = $max_remboursement_paypal - $frais_ms1;
|
||
|
||
// maxblock = limite stricte d’input côté front, selon ton choix actuel
|
||
if ($max_remboursement_paypal < $solde_dispo_ms1) {
|
||
$maxblock = $max_remboursement_paypal;
|
||
} else {
|
||
$maxblock = 0;
|
||
$maxUI = 0;
|
||
}
|
||
// ******************** variable info
|
||
//print_rsl($recCommandes);
|
||
echosl( "no_commande : ".$recCommandes["no_commande"]);
|
||
// echosl( "rem_ajoute_par : ".$_SESSION["usa_info"]["com_prenom"]." ".$_SESSION["usa_info"]["com_nom"]);
|
||
echosl( "eve_id : ".$recCommandes["eve_id"]) ;
|
||
echosl( "Montant remboursable : " . number_format($max_remboursement_paypal, 2) . " " . ($result['refunds']['totals']['currency'] ?? $result['delails']['amount']['currency_code'] ?? ''));
|
||
|
||
|
||
|
||
echo("solde dispo ms1 :");
|
||
echosl($solde_dispo_ms1);
|
||
echo("frais ms1 :");
|
||
$frais_ms1=$recCommandes['fra_total'];
|
||
echosl($frais_ms1);
|
||
echo("max Remboursement :");
|
||
|
||
// $maxUI le max Remboursement
|
||
$maxUI=$max_remboursement_paypal-$frais_ms1;
|
||
echosl($maxUI);
|
||
?>
|
||
<!doctype html>
|
||
<html lang="fr">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<title>Remboursement PayPal</title>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
</head>
|
||
<body style="font-family:system-ui,Arial,Helvetica;">
|
||
|
||
<div style="max-width:560px;border:1px solid #ddd;border-radius:10px;padding:14px;">
|
||
<div style="font-weight:700;font-size:16px;margin-bottom:8px;">Remboursement PayPal (Checkout)</div>
|
||
|
||
<!-- Zone messages AJAX -->
|
||
<div id="refund-msg" style="margin-bottom:10px;display:none;padding:10px;border-radius:8px;"></div>
|
||
|
||
<?php if ($maxUI > 0): ?>
|
||
<div style="background:#f9fbff;border:1px solid #dbe7ff;padding:10px;border-radius:8px;margin-bottom:10px;">
|
||
<div><strong>Capture:</strong> <?= h($CAPTURE_ID) ?></div>
|
||
<div><strong>Max remboursable (affiché):</strong> <span id="pp_ref_max"><?= h(money2($maxUI)) ?></span> <?= h($currencyUI) ?></div>
|
||
</div>
|
||
|
||
<form id="refund-form" method="post" style="display:grid;grid-template-columns:1fr 1fr;gap:10px;">
|
||
<input type="hidden" name="refund_token" value="<?= h($refundToken) ?>">
|
||
<input type="hidden" name="capture_id" value="<?= h($CAPTURE_ID) ?>">
|
||
<input type="hidden" name="currency" value="<?= h($currencyUI) ?>">
|
||
<input type="hidden" name="eve_id" value="<?= h($recCommandes["eve_id"]) ?>">
|
||
<input type="hidden" name="no_commande" value="<?= h($recCommandes["no_commande"]) ?>">
|
||
|
||
<div style="grid-column:1/2;">
|
||
<label style="display:block;font-size:12px;color:#333;margin-bottom:4px;">
|
||
Montant à rembourser (<?= h($currencyUI) ?>)
|
||
</label>
|
||
<input id="pp_ref_amount" type="number" name="refund_amount" step="0.01" min="0.01"
|
||
max="<?= h(money2($maxblock)) ?>"
|
||
value="<?= h(money2($maxUI)) ?>"
|
||
style="width:100%;padding:8px;border:1px solid #ccc;border-radius:8px;">
|
||
</div>
|
||
|
||
<div style="grid-column:2/3;">
|
||
<label style="display:block;font-size:12px;color:#333;margin-bottom:4px;">Note (optionnelle)</label>
|
||
<input type="text" name="refund_note" placeholder="Raison du remboursement"
|
||
style="width:100%;padding:8px;border:1px solid #ccc;border-radius:8px;">
|
||
</div>
|
||
|
||
<div style="grid-column:1/3;text-align:right;margin-top:6px;">
|
||
<button type="submit"
|
||
style="padding:10px 14px;border:1px solid #2d6cdf;border-radius:8px;background:#2d6cdf;color:#fff;cursor:pointer;">
|
||
Rembourser
|
||
</button>
|
||
</div>
|
||
</form>
|
||
<?php else: ?>
|
||
<div style="background:#fff8e6;border:1px solid #ffe1a6;padding:10px;border-radius:8px;">
|
||
Aucun montant remboursable pour cette capture.
|
||
</div>
|
||
<?php endif; ?>
|
||
</div>
|
||
|
||
<script>
|
||
// Petit helper visuel
|
||
function showMsg(text, ok) {
|
||
const box = document.getElementById('refund-msg');
|
||
box.style.display = 'block';
|
||
box.style.background = ok ? '#e9f8ef' : '#feecec';
|
||
box.style.border = ok ? '1px solid #b8e3c5' : '1px solid #f2b8b5';
|
||
box.innerText = text;
|
||
}
|
||
|
||
document.getElementById('refund-form')?.addEventListener('submit', async (e) => {
|
||
e.preventDefault();
|
||
|
||
showMsg('⏳ Traitement en cours...', false);
|
||
|
||
const data = new FormData(e.target);
|
||
const res = await fetch( '<?php echo $vDomaine; ?>/superadm/ajax_refund.php', { method: 'POST', body: data });
|
||
|
||
|
||
// Défensif
|
||
if (!res.ok) {
|
||
showMsg('⚠️ Erreur réseau pendant le remboursement.', false);
|
||
return;
|
||
}
|
||
|
||
let json;
|
||
try { json = await res.json(); } catch (_) {
|
||
showMsg('⚠️ Réponse invalide du serveur.', false);
|
||
return;
|
||
}
|
||
|
||
if (json.success) {
|
||
showMsg('✅ ' + (json.message || 'Remboursement effectué.'), true);
|
||
|
||
// 👉 Ici, tu peux déclencher des actions UI :
|
||
// - recharger une grille des remboursements
|
||
// - rafraîchir un solde
|
||
// - etc.
|
||
// refreshTableRemboursements();
|
||
} else {
|
||
showMsg('⚠️ ' + (json.message || 'Erreur remboursement.'), false);
|
||
}
|
||
});
|
||
</script>
|
||
|
||
</body>
|
||
</html>
|
||
|
||
|
||
|
||
|
||
<h3><img src="images_blue/ico_commandes_big.png" alt="">Détails PAYPAL</h3>
|
||
|
||
<?php
|
||
|
||
|
||
// $sortie=paypaldetails($recCommandes["TransactionID"]);
|
||
|
||
|
||
echo(renderPayPalCaptureHtml($paypaldetails));
|
||
|
||
|
||
|
||
|
||
|
||
?>
|
||
<a class="btn btn-primary rounded-pill" id="link_back" href="<?php echo $vDomaine . $strPage . "/inc_tableau_promoteur?promoteur_eve_id=" . urlencode(base64_encode($intEveId)); ?>">
|
||
<i class="fa fa-chevron-left mr-2" aria-hidden="true"></i>
|
||
<?php afficheTexte('promoteur_back'); ?>
|
||
</a>
|
||
<?php
|