Fix the Apple Pay button in the editor

This commit is contained in:
Daniel Dudzic 2024-04-23 15:00:05 +02:00
parent d1b4d7721e
commit 436fd77587
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
6 changed files with 112 additions and 28 deletions

View file

@ -46,4 +46,10 @@
}
}
}
.ppcp-button-applepay {
apple-pay-button {
display: block;
}
}
}

View file

@ -0,0 +1,36 @@
import ApplepayButton from "./ApplepayButton";
class ApplepayManagerBlockEditor {
constructor(buttonConfig, ppcpConfig) {
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
this.applePayConfig = null;
}
init() {
(async () => {
await this.config();
})();
}
async config() {
try {
this.applePayConfig = await paypal.Applepay().config();
const button = new ApplepayButton(
this.ppcpConfig.context,
null,
this.buttonConfig,
this.ppcpConfig,
);
button.init(this.applePayConfig);
} catch (error) {
console.error('Failed to initialize Apple Pay:', error);
}
}
}
export default ApplepayManagerBlockEditor;

View file

@ -2,9 +2,10 @@ import {useEffect, useState} from '@wordpress/element';
import {registerExpressPaymentMethod} from '@woocommerce/blocks-registry';
import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'
import {cartHasSubscriptionProducts} from '../../../ppcp-blocks/resources/js/Helper/Subscription'
import ApplepayManager from "./ApplepayManager";
import {loadCustomScript} from "@paypal/paypal-js";
import CheckoutHandler from "./Context/CheckoutHandler";
import ApplepayManager from "./ApplepayManager";
import ApplepayManagerBlockEditor from "./ApplepayManagerBlockEditor";
const ppcpData = wc.wcSettings.getSetting('ppcp-gateway_data');
const ppcpConfig = ppcpData.scriptData;
@ -16,13 +17,14 @@ if (typeof window.PayPalCommerceGateway === 'undefined') {
window.PayPalCommerceGateway = ppcpConfig;
}
const ApplePayComponent = () => {
const ApplePayComponent = ( props ) => {
const [bootstrapped, setBootstrapped] = useState(false);
const [paypalLoaded, setPaypalLoaded] = useState(false);
const [applePayLoaded, setApplePayLoaded] = useState(false);
const bootstrap = function () {
const manager = new ApplepayManager(buttonConfig, ppcpConfig);
const ManagerClass = props.isEditing ? ApplepayManagerBlockEditor : ApplepayManager;
const manager = new ManagerClass(buttonConfig, ppcpConfig);
manager.init();
};
@ -32,6 +34,8 @@ const ApplePayComponent = () => {
setApplePayLoaded(true);
});
ppcpConfig.url_params.components += ',applepay';
// Load PayPal
loadPaypalScript(ppcpConfig, () => {
setPaypalLoaded(true);
@ -46,7 +50,10 @@ const ApplePayComponent = () => {
}, [paypalLoaded, applePayLoaded]);
return (
<div id={buttonConfig.button.wrapper.replace('#', '')} className="ppcp-button-apm ppcp-button-applepay"></div>
<div
id={buttonConfig.button.wrapper.replace('#', '')}
className="ppcp-button-apm ppcp-button-applepay">
</div>
);
}

View file

@ -92,6 +92,7 @@ class ApplepayModule implements ModuleInterface {
}
$module->load_admin_assets( $c, $apple_payment_method );
$module->load_block_editor_assets( $c, $apple_payment_method );
},
1
);
@ -177,6 +178,13 @@ class ApplepayModule implements ModuleInterface {
}
}
);
add_action(
'enqueue_block_editor_assets',
function () use ( $c, $button ) {
$button->enqueue_admin_styles();
}
);
add_action(
'woocommerce_blocks_payment_method_type_registration',
function( PaymentMethodRegistry $payment_method_registry ) use ( $c ): void {
@ -207,6 +215,7 @@ class ApplepayModule implements ModuleInterface {
* @psalm-suppress UndefinedInterfaceMethod
*/
$button->enqueue_admin();
$button->enqueue_admin_styles();
}
);
@ -222,6 +231,21 @@ class ApplepayModule implements ModuleInterface {
);
}
public function load_block_editor_assets( ContainerInterface $c, ApplePayButton $button ): void {
// Enqueue backend scripts.
add_action(
'enqueue_block_editor_assets',
static function () use ( $c, $button ) {
/**
* Should add this to the ButtonInterface.
*
* @psalm-suppress UndefinedInterfaceMethod
*/
$button->enqueue_admin_styles();
}
);
}
/**
* Renders the Apple Pay buttons in the enabled places.
*

View file

@ -468,9 +468,9 @@ class ApplePayButton implements ButtonInterface {
return false;
}
return wp_verify_nonce(
$nonce,
'woocommerce-process_checkout'
) === 1;
$nonce,
'woocommerce-process_checkout'
) === 1;
}
/**
@ -559,11 +559,11 @@ class ApplePayButton implements ButtonInterface {
list(
$shipping_methods_array, $selected_shipping_method
) = $this->cart_shipping_methods(
$cart,
$customer_address,
$shipping_method,
$shipping_method_id
);
$cart,
$customer_address,
$shipping_method,
$shipping_method_id
);
}
$cart->calculate_shipping();
$cart->calculate_fees();
@ -717,7 +717,7 @@ class ApplePayButton implements ButtonInterface {
*/
protected function calculate_totals_cart_page(
array $customer_address,
$shipping_method = null
$shipping_method = null
): array {
$results = array();
@ -742,11 +742,11 @@ class ApplePayButton implements ButtonInterface {
list(
$shipping_methods_array, $selected_shipping_method
) = $this->cart_shipping_methods(
$cart,
$customer_address,
$shipping_method,
$shipping_method_id
);
$cart,
$customer_address,
$shipping_method,
$shipping_method_id
);
}
$cart->calculate_shipping();
$cart->calculate_fees();
@ -1044,17 +1044,9 @@ class ApplePayButton implements ButtonInterface {
}
/**
* Enqueues scripts/styles for admin.
* Enqueues scripts for admin.
*/
public function enqueue_admin(): void {
wp_register_style(
'wc-ppcp-applepay-admin',
untrailingslashit( $this->module_url ) . '/assets/css/styles.css',
array(),
$this->version
);
wp_enqueue_style( 'wc-ppcp-applepay-admin' );
wp_register_script(
'wc-ppcp-applepay-admin',
untrailingslashit( $this->module_url ) . '/assets/js/boot-admin.js',
@ -1071,6 +1063,19 @@ class ApplePayButton implements ButtonInterface {
);
}
/**
* Enqueues styles for admin.
*/
public function enqueue_admin_styles(): void {
wp_register_style(
'wc-ppcp-applepay-admin',
untrailingslashit( $this->module_url ) . '/assets/css/styles.css',
array(),
$this->version
);
wp_enqueue_style( 'wc-ppcp-applepay-admin' );
}
/**
* Returns the script data.
*

View file

@ -103,12 +103,18 @@ class BlocksPaymentMethod extends AbstractPaymentMethodType {
public function get_payment_method_data() {
$paypal_data = $this->paypal_payment_method->get_payment_method_data();
if ( is_admin() ) {
$script_data = $this->button->script_data_for_admin();
} else {
$script_data = $this->button->script_data();
}
return array(
'id' => $this->name,
'title' => $paypal_data['title'], // TODO : see if we should use another.
'description' => $paypal_data['description'], // TODO : see if we should use another.
'enabled' => $paypal_data['enabled'], // This button is enabled when PayPal buttons are.
'scriptData' => $this->button->script_data(),
'scriptData' => $script_data,
);
}
}