Fix GooglePay general fixes / improvements

This commit is contained in:
Pedro Silva 2023-09-06 11:55:26 +01:00
parent d6a564f24f
commit 318217acb9
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
9 changed files with 85 additions and 41 deletions

View file

@ -29,7 +29,8 @@ return function ( string $root_dir ): iterable {
( require "$modules_dir/ppcp-blocks/module.php" )(),
);
if ( apply_filters(
'inpsyde_feature_flags_woocommerce_paypal_payments_googlepay_enabled',
//phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores
'woocommerce.feature-flags.woocommerce_paypal_payments.googlepay_enabled',
getenv( 'PCP_GOOGLEPAY_ENABLED' ) === '1'
) ) {
$modules[] = ( require "$modules_dir/ppcp-googlepay/module.php" )();

View file

@ -7,8 +7,7 @@ import {keysToCamelCase} from "./Utils";
// This component may be used by multiple modules. This assures that options are shared between all instances.
let options = window.ppcpWidgetBuilder = window.ppcpWidgetBuilder || {
isLoading: false,
onLoadedCallbacks: [],
loadingWaitTime: 5000 // 5 seconds
onLoadedCallbacks: []
};
export const loadPaypalScript = (config, onLoaded) => {
@ -27,13 +26,6 @@ export const loadPaypalScript = (config, onLoaded) => {
}
options.isLoading = true;
// Arm a timeout so the module isn't locked on isLoading state on failure.
let loadingTimeout = setTimeout(() => {
console.error('Failed to load PayPal script.');
options.isLoading = false;
options.onLoadedCallbacks = [];
}, options.loadingWaitTime);
// Callback to be called once the PayPal script is loaded.
const callback = (paypal) => {
widgetBuilder.setPaypal(paypal);
@ -44,7 +36,6 @@ export const loadPaypalScript = (config, onLoaded) => {
options.isLoading = false;
options.onLoadedCallbacks = [];
clearTimeout(loadingTimeout);
}
// Build the PayPal script options.

View file

@ -462,8 +462,7 @@ class SmartButton implements SmartButtonInterface {
return;
}
$this->button_renderer( PayPalGateway::ID );
do_action( 'woocommerce_paypal_payments_single_product_button_render' );
$this->button_renderer( PayPalGateway::ID, 'woocommerce_paypal_payments_single_product_button_render' );
},
31
);
@ -495,18 +494,16 @@ class SmartButton implements SmartButtonInterface {
add_action(
$this->pay_order_renderer_hook(),
function (): void {
$this->button_renderer( PayPalGateway::ID );
$this->button_renderer( PayPalGateway::ID, 'woocommerce_paypal_payments_payorder_button_render' );
$this->button_renderer( CardButtonGateway::ID );
do_action( 'woocommerce_paypal_payments_payorder_button_render' );
},
20
);
add_action(
$this->checkout_button_renderer_hook(),
function (): void {
$this->button_renderer( PayPalGateway::ID );
$this->button_renderer( PayPalGateway::ID, 'woocommerce_paypal_payments_checkout_button_render' );
$this->button_renderer( CardButtonGateway::ID );
do_action( 'woocommerce_paypal_payments_checkout_button_render' );
}
);
@ -518,8 +515,7 @@ class SmartButton implements SmartButtonInterface {
return;
}
$this->button_renderer( PayPalGateway::ID );
do_action( 'woocommerce_paypal_payments_cart_button_render' );
$this->button_renderer( PayPalGateway::ID, 'woocommerce_paypal_payments_cart_button_render' );
},
20
);
@ -623,8 +619,9 @@ class SmartButton implements SmartButtonInterface {
* Renders the HTML for the buttons.
*
* @param string $gateway_id The gateway ID, like 'ppcp-gateway'.
* @param string|null $action_name The action name to be called.
*/
public function button_renderer( string $gateway_id ) {
public function button_renderer( string $gateway_id, string $action_name = null ) {
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
@ -634,7 +631,14 @@ class SmartButton implements SmartButtonInterface {
// The wrapper is needed for the loading spinner,
// otherwise jQuery block() prevents buttons rendering.
echo '<div class="ppc-button-wrapper"><div id="ppc-button-' . esc_attr( $gateway_id ) . '"></div></div>';
echo '<div class="ppc-button-wrapper">';
echo '<div id="ppc-button-' . esc_attr( $gateway_id ) . '"></div>';
if ( null !== $action_name ) {
do_action( $action_name );
}
echo '</div>';
}
/**

View file

@ -44,7 +44,11 @@ return array(
'handler' => 'SubElementsHandler',
'options' => array(
'values' => array( '1' ),
'elements' => array( '#field-googlepay_button_color', '#field-googlepay_button_type' ),
'elements' => array(
'#field-googlepay_button_color',
'#field-googlepay_button_type',
'#field-googlepay_button_shipping_enabled',
),
),
),
)
@ -74,6 +78,15 @@ return array(
'gateway' => 'paypal',
'requirements' => array(),
),
'googlepay_button_shipping_enabled' => array(
'title' => str_repeat( '&nbsp;', 6 ) . __( 'Shipping Callback', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'label' => __( 'Enable Google Pay shipping callback', 'woocommerce-paypal-payments' ),
'default' => 'no',
'screens' => array( State::STATE_ONBOARDED ),
'gateway' => 'paypal',
'requirements' => array(),
),
)
);
},

View file

@ -1,6 +1,7 @@
.ppcp-button-googlepay {
margin: 5px 0;
margin: 7px 0;
overflow: hidden;
min-height: 40px;
height: 45px;
&.ppcp-button-pill {
@ -15,14 +16,31 @@
.woocommerce-checkout {
.ppcp-button-googlepay {
margin: 0;
border-radius: 18px;
height: 38px;
margin-top: 0;
}
}
.wp-block-woocommerce-cart, .wp-block-woocommerce-checkout {
.ppcp-has-googlepay-block {
.wp-block-woocommerce-checkout {
.ppcp-button-googlepay {
margin: 0;
height: 40px;
}
}
.wp-block-woocommerce-cart {
.ppcp-button-googlepay {
margin: 0;
height: 40px;
}
/* Workaround for blocks grid */
.wc-block-components-express-payment__event-buttons {
display: block;
li[id*="express-payment-method-ppcp-"] {
padding-bottom: 0;
}
}
}
}

View file

@ -126,6 +126,8 @@ class GooglepayButton {
const paymentDataRequest = await this.paymentDataRequest();
console.log('[GooglePayButton] onButtonClick: paymentDataRequest', paymentDataRequest, this.context);
window.ppcpFundingSource = 'googlepay'; // TODO : do this on another place like on create order
this.paymentsClient.loadPaymentData(paymentDataRequest);
}

View file

@ -24,6 +24,13 @@ const GooglePayComponent = () => {
manager.init();
};
useEffect(() => {
const bodyClass = 'ppcp-has-googlepay-block';
if (!document.body.classList.contains(bodyClass)) {
document.body.classList.add(bodyClass);
}
}, []);
useEffect(() => {
// Load GooglePay SDK
loadCustomScript({ url: buttonConfig.sdk_url }).then(() => {
@ -44,7 +51,7 @@ const GooglePayComponent = () => {
}, [paypalLoaded, googlePayLoaded]);
return (
<div id={buttonConfig.button.wrapper.replace('#', '')}></div>
<div id={buttonConfig.button.wrapper.replace('#', '')} className="ppcp-button-googlepay"></div>
);
}

View file

@ -138,7 +138,7 @@ class Button implements ButtonInterface {
*
* @psalm-suppress MissingClosureParamType
*/
private function add_onboarding_options( $options ): string {
public function add_onboarding_options( $options ): string {
$checked = '';
try {
$onboard_with_apple = $this->settings->get( 'ppcp-onboarding-apple' );
@ -162,7 +162,7 @@ class Button implements ButtonInterface {
* @param array $data The referrals data.
* @return array
*/
private function add_partner_referrals_data( array $data ): array {
public function add_partner_referrals_data( array $data ): array {
try {
$onboard_with_google = $this->settings->get( 'ppcp-onboarding-google' );
if ( ! wc_string_to_bool( $onboard_with_google ) ) {
@ -326,11 +326,9 @@ class Button implements ButtonInterface {
*/
private function googlepay_button(): void {
?>
<div class="ppc-button-wrapper">
<div id="ppc-button-googlepay-container" class="ppcp-button-googlepay">
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
</div>
</div>
<?php
}

View file

@ -939,6 +939,13 @@ return array(
'paylater' => _x( 'Pay Later', 'Name of payment method', 'woocommerce-paypal-payments' ),
);
},
'wcgateway.extra-funding-sources' => static function( ContainerInterface $container ): array {
return array(
'googlepay' => _x( 'Google Pay', 'Name of payment method', 'woocommerce-paypal-payments' ),
);
},
/**
* The sources that do not cause issues about redirecting (on mobile, ...) and sometimes not returning back.
*/
@ -1034,7 +1041,10 @@ return array(
'wcgateway.funding-source.renderer' => function ( ContainerInterface $container ) : FundingSourceRenderer {
return new FundingSourceRenderer(
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.all-funding-sources' )
array_merge(
$container->get( 'wcgateway.all-funding-sources' ),
$container->get( 'wcgateway.extra-funding-sources' )
)
);
},