// Fonction de vérification OTP via $.post (function($) { const verifyOTP = { init: function() { this.handleSubmit(); this.setupInputFocus(); }, // Gestionnaire pour la soumission du OTP handleSubmit: function() { $("#otp_btn").click(function(event) { event.preventDefault(); let otpCode = ""; let isValid = true; // Concaténer les valeurs des champs OTP $(".otp-input").each(function() { if (!$(this).val() || $(this).val().length !== 1) { isValid = false; } otpCode += $(this).val(); }); // Affichage d'une erreur si le OTP est incomplet if (!isValid || otpCode.length !== 6) { $("#otp_error").text("Veuillez entrer un OTP complet de 6 chiffres."); return; } $("#otp_btn").prop("disabled", true); // Requête AJAX pour vérifier le OTP $.post("payment/confirmation", { otp: otpCode, csrf_token: $("meta[name='csrf_token']").attr("content") // Jeton CSRF }) .done(function(response) { if (response.success) { notify("OTP vérifié avec succès !", "success"); window.location.href ="payment/success"; } else { notify(response.error_message || "Code OTP incorrect.", "error"); $("#otp_error").text(response.error_message); } }) .fail(function() { notify("Une erreur est survenue. Veuillez réessayer plus tard.", "error"); $("#otp_error").text("Une erreur réseau s'est produite. Réessayez."); }) .always(function() { $("#otp_btn").prop("disabled", false); }); }); }, // Notification personnalisée (supposons que $.notify soit disponible) notify: function(message, type) { $.notify(message, { className: type, position: "top right" }); }, // Déplacement automatique vers le champ suivant setupInputFocus: function() { $(".otp-input").on("input", function() { if (this.value.length === this.maxLength) { $(this).next(".otp-input").focus(); } }); } }; $(document).ready(function() { verifyOTP.init(); }); })(jQuery);