jQuery(function($){ $(document).on('click', '.show-instruction', function(){ $('.global-paypal-instruction').slideToggle(); $(this).text(function(i, text){ return text === 'Show Instruction' ? 'Hide Instruction' : 'Show Instruction'; }); }); $(document).on('click', '.formipay-connect-paypal', function(e) { e.preventDefault(); $.ajax({ url: formipay_admin_setting.ajax_url, type: 'POST', data: { action: 'formipay_oneclick_connect', nonce: formipay_admin_setting.nonce }, beforeSend: function() { $('#connect-status').html('

Initiating PayPal connection...

'); }, success: function(response) { if (response.success) { // Open PayPal auth in popup const popup = window.open( response.data.auth_url, 'paypal_oauth', 'width=600,height=700,scrollbars=yes' ); // Listen for message from popup window.addEventListener('message', function(event) { if (event.data.type === 'formipay-paypal-connect') { if (event.data.success) { $('#connect-status').html(`

${event.data.message}

Webhook ID: ${event.data.webhook_id}

`); } else { $('#connect-status').html(`

${event.data.message}

`); } } }); } else { $('#connect-status').html(`

Connection failed: ${response.data}

`); } }, error: function() { $('#connect-status').html(`

Connection failed: Server error

`); } }); }); });