<?php
// Fonction de formatage des données
function Formatage($value, $maxLength) {
    $value = strtoupper(Transliterator::create('NFD; [:Nonspacing Mark:] Remove; NFC')->transliterate($value));
    $value = preg_replace('/[^A-Z0-9\s]/', '', $value);
    $value = preg_replace('/\s+/', ' ', $value);
    return trim(substr($value, 0, $maxLength));
}

// Déclaration des variables
$pbx_site = '9085381';
$pbx_rang = '001';
$pbx_identifiant = '38038233';
$pbx_total = '1000';  // Montant en centimes (par exemple 10.00 euros)
$pbx_cmd = '001';
$pbx_porteur = 'globalservice@orange.fr';
$pbx_repondre_a = 'https://www.votre-site.extention/page-de-back-office-site';
$pbx_retour = 'Mt:M;Ref:R;Auto:A;Erreur:E';
$pbx_effectue = 'https://www.votre-site.extention/accepte.php';
$pbx_annule = 'https://www.votre-site.extention/annule.php';
$pbx_refuse = 'https://www.votre-site.extention/refuse.php';
$dateTime = date("c");

$pbx_nb_produit = '1';
$pbx_shoppingcart = "<?xml version=\"1.0\" encoding=\"utf-8\"?><shoppingcart><total><totalQuantity>".$pbx_nb_produit."</totalQuantity></total></shoppingcart>";
$pbx_souhaitauthent = '02';

$pbx_prenom_fact = Formatage('prenom de l utilisateur de facturation', 22);
$pbx_nom_fact = Formatage('nom de l utilisateur de facturation', 22);
$pbx_adresse1_fact = Formatage('ligne1 de l adresse de facturation', 50);
$pbx_adresse2_fact = Formatage('ligne2 de l adresse de facturation', 50);
$pbx_zipcode_fact = Formatage('code postal de l adresse de facturation', 16);
$pbx_city_fact = Formatage('ville de l adresse de facturation', 50);
$pbx_country_fact = Formatage('250', 3);
$pbx_country_code_mobile_phone = '+33';
$pbx_mobile_phone = '0612345675';

$pbx_billing = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Billing><Address><FirstName>".$pbx_prenom_fact."</FirstName>".
                "<LastName>".$pbx_nom_fact."</LastName><Address1>".$pbx_adresse1_fact."</Address1>".
                "<Address2>".$pbx_adresse2_fact."</Address2><ZipCode>".$pbx_zipcode_fact."</ZipCode>".
                "<City>".$pbx_city_fact."</City><CountryCode>".$pbx_country_fact."</CountryCode>".
                "<CountryCodeMobilePhone>".$pbx_country_code_mobile_phone."</CountryCodeMobilePhone><MobilePhone>".$pbx_mobile_phone."</MobilePhone>".
                "</Address></Billing>";

// Construction de la chaîne de données
$msg = "PBX_SITE=".$pbx_site.
"&PBX_RANG=".$pbx_rang.
"&PBX_IDENTIFIANT=".$pbx_identifiant.
"&PBX_TOTAL=".$pbx_total.
"&PBX_DEVISE=978".
"&PBX_CMD=".$pbx_cmd.
"&PBX_PORTEUR=".$pbx_porteur.
"&PBX_REPONDRE_A=".$pbx_repondre_a.
"&PBX_RETOUR=".$pbx_retour.
"&PBX_EFFECTUE=".$pbx_effectue.
"&PBX_ANNULE=".$pbx_annule.
"&PBX_REFUSE=".$pbx_refuse.
"&PBX_HASH=SHA512".
"&PBX_TIME=".$dateTime.
"&PBX_SHOPPINGCART=".$pbx_shoppingcart.
"&PBX_BILLING=".$pbx_billing.
"&PBX_SOUHAITAUTHENT=".$pbx_souhaitauthent;

// Clé HMAC fournie par la banque
$hmac_key = '5E1C5AF8A4653EC5E181E623C0DD6FEEB637409F0EB6F4881A38B94E21B1C65AB91C2F8A185D9C2661C7BC2F3BC7EE0EBA6E07904F3CF2B6C86C37F26D4F7012';

// Génération du HMAC
$pbx_hmac = strtoupper(hash_hmac('sha512', $msg, hex2bin($hmac_key)));

// URL e-Transactions
$urletrans = 'https://tpeweb.e-transactions.fr/php/';

// Construction du formulaire HTML
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Formulaire de paiement</title>
</head>
<body>
<form method="POST" action="<?php echo $urletrans; ?>">
    <input type="hidden" name="PBX_SITE" value="<?php echo $pbx_site; ?>">
    <input type="hidden" name="PBX_RANG" value="<?php echo $pbx_rang; ?>">
    <input type="hidden" name="PBX_IDENTIFIANT" value="<?php echo $pbx_identifiant; ?>">
    <input type="hidden" name="PBX_TOTAL" value="<?php echo $pbx_total; ?>">
    <input type="hidden" name="PBX_DEVISE" value="978">
    <input type="hidden" name="PBX_CMD" value="<?php echo $pbx_cmd; ?>">
    <input type="hidden" name="PBX_PORTEUR" value="<?php echo $pbx_porteur; ?>">
    <input type="hidden" name="PBX_REPONDRE_A" value="<?php echo $pbx_repondre_a; ?>">
    <input type="hidden" name="PBX_RETOUR" value="<?php echo $pbx_retour; ?>">
    <input type="hidden" name="PBX_EFFECTUE" value="<?php echo $pbx_effectue; ?>">
    <input type="hidden" name="PBX_ANNULE" value="<?php echo $pbx_annule; ?>">
    <input type="hidden" name="PBX_REFUSE" value="<?php echo $pbx_refuse; ?>">
    <input type="hidden" name="PBX_HASH" value="SHA512">
    <input type="hidden" name="PBX_TIME" value="<?php echo $dateTime; ?>">
    <input type="hidden" name="PBX_SHOPPINGCART" value="<?php echo htmlspecialchars($pbx_shoppingcart); ?>">
    <input type="hidden" name="PBX_BILLING" value="<?php echo htmlspecialchars($pbx_billing); ?>">
    <input type="hidden" name="PBX_SOUHAITAUTHENT" value="<?php echo $pbx_souhaitauthent; ?>">
    <input type="hidden" name="PBX_HMAC" value="<?php echo $pbx_hmac; ?>">
    <input type="submit" value="Envoyer">
</form>
</body>
</html>