Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/e198144299b7dd3e0a3187bd915133f5 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/e198144299b7dd3e0a3187bd915133f5 to your computer and use it in GitHub Desktop.
FunnelKit Stripe – Retry Google Pay express button
<?php
/**
* Plugin Name: FunnelKit Stripe – Retry Google Pay express button
* On checkout, if Stripe first reports Google Pay unavailable, retry that button once
* before FunnelKit Stripe hides it.
* Add with the Code Snippets plugin (Run everywhere). Works alone or with "Show express buttons when ready".
*/
add_action( 'wp_head', function () {
if ( ! function_exists( 'is_checkout' ) || ! is_checkout() ) {
return;
}
?>
<script>
( function () {
/* Shared bootstrap (same in both FunnelKit Stripe express snippets): lets each snippet hook express buttons. */
window.fkwcsExpressHooks = window.fkwcsExpressHooks || [];
if ( ! window.fkwcsExpressHooksReady ) {
window.fkwcsExpressHooksReady = true;
var wrapElements = function ( elements ) {
return new Proxy( elements, { get: function ( t, key ) {
var value = Reflect.get( t, key );
if ( 'create' !== key ) { return 'function' === typeof value ? value.bind( t ) : value; }
return function ( type, options ) {
var el = t.create( type, options );
if ( 'expressCheckout' !== type ) { return el; }
window.fkwcsExpressHooks.forEach( function ( hook ) { el = hook( el, options || {} ); } );
return el;
};
} } );
};
window.fkwcsGetStripe = function ( pubKey, options ) {
window.fkwcsStripeInstances = window.fkwcsStripeInstances || {};
if ( ! window.fkwcsStripeInstances[ pubKey ] ) {
var stripe = Stripe( pubKey, options );
window.fkwcsStripeInstances[ pubKey ] = new Proxy( stripe, { get: function ( t, key ) {
var value = Reflect.get( t, key );
if ( 'elements' === key ) { return function () { return wrapElements( t.elements.apply( t, arguments ) ); }; }
return 'function' === typeof value ? value.bind( t ) : value;
} } );
}
return window.fkwcsStripeInstances[ pubKey ];
};
}
var RETRY_DELAY = 1500;
window.fkwcsExpressHooks.push( function ( el, options ) {
if ( ! options.paymentMethods || 'always' !== options.paymentMethods.googlePay ) {
return el;
}
var readyHandlers = [], target = null, retried = false;
el.on( 'ready', function ( data ) {
var methods = data && data.availablePaymentMethods;
if ( ! retried && target && ! ( methods && methods.googlePay ) ) {
retried = true;
try { el.unmount(); } catch ( e ) {}
setTimeout( function () { el.mount( target ); }, RETRY_DELAY );
return;
}
readyHandlers.forEach( function ( handler ) { handler( data ); } );
} );
return new Proxy( el, { get: function ( t, key ) {
if ( 'on' === key ) {
return function ( event, handler ) {
if ( 'ready' === event ) { readyHandlers.push( handler ); return t; }
return t.on( event, handler );
};
}
if ( 'mount' === key ) {
return function ( selector ) { target = selector; return t.mount( selector ); };
}
var value = Reflect.get( t, key );
return 'function' === typeof value ? value.bind( t ) : value;
} } );
} );
} )();
</script>
<?php
}, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment