1+ /**
2+ * SumUp Terminal Admin JavaScript
3+ */
4+
5+ // Make sure the functions are available globally
6+ window . sumupAdmin = {
7+
8+ init : function ( ) {
9+ console . log ( 'SumUp Admin JS initialized' ) ;
10+
11+ // Make sure ajaxurl is available
12+ if ( typeof ajaxurl === 'undefined' ) {
13+ window . ajaxurl = sumupAdminData . ajaxUrl ;
14+ }
15+
16+ // Test that everything is working
17+ console . log ( 'AJAX URL:' , window . ajaxurl ) ;
18+ console . log ( 'Nonce:' , sumupAdminData . nonce ) ;
19+ } ,
20+
21+ testJS : function ( ) {
22+ alert ( 'JavaScript is working! AJAX URL: ' + window . ajaxurl ) ;
23+ console . log ( 'Test JS function called successfully' ) ;
24+ } ,
25+
26+ pairReader : function ( ) {
27+ console . log ( 'sumupPairReader called' ) ;
28+ const pairingCode = document . getElementById ( 'sumup-pairing-code' ) ;
29+ const resultDiv = document . getElementById ( 'sumup-pair-result' ) ;
30+
31+ if ( ! pairingCode || ! resultDiv ) {
32+ console . error ( 'Required elements not found' ) ;
33+ return ;
34+ }
35+
36+ const code = pairingCode . value . trim ( ) . toUpperCase ( ) ;
37+
38+ if ( ! code ) {
39+ resultDiv . innerHTML = '<div style="color: #d63638; margin-top: 10px;">Please enter a pairing code.</div>' ;
40+ return ;
41+ }
42+
43+ resultDiv . innerHTML = '<div style="color: #0073aa; margin-top: 10px;">Pairing reader...</div>' ;
44+ console . log ( 'Making AJAX request to:' , window . ajaxurl ) ;
45+
46+ fetch ( window . ajaxurl , {
47+ method : 'POST' ,
48+ headers : {
49+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
50+ } ,
51+ body : new URLSearchParams ( {
52+ action : 'sumup_pair_reader' ,
53+ pairing_code : code ,
54+ nonce : sumupAdminData . nonce
55+ } )
56+ } )
57+ . then ( response => {
58+ console . log ( 'Response status:' , response . status ) ;
59+ if ( ! response . ok ) {
60+ throw new Error ( 'HTTP ' + response . status ) ;
61+ }
62+ return response . json ( ) ;
63+ } )
64+ . then ( data => {
65+ console . log ( 'Response data:' , data ) ;
66+ if ( data . success ) {
67+ resultDiv . innerHTML = '<div style="color: #00a32a; margin-top: 10px;">✓ Reader paired successfully! Refreshing page...</div>' ;
68+ setTimeout ( ( ) => location . reload ( ) , 2000 ) ;
69+ } else {
70+ resultDiv . innerHTML = '<div style="color: #d63638; margin-top: 10px;">✗ ' + ( data . data || 'Pairing failed' ) + '</div>' ;
71+ }
72+ } )
73+ . catch ( error => {
74+ console . error ( 'AJAX Error:' , error ) ;
75+ resultDiv . innerHTML = '<div style="color: #d63638; margin-top: 10px;">✗ Network error: ' + error . message + '</div>' ;
76+ } ) ;
77+ } ,
78+
79+ unpairReader : function ( readerId ) {
80+ console . log ( 'sumupUnpairReader called with ID:' , readerId ) ;
81+
82+ if ( ! confirm ( sumupAdminData . strings . confirmUnpair ) ) {
83+ return ;
84+ }
85+
86+ fetch ( window . ajaxurl , {
87+ method : 'POST' ,
88+ headers : {
89+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
90+ } ,
91+ body : new URLSearchParams ( {
92+ action : 'sumup_unpair_reader' ,
93+ reader_id : readerId ,
94+ nonce : sumupAdminData . nonce
95+ } )
96+ } )
97+ . then ( response => response . json ( ) )
98+ . then ( data => {
99+ if ( data . success ) {
100+ alert ( sumupAdminData . strings . unpairSuccess ) ;
101+ location . reload ( ) ;
102+ } else {
103+ alert ( sumupAdminData . strings . unpairFailed + ' ' + ( data . data || sumupAdminData . strings . unknownError ) ) ;
104+ }
105+ } )
106+ . catch ( error => {
107+ console . error ( 'AJAX Error:' , error ) ;
108+ alert ( sumupAdminData . strings . networkError ) ;
109+ } ) ;
110+ } ,
111+
112+
113+ } ;
114+
115+ // Event delegation for button clicks
116+ function setupEventListeners ( ) {
117+ // Use event delegation to handle dynamically added buttons
118+ document . addEventListener ( 'click' , function ( event ) {
119+ // Check if the clicked element has the sumup-btn class
120+ if ( event . target . classList . contains ( 'sumup-btn' ) ) {
121+ event . preventDefault ( ) ;
122+
123+ const action = event . target . getAttribute ( 'data-action' ) ;
124+ console . log ( 'Button clicked with action:' , action ) ;
125+
126+ switch ( action ) {
127+ case 'test-js' :
128+ window . sumupAdmin . testJS ( ) ;
129+ break ;
130+ case 'pair-reader' :
131+ window . sumupAdmin . pairReader ( ) ;
132+ break ;
133+ case 'unpair-reader' :
134+ const readerId = event . target . getAttribute ( 'data-reader-id' ) ;
135+ if ( readerId ) {
136+ window . sumupAdmin . unpairReader ( readerId ) ;
137+ }
138+ break ;
139+ default :
140+ console . warn ( 'Unknown action:' , action ) ;
141+ }
142+ }
143+ } ) ;
144+ }
145+
146+ // Initialize when DOM is ready
147+ function initSumupAdmin ( ) {
148+ window . sumupAdmin . init ( ) ;
149+ setupEventListeners ( ) ;
150+ }
151+
152+ // Initialize based on DOM state
153+ if ( document . readyState === 'loading' ) {
154+ document . addEventListener ( 'DOMContentLoaded' , initSumupAdmin ) ;
155+ } else {
156+ initSumupAdmin ( ) ;
157+ }
158+
159+ // Legacy global functions for backward compatibility (if needed)
160+ window . sumupTestJS = function ( ) {
161+ window . sumupAdmin . testJS ( ) ;
162+ } ;
163+
164+ window . sumupPairReader = function ( ) {
165+ window . sumupAdmin . pairReader ( ) ;
166+ } ;
167+
168+ window . sumupUnpairReader = function ( readerId ) {
169+ window . sumupAdmin . unpairReader ( readerId ) ;
170+ } ;
171+
172+
0 commit comments