| 1 |
( function() { |
| 2 |
// Use event delegation so Reconnect buttons rendered after AJAX seller-status |
| 3 |
// responses also trigger the OAuth flow. |
| 4 |
document.addEventListener( 'click', function( e ) { |
| 5 |
const button = e.target.closest( '.frm-connect-paypal-with-oauth' ); |
| 6 |
if ( ! button ) { |
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
e.preventDefault(); |
| 11 |
|
| 12 |
const { mode, reconnect } = button.dataset; |
| 13 |
const formData = new FormData(); |
| 14 |
formData.append( 'mode', mode ); |
| 15 |
if ( reconnect ) { |
| 16 |
formData.append( 'reconnect', reconnect ); |
| 17 |
} |
| 18 |
frmDom.ajax.doJsonPost( 'paypal_oauth', formData ).then( |
| 19 |
function( response ) { |
| 20 |
if ( response.redirect_url !== undefined ) { |
| 21 |
window.location = response.redirect_url; |
| 22 |
} |
| 23 |
} |
| 24 |
).catch( |
| 25 |
function( error ) { |
| 26 |
console.error( 'PayPal OAuth request failed:', error ); |
| 27 |
} |
| 28 |
); |
| 29 |
} ); |
| 30 |
|
| 31 |
document.addEventListener( |
| 32 |
'click', |
| 33 |
function( event ) { |
| 34 |
if ( ! event.target.id.startsWith( 'frm_disconnect_paypal_' ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
event.preventDefault(); |
| 39 |
const formData = new FormData(); |
| 40 |
formData.append( 'testMode', 'test' === event.target.id.replace( 'frm_disconnect_paypal_', '' ) ? 1 : 0 ); |
| 41 |
frmDom.ajax.doJsonPost( 'paypal_disconnect', formData ).then( |
| 42 |
function( response ) { |
| 43 |
if ( response.success ) { |
| 44 |
window.location.reload(); |
| 45 |
} |
| 46 |
} |
| 47 |
); |
| 48 |
} |
| 49 |
); |
| 50 |
|
| 51 |
document.querySelectorAll( '.frm_paypal_seller_status_placeholder' ).forEach( |
| 52 |
function( placeholder ) { |
| 53 |
const { mode } = placeholder.dataset; |
| 54 |
const interval = setInterval( |
| 55 |
function() { |
| 56 |
if ( placeholder.offsetParent === null ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
clearInterval( interval ); |
| 61 |
|
| 62 |
const formData = new FormData(); |
| 63 |
formData.append( 'testMode', 'test' === mode ? 1 : 0 ); |
| 64 |
frmDom.ajax.doJsonPost( 'paypal_render_seller_status', formData ) |
| 65 |
.then( |
| 66 |
function( sellerStatus ) { |
| 67 |
placeholder.innerHTML = sellerStatus; |
| 68 |
} |
| 69 |
).catch( |
| 70 |
function( error ) { |
| 71 |
if ( 'string' === typeof error ) { |
| 72 |
placeholder.innerHTML = error; |
| 73 |
} |
| 74 |
|
| 75 |
clearInterval( interval ); |
| 76 |
} |
| 77 |
); |
| 78 |
}, |
| 79 |
100 |
| 80 |
); |
| 81 |
} |
| 82 |
); |
| 83 |
}() ); |
| 84 |
|