/** * Payment Requests Admin JavaScript * * Handles the admin interface for payment requests * * 🤖 Generated with Claude Code (https://claude.ai/code) */ jQuery(document).ready(function($) { // Send payment request form $('#payment-request-form').on('submit', function(e) { e.preventDefault(); if (!confirm(ebPaymentRequests.strings.confirmSend)) { return; } var $form = $(this); var $button = $form.find('button[type="submit"]'); var originalText = $button.text(); // Show loading state $button.text('Sending...').prop('disabled', true); var formData = { action: 'send_payment_request', nonce: ebPaymentRequests.nonce, booking_id: $('#booking_id').val(), customer_email: $('#customer_email').val(), amount: $('#amount').val(), description: $('#description').val(), expires_in: $('#expires_in').val() }; $.ajax({ url: ebPaymentRequests.ajaxurl, type: 'POST', data: formData, success: function(response) { if (response.success) { showNotice('Payment request sent successfully!', 'success'); $form[0].reset(); // Reload the page to show updated table location.reload(); } else { showNotice('Error: ' + response.data, 'error'); } }, error: function() { showNotice('An error occurred while sending the payment request.', 'error'); }, complete: function() { $button.text(originalText).prop('disabled', false); } }); }); // Auto payment settings form $('#auto-payment-settings').on('submit', function(e) { e.preventDefault(); var $form = $(this); var $button = $form.find('button[type="submit"]'); var originalText = $button.text(); $button.text('Saving...').prop('disabled', true); var formData = { action: 'save_auto_payment_settings', nonce: ebPaymentRequests.nonce, auto_requests_enabled: $('#auto_requests_enabled').is(':checked') ? 1 : 0, auto_percentage: $('#auto_percentage').val(), days_before: $('#days_before').val() }; $.ajax({ url: ebPaymentRequests.ajaxurl, type: 'POST', data: formData, success: function(response) { if (response.success) { showNotice('Settings saved successfully!', 'success'); } else { showNotice('Error: ' + response.data, 'error'); } }, error: function() { showNotice('An error occurred while saving settings.', 'error'); }, complete: function() { $button.text(originalText).prop('disabled', false); } }); }); // Load booking details $('#load-booking').on('click', function() { var bookingId = $('#booking_id').val(); if (!bookingId) { alert('Please enter a booking ID first.'); return; } var $button = $(this); var originalText = $button.text(); $button.text('Loading...').prop('disabled', true); $.ajax({ url: ebPaymentRequests.ajaxurl, type: 'POST', data: { action: 'load_booking_details', nonce: ebPaymentRequests.nonce, booking_id: bookingId }, success: function(response) { if (response.success) { var booking = response.data; $('#customer_email').val(booking.email); $('#amount').val(booking.total_price); $('#description').val('Payment for Hotel Raxa booking #' + bookingId); showNotice('Booking details loaded successfully!', 'success'); } else { showNotice('Error: ' + response.data, 'error'); } }, error: function() { showNotice('An error occurred while loading booking details.', 'error'); }, complete: function() { $button.text(originalText).prop('disabled', false); } }); }); // Copy payment link functionality $(document).on('click', '.copy-link, .copy-payment-link', function() { var link = $(this).data('link'); if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(link).then(function() { showNotice(ebPaymentRequests.strings.linkCopied, 'success'); }).catch(function() { fallbackCopyTextToClipboard(link); }); } else { fallbackCopyTextToClipboard(link); } }); // Fallback copy function for older browsers function fallbackCopyTextToClipboard(text) { var textArea = document.createElement("textarea"); textArea.value = text; // Avoid scrolling to bottom textArea.style.top = "0"; textArea.style.left = "0"; textArea.style.position = "fixed"; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { var successful = document.execCommand('copy'); if (successful) { showNotice(ebPaymentRequests.strings.linkCopied, 'success'); } else { showNotice(ebPaymentRequests.strings.copyFailed, 'error'); } } catch (err) { showNotice(ebPaymentRequests.strings.copyFailed, 'error'); } document.body.removeChild(textArea); } // Resend payment request $(document).on('click', '.resend-request', function() { var requestId = $(this).data('request-id'); if (!confirm('Are you sure you want to resend this payment request?')) { return; } var $button = $(this); var originalText = $button.text(); $button.text('Sending...').prop('disabled', true); $.ajax({ url: ebPaymentRequests.ajaxurl, type: 'POST', data: { action: 'resend_payment_request', nonce: ebPaymentRequests.nonce, request_id: requestId }, success: function(response) { if (response.success) { showNotice('Payment request resent successfully!', 'success'); location.reload(); } else { showNotice('Error: ' + response.data, 'error'); } }, error: function() { showNotice('An error occurred while resending the payment request.', 'error'); }, complete: function() { $button.text(originalText).prop('disabled', false); } }); }); // Send new payment request from booking metabox $(document).on('click', '#send-new-payment-request', function() { var bookingId = $(this).data('booking-id'); // Create modal or redirect to payment requests page with booking ID pre-filled var paymentRequestsUrl = 'admin.php?page=eb-payment-requests&booking_id=' + bookingId; window.open(paymentRequestsUrl, '_blank'); }); // Auto-fill booking ID from URL parameter if (window.location.search.includes('booking_id=')) { var urlParams = new URLSearchParams(window.location.search); var bookingId = urlParams.get('booking_id'); if (bookingId) { $('#booking_id').val(bookingId).trigger('change'); $('#load-booking').click(); } } // Real-time form validation $('#payment-request-form input[required]').on('blur', function() { var $input = $(this); var value = $input.val().trim(); if (!value) { $input.css('border-color', '#dc3545'); $input.next('.validation-message').remove(); $input.after(''); } else { $input.css('border-color', ''); $input.next('.validation-message').remove(); } }); // Email validation $('#customer_email').on('blur', function() { var $input = $(this); var email = $input.val().trim(); var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (email && !emailRegex.test(email)) { $input.css('border-color', '#dc3545'); $input.next('.validation-message').remove(); $input.after(''); } else if (email) { $input.css('border-color', '#28a745'); $input.next('.validation-message').remove(); } }); // Amount validation $('#amount').on('blur', function() { var $input = $(this); var amount = parseFloat($input.val()); if (amount && (amount <= 0 || amount > 10000)) { $input.css('border-color', '#dc3545'); $input.next('.validation-message').remove(); $input.after(''); } else if (amount) { $input.css('border-color', '#28a745'); $input.next('.validation-message').remove(); } }); // Show admin notices function showNotice(message, type) { var noticeClass = type === 'success' ? 'notice-success' : 'notice-error'; var $notice = $('
' + message + '