| 1 |
( function() { |
| 2 |
const buttons = document.querySelectorAll( '.frm-connect-square-with-oauth' ); |
| 3 |
buttons.forEach( function( button ) { |
| 4 |
button.addEventListener( 'click', function( e ) { |
| 5 |
e.preventDefault(); |
| 6 |
|
| 7 |
const mode = button.dataset.mode; |
| 8 |
if ( 'test' === mode ) { |
| 9 |
frmDom.modal.maybeCreateModal( |
| 10 |
'frm_square_test_setup_modal', |
| 11 |
{ |
| 12 |
title: 'Setting up Square for Test payments', |
| 13 |
content: getSquareTestSetupModalContent() |
| 14 |
} |
| 15 |
); |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
const formData = new FormData(); |
| 20 |
formData.append( 'mode', mode ); |
| 21 |
frmDom.ajax.doJsonPost( 'square_oauth', formData ).then( |
| 22 |
function( response ) { |
| 23 |
if ( 'undefined' !== typeof response.redirect_url ) { |
| 24 |
window.location = response.redirect_url; |
| 25 |
} |
| 26 |
} |
| 27 |
); |
| 28 |
} ); |
| 29 |
} ); |
| 30 |
|
| 31 |
document.addEventListener( |
| 32 |
'click', |
| 33 |
function( event ) { |
| 34 |
if ( ! event.target.id.startsWith( 'frm_disconnect_square_' ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
event.preventDefault(); |
| 39 |
const formData = new FormData(); |
| 40 |
formData.append( 'testMode', 'test' === event.target.id.replace( 'frm_disconnect_square_', '' ) ? 1 : 0 ); |
| 41 |
frmDom.ajax.doJsonPost( 'square_disconnect', formData ).then( |
| 42 |
function( response ) { |
| 43 |
if ( 'undefined' !== typeof response.success && response.success ) { |
| 44 |
window.location.reload(); |
| 45 |
} |
| 46 |
} |
| 47 |
); |
| 48 |
} |
| 49 |
); |
| 50 |
|
| 51 |
function getSquareTestSetupModalContent() { |
| 52 |
const signUpUrl = 'https://app.squareup.com/signup/'; |
| 53 |
const confirmationTrigger = frmDom.a({ id: 'frm_confirm_square_test_modal', text: 'here', target: '_blank' }); |
| 54 |
confirmationTrigger.addEventListener( 'click', function( e ) { |
| 55 |
e.preventDefault(); |
| 56 |
const modal = document.getElementById( 'frm_square_test_setup_modal' ); |
| 57 |
|
| 58 |
const formData = new FormData(); |
| 59 |
formData.append( 'mode', 'test' ); |
| 60 |
frmDom.ajax.doJsonPost( 'square_oauth', formData ).then( |
| 61 |
function( response ) { |
| 62 |
if ( 'undefined' !== typeof response.redirect_url ) { |
| 63 |
window.location = response.redirect_url; |
| 64 |
jQuery( modal ).dialog( 'close' ); |
| 65 |
} |
| 66 |
} |
| 67 |
); |
| 68 |
} ); |
| 69 |
const content = frmDom.div( |
| 70 |
{ |
| 71 |
children: [ |
| 72 |
frmDom.tag( 'div', { className: 'frm_note_style', text: 'Important! If you skip these initial steps, you will get stuck on a white screen.' } ), |
| 73 |
frmDom.tag( 'ol', |
| 74 |
{ children: [ |
| 75 |
frmDom.span( { children: [ 'Click ', frmDom.a({ href: signUpUrl, text: 'here' }), ' to create a Square account if you do not already have one.' ] } ), |
| 76 |
frmDom.span( { children: [ 'Click ', frmDom.a({ href: 'https://developer.squareup.com/console/en/sandbox-test-accounts', text: 'here', target: '_blank' }), ' and create a Square sandbox test account.' ] } ), |
| 77 |
'Click "Square Dashboard" for the new sandbox test account. Leave the tab open and return to this page.', |
| 78 |
frmDom.span( { children: [ 'Click ', confirmationTrigger, '. You will be taken to Square to allow the required permissions for handling payments.' ] } ), |
| 79 |
].map( |
| 80 |
function( item ) { |
| 81 |
if ( 'string' === typeof item ) { |
| 82 |
return frmDom.tag( 'li', item ); |
| 83 |
} |
| 84 |
return frmDom.tag( 'li', { child: item } ); |
| 85 |
} |
| 86 |
) } |
| 87 |
) |
| 88 |
] |
| 89 |
} |
| 90 |
); |
| 91 |
content.style.padding = '0 var(--gap-md) var(--gap-md) var(--gap-md)'; |
| 92 |
return content; |
| 93 |
} |
| 94 |
}() ); |
| 95 |
|