@@ -26,17 +26,35 @@ jQuery(
2626 form_data_timer : null ,
2727 checkout_form_identifier : 'form.checkout' ,
2828 checkout_payment_area : '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ,
29+ $order_review : $ ( '#order_review' ) ,
30+ $checkout_form : $ ( 'form.checkout' ) ,
31+ $order_button : $ ( '#place_order' ) ,
32+
33+ /**
34+ * This function gets interesting info for testing
35+ */
36+ info : function ( ) {
37+ var versions = window . postfinancecheckout_js_params . versions || { } ;
38+ var info = [
39+ { library : 'integration' , version : window . postfinancecheckout_js_params . integration } ,
40+ { library : 'jQuery' , version : $ ( ) . jquery } ,
41+ { library : 'wordpress' , version : versions . wordpress || null } ,
42+ { library : 'woocommerce' , version : versions . woocommerce || null } ,
43+ { library : 'woo-postfinancecheckout' , version : versions . postfinancecheckout || null } ,
44+ ] ;
45+ console . table ( info , [ ] ) ;
46+ } ,
2947
3048 init : function ( ) {
3149 // Payment methods.
32- $ ( 'form.checkout' ) . off ( 'click.woo-postfinancecheckout' ) . on (
50+ this . $checkout_form . off ( 'click.woo-postfinancecheckout' ) . on (
3351 'click.woo-postfinancecheckout' ,
3452 'input[name="payment_method"]' ,
3553 { self : this } ,
3654 this . payment_method_click
3755 ) ;
3856
39- $ ( 'form.checkout' ) . off ( 'button#place_order' ) . on (
57+ this . $checkout_form . off ( 'button#place_order' ) . on (
4058 'button#place_order' ,
4159 { self : this } ,
4260 function ( event ) {
@@ -48,7 +66,8 @@ jQuery(
4866
4967 if ( $ ( document . body ) . hasClass ( 'woocommerce-order-pay' ) ) {
5068 this . checkout_form_identifier = '#order_review' ;
51- $ ( '#order_review' ) . off ( 'click.woo-postfinancecheckout' ) . on (
69+ this . $checkout_form = $ ( '#order_review' ) ;
70+ this . $order_review . off ( 'click.woo-postfinancecheckout' ) . on (
5271 'click.woo-postfinancecheckout' ,
5372 'input[name="payment_method"]' ,
5473 {
@@ -58,13 +77,14 @@ jQuery(
5877 ) ;
5978 }
6079 this . register_ajax_prefilter ( ) ;
80+ this . register_window_fetch_prefilter ( ) ;
6181 this . form_data_timer = setInterval ( this . check_form_data_change . bind ( this ) , 4000 ) ;
62- $ ( this . checkout_form_identifier ) . find ( 'input[name="payment_method"]:checked' ) . trigger ( "click" ) ;
82+ this . $checkout_form . find ( 'input[name="payment_method"]:checked' ) . trigger ( "click" ) ;
6383 window . wc_postfinancecheckout_checkout = this ;
6484 } ,
6585
6686 check_form_data_change : function ( ) {
67- var $required_inputs = $ ( this . checkout_form_identifier ) . find ( '.address-field.validate-required' ) . find ( 'input, select' ) ;
87+ var $required_inputs = this . $checkout_form . find ( '.address-field.validate-required' ) . find ( 'input, select' ) ;
6888 var current = '' ;
6989 var complete = true ;
7090 if ( $required_inputs . length ) {
@@ -81,7 +101,7 @@ jQuery(
81101 }
82102 ) ;
83103 // no updates on invalid fields.
84- if ( $ ( self . checkout_form_identifier + ' .woocommerce-invalid' ) . length ) {
104+ if ( $ ( this . checkout_form_identifier + ' .woocommerce-invalid' ) . length ) {
85105 complete = false ;
86106 return false ;
87107 }
@@ -128,15 +148,16 @@ jQuery(
128148 var description = configuration . data ( "description-available" ) ;
129149
130150 // Hide iFrame by moving it (display:none leads to issues).
131- var item = $ ( this . checkout_form_identifier )
151+ var item = this . $checkout_form
132152 . find ( 'input[name="payment_method"]:checked' )
133153 . closest ( 'li.wc_payment_method' )
134154 . find ( 'div.payment_box' ) ;
155+
135156 var form = item . find ( '#payment-form-' + method_id ) ;
136157 form . css ( 'display' , '' ) ;
137158
138- var required_inputs = $ ( this . checkout_form_identifier ) . find ( '.address-field.validate-required:visible' ) ;
139- var has_full_address = true ;
159+ var required_inputs = this . $checkout_form . find ( '.address-field.validate-required:visible' ) ;
160+ var has_full_address = true ;
140161
141162 if ( required_inputs . length ) {
142163 required_inputs . each (
@@ -176,19 +197,18 @@ jQuery(
176197 } ,
177198
178199 enable_place_order_button : function ( ) {
179- var order_button = $ ( '#place_order' ) ;
180- order_button . removeAttr ( 'disabled' ) ;
181- order_button . removeClass ( 'postfinancecheckout-disabled' ) ;
200+ this . $order_button . removeAttr ( 'disabled' ) ;
201+ this . $order_button . removeClass ( 'postfinancecheckout-disabled' ) ;
182202 } ,
183203
184204 disable_place_order_button : function ( ) {
185- var order_button = $ ( '#place_order' ) ;
186- order_button . prop ( 'disabled' , true ) ;
187- order_button . addClass ( 'postfinancecheckout-disabled' ) ;
205+ this . $order_button . prop ( 'disabled' , true ) ;
206+ this . $order_button . addClass ( 'postfinancecheckout-disabled' ) ;
188207 } ,
189208
190209 /**
191210 * This function handle the success function of Place Order in WooCommerce
211+ * @version <=7.4.1
192212 */
193213 register_ajax_prefilter : function ( ) {
194214 var self = this ;
@@ -236,15 +256,43 @@ jQuery(
236256 }
237257 }
238258 ) ;
259+ } ,
260+
261+ /**
262+ * This function handle the success function of Place Order in WooCommerce
263+ * @version >=7.5.0
264+ */
265+ register_window_fetch_prefilter : function ( ) {
266+ var { fetch : origFetch } = window ;
267+ var self = this ;
239268
269+ window . fetch = async ( url , options ) => {
270+ var response = await origFetch ( url , options ) ;
271+
272+ /* work with the cloned response in a separate promise chain -- could use the same chain with `await`. */
273+ if ( url === wc_checkout_params . checkout_url && self . is_supported_method ( self . get_selected_payment_method ( ) ) ) {
274+ response
275+ . clone ( )
276+ . json ( )
277+ . then ( body => {
278+ if ( body . result !== undefined && 'success' === body . result ) {
279+ self . process_order_created ( body ) ;
280+ }
281+ } )
282+ . catch ( err => console . error ( err ) ) ;
283+ }
284+
285+ /* the original response can be resolved unmodified: */
286+ return response ;
287+ } ;
240288 } ,
241289
242290 is_supported_method : function ( method_id ) {
243291 return method_id && ( method_id . indexOf ( 'postfinancecheckout_' ) === 0 ) ;
244292 } ,
245293
246294 get_selected_payment_method : function ( ) {
247- return $ ( this . checkout_form_identifier ) . find ( 'input[name="payment_method"]:checked' ) . val ( ) ;
295+ return this . $checkout_form . find ( 'input[name="payment_method"]:checked' ) . val ( ) ;
248296 } ,
249297
250298 register_method : function ( method_id , configuration_id , container_id ) {
@@ -261,7 +309,7 @@ jQuery(
261309 }
262310 var self = this ;
263311
264- $ ( this . checkout_form_identifier ) . block (
312+ this . $checkout_form . block (
265313 {
266314 message : null ,
267315 overlayCSS : {
@@ -296,7 +344,7 @@ jQuery(
296344
297345 this . payment_methods [ method_id ] . handler . setInitializeCallback (
298346 function ( ) {
299- $ ( self . checkout_form_identifier ) . unblock ( ) ;
347+ self . $checkout_form . unblock ( ) ;
300348 }
301349 ) ;
302350
@@ -322,16 +370,16 @@ jQuery(
322370 this . payment_methods [ method_id ] . container_id = container_id ;
323371
324372 if ( this . checkout_form_identifier === '#order_review' ) {
325- $ ( this . checkout_form_identifier ) . off ( 'submit.postfinancecheckout' ) . on (
373+ this . $checkout_form . off ( 'submit.postfinancecheckout' )
374+ . on (
326375 'submit.postfinancecheckout' ,
327376 function ( ) {
328377 var method_id = self . get_selected_payment_method ( ) ;
329378 return self . process_submit ( method_id ) ;
330379 }
331380 ) ;
332381 } else {
333- var form = $ ( this . checkout_form_identifier ) ;
334- form . off ( 'checkout_place_order_' + method_id + '.postfinancecheckout' )
382+ this . $checkout_form . off ( 'checkout_place_order_' + method_id + '.postfinancecheckout' )
335383 . on (
336384 'checkout_place_order_' + method_id + '.postfinancecheckout' ,
337385 function ( ) {
@@ -346,8 +394,8 @@ jQuery(
346394 return true ;
347395 }
348396
349- var form = $ ( this . checkout_form_identifier ) ;
350- var required_inputs = $ ( this . checkout_form_identifier ) . find ( '.address-field.validate-required:visible' ) ;
397+ var form = this . $checkout_form ;
398+ var required_inputs = this . $checkout_form . find ( '.address-field.validate-required:visible' ) ;
351399 var has_full_address = true ;
352400
353401 if ( required_inputs . length ) {
@@ -361,7 +409,7 @@ jQuery(
361409 ) ;
362410 }
363411 if ( ! has_full_address ) {
364- $ ( this . checkout_form_identifier ) . trigger ( 'validate' ) ;
412+ this . $checkout_form . trigger ( 'validate' ) ;
365413 this . submit_error ( postfinancecheckout_js_params . i18n_not_complete ) ;
366414 return false ;
367415 }
@@ -382,12 +430,12 @@ jQuery(
382430 } else {
383431 if ( this . checkout_form_identifier === '#order_review' ) {
384432
385- self = this ;
433+ var self = this ;
386434 $ . ajax (
387435 {
388436 type : 'POST' ,
389437 url : window . location . href ,
390- data : form . serialize ( ) ,
438+ data : new URLSearchParams ( form [ 0 ] ) . toString ( ) ,
391439 dataType : 'json' ,
392440 success : function ( result ) {
393441 self . validated = false ;
@@ -413,7 +461,7 @@ jQuery(
413461 var self = this ;
414462 // handle lightbox integration.
415463 if ( postfinancecheckout_js_params . integration && postfinancecheckout_js_params . integration === self . integrations . LIGHTBOX ) {
416- var required_inputs = $ ( self . checkout_form_identifier ) . find ( '.validate-required:visible' ) ;
464+ var required_inputs = self . $checkout_form . find ( '.validate-required:visible' ) ;
417465 var has_full_address = true ;
418466 if ( required_inputs . length ) {
419467 required_inputs . each (
@@ -431,7 +479,7 @@ jQuery(
431479 ) ;
432480 }
433481 if ( ! has_full_address ) {
434- $ ( self . checkout_form_identifier ) . trigger ( 'validate' ) ;
482+ self . $checkout_form . trigger ( 'validate' ) ;
435483 self . submit_error ( postfinancecheckout_js_params . i18n_not_complete ) ;
436484 return false ;
437485 }
@@ -457,11 +505,10 @@ jQuery(
457505 process_validation : function ( method_id , validation_result ) {
458506 if ( validation_result . success ) {
459507 this . validated = true ;
460- $ ( this . checkout_form_identifier ) . submit ( ) ;
508+ this . $checkout_form . submit ( ) ;
461509 return true ;
462510 } else {
463- var form = $ ( this . checkout_form_identifier ) ;
464- form . unblock ( ) ;
511+ this . $checkout_form . unblock ( ) ;
465512 this . form_data_timer = setInterval ( this . check_form_data_change . bind ( this ) , 3000 ) ;
466513 if ( validation_result . errors ) {
467514 this . submit_error ( validation_result . errors ) ;
@@ -481,14 +528,15 @@ jQuery(
481528
482529 // We emulate the woocommerce submit_error function, as it is not callable from outside.
483530 submit_error : function ( error_message ) {
531+ var self = this ;
484532 var formatted_message = '<div class="woocommerce-error">' + this . format_error_messages ( error_message ) + '</div>' ;
485- $ ( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ) . remove ( ) ;
486- $ ( this . checkout_form_identifier ) . prepend ( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + formatted_message + '</div>' ) ;
487- $ ( this . checkout_form_identifier ) . removeClass ( 'processing' ) . unblock ( ) ;
488- $ ( this . checkout_form_identifier ) . find ( '.input-text, select, input:checkbox' ) . trigger ( 'validate' ) . blur ( ) ;
533+ $ ( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ) . remove ( ) ;
534+ this . $checkout_form . prepend ( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + formatted_message + '</div>' ) ;
535+ this . $checkout_form . removeClass ( 'processing' ) . unblock ( ) ;
536+ this . $checkout_form . find ( '.input-text, select, input:checkbox' ) . trigger ( 'validate' ) . blur ( ) ;
489537 $ ( 'html, body' ) . animate (
490538 {
491- scrollTop : ( $ ( this . checkout_form_identifier ) . offset ( ) . top - 100 )
539+ scrollTop : ( self . $checkout_form . offset ( ) . top - 100 )
492540 } ,
493541 1000
494542 ) ;
0 commit comments