jQuery(document).ready(function($) { 'use strict'; /** * PayPal Integration Object */ window.WPDD_PayPal = { init: function() { if (typeof paypal !== 'undefined') { this.renderPayPalButton(); } }, renderPayPalButton: function() { var $container = $('#wpdd-paypal-button'); if (!$container.length) { return; } var $form = $container.closest('form'); paypal.Buttons({ createOrder: function(data, actions) { return WPDD_PayPal.createOrder($form); }, onApprove: function(data, actions) { return WPDD_PayPal.captureOrder(data.orderID); }, onError: function(err) { console.error('PayPal Error:', err); WPDD_PayPal.showError('Payment processing failed. Please try again.'); }, onCancel: function(data) { WPDD_PayPal.showNotice('Payment was cancelled.', 'info'); } }).render('#wpdd-paypal-button'); }, createOrder: function($form) { return new Promise(function(resolve, reject) { var formData = $form.serialize(); $.ajax({ url: wpdd_paypal.ajax_url, type: 'POST', data: formData + '&action=wpdd_create_paypal_order&nonce=' + wpdd_paypal.nonce, success: function(response) { if (response.success) { resolve(response.data.orderID); } else { reject(new Error(response.data || 'Failed to create PayPal order')); } }, error: function(xhr, status, error) { reject(new Error('Network error: ' + error)); } }); }); }, captureOrder: function(orderID) { return new Promise(function(resolve, reject) { $.ajax({ url: wpdd_paypal.ajax_url, type: 'POST', data: { action: 'wpdd_capture_paypal_order', orderID: orderID, nonce: wpdd_paypal.nonce }, success: function(response) { if (response.success) { // Redirect to thank you page window.location.href = response.data.redirect_url; } else { reject(new Error(response.data || 'Failed to capture payment')); } }, error: function(xhr, status, error) { reject(new Error('Network error: ' + error)); } }); }); }, showError: function(message) { this.showNotice(message, 'error'); }, showNotice: function(message, type) { type = type || 'info'; // Remove existing notices $('.wpdd-paypal-notice').remove(); var notice = $('
' + '

' + message + '

' + '' + '
'); $('#wpdd-paypal-button').before(notice); // Auto-dismiss after 5 seconds setTimeout(function() { notice.fadeOut(function() { notice.remove(); }); }, 5000); // Manual dismiss notice.find('.wpdd-notice-dismiss').on('click', function() { notice.fadeOut(function() { notice.remove(); }); }); }, validateForm: function($form) { var isValid = true; var errors = []; // Check required fields $form.find('input[required]').each(function() { var $field = $(this); var value = $field.val().trim(); var fieldName = $field.attr('name') || $field.attr('id'); if (!value) { isValid = false; errors.push(fieldName + ' is required'); $field.addClass('error'); } else { $field.removeClass('error'); } }); // Validate email format $form.find('input[type="email"]').each(function() { var $field = $(this); var value = $field.val().trim(); var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (value && !emailRegex.test(value)) { isValid = false; errors.push('Please enter a valid email address'); $field.addClass('error'); } }); if (!isValid) { this.showError(errors.join('
')); } return isValid; }, showLoadingState: function() { $('#wpdd-paypal-button').addClass('wpdd-loading'); }, hideLoadingState: function() { $('#wpdd-paypal-button').removeClass('wpdd-loading'); }, disableForm: function($form) { $form.find('input, select, button').prop('disabled', true); }, enableForm: function($form) { $form.find('input, select, button').prop('disabled', false); } }; // Initialize PayPal integration after object definition WPDD_PayPal.init(); // Add PayPal-specific styles $('