mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Add ButtonModuleWatcher
Adjustments for APM support
This commit is contained in:
parent
e7e5ebde4e
commit
93afeeba68
6 changed files with 59 additions and 14 deletions
|
@ -2,6 +2,7 @@ import {useEffect, useState} from '@wordpress/element';
|
||||||
import {registerExpressPaymentMethod, registerPaymentMethod} from '@woocommerce/blocks-registry';
|
import {registerExpressPaymentMethod, registerPaymentMethod} from '@woocommerce/blocks-registry';
|
||||||
import {paypalAddressToWc, paypalOrderToWcAddresses} from "./Helper/Address";
|
import {paypalAddressToWc, paypalOrderToWcAddresses} from "./Helper/Address";
|
||||||
import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'
|
import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'
|
||||||
|
import buttonModuleWatcher from "../../../ppcp-button/resources/js/modules/ButtonModuleWatcher";
|
||||||
|
|
||||||
const config = wc.wcSettings.getSetting('ppcp-gateway_data');
|
const config = wc.wcSettings.getSetting('ppcp-gateway_data');
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ const PayPalComponent = ({
|
||||||
if (!loaded) {
|
if (!loaded) {
|
||||||
loadPaypalScript(config.scriptData, () => {
|
loadPaypalScript(config.scriptData, () => {
|
||||||
setLoaded(true);
|
setLoaded(true);
|
||||||
|
buttonModuleWatcher.registerContextBootstrap(config.scriptData.context, this);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [loaded]);
|
}, [loaded]);
|
||||||
|
|
|
@ -19,6 +19,7 @@ import FreeTrialHandler from "./modules/ActionHandler/FreeTrialHandler";
|
||||||
import FormSaver from './modules/Helper/FormSaver';
|
import FormSaver from './modules/Helper/FormSaver';
|
||||||
import FormValidator from "./modules/Helper/FormValidator";
|
import FormValidator from "./modules/Helper/FormValidator";
|
||||||
import {loadPaypalScript} from "./modules/Helper/ScriptLoading";
|
import {loadPaypalScript} from "./modules/Helper/ScriptLoading";
|
||||||
|
import buttonModuleWatcher from "./modules/ButtonModuleWatcher";
|
||||||
|
|
||||||
// TODO: could be a good idea to have a separate spinner for each gateway,
|
// TODO: could be a good idea to have a separate spinner for each gateway,
|
||||||
// but I think we care mainly about the script loading, so one spinner should be enough.
|
// but I think we care mainly about the script loading, so one spinner should be enough.
|
||||||
|
@ -60,6 +61,11 @@ const bootstrap = () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const hasMessages = () => {
|
||||||
|
return PayPalCommerceGateway.messages.is_hidden === false
|
||||||
|
&& document.querySelector(PayPalCommerceGateway.messages.wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
const onSmartButtonClick = async (data, actions) => {
|
const onSmartButtonClick = async (data, actions) => {
|
||||||
window.ppcpFundingSource = data.fundingSource;
|
window.ppcpFundingSource = data.fundingSource;
|
||||||
const requiredFields = jQuery('form.woocommerce-checkout .validate-required:visible :input');
|
const requiredFields = jQuery('form.woocommerce-checkout .validate-required:visible :input');
|
||||||
|
@ -146,6 +152,7 @@ const bootstrap = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
miniCartBootstrap.init();
|
miniCartBootstrap.init();
|
||||||
|
buttonModuleWatcher.registerContextBootstrap('mini-cart', miniCartBootstrap);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -163,6 +170,7 @@ const bootstrap = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
singleProductBootstrap.init();
|
singleProductBootstrap.init();
|
||||||
|
buttonModuleWatcher.registerContextBootstrap('product', singleProductBootstrap);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context === 'cart') {
|
if (context === 'cart') {
|
||||||
|
@ -174,6 +182,7 @@ const bootstrap = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
cartBootstrap.init();
|
cartBootstrap.init();
|
||||||
|
buttonModuleWatcher.registerContextBootstrap('cart', cartBootstrap);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context === 'checkout') {
|
if (context === 'checkout') {
|
||||||
|
@ -186,6 +195,7 @@ const bootstrap = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
checkoutBootstap.init();
|
checkoutBootstap.init();
|
||||||
|
buttonModuleWatcher.registerContextBootstrap('checkout', checkoutBootstap);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context === 'pay-now' ) {
|
if (context === 'pay-now' ) {
|
||||||
|
@ -197,15 +207,11 @@ const bootstrap = () => {
|
||||||
errorHandler,
|
errorHandler,
|
||||||
);
|
);
|
||||||
payNowBootstrap.init();
|
payNowBootstrap.init();
|
||||||
|
buttonModuleWatcher.registerContextBootstrap('pay-now', payNowBootstrap);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const hasMessages = () => {
|
|
||||||
return PayPalCommerceGateway.messages.is_hidden === false
|
|
||||||
&& document.querySelector(PayPalCommerceGateway.messages.wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener(
|
document.addEventListener(
|
||||||
'DOMContentLoaded',
|
'DOMContentLoaded',
|
||||||
() => {
|
() => {
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
|
||||||
|
class ButtonModuleWatcher {
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.contextBootstrapRegistry = {};
|
||||||
|
this.contextBootstrapWatchers = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
watchContextBootstrap(callable) {
|
||||||
|
this.contextBootstrapWatchers.push(callable);
|
||||||
|
Object.values(this.contextBootstrapRegistry).forEach(callable);
|
||||||
|
}
|
||||||
|
|
||||||
|
registerContextBootstrap(context, handler) {
|
||||||
|
this.contextBootstrapRegistry[context] = {
|
||||||
|
context: context,
|
||||||
|
handler: handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call registered watchers
|
||||||
|
for (const callable of this.contextBootstrapWatchers) {
|
||||||
|
callable(this.contextBootstrapRegistry[context]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
window.ppcpResources = window.ppcpResources || {};
|
||||||
|
const buttonModuleWatcher = window.ppcpResources['ButtonModuleWatcher'] = window.ppcpResources['ButtonModuleWatcher'] || new ButtonModuleWatcher();
|
||||||
|
|
||||||
|
export default buttonModuleWatcher;
|
|
@ -1,5 +1,4 @@
|
||||||
import {loadScript} from "@paypal/paypal-js";
|
import {loadScript} from "@paypal/paypal-js";
|
||||||
import widgetBuilder from "./Renderer/WidgetBuilder";
|
|
||||||
|
|
||||||
const storageKey = 'ppcp-data-client-id';
|
const storageKey = 'ppcp-data-client-id';
|
||||||
|
|
||||||
|
|
|
@ -480,11 +480,10 @@ class SmartButton implements SmartButtonInterface {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
echo '<p
|
echo '<p class="woocommerce-mini-cart__buttons buttons">';
|
||||||
id="ppc-button-minicart"
|
echo '<span id="ppc-button-minicart"></span>';
|
||||||
class="woocommerce-mini-cart__buttons buttons"
|
|
||||||
></p>';
|
|
||||||
do_action( 'woocommerce_paypal_payments_minicart_button_render' );
|
do_action( 'woocommerce_paypal_payments_minicart_button_render' );
|
||||||
|
echo '</div>';
|
||||||
},
|
},
|
||||||
30
|
30
|
||||||
);
|
);
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
|
||||||
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
|
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,9 +101,16 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
|
||||||
$button_enabled = $button_enabled && ! $this->smart_button->is_button_disabled( 'product', $context_data );
|
$button_enabled = $button_enabled && ! $this->smart_button->is_button_disabled( 'product', $context_data );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$base_location = wc_get_base_location();
|
||||||
|
$shop_country_code = $base_location['country'];
|
||||||
|
$currency_code = get_woocommerce_currency();
|
||||||
|
|
||||||
wp_send_json_success(
|
wp_send_json_success(
|
||||||
array(
|
array(
|
||||||
'total' => $total,
|
'total' => $total,
|
||||||
|
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
|
||||||
|
'currency_code' => $currency_code,
|
||||||
|
'country_code' => $shop_country_code,
|
||||||
'funding' => array(
|
'funding' => array(
|
||||||
'paylater' => array(
|
'paylater' => array(
|
||||||
'enabled' => $pay_later_enabled,
|
'enabled' => $pay_later_enabled,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue