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 + '
' + '' + '