jQuery( function ( $ ) { 'use strict'; var type = recaptcha_data.type; var site_key = recaptcha_data.site_key; // ------------------------------------------------------------------------- // v3 — hidden input mein token generate karo // ------------------------------------------------------------------------- function sa_ofp_generate_v3_token() { if ( jQuery( '#g-recaptcha-response' ).length === 0 ) return; if ( typeof grecaptcha === 'undefined' ) return; grecaptcha.ready( function () { grecaptcha.execute( site_key, { action: 'woocommerce_checkout' } ).then( function ( token ) { jQuery( '#g-recaptcha-response' ).val( token ); } ); } ); } // ------------------------------------------------------------------------- // invisible — form submit pe execute karo, token milne pe form submit karo // ------------------------------------------------------------------------- function sa_ofp_setup_invisible() { if ( typeof grecaptcha === 'undefined' ) return; jQuery( document.body ).on( 'checkout_place_order', function () { var response = grecaptcha.getResponse(); if ( response && response.length > 0 ) { return true; } grecaptcha.execute(); return false; } ); } // ------------------------------------------------------------------------- // v2 checkbox — submit se pehle response check karo // ------------------------------------------------------------------------- function sa_ofp_setup_v2_checkbox() { jQuery( document.body ).on( 'checkout_place_order', function () { if ( typeof grecaptcha === 'undefined' ) return true; var response = grecaptcha.getResponse(); // console.log(response); return false; if ( ! response || response.length === 0 ) { alert( 'Please complete the reCAPTCHA verification.' ); return false; } return true; } ); } // ------------------------------------------------------------------------- // Init // ------------------------------------------------------------------------- if ( 'v3' === type ) { jQuery( document ).ready( function () { setTimeout( sa_ofp_generate_v3_token, 1000 ); } ); // Checkout update hone k baad token refresh karo jQuery( document.body ).on( 'updated_checkout', sa_ofp_generate_v3_token ); } else if ( 'invisible' === type ) { jQuery( document ).ready( sa_ofp_setup_invisible ); } else if ( 'v2' === type ) { jQuery( document ).ready( sa_ofp_setup_v2_checkbox ); } } ); window.sa_ofp_on_invisible_captcha_success = function ( token ) { jQuery( '#g-recaptcha-response' ).val( token ); jQuery( '[name="woocommerce_checkout_place_order"]' ).trigger( 'click' ); };